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-2010 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 "magick/studio.h"
43 #include "magick/cache.h"
44 #include "magick/configure.h"
45 #include "magick/exception.h"
46 #include "magick/exception-private.h"
47 #include "magick/hashmap.h"
48 #include "magick/log.h"
49 #include "magick/image.h"
50 #include "magick/memory_.h"
51 #include "magick/option.h"
52 #include "magick/policy.h"
53 #include "magick/random_.h"
54 #include "magick/registry.h"
55 #include "magick/resource_.h"
56 #include "magick/semaphore.h"
57 #include "magick/signature-private.h"
58 #include "magick/string_.h"
59 #include "magick/string-private.h"
60 #include "magick/splay-tree.h"
61 #include "magick/thread-private.h"
62 #include "magick/token.h"
63 #include "magick/utility.h"
68 typedef struct _ResourceInfo
93 *random_info = (RandomInfo *) NULL;
100 MagickULLConstant(0),
101 MagickULLConstant(0),
102 MagickULLConstant(0),
103 MagickULLConstant(0),
104 MagickULLConstant(0),
105 MagickULLConstant(2048)*1024*1024,
106 MagickULLConstant(1536)*1024*1024,
107 MagickULLConstant(8192)*1024*1024,
108 MagickResourceInfinity,
109 MagickULLConstant(768),
110 MagickULLConstant(8),
111 MagickResourceInfinity
115 *resource_semaphore = (SemaphoreInfo *) NULL;
118 *temporary_resources = (SplayTreeInfo *) NULL;
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125 % A c q u i r e M a g i c k R e s o u r c e %
129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131 % AcquireMagickResource() acquires resources of the specified type.
132 % MagickFalse is returned if the specified resource is exhausted otherwise
135 % The format of the AcquireMagickResource() method is:
137 % MagickBooleanType AcquireMagickResource(const ResourceType type,
138 % const MagickSizeType size)
140 % A description of each parameter follows:
142 % o type: the type of resource.
144 % o size: the number of bytes needed from for this resource.
147 MagickExport MagickBooleanType AcquireMagickResource(const ResourceType type,
148 const MagickSizeType size)
151 resource_current[MaxTextExtent],
152 resource_limit[MaxTextExtent],
153 resource_request[MaxTextExtent];
162 (void) FormatMagickSize(size,MagickFalse,resource_request);
163 if (resource_semaphore == (SemaphoreInfo *) NULL)
164 AcquireSemaphoreInfo(&resource_semaphore);
165 LockSemaphoreInfo(resource_semaphore);
170 resource_info.area=(MagickOffsetType) size;
171 limit=resource_info.area_limit;
172 status=(resource_info.area_limit == MagickResourceInfinity) ||
173 (size < limit) ? MagickTrue : MagickFalse;
174 (void) FormatMagickSize((MagickSizeType) resource_info.area,MagickFalse,
176 (void) FormatMagickSize(resource_info.area_limit,MagickFalse,
182 resource_info.memory+=size;
183 limit=resource_info.memory_limit;
184 status=(resource_info.memory_limit == MagickResourceInfinity) ||
185 ((MagickSizeType) resource_info.memory < limit) ?
186 MagickTrue : MagickFalse;
187 (void) FormatMagickSize((MagickSizeType) resource_info.memory,MagickTrue,
189 (void) FormatMagickSize(resource_info.memory_limit,MagickTrue,
195 resource_info.map+=size;
196 limit=resource_info.map_limit;
197 status=(resource_info.map_limit == MagickResourceInfinity) ||
198 ((MagickSizeType) resource_info.map < limit) ?
199 MagickTrue : MagickFalse;
200 (void) FormatMagickSize((MagickSizeType) resource_info.map,MagickTrue,
202 (void) FormatMagickSize(resource_info.map_limit,MagickTrue,
208 resource_info.disk+=size;
209 limit=resource_info.disk_limit;
210 status=(resource_info.disk_limit == MagickResourceInfinity) ||
211 ((MagickSizeType) resource_info.disk < limit) ?
212 MagickTrue : MagickFalse;
213 (void) FormatMagickSize((MagickSizeType) resource_info.disk,MagickFalse,
215 (void) FormatMagickSize(resource_info.disk_limit,MagickFalse,
221 resource_info.file+=size;
222 limit=resource_info.file_limit;
223 status=(resource_info.file_limit == MagickResourceInfinity) ||
224 ((MagickSizeType) resource_info.file < limit) ?
225 MagickTrue : MagickFalse;
226 (void) FormatMagickSize((MagickSizeType) resource_info.file,MagickFalse,
228 (void) FormatMagickSize((MagickSizeType) resource_info.file_limit,
229 MagickFalse,resource_limit);
234 resource_info.thread+=size;
235 limit=resource_info.thread_limit;
236 status=(resource_info.thread_limit == MagickResourceInfinity) ||
237 ((MagickSizeType) resource_info.thread < limit) ?
238 MagickTrue : MagickFalse;
239 (void) FormatMagickSize((MagickSizeType) resource_info.thread,MagickFalse,
241 (void) FormatMagickSize((MagickSizeType) resource_info.thread_limit,
242 MagickFalse,resource_limit);
247 resource_info.time+=size;
248 limit=resource_info.time_limit;
249 status=(resource_info.time_limit == MagickResourceInfinity) ||
250 ((MagickSizeType) resource_info.time < limit) ?
251 MagickTrue : MagickFalse;
252 (void) FormatMagickSize((MagickSizeType) resource_info.time,MagickFalse,
254 (void) FormatMagickSize((MagickSizeType) resource_info.time_limit,
255 MagickFalse,resource_limit);
261 UnlockSemaphoreInfo(resource_semaphore);
262 (void) LogMagickEvent(ResourceEvent,GetMagickModule(),"%s: %sB/%sB/%sB",
263 MagickOptionToMnemonic(MagickResourceOptions,(long) type),resource_request,
264 resource_current,resource_limit);
269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 + 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 %
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 % AsynchronousResourceComponentTerminus() destroys the resource environment.
280 % It differs from ResourceComponentTerminus() in that it can be called from a
281 % asynchronous signal handler.
283 % The format of the ResourceComponentTerminus() method is:
285 % ResourceComponentTerminus(void)
288 MagickExport void AsynchronousResourceComponentTerminus(void)
293 if (temporary_resources == (SplayTreeInfo *) NULL)
296 Remove any lingering temporary files.
298 ResetSplayTreeIterator(temporary_resources);
299 path=(const char *) GetNextKeyInSplayTree(temporary_resources);
300 while (path != (const char *) NULL)
303 path=(const char *) GetNextKeyInSplayTree(temporary_resources);
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312 % 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 %
316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
318 % AcquireUniqueFileResource() returns a unique file name, and returns a file
319 % descriptor for the file open for reading and writing.
321 % The format of the AcquireUniqueFileResource() method is:
323 % int AcquireUniqueFileResource(char *path)
325 % A description of each parameter follows:
327 % o path: Specifies a pointer to an array of characters. The unique path
328 % name is returned in this array.
332 static void *DestroyTemporaryResources(void *temporary_resource)
334 (void) remove((char *) temporary_resource);
335 return((void *) NULL);
338 static MagickBooleanType GetPathTemplate(char *path)
355 (void) CopyMagickString(path,"magick-XXXXXXXX",MaxTextExtent);
356 exception=AcquireExceptionInfo();
357 directory=(char *) GetImageRegistry(StringRegistryType,"temporary-path",
359 exception=DestroyExceptionInfo(exception);
360 if (directory == (char *) NULL)
361 directory=GetEnvironmentValue("MAGICK_TEMPORARY_PATH");
362 if (directory == (char *) NULL)
363 directory=GetEnvironmentValue("MAGICK_TMPDIR");
364 if (directory == (char *) NULL)
365 directory=GetPolicyValue("temporary-path");
366 if (directory == (char *) NULL)
367 directory=GetEnvironmentValue("TMPDIR");
368 #if defined(__WINDOWS__) || defined(__OS2__)
369 if (directory == (char *) NULL)
370 directory=GetEnvironmentValue("TMP");
371 if (directory == (char *) NULL)
372 directory=GetEnvironmentValue("TEMP");
375 if (directory == (char *) NULL)
376 directory=GetEnvironmentValue("MTMPDIR");
378 #if defined(P_tmpdir)
379 if (directory == (char *) NULL)
380 directory=ConstantString(P_tmpdir);
382 if (directory == (char *) NULL)
384 if (strlen(directory) > (MaxTextExtent-15))
386 directory=DestroyString(directory);
389 status=GetPathAttributes(directory,&attributes);
390 if ((status == MagickFalse) || !S_ISDIR(attributes.st_mode))
392 directory=DestroyString(directory);
395 if (directory[strlen(directory)-1] == *DirectorySeparator)
396 (void) FormatMagickString(path,MaxTextExtent,"%smagick-XXXXXXXX",directory);
398 (void) FormatMagickString(path,MaxTextExtent,"%s%smagick-XXXXXXXX",
399 directory,DirectorySeparator);
400 directory=DestroyString(directory);
401 if (*DirectorySeparator != '/')
402 for (p=path; *p != '\0'; p++)
403 if (*p == *DirectorySeparator)
408 MagickExport int AcquireUniqueFileResource(char *path)
410 #if !defined(O_NOFOLLOW)
413 #if !defined(TMP_MAX)
414 # define TMP_MAX 238328
431 portable_filename[65] =
432 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
440 assert(path != (char *) NULL);
441 (void) LogMagickEvent(ResourceEvent,GetMagickModule(),"%s",path);
442 if (random_info == (RandomInfo *) NULL)
443 random_info=AcquireRandomInfo();
445 for (i=0; i < TMP_MAX; i++)
448 Get temporary pathname.
450 (void) GetPathTemplate(path);
451 #if defined(MAGICKCORE_HAVE_MKSTEMP)
454 setmode(file,O_BINARY);
459 key=GetRandomKey(random_info,8);
460 p=path+strlen(path)-8;
461 datum=GetStringInfoDatum(key);
462 for (i=0; i < 8; i++)
464 c=(int) (datum[i] & 0x3f);
465 *p++=portable_filename[c];
467 key=DestroyStringInfo(key);
468 file=open(path,O_RDWR | O_CREAT | O_EXCL | O_BINARY | O_NOFOLLOW,S_MODE);
469 if ((file > 0) || (errno != EEXIST))
472 (void) LogMagickEvent(ResourceEvent,GetMagickModule(),"%s",path);
475 if (resource_semaphore == (SemaphoreInfo *) NULL)
476 AcquireSemaphoreInfo(&resource_semaphore);
477 LockSemaphoreInfo(resource_semaphore);
478 if (temporary_resources == (SplayTreeInfo *) NULL)
479 temporary_resources=NewSplayTree(CompareSplayTreeString,
480 RelinquishMagickMemory,DestroyTemporaryResources);
481 UnlockSemaphoreInfo(resource_semaphore);
482 resource=ConstantString(path);
483 (void) AddValueToSplayTree(temporary_resources,resource,resource);
488 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
492 % G e t M a g i c k R e s o u r c e %
496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
498 % GetMagickResource() returns the specified resource.
500 % The format of the GetMagickResource() method is:
502 % MagickSizeType GetMagickResource(const ResourceType type)
504 % A description of each parameter follows:
506 % o type: the type of resource.
509 MagickExport MagickSizeType GetMagickResource(const ResourceType type)
515 LockSemaphoreInfo(resource_semaphore);
520 resource=(MagickSizeType) resource_info.area;
525 resource=(MagickSizeType) resource_info.memory;
530 resource=(MagickSizeType) resource_info.map;
535 resource=(MagickSizeType) resource_info.disk;
540 resource=(MagickSizeType) resource_info.file;
545 resource=(MagickSizeType) resource_info.thread;
550 resource=(MagickSizeType) resource_info.time;
556 UnlockSemaphoreInfo(resource_semaphore);
561 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
565 % G e t M a g i c k R e s o u r c e L i m i t %
569 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
571 % GetMagickResource() returns the specified resource limit.
573 % The format of the GetMagickResourceLimit() method is:
575 % MagickSizeType GetMagickResourceLimit(const ResourceType type)
577 % A description of each parameter follows:
579 % o type: the type of resource.
582 MagickExport MagickSizeType GetMagickResourceLimit(const ResourceType type)
588 if (resource_semaphore == (SemaphoreInfo *) NULL)
589 AcquireSemaphoreInfo(&resource_semaphore);
590 LockSemaphoreInfo(resource_semaphore);
595 resource=resource_info.area_limit;
600 resource=resource_info.memory_limit;
605 resource=resource_info.map_limit;
610 resource=resource_info.disk_limit;
615 resource=resource_info.file_limit;
620 resource=resource_info.thread_limit;
625 resource=resource_info.time_limit;
631 UnlockSemaphoreInfo(resource_semaphore);
636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
640 % L i s t M a g i c k R e s o u r c e I n f o %
644 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
646 % ListMagickResourceInfo() lists the resource info to a file.
648 % The format of the ListMagickResourceInfo method is:
650 % MagickBooleanType ListMagickResourceInfo(FILE *file,
651 % ExceptionInfo *exception)
653 % A description of each parameter follows.
655 % o file: An pointer to a FILE.
657 % o exception: return any errors or warnings in this structure.
660 MagickExport MagickBooleanType ListMagickResourceInfo(FILE *file,
661 ExceptionInfo *magick_unused(exception))
664 area_limit[MaxTextExtent],
665 disk_limit[MaxTextExtent],
666 map_limit[MaxTextExtent],
667 memory_limit[MaxTextExtent],
668 time_limit[MaxTextExtent];
670 if (file == (const FILE *) NULL)
672 if (resource_semaphore == (SemaphoreInfo *) NULL)
673 AcquireSemaphoreInfo(&resource_semaphore);
674 LockSemaphoreInfo(resource_semaphore);
675 (void) FormatMagickSize(resource_info.area_limit,MagickFalse,area_limit);
676 (void) FormatMagickSize(resource_info.memory_limit,MagickTrue,memory_limit);
677 (void) FormatMagickSize(resource_info.map_limit,MagickTrue,map_limit);
678 (void) FormatMagickSize(resource_info.disk_limit,MagickFalse,disk_limit);
679 (void) CopyMagickString(time_limit,"unlimited",MaxTextExtent);
680 if (resource_info.time_limit != MagickResourceInfinity)
681 (void) FormatMagickString(time_limit,MaxTextExtent,"%lu",(unsigned long)
682 resource_info.time_limit);
683 (void) fprintf(file,"File Area Memory Map"
684 " Disk Thread Time\n");
685 (void) fprintf(file,"--------------------------------------------------------"
686 "-----------------------\n");
687 (void) fprintf(file,"%4lu %10sB %10sB %10sB %10sB %6lu %11s\n",
688 (unsigned long) resource_info.file_limit,area_limit,memory_limit,map_limit,
689 disk_limit,(unsigned long) resource_info.thread_limit,time_limit);
691 UnlockSemaphoreInfo(resource_semaphore);
696 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
700 % R e l i n q u i s h M a g i c k R e s o u r c e %
704 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
706 % RelinquishMagickResource() relinquishes resources of the specified type.
708 % The format of the RelinquishMagickResource() method is:
710 % void RelinquishMagickResource(const ResourceType type,
711 % const MagickSizeType size)
713 % A description of each parameter follows:
715 % o type: the type of resource.
717 % o size: the size of the resource.
720 MagickExport void RelinquishMagickResource(const ResourceType type,
721 const MagickSizeType size)
724 resource_current[MaxTextExtent],
725 resource_limit[MaxTextExtent],
726 resource_request[MaxTextExtent];
728 (void) FormatMagickSize(size,MagickFalse,resource_request);
729 if (resource_semaphore == (SemaphoreInfo *) NULL)
730 AcquireSemaphoreInfo(&resource_semaphore);
731 LockSemaphoreInfo(resource_semaphore);
736 resource_info.area=(MagickOffsetType) size;
737 (void) FormatMagickSize((MagickSizeType) resource_info.area,MagickFalse,
739 (void) FormatMagickSize(resource_info.area_limit,MagickFalse,
745 resource_info.memory-=size;
746 (void) FormatMagickSize((MagickSizeType) resource_info.memory,
747 MagickTrue,resource_current);
748 (void) FormatMagickSize(resource_info.memory_limit,MagickTrue,
754 resource_info.map-=size;
755 (void) FormatMagickSize((MagickSizeType) resource_info.map,MagickTrue,
757 (void) FormatMagickSize(resource_info.map_limit,MagickTrue,
763 resource_info.disk-=size;
764 (void) FormatMagickSize((MagickSizeType) resource_info.disk,MagickFalse,
766 (void) FormatMagickSize(resource_info.disk_limit,MagickFalse,
772 resource_info.file-=size;
773 (void) FormatMagickSize((MagickSizeType) resource_info.file,MagickFalse,
775 (void) FormatMagickSize((MagickSizeType) resource_info.file_limit,
776 MagickFalse,resource_limit);
781 resource_info.thread-=size;
782 (void) FormatMagickSize((MagickSizeType) resource_info.thread,MagickFalse,
784 (void) FormatMagickSize((MagickSizeType) resource_info.thread_limit,
785 MagickFalse,resource_limit);
790 resource_info.time-=size;
791 (void) FormatMagickSize((MagickSizeType) resource_info.time,MagickFalse,
793 (void) FormatMagickSize((MagickSizeType) resource_info.time_limit,
794 MagickFalse,resource_limit);
800 UnlockSemaphoreInfo(resource_semaphore);
801 (void) LogMagickEvent(ResourceEvent,GetMagickModule(),"%s: %sB/%sB/%sB",
802 MagickOptionToMnemonic(MagickResourceOptions,(long) type),resource_request,
803 resource_current,resource_limit);
807 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
811 % 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 %
815 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
817 % RelinquishUniqueFileResource() relinquishes a unique file resource.
819 % The format of the RelinquishUniqueFileResource() method is:
821 % MagickBooleanType RelinquishUniqueFileResource(const char *path)
823 % A description of each parameter follows:
825 % o name: the name of the temporary resource.
828 MagickExport MagickBooleanType RelinquishUniqueFileResource(const char *path)
831 cache_path[MaxTextExtent];
833 assert(path != (const char *) NULL);
834 (void) LogMagickEvent(ResourceEvent,GetMagickModule(),"%s",path);
835 if (temporary_resources != (SplayTreeInfo *) NULL)
840 ResetSplayTreeIterator(temporary_resources);
841 p=(char *) GetNextKeyInSplayTree(temporary_resources);
842 while (p != (char *) NULL)
844 if (LocaleCompare(p,path) == 0)
846 p=(char *) GetNextKeyInSplayTree(temporary_resources);
848 if (p != (char *) NULL)
849 (void) DeleteNodeFromSplayTree(temporary_resources,p);
851 (void) CopyMagickString(cache_path,path,MaxTextExtent);
852 AppendImageFormat("cache",cache_path);
853 (void) remove(cache_path);
854 return(remove(path) == 0 ? MagickTrue : MagickFalse);
858 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
862 + R e s o u r c e C o m p o n e n t G e n e s i s %
866 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
868 % ResourceComponentGenesis() instantiates the resource component.
870 % The format of the ResourceComponentGenesis method is:
872 % MagickBooleanType ResourceComponentGenesis(void)
876 static inline unsigned long MagickMax(const unsigned long x,
877 const unsigned long y)
884 static inline MagickSizeType StringToSizeType(const char *string,
885 const double interval)
890 value=SiPrefixToDouble(string,interval);
891 if (value >= (double) MagickULLConstant(~0))
892 return(MagickULLConstant(~0));
893 return((MagickSizeType) value);
896 MagickExport MagickBooleanType ResourceComponentGenesis(void)
910 Set Magick resource limits.
912 AcquireSemaphoreInfo(&resource_semaphore);
913 pagesize=GetMagickPageSize();
915 #if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PHYS_PAGES)
916 pages=sysconf(_SC_PHYS_PAGES);
918 memory=(MagickSizeType) pages*pagesize;
919 if ((pagesize <= 0) || (pages <= 0))
920 memory=2048UL*1024UL*1024UL;
921 #if defined(PixelCacheThreshold)
922 memory=PixelCacheThreshold;
924 (void) SetMagickResourceLimit(AreaResource,2UL*memory);
925 (void) SetMagickResourceLimit(MemoryResource,3UL*memory/2UL);
926 (void) SetMagickResourceLimit(MapResource,4UL*memory);
927 limit=GetEnvironmentValue("MAGICK_AREA_LIMIT");
928 if (limit == (char *) NULL)
929 limit=GetPolicyValue("area");
930 if (limit != (char *) NULL)
932 (void) SetMagickResourceLimit(AreaResource,StringToSizeType(limit,100.0));
933 limit=DestroyString(limit);
935 limit=GetEnvironmentValue("MAGICK_MEMORY_LIMIT");
936 if (limit == (char *) NULL)
937 limit=GetPolicyValue("memory");
938 if (limit != (char *) NULL)
940 (void) SetMagickResourceLimit(MemoryResource,
941 StringToSizeType(limit,100.0));
942 limit=DestroyString(limit);
944 limit=GetEnvironmentValue("MAGICK_MAP_LIMIT");
945 if (limit == (char *) NULL)
946 limit=GetPolicyValue("map");
947 if (limit != (char *) NULL)
949 (void) SetMagickResourceLimit(MapResource,StringToSizeType(limit,100.0));
950 limit=DestroyString(limit);
952 limit=GetEnvironmentValue("MAGICK_DISK_LIMIT");
953 if (limit == (char *) NULL)
954 limit=GetPolicyValue("disk");
955 if (limit != (char *) NULL)
957 (void) SetMagickResourceLimit(DiskResource,StringToSizeType(limit,100.0));
958 limit=DestroyString(limit);
961 #if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_OPEN_MAX)
962 files=sysconf(_SC_OPEN_MAX);
964 #if defined(MAGICKCORE_HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
970 if (getrlimit(RLIMIT_NOFILE,&resources) != -1)
971 files=(long) resources.rlim_cur;
974 #if defined(MAGICKCORE_HAVE_GETDTABLESIZE) && defined(MAGICKCORE_POSIX_SUPPORT)
976 files=getdtablesize();
980 (void) SetMagickResourceLimit(FileResource,MagickMax((unsigned long)
982 limit=GetEnvironmentValue("MAGICK_FILE_LIMIT");
983 if (limit == (char *) NULL)
984 limit=GetPolicyValue("file");
985 if (limit != (char *) NULL)
987 (void) SetMagickResourceLimit(FileResource,StringToSizeType(limit,100.0));
988 limit=DestroyString(limit);
990 (void) SetMagickResourceLimit(ThreadResource,GetOpenMPMaximumThreads());
991 limit=GetEnvironmentValue("MAGICK_THREAD_LIMIT");
992 if (limit == (char *) NULL)
993 limit=GetPolicyValue("thread");
994 if (limit != (char *) NULL)
996 SetOpenMPMaximumThreads(StringToUnsignedLong(limit));
997 (void) SetMagickResourceLimit(ThreadResource,StringToSizeType(limit,
999 limit=DestroyString(limit);
1001 limit=GetEnvironmentValue("MAGICK_TIME_LIMIT");
1002 if (limit == (char *) NULL)
1003 limit=GetPolicyValue("time");
1004 if (limit != (char *) NULL)
1006 (void) SetMagickResourceLimit(TimeResource,StringToSizeType(limit,100.0));
1007 limit=DestroyString(limit);
1013 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1017 + 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 %
1021 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1023 % ResourceComponentTerminus() destroys the resource component.
1025 % The format of the ResourceComponentTerminus() method is:
1027 % ResourceComponentTerminus(void)
1030 MagickExport void ResourceComponentTerminus(void)
1032 if (resource_semaphore == (SemaphoreInfo *) NULL)
1033 AcquireSemaphoreInfo(&resource_semaphore);
1034 LockSemaphoreInfo(resource_semaphore);
1035 if (temporary_resources != (SplayTreeInfo *) NULL)
1036 temporary_resources=DestroySplayTree(temporary_resources);
1037 if (random_info != (RandomInfo *) NULL)
1038 random_info=DestroyRandomInfo(random_info);
1039 UnlockSemaphoreInfo(resource_semaphore);
1040 DestroySemaphoreInfo(&resource_semaphore);
1044 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1048 % S e t M a g i c k R e s o u r c e L i m i t %
1052 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1054 % SetMagickResourceLimit() sets the limit for a particular resource.
1056 % The format of the SetMagickResourceLimit() method is:
1058 % MagickBooleanType SetMagickResourceLimit(const ResourceType type,
1059 % const MagickSizeType limit)
1061 % A description of each parameter follows:
1063 % o type: the type of resource.
1065 % o limit: the maximum limit for the resource.
1068 MagickExport MagickBooleanType SetMagickResourceLimit(const ResourceType type,
1069 const MagickSizeType limit)
1071 if (resource_semaphore == (SemaphoreInfo *) NULL)
1072 AcquireSemaphoreInfo(&resource_semaphore);
1073 LockSemaphoreInfo(resource_semaphore);
1078 resource_info.area_limit=limit;
1081 case MemoryResource:
1083 resource_info.memory_limit=limit;
1088 resource_info.map_limit=limit;
1093 resource_info.disk_limit=limit;
1098 resource_info.file_limit=limit;
1101 case ThreadResource:
1103 SetOpenMPMaximumThreads((unsigned long) limit);
1104 resource_info.thread_limit=GetOpenMPMaximumThreads();
1109 resource_info.time_limit=limit;
1115 UnlockSemaphoreInfo(resource_semaphore);