MagickCore 7.0.10
resource.c
Go to the documentation of this file.
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
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 %
11% %
12% %
13% Get/Set Wizard's Toolkit Resources. %
14% %
15% Software Design %
16% Cristy %
17% September 2002 %
18% %
19% %
20% Copyright 1999-2021 ImageWizard Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% https://imagemagick.org/script/license.php %
27% %
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. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
42#include "wizard/studio.h"
43#include "wizard/exception.h"
45#include "wizard/hashmap.h"
46#include "wizard/log.h"
47#include "wizard/memory_.h"
48#include "wizard/option.h"
49#include "wizard/random_.h"
50#include "wizard/resource_.h"
51#include "wizard/semaphore.h"
52#include "wizard/signature.h"
53#include "wizard/splay-tree.h"
54#include "wizard/string_.h"
56#include "wizard/token.h"
57#include "wizard/utility.h"
59
60/*
61 Define declarations.
62*/
63#define WizardResourceInfinity (~0UL)
64
65/*
66 Typedef declarations.
67*/
68typedef struct _ResourceInfo
69{
76
84
85/*
86 Global declarations.
87*/
88static RandomInfo
90
91static ResourceInfo
93 {
99 WizardULLConstant(1536)*1024*1024/sizeof(void *),
100 WizardULLConstant(1536)*1024*1024,
101 WizardULLConstant(3072)*1024*1024,
104 };
105
106static SemaphoreInfo
108
109static SplayTreeInfo
111
112/*
113%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114% %
115% %
116% %
117% A c q u i r e U n i q u e F i l e n a m e %
118% %
119% %
120% %
121%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122%
123% AcquireUniqueFilename() replaces the contents of path by a unique path name.
124%
125% The format of the AcquireUniqueFilename method is:
126%
127% WizardBooleanType AcquireUniqueFilename(char *path,Exception *exception)
128%
129 A description of each parameter follows.
130%
131% o path: Specifies a pointer to an array of characters. The unique path
132% name is returned in this array.
133%
134% o exception: Return any errors or warnings in this structure.
135%
136*/
138 ExceptionInfo *exception)
139{
140 int
141 file;
142
143 file=AcquireUniqueFileResource("",path,exception);
144 if (file == -1)
145 return(WizardFalse);
146 if (close(file) == -1)
148 "unable to close file `%s': %s",path,strerror(errno));
149 return(WizardTrue);
150}
151
152/*
153%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154% %
155% %
156% %
157% 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 %
158% %
159% %
160% %
161%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162%
163% AcquireUniqueFileResource() returns a unique file name, and returns a file
164% descriptor for the file open for reading and writing.
165%
166% The format of the AcquireUniqueFileResource() method is:
167%
168% int AcquireUniqueFileResource(const char *path,char *filename,
169% ExceptionInfo *exception)
170%
171% A description of each parameter follows:
172%
173% o path: Open the file in this directory path.
174%
175% o filename: The unique filename is returned in this array.
176%
177% o exception: Return any errors or warnings in this structure.
178%
179*/
180
181static void *DestroyTemporaryResources(void *temporary_resource)
182{
183 (void) remove_utf8((char *) temporary_resource);
184 temporary_resource=DestroyString((char *) temporary_resource);
185 return((void *) NULL);
186}
187
188WizardExport WizardBooleanType GetPathTemplate(const char *path,char *filename)
189{
190 char
191 *directory;
192
193 int
194 status;
195
196 char
197 *p;
198
199 struct stat
200 file_info;
201
202 (void) FormatLocaleString(filename,WizardPathExtent,
203 "wizard-%.20gXXXXXXXXXXXX",(double) getpid());
204 if ((path == (const char *) NULL) || (*path != '\0'))
205 directory=ConstantString(path);
206 else
207 {
208 directory=GetEnvironmentValue("WIZARD_TMPDIR");
209 if (directory == (char *) NULL)
210 directory=GetEnvironmentValue("TMPDIR");
211#if defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT) || defined(__OS2__) || defined(__CYGWIN__)
212 if (directory == (char *) NULL)
213 directory=GetEnvironmentValue("TMP");
214 if (directory == (char *) NULL)
215 directory=GetEnvironmentValue("TEMP");
216#endif
217#if defined(P_tmpdir)
218 if (directory == (char *) NULL)
219 directory=ConstantString(P_tmpdir);
220#endif
221 if (directory == (char *) NULL)
222 return(WizardTrue);
223 }
224 if (strlen(directory) > (WizardPathExtent-25))
225 {
226 directory=(char *) RelinquishWizardMemory(directory);
227 return(WizardFalse);
228 }
229 status=stat_utf8(directory,&file_info);
230 if ((status != 0) || !S_ISDIR(file_info.st_mode))
231 {
232 directory=(char *) RelinquishWizardMemory(directory);
233 return(WizardFalse);
234 }
235 if (directory[strlen(directory)-1] == *DirectorySeparator)
236 (void) FormatLocaleString(filename,WizardPathExtent,
237 "%swizard-%.20gXXXXXXXXXXXX",directory,(double) getpid());
238 else
239 (void) FormatLocaleString(filename,WizardPathExtent,
240 "%s%swizard-%.20gXXXXXXXXXXXX",directory,DirectorySeparator,(double)
241 getpid());
242 directory=(char *) RelinquishWizardMemory(directory);
243 if (*DirectorySeparator != '/')
244 for (p=filename; *p != '\0'; p++)
245 if (*p == *DirectorySeparator)
246 *p='/';
247 return(WizardTrue);
248}
249
250WizardExport int AcquireUniqueFileResource(const char *path,char *filename,
251 ExceptionInfo *exception)
252{
253#if !defined(O_NOFOLLOW)
254#define O_NOFOLLOW 0
255#endif
256#if !defined(TMP_MAX)
257# define TMP_MAX 238328
258#endif
259
260 int
261 c,
262 file;
263
264 char
265 *p;
266
267 ssize_t
268 i;
269
270 static const char
271 portable_filename[] =
272 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
273
275 *key;
276
277 unsigned char
278 *datum;
279
280 WizardAssert(ResourceDomain,path != (const char *) NULL);
281 WizardAssert(ResourceDomain,filename != (char *) NULL);
282 (void) LogWizardEvent(ResourceEvent,GetWizardModule(),"%s",path);
283 if (random_info == (RandomInfo *) NULL)
284 {
286 if (random_info == (RandomInfo *) NULL)
289 }
290 file=(-1);
291 for (i=0; i < (ssize_t) TMP_MAX; i++)
292 {
293 ssize_t
294 j;
295
296 /*
297 Get temporary pathname.
298 */
299 (void) GetPathTemplate(path,filename);
301 p=filename+strlen(filename)-12;
302 datum=GetStringInfoDatum(key);
303 for (j=0; j < (ssize_t) GetStringInfoLength(key); j++)
304 {
305 c=(int) (datum[j] & 0x3f);
306 *p++=portable_filename[c];
307 }
308 key=DestroyStringInfo(key);
309#if defined(WIZARDSTOOLKIT_HAVE_MKSTEMP)
310 file=mkstemp(filename);
311 if (file != -1)
312 {
313#if defined(WIZARDSTOOLKIT_HAVE_FCHMOD)
314 (void) fchmod(file,0600);
315#endif
316#if defined(__OS2__)
317 setmode(file,O_BINARY);
318#endif
319 break;
320 }
321#endif
323 p=filename+strlen(filename)-12;
324 datum=GetStringInfoDatum(key);
325 for (j=0; j < (ssize_t) GetStringInfoLength(key); j++)
326 {
327 c=(int) (datum[j] & 0x3f);
328 *p++=portable_filename[c];
329 }
330 key=DestroyStringInfo(key);
331 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY | O_NOFOLLOW,
332 S_MODE);
333 if ((file >= 0) || (errno != EEXIST))
334 break;
335 }
336 (void) LogWizardEvent(ResourceEvent,GetWizardModule(),"%s",filename);
337 if (file == -1)
338 return(file);
340 {
341 if (close(file) == -1)
343 "unable to close file `%s': %s",filename,strerror(errno));
344 if (remove_utf8(path) == -1)
346 "unable to remove file `%s': %s",filename,strerror(errno));
347 return(-1);
348 }
349 if (resource_semaphore == (SemaphoreInfo *) NULL)
352 if (temporary_resources == (SplayTreeInfo *) NULL)
354 DestroyTemporaryResources,(void *(*)(void *)) NULL);
357 (const void *) NULL);
358 return(file);
359}
360
361/*
362%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
363% %
364% %
365% %
366% A c q u i r e W i z a r d R e s o u r c e %
367% %
368% %
369% %
370%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
371%
372% AcquireWizardResource() acquires resources of the specified type.
373% WizardFalse is returned if the specified resource is exhausted otherwise
374% WizardTrue.
375%
376% The format of the AcquireWizardResource() method is:
377%
378% WizardBooleanType AcquireWizardResource(const ResourceType type,
379% const WizardSizeType size)
380%
381% A description of each parameter follows:
382%
383% o type: The type of resource.
384%
385% o size: The number of bytes needed from for this resource.
386%
387*/
389 const WizardSizeType size)
390{
391 char
392 resource_current[WizardFormatExtent] = "",
393 resource_limit[WizardFormatExtent] = "",
394 resource_request[WizardFormatExtent] = "";
395
397 status;
398
400 limit;
401
402 status=WizardFalse;
403 (void) FormatWizardSize(size,WizardFalse,WizardFormatExtent,resource_request);
404 if (resource_semaphore == (SemaphoreInfo *) NULL)
407 switch (type)
408 {
409 case AreaResource:
410 {
414 (size < limit) ? WizardTrue : WizardFalse;
416 WizardFormatExtent,resource_current);
418 WizardFormatExtent,resource_limit);
419 break;
420 }
421 case MemoryResource:
422 {
423 resource_info.memory+=size;
425 if ((limit == WizardResourceInfinity) ||
427 status=WizardTrue;
428 else
431 WizardFormatExtent,resource_current);
433 WizardFormatExtent,resource_limit);
434 break;
435 }
436 case MapResource:
437 {
438 resource_info.map+=size;
440 if ((limit == WizardResourceInfinity) ||
442 status=WizardTrue;
443 else
446 WizardFormatExtent,resource_current);
448 WizardFormatExtent,resource_limit);
449 break;
450 }
451 case DiskResource:
452 {
453 resource_info.disk+=size;
455 if ((limit == WizardResourceInfinity) ||
457 status=WizardTrue;
458 else
461 WizardFormatExtent,resource_current);
463 WizardFormatExtent,resource_limit);
464 break;
465 }
466 case FileResource:
467 {
468 resource_info.file+=size;
470 if ((limit == WizardResourceInfinity) ||
472 status=WizardTrue;
473 else
476 WizardFormatExtent,resource_current);
478 WizardFalse,WizardFormatExtent,resource_limit);
479 break;
480 }
481 default:
482 break;
483 }
485 (void) LogWizardEvent(ResourceEvent,GetWizardModule(),"%s: %s/%s/%s",
487 resource_request,resource_current,resource_limit);
488 return(status);
489}
490
491/*
492%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
493% %
494% %
495% %
496+ 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 %
497% %
498% %
499% %
500%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
501%
502% AsynchronousResourceComponentTerminus() destroys the resource environment.
503% It differs from ResourceComponentTerminus() in that it can be called from a
504% asynchronous signal handler.
505%
506% The format of the ResourceComponentTerminus() method is:
507%
508% ResourceComponentTerminus(void)
509%
510*/
512{
513 const char
514 *path;
515
516 if (temporary_resources == (SplayTreeInfo *) NULL)
517 return;
518 /*
519 Remove any lingering temporary files.
520 */
522 path=(const char *) GetNextKeyInSplayTree(temporary_resources);
523 while (path != (const char *) NULL)
524 {
525 (void) remove_utf8(path);
526 path=(const char *) GetNextKeyInSplayTree(temporary_resources);
527 }
528 if (random_info != (RandomInfo *) NULL)
530}
531
532/*
533%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
534% %
535% %
536% %
537% G e t W i z a r d R e s o u r c e %
538% %
539% %
540% %
541%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
542%
543% GetWizardResource() returns the the specified resource in megabytes.
544%
545% The format of the GetWizardResource() method is:
546%
547% WizardSizeType GetWizardResource(const ResourceType type)
548%
549% A description of each parameter follows:
550%
551% o type: The type of resource.
552%
553*/
555{
557 resource;
558
559 resource=0;
560 if (resource_semaphore == (SemaphoreInfo *) NULL)
563 switch (type)
564 {
565 case AreaResource:
566 {
568 break;
569 }
570 case MemoryResource:
571 {
573 break;
574 }
575 case MapResource:
576 {
578 break;
579 }
580 case DiskResource:
581 {
583 break;
584 }
585 case FileResource:
586 {
588 break;
589 }
590 default:
591 break;
592 }
594 return(resource);
595}
596
597/*
598%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
599% %
600% %
601% %
602% G e t W i z a r d R e s o u r c e L i m i t %
603% %
604% %
605% %
606%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
607%
608% GetWizardResourceLimit() returns the the specified resource limit in
609% megabytes.
610%
611% The format of the GetWizardResourceLimit() method is:
612%
613% size_t GetWizardResourceLimit(const ResourceType type)
614%
615% A description of each parameter follows:
616%
617% o type: The type of resource.
618%
619*/
621{
623 resource;
624
625 resource=0;
626 if (resource_semaphore == (SemaphoreInfo *) NULL)
629 switch (type)
630 {
631 case AreaResource:
632 {
634 break;
635 }
636 case MemoryResource:
637 {
639 break;
640 }
641 case MapResource:
642 {
644 break;
645 }
646 case DiskResource:
647 {
649 break;
650 }
651 case FileResource:
652 {
654 break;
655 }
656 default:
657 break;
658 }
660 return(resource);
661}
662
663/*
664%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
665% %
666% %
667% %
668% L i s t W i z a r d R e s o u r c e I n f o %
669% %
670% %
671% %
672%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
673%
674% ListWizardResourceInfo() lists the resource info to a file.
675%
676% The format of the ListWizardResourceInfo method is:
677%
678% WizardBooleanType ListWizardResourceInfo(FILE *file,
679% ExceptionInfo *exception)
680%
681% A description of each parameter follows.
682%
683% o file: An pointer to a FILE.
684%
685% o exception: Return any errors or warnings in this structure.
686%
687*/
689 ExceptionInfo *wizard_unused(exception))
690{
691 char
692 area_limit[WizardFormatExtent],
693 disk_limit[WizardFormatExtent],
694 map_limit[WizardFormatExtent],
695 memory_limit[WizardFormatExtent];
696
697 if (file == (const FILE *) NULL)
698 file=stdout;
699 if (resource_semaphore == (SemaphoreInfo *) NULL)
703 WizardFormatExtent,area_limit);
705 map_limit);
707 WizardFormatExtent,memory_limit);
708 (void) CopyWizardString(disk_limit,"unlimited",WizardFormatExtent);
711 WizardFormatExtent,disk_limit);
712 (void) fprintf(file,"File Area Memory Map Disk\n");
713 (void) fprintf(file,"----------------------------------------------------\n");
714 (void) fprintf(file,"%4g %9s %9s %9s %9s\n",(double)
715 resource_info.file_limit,area_limit,memory_limit,map_limit,disk_limit);
716 (void) fflush(file);
718 return(WizardTrue);
719}
720
721/*
722%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
723% %
724% %
725% %
726% R e l i n q u i s h W i z a r d R e s o u r c e %
727% %
728% %
729% %
730%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
731%
732% RelinquishWizardResource() relinquishes resources of the specified type.
733%
734% The format of the RelinquishWizardResource() method is:
735%
736% void RelinquishWizardResource(const ResourceType type,
737% const WizardSizeType size)
738%
739% A description of each parameter follows:
740%
741% o type: The type of resource.
742%
743% o size: The size of the resource.
744%
745*/
747 const WizardSizeType size)
748{
749 char
750 resource_current[WizardFormatExtent] = "",
751 resource_limit[WizardFormatExtent] = "",
752 resource_request[WizardFormatExtent] = "";
753
754 (void) FormatWizardSize(size,WizardFalse,WizardFormatExtent,resource_request);
755 if (resource_semaphore == (SemaphoreInfo *) NULL)
758 switch (type)
759 {
760 case AreaResource:
761 {
764 WizardFalse,WizardFormatExtent,resource_current);
766 WizardFalse,WizardFormatExtent,resource_limit);
767 break;
768 }
769 case MemoryResource:
770 {
771 resource_info.memory-=size;
772 assert(resource_info.memory >= 0);
774 WizardFormatExtent,resource_current);
776 WizardFormatExtent,resource_limit);
777 break;
778 }
779 case MapResource:
780 {
781 resource_info.map-=size;
782 assert(resource_info.map >= 0);
784 WizardFormatExtent,resource_current);
786 WizardFormatExtent,resource_limit);
787 break;
788 }
789 case DiskResource:
790 {
791 resource_info.disk-=size;
792 assert(resource_info.disk >= 0);
794 WizardFormatExtent,resource_current);
796 WizardFormatExtent,resource_limit);
797 break;
798 }
799 case FileResource:
800 {
801 resource_info.file-=size;
802 assert(resource_info.file >= 0);
804 WizardFormatExtent,resource_current);
806 WizardFalse,WizardFormatExtent,resource_limit);
807 break;
808 }
809 default:
810 break;
811 }
813 (void) LogWizardEvent(ResourceEvent,GetWizardModule(),"%s: %s/%s/%s",
815 resource_request,resource_current,resource_limit);
816}
817
818/*
819%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
820% %
821% %
822% %
823% 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 %
824% %
825% %
826% %
827%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
828%
829% RelinquishUniqueFileResource() relinquishes a unique file resource.
830%
831% The format of the RelinquishUniqueFileResource() method is:
832%
833% WizardBooleanType RelinquishUniqueFileResource(const char *path,
834% const WizardBooleanType trash))
835%
836% A description of each parameter follows:
837%
838% o name: the name of the temporary resource.
839%
840% o trash: a value other than 0 trashs the file at the specified path.
841%
842*/
844 const WizardBooleanType trash)
845{
846 WizardAssert(ResourceDomain,path != (const char *) NULL);
847 (void) LogWizardEvent(ResourceEvent,GetWizardModule(),"%s",path);
848 if (temporary_resources != (SplayTreeInfo *) NULL)
849 {
850 char
851 *p;
852
855 while (p != (char *) NULL)
856 {
857 if (LocaleCompare(p,path) == 0)
858 break;
860 }
861 if (p != (char *) NULL)
863 }
864 if (trash == WizardFalse)
865 return(WizardTrue);
866 return(remove_utf8(path) == 0 ? WizardTrue : WizardFalse);
867}
868
869/*
870%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
871% %
872% %
873% %
874+ R e s o u r c e C o m p o n e n t G e n e s i s %
875% %
876% %
877% %
878%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
879%
880% ResourceComponentGenesis() instantiates the resource component.
881%
882% The format of the ResourceComponentGenesis method is:
883%
884% WizardBooleanType ResourceComponentGenesis(void)
885%
886*/
888{
889 char
890 *limit;
891
892 ssize_t
893 files,
894 pages,
895 pagesize;
896
897 size_t
898 memory;
899
900 /*
901 Set Wizard resource limits.
902 */
903 if (resource_semaphore == (SemaphoreInfo *) NULL)
905 pagesize=(-1);
906#if defined(WIZARDSTOOLKIT_HAVE_SYSCONF) && defined(_SC_PAGESIZE)
907 pagesize=sysconf(_SC_PAGESIZE);
908#elif defined(WIZARDSTOOLKIT_HAVE_GETPAGESIZE) && defined(POSIX)
909 pagesize=getpagesize();
910#endif
911 pages=(-1);
912#if defined(WIZARDSTOOLKIT_HAVE_SYSCONF) && defined(_SC_PHYS_PAGES)
913 pages=sysconf(_SC_PHYS_PAGES);
914#endif
915 memory=(size_t) ((pages+512)/1024)*((pagesize+512)/1024);
916 if ((pagesize <= 0) || (pages <= 0))
917 memory=2048UL;
918#if defined(PixelCacheThreshold)
919 memory=PixelCacheThreshold;
920#endif
921 (void) SetWizardResourceLimit(AreaResource,memory/sizeof(void *));
923 (void) SetWizardResourceLimit(MapResource,2*memory);
924 limit=GetEnvironmentValue("WIZARD_AREA_LIMIT");
925 if (limit != (char *) NULL)
926 {
928 limit=DestroyString(limit);
929 }
930 limit=GetEnvironmentValue("WIZARD_MEMORY_LIMIT");
931 if (limit != (char *) NULL)
932 {
934 limit=DestroyString(limit);
935 }
936 limit=GetEnvironmentValue("WIZARD_MAP_LIMIT");
937 if (limit != (char *) NULL)
938 {
940 limit=DestroyString(limit);
941 }
942 limit=GetEnvironmentValue("WIZARD_DISK_LIMIT");
943 if (limit != (char *) NULL)
944 {
946 limit=DestroyString(limit);
947 }
948 files=(-1);
949#if defined(WIZARDSTOOLKIT_HAVE_SYSCONF) && defined(_SC_OPEN_MAX)
950 files=sysconf(_SC_OPEN_MAX);
951#elif defined(WIZARDSTOOLKIT_HAVE_GETDTABLESIZE) && defined(POSIX)
952 files=getdtablesize();
953#endif
955 WizardMax(3L*files/4L,64L));
956 limit=GetEnvironmentValue("WIZARD_FILE_LIMIT");
957 if (limit != (char *) NULL)
958 {
960 limit=DestroyString(limit);
961 }
962 return(WizardTrue);
963}
964
965/*
966%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
967% %
968% %
969% %
970+ R e s o u r c e C o m p o n e n t G e n e s i s %
971% %
972% %
973% %
974%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
975%
976% ResourceComponentTerminus() destroys the resource component.
977%
978% The format of the ResourceComponentTerminus() method is:
979%
980% ResourceComponentTerminus(void)
981%
982*/
984{
985 if (resource_semaphore == (SemaphoreInfo *) NULL)
988 if (temporary_resources != (SplayTreeInfo *) NULL)
992}
993
994/*
995%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
996% %
997% %
998% %
999% S e t W i z a r d R e s o u r c e L i m i t %
1000% %
1001% %
1002% %
1003%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1004%
1005% SetWizardResourceLimit() sets the limit for a particular resource in bytes.
1006%
1007% The format of the SetWizardResourceLimit() method is:
1008%
1009% WizardBooleanType SetWizardResourceLimit(const ResourceType type,
1010% const WizardResourceType limit)
1011%
1012% A description of each parameter follows:
1013%
1014% o type: The type of resource.
1015%
1016% o limit: The maximum limit for the resource.
1017%
1018*/
1020 const WizardSizeType limit)
1021{
1022 if (resource_semaphore == (SemaphoreInfo *) NULL)
1025 switch (type)
1026 {
1027 case AreaResource:
1028 {
1030 break;
1031 }
1032 case MemoryResource:
1033 {
1035 break;
1036 }
1037 case MapResource:
1038 {
1040 break;
1041 }
1042 case DiskResource:
1043 {
1045 break;
1046 }
1047 case FileResource:
1048 {
1050 break;
1051 }
1052 default:
1053 break;
1054 }
1056 return(WizardTrue);
1057}
#define WizardAssert(domain, predicate)
WizardExport WizardBooleanType ThrowWizardException(ExceptionInfo *exception, const char *module, const char *function, const size_t line, const ExceptionType severity, const char *format,...)
Definition exception.c:1029
@ ResourceDomain
Definition exception.h:43
@ ResourceError
Definition exception.h:101
@ SHA2256Hash
Definition hash.h:36
WizardExport ssize_t FormatLocaleString(char *string, const size_t length, const char *format,...)
Definition locale.c:465
WizardBooleanType LogWizardEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition log.c:1390
@ ResourceEvent
Definition log.h:49
#define GetWizardModule()
Definition log.h:30
WizardExport void * RelinquishWizardMemory(void *memory)
Definition memory.c:1039
#define WizardExport
#define WizardPathExtent
#define wizard_unused(x)
WizardExport const char * WizardOptionToMnemonic(const WizardOption option, const ssize_t type)
Definition option.c:531
@ WizardResourceOptions
Definition option.h:53
WizardExport RandomInfo * AcquireRandomInfo(const HashType hash)
Definition random.c:165
WizardExport StringInfo * GetRandomKey(RandomInfo *random_info, const size_t length)
Definition random.c:820
WizardExport RandomInfo * DestroyRandomInfo(RandomInfo *random_info)
Definition random.c:289
WizardExport WizardSizeType GetWizardResourceLimit(const ResourceType type)
Definition resource.c:620
WizardExport WizardBooleanType GetPathTemplate(const char *path, char *filename)
Definition resource.c:188
static void * DestroyTemporaryResources(void *temporary_resource)
Definition resource.c:181
WizardExport WizardBooleanType ResourceComponentGenesis(void)
Definition resource.c:887
WizardExport WizardBooleanType ListWizardResourceInfo(FILE *file, ExceptionInfo *wizard_unused(exception))
Definition resource.c:688
static ResourceInfo resource_info
Definition resource.c:92
WizardExport WizardSizeType GetWizardResource(const ResourceType type)
Definition resource.c:554
WizardExport WizardBooleanType SetWizardResourceLimit(const ResourceType type, const WizardSizeType limit)
Definition resource.c:1019
WizardExport WizardBooleanType AcquireWizardResource(const ResourceType type, const WizardSizeType size)
Definition resource.c:388
static RandomInfo * random_info
Definition resource.c:89
#define O_NOFOLLOW
#define WizardResourceInfinity
Definition resource.c:63
static SemaphoreInfo * resource_semaphore
Definition resource.c:107
WizardExport void RelinquishWizardResource(const ResourceType type, const WizardSizeType size)
Definition resource.c:746
WizardExport WizardBooleanType RelinquishUniqueFileResource(const char *path, const WizardBooleanType trash)
Definition resource.c:843
WizardExport WizardBooleanType AcquireUniqueFilename(char *path, ExceptionInfo *exception)
Definition resource.c:137
WizardExport int AcquireUniqueFileResource(const char *path, char *filename, ExceptionInfo *exception)
Definition resource.c:250
#define TMP_MAX
WizardExport void AsynchronousResourceComponentTerminus(void)
Definition resource.c:511
static SplayTreeInfo * temporary_resources
Definition resource.c:110
WizardExport void ResourceComponentTerminus(void)
Definition resource.c:983
struct _ResourceInfo ResourceInfo
ResourceType
Definition resource_.h:33
@ DiskResource
Definition resource_.h:36
@ AreaResource
Definition resource_.h:35
@ MapResource
Definition resource_.h:38
@ FileResource
Definition resource_.h:37
@ MemoryResource
Definition resource_.h:39
#define WizardFormatExtent
Definition resource_.h:26
WizardExport void RelinquishSemaphoreInfo(SemaphoreInfo **semaphore_info)
Definition semaphore.c:338
WizardExport SemaphoreInfo * AcquireSemaphoreInfo(void)
Definition semaphore.c:156
WizardExport void LockSemaphoreInfo(SemaphoreInfo *semaphore_info)
Definition semaphore.c:282
WizardExport void UnlockSemaphoreInfo(SemaphoreInfo *semaphore_info)
Definition semaphore.c:437
WizardExport void ActivateSemaphoreInfo(SemaphoreInfo **semaphore_info)
Definition semaphore.c:247
WizardExport WizardBooleanType DeleteNodeFromSplayTree(SplayTreeInfo *splay_tree, const void *key)
Definition splay-tree.c:599
WizardExport const void * GetNextKeyInSplayTree(SplayTreeInfo *splay_tree)
Definition splay-tree.c:765
WizardExport void ResetSplayTreeIterator(SplayTreeInfo *splay_tree)
WizardExport int CompareSplayTreeString(const void *target, const void *source)
Definition splay-tree.c:410
WizardExport SplayTreeInfo * DestroySplayTree(SplayTreeInfo *splay_tree)
Definition splay-tree.c:678
WizardExport WizardBooleanType AddValueToSplayTree(SplayTreeInfo *splay_tree, const void *key, const void *value)
Definition splay-tree.c:152
WizardExport SplayTreeInfo * NewSplayTree(int(*compare)(const void *, const void *), void *(*relinquish_key)(void *), void *(*relinquish_value)(void *))
static unsigned long StringToUnsignedLong(const char *value)
WizardExport char * DestroyString(char *string)
Definition string.c:830
WizardExport ssize_t FormatWizardSize(const WizardSizeType size, const WizardBooleanType bi, const size_t length, char *format)
Definition string.c:1024
WizardExport int LocaleCompare(const char *p, const char *q)
Definition string.c:1510
WizardExport char * ConstantString(const char *source)
Definition string.c:655
WizardExport size_t GetStringInfoLength(const StringInfo *string_info)
Definition string.c:1280
WizardExport unsigned char * GetStringInfoDatum(const StringInfo *string_info)
Definition string.c:1251
WizardExport char * GetEnvironmentValue(const char *name)
Definition string.c:1177
WizardExport size_t CopyWizardString(char *destination, const char *source, const size_t length)
Definition string.c:762
WizardExport StringInfo * DestroyStringInfo(StringInfo *string_info)
Definition string.c:857
WizardOffsetType memory
Definition resource.c:72
WizardSizeType map_limit
Definition resource.c:80
WizardSizeType area_limit
Definition resource.c:78
WizardOffsetType area
Definition resource.c:71
WizardOffsetType map
Definition resource.c:73
WizardOffsetType file
Definition resource.c:75
WizardOffsetType disk
Definition resource.c:74
WizardSizeType memory_limit
Definition resource.c:79
WizardSizeType file_limit
Definition resource.c:82
WizardSizeType disk_limit
Definition resource.c:81
#define O_BINARY
Definition studio.h:326
#define S_ISDIR(mode)
Definition studio.h:188
#define DirectorySeparator
Definition studio.h:239
#define S_MODE
Definition studio.h:334
static int open_utf8(const char *path, int flags, mode_t mode)
#define WizardMax(x, y)
static int remove_utf8(const char *path)
static int stat_utf8(const char *path, struct stat *attributes)
#define WizardULLConstant(c)
Definition wizard-type.h:36
size_t WizardSizeType
Definition wizard-type.h:51
ssize_t WizardOffsetType
Definition wizard-type.h:50
WizardBooleanType
Definition wizard-type.h:26
@ WizardTrue
Definition wizard-type.h:28
@ WizardFalse
Definition wizard-type.h:27