2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % RRRR EEEEE SSSSS OOO U U RRRR CCCC EEEEE %
7 % R R E SS O O U U R R C E %
8 % RRRR EEE SSS O O U U RRRR C EEE %
9 % R R E SS O O U U R R C E %
10 % R R EEEEE SSSSS OOO UUU R R CCCC EEEEE %
13 % Get/Set MagickCore Resources %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/cache.h"
44 #include "MagickCore/configure.h"
45 #include "MagickCore/exception.h"
46 #include "MagickCore/exception-private.h"
47 #include "MagickCore/hashmap.h"
48 #include "MagickCore/log.h"
49 #include "MagickCore/image.h"
50 #include "MagickCore/memory_.h"
51 #include "MagickCore/option.h"
52 #include "MagickCore/policy.h"
53 #include "MagickCore/random_.h"
54 #include "MagickCore/registry.h"
55 #include "MagickCore/resource_.h"
56 #include "MagickCore/resource-private.h"
57 #include "MagickCore/semaphore.h"
58 #include "MagickCore/signature-private.h"
59 #include "MagickCore/string_.h"
60 #include "MagickCore/string-private.h"
61 #include "MagickCore/splay-tree.h"
62 #include "MagickCore/thread-private.h"
63 #include "MagickCore/token.h"
64 #include "MagickCore/utility.h"
65 #include "MagickCore/utility-private.h"
70 typedef struct _ResourceInfo
95 *random_info = (RandomInfo *) NULL;
100 MagickULLConstant(0),
101 MagickULLConstant(0),
102 MagickULLConstant(0),
103 MagickULLConstant(0),
104 MagickULLConstant(0),
105 MagickULLConstant(0),
106 MagickULLConstant(0),
107 MagickULLConstant(3072)*1024*1024,
108 MagickULLConstant(1536)*1024*1024,
109 MagickULLConstant(3072)*1024*1024,
110 MagickResourceInfinity,
111 MagickULLConstant(768),
112 MagickULLConstant(1),
113 MagickResourceInfinity
117 *resource_semaphore = (SemaphoreInfo *) NULL;
120 *temporary_resources = (SplayTreeInfo *) NULL;
123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127 % A c q u i r e M a g i c k R e s o u r c e %
131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133 % AcquireMagickResource() acquires resources of the specified type.
134 % MagickFalse is returned if the specified resource is exhausted otherwise
137 % The format of the AcquireMagickResource() method is:
139 % MagickBooleanType AcquireMagickResource(const ResourceType type,
140 % const MagickSizeType size)
142 % A description of each parameter follows:
144 % o type: the type of resource.
146 % o size: the number of bytes needed from for this resource.
149 MagickExport MagickBooleanType AcquireMagickResource(const ResourceType type,
150 const MagickSizeType size)
153 resource_current[MaxTextExtent],
154 resource_limit[MaxTextExtent],
155 resource_request[MaxTextExtent];
164 (void) FormatMagickSize(size,MagickFalse,resource_request);
165 if (resource_semaphore == (SemaphoreInfo *) NULL)
166 AcquireSemaphoreInfo(&resource_semaphore);
167 LockSemaphoreInfo(resource_semaphore);
172 resource_info.area=(MagickOffsetType) size;
173 limit=resource_info.area_limit;
174 status=(resource_info.area_limit == MagickResourceInfinity) ||
175 (size < limit) ? MagickTrue : MagickFalse;
176 (void) FormatMagickSize((MagickSizeType) resource_info.area,MagickFalse,
178 (void) FormatMagickSize(resource_info.area_limit,MagickFalse,
184 resource_info.memory+=size;
185 limit=resource_info.memory_limit;
186 status=(resource_info.memory_limit == MagickResourceInfinity) ||
187 ((MagickSizeType) resource_info.memory < limit) ?
188 MagickTrue : MagickFalse;
189 (void) FormatMagickSize((MagickSizeType) resource_info.memory,MagickTrue,
191 (void) FormatMagickSize(resource_info.memory_limit,MagickTrue,
197 resource_info.map+=size;
198 limit=resource_info.map_limit;
199 status=(resource_info.map_limit == MagickResourceInfinity) ||
200 ((MagickSizeType) resource_info.map < limit) ?
201 MagickTrue : MagickFalse;
202 (void) FormatMagickSize((MagickSizeType) resource_info.map,MagickTrue,
204 (void) FormatMagickSize(resource_info.map_limit,MagickTrue,
210 resource_info.disk+=size;
211 limit=resource_info.disk_limit;
212 status=(resource_info.disk_limit == MagickResourceInfinity) ||
213 ((MagickSizeType) resource_info.disk < limit) ?
214 MagickTrue : MagickFalse;
215 (void) FormatMagickSize((MagickSizeType) resource_info.disk,MagickTrue,
217 (void) FormatMagickSize(resource_info.disk_limit,MagickTrue,
223 resource_info.file+=size;
224 limit=resource_info.file_limit;
225 status=(resource_info.file_limit == MagickResourceInfinity) ||
226 ((MagickSizeType) resource_info.file < limit) ?
227 MagickTrue : MagickFalse;
228 (void) FormatMagickSize((MagickSizeType) resource_info.file,MagickFalse,
230 (void) FormatMagickSize((MagickSizeType) resource_info.file_limit,
231 MagickFalse,resource_limit);
236 limit=resource_info.thread_limit;
237 status=(resource_info.thread_limit == MagickResourceInfinity) ||
238 ((MagickSizeType) resource_info.thread < limit) ?
239 MagickTrue : MagickFalse;
240 (void) FormatMagickSize((MagickSizeType) resource_info.thread,MagickFalse,
242 (void) FormatMagickSize((MagickSizeType) resource_info.thread_limit,
243 MagickFalse,resource_limit);
248 resource_info.time+=size;
249 limit=resource_info.time_limit;
250 status=(resource_info.time_limit == MagickResourceInfinity) ||
251 ((MagickSizeType) resource_info.time < limit) ?
252 MagickTrue : MagickFalse;
253 (void) FormatMagickSize((MagickSizeType) resource_info.time,MagickFalse,
255 (void) FormatMagickSize((MagickSizeType) resource_info.time_limit,
256 MagickFalse,resource_limit);
262 UnlockSemaphoreInfo(resource_semaphore);
263 (void) LogMagickEvent(ResourceEvent,GetMagickModule(),"%s: %s/%s/%s",
264 CommandOptionToMnemonic(MagickResourceOptions,(ssize_t) type),
265 resource_request,resource_current,resource_limit);
270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 + A s y n c h r o n o u s R e s o u r c e C o m p o n e n t T e r m i n u s %
278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 % AsynchronousResourceComponentTerminus() destroys the resource environment.
281 % It differs from ResourceComponentTerminus() in that it can be called from a
282 % asynchronous signal handler.
284 % The format of the ResourceComponentTerminus() method is:
286 % ResourceComponentTerminus(void)
289 MagickPrivate void AsynchronousResourceComponentTerminus(void)
294 if (temporary_resources == (SplayTreeInfo *) NULL)
297 Remove any lingering temporary files.
299 ResetSplayTreeIterator(temporary_resources);
300 path=(const char *) GetNextKeyInSplayTree(temporary_resources);
301 while (path != (const char *) NULL)
303 (void) remove_utf8(path);
304 path=(const char *) GetNextKeyInSplayTree(temporary_resources);
309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313 % A c q u i r e U n i q u e F i l e R e s o u r c e %
317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319 % AcquireUniqueFileResource() returns a unique file name, and returns a file
320 % descriptor for the file open for reading and writing.
322 % The format of the AcquireUniqueFileResource() method is:
324 % int AcquireUniqueFileResource(char *path)
326 % A description of each parameter follows:
328 % o path: Specifies a pointer to an array of characters. The unique path
329 % name is returned in this array.
333 static void *DestroyTemporaryResources(void *temporary_resource)
335 (void) remove_utf8((char *) temporary_resource);
336 temporary_resource=DestroyString((char *) temporary_resource);
337 return((void *) NULL);
340 static MagickBooleanType GetPathTemplate(char *path)
357 (void) FormatLocaleString(path,MaxTextExtent,"magick-%.20gXXXXXXXXXXXX",
359 exception=AcquireExceptionInfo();
360 directory=(char *) GetImageRegistry(StringRegistryType,"temporary-path",
362 exception=DestroyExceptionInfo(exception);
363 if (directory == (char *) NULL)
364 directory=GetEnvironmentValue("MAGICK_TEMPORARY_PATH");
365 if (directory == (char *) NULL)
366 directory=GetEnvironmentValue("MAGICK_TMPDIR");
367 if (directory == (char *) NULL)
368 directory=GetPolicyValue("temporary-path");
369 if (directory == (char *) NULL)
370 directory=GetEnvironmentValue("TMPDIR");
371 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
372 if (directory == (char *) NULL)
373 directory=GetEnvironmentValue("TMP");
374 if (directory == (char *) NULL)
375 directory=GetEnvironmentValue("TEMP");
378 if (directory == (char *) NULL)
379 directory=GetEnvironmentValue("MTMPDIR");
381 #if defined(P_tmpdir)
382 if (directory == (char *) NULL)
383 directory=ConstantString(P_tmpdir);
385 if (directory == (char *) NULL)
387 if (strlen(directory) > (MaxTextExtent-25))
389 directory=DestroyString(directory);
392 status=GetPathAttributes(directory,&attributes);
393 if ((status == MagickFalse) || !S_ISDIR(attributes.st_mode))
395 directory=DestroyString(directory);
398 if (directory[strlen(directory)-1] == *DirectorySeparator)
399 (void) FormatLocaleString(path,MaxTextExtent,"%smagick-%.20gXXXXXXXXXXXX",
400 directory,(double) getpid());
402 (void) FormatLocaleString(path,MaxTextExtent,"%s%smagick-%.20gXXXXXXXXXXXX",
403 directory,DirectorySeparator,(double) getpid());
404 directory=DestroyString(directory);
405 if (*DirectorySeparator != '/')
406 for (p=path; *p != '\0'; p++)
407 if (*p == *DirectorySeparator)
412 MagickExport int AcquireUniqueFileResource(char *path)
414 #if !defined(O_NOFOLLOW)
417 #if !defined(TMP_MAX)
418 # define TMP_MAX 238328
432 portable_filename[65] =
433 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
441 assert(path != (char *) NULL);
442 (void) LogMagickEvent(ResourceEvent,GetMagickModule(),"%s",path);
443 if (random_info == (RandomInfo *) NULL)
444 random_info=AcquireRandomInfo();
446 for (i=0; i < (ssize_t) TMP_MAX; i++)
449 Get temporary pathname.
451 (void) GetPathTemplate(path);
452 key=GetRandomKey(random_info,2);
453 p=path+strlen(path)-12;
454 datum=GetStringInfoDatum(key);
455 for (i=0; i < (ssize_t) GetStringInfoLength(key); i++)
457 c=(int) (datum[i] & 0x3f);
458 *p++=portable_filename[c];
460 key=DestroyStringInfo(key);
461 #if defined(MAGICKCORE_HAVE_MKSTEMP)
464 setmode(file,O_BINARY);
469 key=GetRandomKey(random_info,12);
470 p=path+strlen(path)-12;
471 datum=GetStringInfoDatum(key);
472 for (i=0; i < (ssize_t) GetStringInfoLength(key); i++)
474 c=(int) (datum[i] & 0x3f);
475 *p++=portable_filename[c];
477 key=DestroyStringInfo(key);
478 file=open_utf8(path,O_RDWR | O_CREAT | O_EXCL | O_BINARY | O_NOFOLLOW,
480 if ((file >= 0) || (errno != EEXIST))
483 (void) LogMagickEvent(ResourceEvent,GetMagickModule(),"%s",path);
486 if (resource_semaphore == (SemaphoreInfo *) NULL)
487 AcquireSemaphoreInfo(&resource_semaphore);
488 LockSemaphoreInfo(resource_semaphore);
489 if (temporary_resources == (SplayTreeInfo *) NULL)
490 temporary_resources=NewSplayTree(CompareSplayTreeString,
491 DestroyTemporaryResources,(void *(*)(void *)) NULL);
492 UnlockSemaphoreInfo(resource_semaphore);
493 (void) AddValueToSplayTree(temporary_resources,ConstantString(path),
494 (const void *) NULL);
499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
503 % G e t M a g i c k R e s o u r c e %
507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
509 % GetMagickResource() returns the specified resource.
511 % The format of the GetMagickResource() method is:
513 % MagickSizeType GetMagickResource(const ResourceType type)
515 % A description of each parameter follows:
517 % o type: the type of resource.
520 MagickExport MagickSizeType GetMagickResource(const ResourceType type)
526 LockSemaphoreInfo(resource_semaphore);
531 resource=(MagickSizeType) resource_info.area;
536 resource=(MagickSizeType) resource_info.memory;
541 resource=(MagickSizeType) resource_info.map;
546 resource=(MagickSizeType) resource_info.disk;
551 resource=(MagickSizeType) resource_info.file;
556 resource=(MagickSizeType) resource_info.thread;
561 resource=(MagickSizeType) resource_info.time;
567 UnlockSemaphoreInfo(resource_semaphore);
572 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576 % G e t M a g i c k R e s o u r c e L i m i t %
580 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
582 % GetMagickResourceLimit() returns the specified resource limit.
584 % The format of the GetMagickResourceLimit() method is:
586 % MagickSizeType GetMagickResourceLimit(const ResourceType type)
588 % A description of each parameter follows:
590 % o type: the type of resource.
593 MagickExport MagickSizeType GetMagickResourceLimit(const ResourceType type)
599 if (resource_semaphore == (SemaphoreInfo *) NULL)
600 AcquireSemaphoreInfo(&resource_semaphore);
601 LockSemaphoreInfo(resource_semaphore);
606 resource=resource_info.area_limit;
611 resource=resource_info.memory_limit;
616 resource=resource_info.map_limit;
621 resource=resource_info.disk_limit;
626 resource=resource_info.file_limit;
631 resource=resource_info.thread_limit;
636 resource=resource_info.time_limit;
642 UnlockSemaphoreInfo(resource_semaphore);
647 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
651 % L i s t M a g i c k R e s o u r c e I n f o %
655 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
657 % ListMagickResourceInfo() lists the resource info to a file.
659 % The format of the ListMagickResourceInfo method is:
661 % MagickBooleanType ListMagickResourceInfo(FILE *file,
662 % ExceptionInfo *exception)
664 % A description of each parameter follows.
666 % o file: An pointer to a FILE.
668 % o exception: return any errors or warnings in this structure.
671 MagickExport MagickBooleanType ListMagickResourceInfo(FILE *file,
672 ExceptionInfo *magick_unused(exception))
675 area_limit[MaxTextExtent],
676 disk_limit[MaxTextExtent],
677 map_limit[MaxTextExtent],
678 memory_limit[MaxTextExtent],
679 time_limit[MaxTextExtent];
681 if (file == (const FILE *) NULL)
683 if (resource_semaphore == (SemaphoreInfo *) NULL)
684 AcquireSemaphoreInfo(&resource_semaphore);
685 LockSemaphoreInfo(resource_semaphore);
686 (void) FormatMagickSize(resource_info.area_limit,MagickFalse,area_limit);
687 (void) FormatMagickSize(resource_info.memory_limit,MagickTrue,memory_limit);
688 (void) FormatMagickSize(resource_info.map_limit,MagickTrue,map_limit);
689 (void) CopyMagickString(disk_limit,"unlimited",MaxTextExtent);
690 if (resource_info.disk_limit != MagickResourceInfinity)
691 (void) FormatMagickSize(resource_info.disk_limit,MagickTrue,disk_limit);
692 (void) CopyMagickString(time_limit,"unlimited",MaxTextExtent);
693 if (resource_info.time_limit != MagickResourceInfinity)
694 (void) FormatLocaleString(time_limit,MaxTextExtent,"%.20g",(double)
695 ((MagickOffsetType) resource_info.time_limit));
696 (void) FormatLocaleFile(file,"File Area Memory Map"
697 " Disk Thread Time\n");
698 (void) FormatLocaleFile(file,
699 "--------------------------------------------------------"
700 "-----------------------\n");
701 (void) FormatLocaleFile(file,"%4g %10s %10s %10s %10s %6g %11s\n",
702 (double) ((MagickOffsetType) resource_info.file_limit),area_limit,
703 memory_limit,map_limit,disk_limit,(double) ((MagickOffsetType)
704 resource_info.thread_limit),time_limit);
706 UnlockSemaphoreInfo(resource_semaphore);
711 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
715 % R e l i n q u i s h M a g i c k R e s o u r c e %
719 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
721 % RelinquishMagickResource() relinquishes resources of the specified type.
723 % The format of the RelinquishMagickResource() method is:
725 % void RelinquishMagickResource(const ResourceType type,
726 % const MagickSizeType size)
728 % A description of each parameter follows:
730 % o type: the type of resource.
732 % o size: the size of the resource.
735 MagickExport void RelinquishMagickResource(const ResourceType type,
736 const MagickSizeType size)
739 resource_current[MaxTextExtent],
740 resource_limit[MaxTextExtent],
741 resource_request[MaxTextExtent];
743 (void) FormatMagickSize(size,MagickFalse,resource_request);
744 if (resource_semaphore == (SemaphoreInfo *) NULL)
745 AcquireSemaphoreInfo(&resource_semaphore);
746 LockSemaphoreInfo(resource_semaphore);
751 resource_info.area=(MagickOffsetType) size;
752 (void) FormatMagickSize((MagickSizeType) resource_info.area,MagickFalse,
754 (void) FormatMagickSize(resource_info.area_limit,MagickFalse,
760 resource_info.memory-=size;
761 (void) FormatMagickSize((MagickSizeType) resource_info.memory,
762 MagickTrue,resource_current);
763 (void) FormatMagickSize(resource_info.memory_limit,MagickTrue,
769 resource_info.map-=size;
770 (void) FormatMagickSize((MagickSizeType) resource_info.map,MagickTrue,
772 (void) FormatMagickSize(resource_info.map_limit,MagickTrue,
778 resource_info.disk-=size;
779 (void) FormatMagickSize((MagickSizeType) resource_info.disk,MagickTrue,
781 (void) FormatMagickSize(resource_info.disk_limit,MagickTrue,
787 resource_info.file-=size;
788 (void) FormatMagickSize((MagickSizeType) resource_info.file,MagickFalse,
790 (void) FormatMagickSize((MagickSizeType) resource_info.file_limit,
791 MagickFalse,resource_limit);
796 (void) FormatMagickSize((MagickSizeType) resource_info.thread,MagickFalse,
798 (void) FormatMagickSize((MagickSizeType) resource_info.thread_limit,
799 MagickFalse,resource_limit);
804 resource_info.time-=size;
805 (void) FormatMagickSize((MagickSizeType) resource_info.time,MagickFalse,
807 (void) FormatMagickSize((MagickSizeType) resource_info.time_limit,
808 MagickFalse,resource_limit);
814 UnlockSemaphoreInfo(resource_semaphore);
815 (void) LogMagickEvent(ResourceEvent,GetMagickModule(),"%s: %s/%s/%s",
816 CommandOptionToMnemonic(MagickResourceOptions,(ssize_t) type),
817 resource_request,resource_current,resource_limit);
821 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
825 % R e l i n q u i s h U n i q u e F i l e R e s o u r c e %
829 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
831 % RelinquishUniqueFileResource() relinquishes a unique file resource.
833 % The format of the RelinquishUniqueFileResource() method is:
835 % MagickBooleanType RelinquishUniqueFileResource(const char *path)
837 % A description of each parameter follows:
839 % o name: the name of the temporary resource.
842 MagickExport MagickBooleanType RelinquishUniqueFileResource(const char *path)
845 cache_path[MaxTextExtent];
847 assert(path != (const char *) NULL);
848 (void) LogMagickEvent(ResourceEvent,GetMagickModule(),"%s",path);
849 if (temporary_resources != (SplayTreeInfo *) NULL)
854 ResetSplayTreeIterator(temporary_resources);
855 p=(char *) GetNextKeyInSplayTree(temporary_resources);
856 while (p != (char *) NULL)
858 if (LocaleCompare(p,path) == 0)
860 p=(char *) GetNextKeyInSplayTree(temporary_resources);
862 if (p != (char *) NULL)
863 (void) DeleteNodeFromSplayTree(temporary_resources,p);
865 (void) CopyMagickString(cache_path,path,MaxTextExtent);
866 AppendImageFormat("cache",cache_path);
867 (void) remove_utf8(cache_path);
868 return(remove_utf8(path) == 0 ? MagickTrue : MagickFalse);
872 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
876 + R e s o u r c e C o m p o n e n t G e n e s i s %
880 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
882 % ResourceComponentGenesis() instantiates the resource component.
884 % The format of the ResourceComponentGenesis method is:
886 % MagickBooleanType ResourceComponentGenesis(void)
890 static inline size_t MagickMax(const size_t x,const size_t y)
897 static inline MagickSizeType StringToSizeType(const char *string,
898 const double interval)
903 value=SiPrefixToDoubleInterval(string,interval);
904 if (value >= (double) MagickULLConstant(~0))
905 return(MagickULLConstant(~0));
906 return((MagickSizeType) value);
909 MagickPrivate MagickBooleanType ResourceComponentGenesis(void)
923 Set Magick resource limits.
925 AcquireSemaphoreInfo(&resource_semaphore);
926 pagesize=GetMagickPageSize();
928 #if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PHYS_PAGES)
929 pages=(ssize_t) sysconf(_SC_PHYS_PAGES);
931 memory=(MagickSizeType) pages*pagesize;
932 if ((pagesize <= 0) || (pages <= 0))
933 memory=2048UL*1024UL*1024UL;
934 #if defined(PixelCacheThreshold)
935 memory=PixelCacheThreshold;
937 (void) SetMagickResourceLimit(AreaResource,2*memory);
938 (void) SetMagickResourceLimit(MemoryResource,memory);
939 (void) SetMagickResourceLimit(MapResource,2*memory);
940 limit=GetEnvironmentValue("MAGICK_AREA_LIMIT");
941 if (limit == (char *) NULL)
942 limit=GetPolicyValue("area");
943 if (limit != (char *) NULL)
945 (void) SetMagickResourceLimit(AreaResource,StringToSizeType(limit,100.0));
946 limit=DestroyString(limit);
948 limit=GetEnvironmentValue("MAGICK_MEMORY_LIMIT");
949 if (limit == (char *) NULL)
950 limit=GetPolicyValue("memory");
951 if (limit != (char *) NULL)
953 (void) SetMagickResourceLimit(MemoryResource,
954 StringToSizeType(limit,100.0));
955 limit=DestroyString(limit);
957 limit=GetEnvironmentValue("MAGICK_MAP_LIMIT");
958 if (limit == (char *) NULL)
959 limit=GetPolicyValue("map");
960 if (limit != (char *) NULL)
962 (void) SetMagickResourceLimit(MapResource,StringToSizeType(limit,100.0));
963 limit=DestroyString(limit);
965 limit=GetEnvironmentValue("MAGICK_DISK_LIMIT");
966 if (limit == (char *) NULL)
967 limit=GetPolicyValue("disk");
968 if (limit != (char *) NULL)
970 (void) SetMagickResourceLimit(DiskResource,StringToSizeType(limit,100.0));
971 limit=DestroyString(limit);
974 #if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_OPEN_MAX)
975 files=(ssize_t) sysconf(_SC_OPEN_MAX);
977 #if defined(MAGICKCORE_HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
983 if (getrlimit(RLIMIT_NOFILE,&resources) != -1)
984 files=(ssize_t) resources.rlim_cur;
987 #if defined(MAGICKCORE_HAVE_GETDTABLESIZE) && defined(MAGICKCORE_POSIX_SUPPORT)
989 files=(ssize_t) getdtablesize();
993 (void) SetMagickResourceLimit(FileResource,MagickMax((size_t)
995 limit=GetEnvironmentValue("MAGICK_FILE_LIMIT");
996 if (limit == (char *) NULL)
997 limit=GetPolicyValue("file");
998 if (limit != (char *) NULL)
1000 (void) SetMagickResourceLimit(FileResource,StringToSizeType(limit,
1002 limit=DestroyString(limit);
1004 (void) SetMagickResourceLimit(ThreadResource,GetOpenMPMaximumThreads());
1005 limit=GetEnvironmentValue("MAGICK_THREAD_LIMIT");
1006 if (limit == (char *) NULL)
1007 limit=GetPolicyValue("thread");
1008 if (limit != (char *) NULL)
1010 (void) SetMagickResourceLimit(ThreadResource,StringToSizeType(limit,
1012 limit=DestroyString(limit);
1014 limit=GetEnvironmentValue("MAGICK_TIME_LIMIT");
1015 if (limit == (char *) NULL)
1016 limit=GetPolicyValue("time");
1017 if (limit != (char *) NULL)
1019 (void) SetMagickResourceLimit(TimeResource,StringToSizeType(limit,100.0));
1020 limit=DestroyString(limit);
1026 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1030 + R e s o u r c e C o m p o n e n t T e r m i n u s %
1034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1036 % ResourceComponentTerminus() destroys the resource component.
1038 % The format of the ResourceComponentTerminus() method is:
1040 % ResourceComponentTerminus(void)
1043 MagickPrivate void ResourceComponentTerminus(void)
1045 if (resource_semaphore == (SemaphoreInfo *) NULL)
1046 AcquireSemaphoreInfo(&resource_semaphore);
1047 LockSemaphoreInfo(resource_semaphore);
1048 if (temporary_resources != (SplayTreeInfo *) NULL)
1049 temporary_resources=DestroySplayTree(temporary_resources);
1050 if (random_info != (RandomInfo *) NULL)
1051 random_info=DestroyRandomInfo(random_info);
1052 UnlockSemaphoreInfo(resource_semaphore);
1053 DestroySemaphoreInfo(&resource_semaphore);
1057 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1061 % S e t M a g i c k R e s o u r c e L i m i t %
1065 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1067 % SetMagickResourceLimit() sets the limit for a particular resource.
1069 % The format of the SetMagickResourceLimit() method is:
1071 % MagickBooleanType SetMagickResourceLimit(const ResourceType type,
1072 % const MagickSizeType limit)
1074 % A description of each parameter follows:
1076 % o type: the type of resource.
1078 % o limit: the maximum limit for the resource.
1082 static inline MagickSizeType MagickMin(const MagickSizeType x,
1083 const MagickSizeType y)
1090 MagickExport MagickBooleanType SetMagickResourceLimit(const ResourceType type,
1091 const MagickSizeType limit)
1096 if (resource_semaphore == (SemaphoreInfo *) NULL)
1097 AcquireSemaphoreInfo(&resource_semaphore);
1098 LockSemaphoreInfo(resource_semaphore);
1099 value=(char *) NULL;
1104 resource_info.area_limit=limit;
1105 value=GetPolicyValue("area");
1106 if (value != (char *) NULL)
1107 resource_info.area_limit=MagickMin(limit,StringToSizeType(value,100.0));
1110 case MemoryResource:
1112 resource_info.memory_limit=limit;
1113 value=GetPolicyValue("memory");
1114 if (value != (char *) NULL)
1115 resource_info.memory_limit=MagickMin(limit,StringToSizeType(value,
1121 resource_info.map_limit=limit;
1122 value=GetPolicyValue("map");
1123 if (value != (char *) NULL)
1124 resource_info.map_limit=MagickMin(limit,StringToSizeType(value,100.0));
1129 resource_info.disk_limit=limit;
1130 value=GetPolicyValue("disk");
1131 if (value != (char *) NULL)
1132 resource_info.disk_limit=MagickMin(limit,StringToSizeType(value,100.0));
1137 resource_info.file_limit=limit;
1138 value=GetPolicyValue("file");
1139 if (value != (char *) NULL)
1140 resource_info.file_limit=MagickMin(limit,StringToSizeType(value,100.0));
1143 case ThreadResource:
1145 resource_info.thread_limit=limit;
1146 value=GetPolicyValue("thread");
1147 if (value != (char *) NULL)
1148 resource_info.thread_limit=MagickMin(limit,StringToSizeType(value,
1150 if (resource_info.thread_limit > GetOpenMPMaximumThreads())
1151 resource_info.thread_limit=GetOpenMPMaximumThreads();
1156 resource_info.time_limit=limit;
1157 value=GetPolicyValue("time");
1158 if (value != (char *) NULL)
1159 resource_info.time_limit=MagickMin(limit,StringToSizeType(value,100.0));
1165 if (value != (char *) NULL)
1166 value=DestroyString(value);
1167 UnlockSemaphoreInfo(resource_semaphore);