MagickCore 7.1.2
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
memory.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% M M EEEEE M M OOO RRRR Y Y %
7% MM MM E MM MM O O R R Y Y %
8% M M M EEE M M M O O RRRR Y %
9% M M E M M O O R R Y %
10% M M EEEEE M M OOO R R Y %
11% %
12% %
13% MagickCore Memory Allocation Methods %
14% %
15% Software Design %
16% Cristy %
17% July 1998 %
18% %
19% %
20% Copyright @ 1999 ImageMagick 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% We provide these memory allocators:
37%
38% AcquireCriticalMemory(): allocate a small memory request with
39% AcquireMagickMemory(), however, on fail throw a fatal exception and exit.
40% Free the memory reserve with RelinquishMagickMemory().
41% AcquireAlignedMemory(): allocate a small memory request that is aligned
42% on a cache line. On fail, return NULL for possible recovery.
43% Free the memory reserve with RelinquishMagickMemory().
44% AcquireMagickMemory()/ResizeMagickMemory(): allocate a small to medium
45% memory request, typically with malloc()/realloc(). On fail, return NULL
46% for possible recovery. Free the memory reserve with
47% RelinquishMagickMemory().
48% AcquireQuantumMemory()/ResizeQuantumMemory(): allocate a small to medium
49% memory request. This is a secure memory allocator as it accepts two
50% parameters, count and quantum, to ensure the request does not overflow.
51% It also check to ensure the request does not exceed the maximum memory
52% per the security policy. Free the memory reserve with
53% RelinquishMagickMemory().
54% AcquireVirtualMemory(): allocate a large memory request either in heap,
55% memory-mapped, or memory-mapped on disk depending on whether heap
56% allocation fails or if the request exceeds the maximum memory policy.
57% Free the memory reserve with RelinquishVirtualMemory().
58% ResetMagickMemory(): fills the bytes of the memory area with a constant
59% byte.
60%
61% In addition, we provide hooks for your own memory constructor/destructors.
62% You can also utilize our internal custom allocator as follows: Segregate
63% our memory requirements from any program that calls our API. This should
64% help reduce the risk of others changing our program state or causing memory
65% corruption.
66%
67% Our custom memory allocation manager implements a best-fit allocation policy
68% using segregated free lists. It uses a linear distribution of size classes
69% for lower sizes and a power of two distribution of size classes at higher
70% sizes. It is based on the paper, "Fast Memory Allocation using Lazy Fits."
71% written by Yoo C. Chung.
72%
73% By default, C's standard library is used (e.g. malloc); use the
74% custom memory allocator by defining MAGICKCORE_ANONYMOUS_MEMORY_SUPPORT
75% to allocate memory with private anonymous mapping rather than from the
76% heap.
77%
78*/
79
80/*
81 Include declarations.
82*/
83#include "MagickCore/studio.h"
84#include "MagickCore/blob.h"
85#include "MagickCore/blob-private.h"
86#include "MagickCore/exception.h"
87#include "MagickCore/exception-private.h"
88#include "MagickCore/image-private.h"
89#include "MagickCore/memory_.h"
90#include "MagickCore/memory-private.h"
91#include "MagickCore/policy.h"
92#include "MagickCore/resource_.h"
93#include "MagickCore/semaphore.h"
94#include "MagickCore/string_.h"
95#include "MagickCore/string-private.h"
96#include "MagickCore/utility-private.h"
97
98/*
99 Define declarations.
100*/
101#define BlockFooter(block,size) \
102 ((size_t *) ((char *) (block)+(size)-2*sizeof(size_t)))
103#define BlockHeader(block) ((size_t *) (block)-1)
104#define BlockThreshold 1024
105#define MaxBlockExponent 16
106#define MaxBlocks ((BlockThreshold/(4*sizeof(size_t)))+MaxBlockExponent+1)
107#define MaxSegments 1024
108#define NextBlock(block) ((char *) (block)+SizeOfBlock(block))
109#define NextBlockInList(block) (*(void **) (block))
110#define PreviousBlock(block) ((char *) (block)-(*((size_t *) (block)-2)))
111#define PreviousBlockBit 0x01
112#define PreviousBlockInList(block) (*((void **) (block)+1))
113#define SegmentSize (2*1024*1024)
114#define SizeMask (~0x01)
115#define SizeOfBlock(block) (*BlockHeader(block) & SizeMask)
116
117/*
118 Typedef declarations.
119*/
120typedef enum
121{
122 UndefinedVirtualMemory,
123 AlignedVirtualMemory,
124 MapVirtualMemory,
125 UnalignedVirtualMemory
126} VirtualMemoryType;
127
128typedef struct _DataSegmentInfo
129{
130 void
131 *allocation,
132 *bound;
133
134 MagickBooleanType
135 mapped;
136
137 size_t
138 length;
139
140 struct _DataSegmentInfo
141 *previous,
142 *next;
143} DataSegmentInfo;
144
146{
147 AcquireMemoryHandler
148 acquire_memory_handler;
149
150 ResizeMemoryHandler
151 resize_memory_handler;
152
153 DestroyMemoryHandler
154 destroy_memory_handler;
155
156 AcquireAlignedMemoryHandler
157 acquire_aligned_memory_handler;
158
159 RelinquishAlignedMemoryHandler
160 relinquish_aligned_memory_handler;
161} MagickMemoryMethods;
162
164{
165 char
166 filename[MagickPathExtent];
167
168 VirtualMemoryType
169 type;
170
171 size_t
172 length;
173
174 void
175 *blob;
176
177 size_t
178 signature;
179};
180
181typedef struct _MemoryPool
182{
183 size_t
184 allocation;
185
186 void
187 *blocks[MaxBlocks+1];
188
189 size_t
190 number_segments;
191
192 DataSegmentInfo
193 *segments[MaxSegments],
194 segment_pool[MaxSegments];
195} MemoryPool;
196
197/*
198 Global declarations.
199*/
200static size_t
201 max_memory_request = 0,
202 max_profile_size = 0,
203 virtual_anonymous_memory = 0;
204
205static MagickMemoryMethods
206 memory_methods =
207 {
208 (AcquireMemoryHandler) malloc,
209 (ResizeMemoryHandler) realloc,
210 (DestroyMemoryHandler) free,
211 (AcquireAlignedMemoryHandler) NULL,
212 (RelinquishAlignedMemoryHandler) NULL
213 };
214#if defined(MAGICKCORE_ANONYMOUS_MEMORY_SUPPORT)
215static MemoryPool
216 memory_pool;
217
218static SemaphoreInfo
219 *memory_semaphore = (SemaphoreInfo *) NULL;
220
221static volatile DataSegmentInfo
222 *free_segments = (DataSegmentInfo *) NULL;
223
224/*
225 Forward declarations.
226*/
227static MagickBooleanType
228 ExpandHeap(size_t);
229#endif
230
231/*
232%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233% %
234% %
235% %
236% A c q u i r e A l i g n e d M e m o r y %
237% %
238% %
239% %
240%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241%
242% AcquireAlignedMemory() returns a pointer to a block of memory whose size is
243% at least (count*quantum) bytes, and whose address is aligned on a cache line.
244%
245% The format of the AcquireAlignedMemory method is:
246%
247% void *AcquireAlignedMemory(const size_t count,const size_t quantum)
248%
249% A description of each parameter follows:
250%
251% o count: the number of objects to allocate contiguously.
252%
253% o quantum: the size (in bytes) of each object.
254%
255*/
256#if defined(MAGICKCORE_HAVE_ALIGNED_MALLOC)
257#define AcquireAlignedMemory_Actual AcquireAlignedMemory_STDC
258static inline void *AcquireAlignedMemory_STDC(const size_t size)
259{
260 size_t
261 extent = CACHE_ALIGNED(size);
262
263 if (extent < size)
264 {
265 errno=ENOMEM;
266 return(NULL);
267 }
268 return(aligned_alloc(CACHE_LINE_SIZE,extent));
269}
270#elif defined(MAGICKCORE_HAVE_POSIX_MEMALIGN)
271#define AcquireAlignedMemory_Actual AcquireAlignedMemory_POSIX
272static inline void *AcquireAlignedMemory_POSIX(const size_t size)
273{
274 void
275 *memory;
276
277 if (posix_memalign(&memory,CACHE_LINE_SIZE,size))
278 return(NULL);
279 return(memory);
280}
281#elif defined(MAGICKCORE_HAVE__ALIGNED_MALLOC)
282#define AcquireAlignedMemory_Actual AcquireAlignedMemory_WinAPI
283static inline void *AcquireAlignedMemory_WinAPI(const size_t size)
284{
285 return(_aligned_malloc(size,CACHE_LINE_SIZE));
286}
287#else
288#define ALIGNMENT_OVERHEAD \
289 (MAGICKCORE_MAX_ALIGNMENT_PADDING(CACHE_LINE_SIZE) + MAGICKCORE_SIZEOF_VOID_P)
290static inline void *reserve_space_for_actual_base_address(void *const p)
291{
292 return((void **) p+1);
293}
294
295static inline void **pointer_to_space_for_actual_base_address(void *const p)
296{
297 return((void **) p-1);
298}
299
300static inline void *actual_base_address(void *const p)
301{
302 return(*pointer_to_space_for_actual_base_address(p));
303}
304
305static inline void *align_to_cache(void *const p)
306{
307 return((void *) CACHE_ALIGNED((MagickAddressType) p));
308}
309
310static inline void *adjust(void *const p)
311{
312 return(align_to_cache(reserve_space_for_actual_base_address(p)));
313}
314
315#define AcquireAlignedMemory_Actual AcquireAlignedMemory_Generic
316static inline void *AcquireAlignedMemory_Generic(const size_t size)
317{
318 size_t
319 extent;
320
321 void
322 *memory,
323 *p;
324
325 #if SIZE_MAX < ALIGNMENT_OVERHEAD
326 #error "CACHE_LINE_SIZE is way too big."
327 #endif
328 extent=(size+ALIGNMENT_OVERHEAD);
329 if (extent <= size)
330 {
331 errno=ENOMEM;
332 return(NULL);
333 }
334 p=AcquireMagickMemory(extent);
335 if (p == NULL)
336 return(NULL);
337 memory=adjust(p);
338 *pointer_to_space_for_actual_base_address(memory)=p;
339 return(memory);
340}
341#endif
342
343MagickExport void *AcquireAlignedMemory(const size_t count,const size_t quantum)
344{
345 size_t
346 size;
347
348 if (HeapOverflowSanityCheckGetSize(count,quantum,&size) != MagickFalse)
349 {
350 errno=ENOMEM;
351 return(NULL);
352 }
353 if (memory_methods.acquire_aligned_memory_handler != (AcquireAlignedMemoryHandler) NULL)
354 return(memory_methods.acquire_aligned_memory_handler(size,CACHE_LINE_SIZE));
355 return(AcquireAlignedMemory_Actual(size));
356}
357
358#if defined(MAGICKCORE_ANONYMOUS_MEMORY_SUPPORT)
359/*
360%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361% %
362% %
363% %
364+ A c q u i r e B l o c k %
365% %
366% %
367% %
368%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
369%
370% AcquireBlock() returns a pointer to a block of memory at least size bytes
371% suitably aligned for any use.
372%
373% The format of the AcquireBlock method is:
374%
375% void *AcquireBlock(const size_t size)
376%
377% A description of each parameter follows:
378%
379% o size: the size of the memory in bytes to allocate.
380%
381*/
382
383static inline size_t AllocationPolicy(size_t size)
384{
385 size_t
386 blocksize;
387
388 /*
389 The linear distribution.
390 */
391 assert(size != 0);
392 assert(size % (4*sizeof(size_t)) == 0);
393 if (size <= BlockThreshold)
394 return(size/(4*sizeof(size_t)));
395 /*
396 Check for the largest block size.
397 */
398 if (size > (size_t) (BlockThreshold*(1L << (MaxBlockExponent-1L))))
399 return(MaxBlocks-1L);
400 /*
401 Otherwise use a power of two distribution.
402 */
403 blocksize=BlockThreshold/(4*sizeof(size_t));
404 for ( ; size > BlockThreshold; size/=2)
405 blocksize++;
406 assert(blocksize > (BlockThreshold/(4*sizeof(size_t))));
407 assert(blocksize < (MaxBlocks-1L));
408 return(blocksize);
409}
410
411static inline void InsertFreeBlock(void *block,const size_t i)
412{
413 void
414 *next,
415 *previous;
416
417 size_t
418 size;
419
420 size=SizeOfBlock(block);
421 previous=(void *) NULL;
422 next=memory_pool.blocks[i];
423 while ((next != (void *) NULL) && (SizeOfBlock(next) < size))
424 {
425 previous=next;
426 next=NextBlockInList(next);
427 }
428 PreviousBlockInList(block)=previous;
429 NextBlockInList(block)=next;
430 if (previous != (void *) NULL)
431 NextBlockInList(previous)=block;
432 else
433 memory_pool.blocks[i]=block;
434 if (next != (void *) NULL)
435 PreviousBlockInList(next)=block;
436}
437
438static inline void RemoveFreeBlock(void *block,const size_t i)
439{
440 void
441 *next,
442 *previous;
443
444 next=NextBlockInList(block);
445 previous=PreviousBlockInList(block);
446 if (previous == (void *) NULL)
447 memory_pool.blocks[i]=next;
448 else
449 NextBlockInList(previous)=next;
450 if (next != (void *) NULL)
451 PreviousBlockInList(next)=previous;
452}
453
454static void *AcquireBlock(size_t size)
455{
456 size_t
457 i;
458
459 void
460 *block;
461
462 /*
463 Find free block.
464 */
465 size=(size_t) (size+sizeof(size_t)+6*sizeof(size_t)-1) & -(4U*sizeof(size_t));
466 i=AllocationPolicy(size);
467 block=memory_pool.blocks[i];
468 while ((block != (void *) NULL) && (SizeOfBlock(block) < size))
469 block=NextBlockInList(block);
470 if (block == (void *) NULL)
471 {
472 i++;
473 while (memory_pool.blocks[i] == (void *) NULL)
474 i++;
475 block=memory_pool.blocks[i];
476 if (i >= MaxBlocks)
477 return((void *) NULL);
478 }
479 assert((*BlockHeader(NextBlock(block)) & PreviousBlockBit) == 0);
480 assert(SizeOfBlock(block) >= size);
481 RemoveFreeBlock(block,AllocationPolicy(SizeOfBlock(block)));
482 if (SizeOfBlock(block) > size)
483 {
484 size_t
485 blocksize;
486
487 void
488 *next;
489
490 /*
491 Split block.
492 */
493 next=(char *) block+size;
494 blocksize=SizeOfBlock(block)-size;
495 *BlockHeader(next)=blocksize;
496 *BlockFooter(next,blocksize)=blocksize;
497 InsertFreeBlock(next,AllocationPolicy(blocksize));
498 *BlockHeader(block)=size | (*BlockHeader(block) & ~SizeMask);
499 }
500 assert(size == SizeOfBlock(block));
501 *BlockHeader(NextBlock(block))|=PreviousBlockBit;
502 memory_pool.allocation+=size;
503 return(block);
504}
505#endif
506
507/*
508%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
509% %
510% %
511% %
512% A c q u i r e M a g i c k M e m o r y %
513% %
514% %
515% %
516%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
517%
518% AcquireMagickMemory() returns a pointer to a block of memory at least size
519% bytes suitably aligned for any use.
520%
521% The format of the AcquireMagickMemory method is:
522%
523% void *AcquireMagickMemory(const size_t size)
524%
525% A description of each parameter follows:
526%
527% o size: the size of the memory in bytes to allocate.
528%
529*/
530MagickExport void *AcquireMagickMemory(const size_t size)
531{
532 void
533 *memory;
534
535#if !defined(MAGICKCORE_ANONYMOUS_MEMORY_SUPPORT)
536 memory=memory_methods.acquire_memory_handler(size == 0 ? 1UL : size);
537#else
538 if (memory_semaphore == (SemaphoreInfo *) NULL)
539 ActivateSemaphoreInfo(&memory_semaphore);
540 if (free_segments == (DataSegmentInfo *) NULL)
541 {
542 LockSemaphoreInfo(memory_semaphore);
543 if (free_segments == (DataSegmentInfo *) NULL)
544 {
545 ssize_t
546 i;
547
548 assert(2*sizeof(size_t) > (size_t) (~SizeMask));
549 (void) memset(&memory_pool,0,sizeof(memory_pool));
550 memory_pool.allocation=SegmentSize;
551 memory_pool.blocks[MaxBlocks]=(void *) (-1);
552 for (i=0; i < MaxSegments; i++)
553 {
554 if (i != 0)
555 memory_pool.segment_pool[i].previous=
556 (&memory_pool.segment_pool[i-1]);
557 if (i != (MaxSegments-1))
558 memory_pool.segment_pool[i].next=(&memory_pool.segment_pool[i+1]);
559 }
560 free_segments=(&memory_pool.segment_pool[0]);
561 }
562 UnlockSemaphoreInfo(memory_semaphore);
563 }
564 LockSemaphoreInfo(memory_semaphore);
565 memory=AcquireBlock(size == 0 ? 1UL : size);
566 if (memory == (void *) NULL)
567 {
568 if (ExpandHeap(size == 0 ? 1UL : size) != MagickFalse)
569 memory=AcquireBlock(size == 0 ? 1UL : size);
570 }
571 UnlockSemaphoreInfo(memory_semaphore);
572#endif
573 return(memory);
574}
575
576/*
577%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
578% %
579% %
580% %
581% A c q u i r e C r i t i c a l M e m o r y %
582% %
583% %
584% %
585%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
586%
587% AcquireCriticalMemory() is just like AcquireMagickMemory(), throws a fatal
588% exception if the memory cannot be acquired.
589%
590% That is, AcquireCriticalMemory() returns a pointer to a block of memory that
591% is at least size bytes, and that is suitably aligned for any use; however,
592% if this is not possible, it throws an exception and terminates the program
593% as unceremoniously as possible.
594%
595% The format of the AcquireCriticalMemory method is:
596%
597% void *AcquireCriticalMemory(const size_t size)
598%
599% A description of each parameter follows:
600%
601% o size: the size (in bytes) of the memory to allocate.
602%
603*/
604MagickExport void *AcquireCriticalMemory(const size_t size)
605{
606 void
607 *memory;
608
609 /*
610 Fail if memory request cannot be fulfilled.
611 */
612 memory=AcquireMagickMemory(size);
613 if (memory == (void *) NULL)
614 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
615 return(memory);
616}
617
618/*
619%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620% %
621% %
622% %
623% A c q u i r e Q u a n t u m M e m o r y %
624% %
625% %
626% %
627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628%
629% AcquireQuantumMemory() returns a pointer to a block of memory at least
630% count * quantum bytes suitably aligned for any use.
631%
632% The format of the AcquireQuantumMemory method is:
633%
634% void *AcquireQuantumMemory(const size_t count,const size_t quantum)
635%
636% A description of each parameter follows:
637%
638% o count: the number of objects to allocate contiguously.
639%
640% o quantum: the size (in bytes) of each object.
641%
642*/
643MagickExport void *AcquireQuantumMemory(const size_t count,const size_t quantum)
644{
645 size_t
646 size;
647
648 if ((HeapOverflowSanityCheckGetSize(count,quantum,&size) != MagickFalse) ||
649 (size > GetMaxMemoryRequest()))
650 {
651 errno=ENOMEM;
652 return(NULL);
653 }
654 return(AcquireMagickMemory(size));
655}
656
657/*
658%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
659% %
660% %
661% %
662% A c q u i r e V i r t u a l M e m o r y %
663% %
664% %
665% %
666%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
667%
668% AcquireVirtualMemory() allocates a pointer to a block of memory at least
669% size bytes suitably aligned for any use. In addition to heap, it also
670% supports memory-mapped and file-based memory-mapped memory requests.
671%
672% The format of the AcquireVirtualMemory method is:
673%
674% MemoryInfo *AcquireVirtualMemory(const size_t count,const size_t quantum)
675%
676% A description of each parameter follows:
677%
678% o count: the number of objects to allocate contiguously.
679%
680% o quantum: the size (in bytes) of each object.
681%
682*/
683MagickExport MemoryInfo *AcquireVirtualMemory(const size_t count,
684 const size_t quantum)
685{
686 char
687 *value;
688
689 MemoryInfo
690 *memory_info;
691
692 size_t
693 size;
694
695 if (HeapOverflowSanityCheckGetSize(count,quantum,&size) != MagickFalse)
696 {
697 errno=ENOMEM;
698 return((MemoryInfo *) NULL);
699 }
700 if (virtual_anonymous_memory == 0)
701 {
702 virtual_anonymous_memory=1;
703 value=GetPolicyValue("system:memory-map");
704 if (LocaleCompare(value,"anonymous") == 0)
705 {
706 /*
707 The security policy sets anonymous mapping for the memory request.
708 */
709#if defined(MAGICKCORE_HAVE_MMAP) && defined(MAP_ANONYMOUS)
710 virtual_anonymous_memory=2;
711#endif
712 }
713 value=DestroyString(value);
714 }
715 memory_info=(MemoryInfo *) MagickAssumeAligned(AcquireAlignedMemory(1,
716 sizeof(*memory_info)));
717 if (memory_info == (MemoryInfo *) NULL)
718 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
719 (void) memset(memory_info,0,sizeof(*memory_info));
720 memory_info->length=size;
721 memory_info->signature=MagickCoreSignature;
722 if ((virtual_anonymous_memory == 1) && (size <= GetMaxMemoryRequest()))
723 {
724 memory_info->blob=AcquireAlignedMemory(1,size);
725 if (memory_info->blob != NULL)
726 memory_info->type=AlignedVirtualMemory;
727 }
728 if (memory_info->blob == NULL)
729 {
730 /*
731 Acquire anonymous memory map.
732 */
733 memory_info->blob=NULL;
734 if (size <= GetMaxMemoryRequest())
735 memory_info->blob=MapBlob(-1,IOMode,0,size);
736 if (memory_info->blob != NULL)
737 memory_info->type=MapVirtualMemory;
738 else
739 {
740 int
741 file;
742
743 /*
744 Anonymous memory mapping failed, try file-backed memory mapping.
745 */
746 file=AcquireUniqueFileResource(memory_info->filename);
747 if (file != -1)
748 {
749 MagickOffsetType
750 offset;
751
752 offset=(MagickOffsetType) lseek(file,(off_t) (size-1),SEEK_SET);
753 if ((offset == (MagickOffsetType) (size-1)) &&
754 (write(file,"",1) == 1))
755 {
756#if !defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
757 memory_info->blob=MapBlob(file,IOMode,0,size);
758#else
759 if (posix_fallocate(file,0,(MagickOffsetType) size) == 0)
760 memory_info->blob=MapBlob(file,IOMode,0,size);
761#endif
762 if (memory_info->blob != NULL)
763 memory_info->type=MapVirtualMemory;
764 else
765 {
766 (void) RelinquishUniqueFileResource(
767 memory_info->filename);
768 *memory_info->filename='\0';
769 }
770 }
771 (void) close_utf8(file);
772 }
773 }
774 }
775 if (memory_info->blob == NULL)
776 {
777 memory_info->blob=AcquireQuantumMemory(1,size);
778 if (memory_info->blob != NULL)
779 memory_info->type=UnalignedVirtualMemory;
780 }
781 if (memory_info->blob == NULL)
782 memory_info=RelinquishVirtualMemory(memory_info);
783 return(memory_info);
784}
785
786/*
787%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
788% %
789% %
790% %
791% C o p y M a g i c k M e m o r y %
792% %
793% %
794% %
795%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796%
797% CopyMagickMemory() copies size bytes from memory area source to the
798% destination. Copying between objects that overlap will take place
799% correctly. It returns destination.
800%
801% The format of the CopyMagickMemory method is:
802%
803% void *CopyMagickMemory(void *magick_restrict destination,
804% const void *magick_restrict source,const size_t size)
805%
806% A description of each parameter follows:
807%
808% o destination: the destination.
809%
810% o source: the source.
811%
812% o size: the size of the memory in bytes to allocate.
813%
814*/
815MagickExport void *CopyMagickMemory(void *magick_restrict destination,
816 const void *magick_restrict source,const size_t size)
817{
818 const unsigned char
819 *p;
820
821 unsigned char
822 *q;
823
824 assert(destination != (void *) NULL);
825 assert(source != (const void *) NULL);
826 p=(const unsigned char *) source;
827 q=(unsigned char *) destination;
828 if (((q+size) < p) || (q > (p+size)))
829 switch (size)
830 {
831 default: return(memcpy(destination,source,size));
832 case 8: *q++=(*p++); magick_fallthrough;
833 case 7: *q++=(*p++); magick_fallthrough;
834 case 6: *q++=(*p++); magick_fallthrough;
835 case 5: *q++=(*p++); magick_fallthrough;
836 case 4: *q++=(*p++); magick_fallthrough;
837 case 3: *q++=(*p++); magick_fallthrough;
838 case 2: *q++=(*p++); magick_fallthrough;
839 case 1: *q++=(*p++); magick_fallthrough;
840 case 0: return(destination);
841 }
842 return(memmove(destination,source,size));
843}
844
845/*
846%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
847% %
848% %
849% %
850+ D e s t r o y M a g i c k M e m o r y %
851% %
852% %
853% %
854%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
855%
856% DestroyMagickMemory() deallocates memory associated with the memory manager.
857%
858% The format of the DestroyMagickMemory method is:
859%
860% DestroyMagickMemory(void)
861%
862*/
863MagickExport void DestroyMagickMemory(void)
864{
865#if defined(MAGICKCORE_ANONYMOUS_MEMORY_SUPPORT)
866 ssize_t
867 i;
868
869 if (memory_semaphore == (SemaphoreInfo *) NULL)
870 ActivateSemaphoreInfo(&memory_semaphore);
871 LockSemaphoreInfo(memory_semaphore);
872 for (i=0; i < (ssize_t) memory_pool.number_segments; i++)
873 if (memory_pool.segments[i]->mapped == MagickFalse)
874 memory_methods.destroy_memory_handler(
875 memory_pool.segments[i]->allocation);
876 else
877 (void) UnmapBlob(memory_pool.segments[i]->allocation,
878 memory_pool.segments[i]->length);
879 free_segments=(DataSegmentInfo *) NULL;
880 (void) memset(&memory_pool,0,sizeof(memory_pool));
881 UnlockSemaphoreInfo(memory_semaphore);
882 RelinquishSemaphoreInfo(&memory_semaphore);
883#endif
884}
885
886#if defined(MAGICKCORE_ANONYMOUS_MEMORY_SUPPORT)
887/*
888%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
889% %
890% %
891% %
892+ E x p a n d H e a p %
893% %
894% %
895% %
896%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
897%
898% ExpandHeap() get more memory from the system. It returns MagickTrue on
899% success otherwise MagickFalse.
900%
901% The format of the ExpandHeap method is:
902%
903% MagickBooleanType ExpandHeap(size_t size)
904%
905% A description of each parameter follows:
906%
907% o size: the size of the memory in bytes we require.
908%
909*/
910static MagickBooleanType ExpandHeap(size_t size)
911{
912 DataSegmentInfo
913 *segment_info;
914
915 MagickBooleanType
916 mapped;
917
918 ssize_t
919 i;
920
921 void
922 *block;
923
924 size_t
925 blocksize;
926
927 void
928 *segment;
929
930 blocksize=((size+12*sizeof(size_t))+SegmentSize-1) & -SegmentSize;
931 assert(memory_pool.number_segments < MaxSegments);
932 segment=MapBlob(-1,IOMode,0,blocksize);
933 mapped=segment != (void *) NULL ? MagickTrue : MagickFalse;
934 if (segment == (void *) NULL)
935 segment=(void *) memory_methods.acquire_memory_handler(blocksize);
936 if (segment == (void *) NULL)
937 return(MagickFalse);
938 segment_info=(DataSegmentInfo *) free_segments;
939 free_segments=segment_info->next;
940 segment_info->mapped=mapped;
941 segment_info->length=blocksize;
942 segment_info->allocation=segment;
943 segment_info->bound=(char *) segment+blocksize;
944 i=(ssize_t) memory_pool.number_segments-1;
945 for ( ; (i >= 0) && (memory_pool.segments[i]->allocation > segment); i--)
946 memory_pool.segments[i+1]=memory_pool.segments[i];
947 memory_pool.segments[i+1]=segment_info;
948 memory_pool.number_segments++;
949 size=blocksize-12*sizeof(size_t);
950 block=(char *) segment_info->allocation+4*sizeof(size_t);
951 *BlockHeader(block)=size | PreviousBlockBit;
952 *BlockFooter(block,size)=size;
953 InsertFreeBlock(block,AllocationPolicy(size));
954 block=NextBlock(block);
955 assert(block < segment_info->bound);
956 *BlockHeader(block)=2*sizeof(size_t);
957 *BlockHeader(NextBlock(block))=PreviousBlockBit;
958 return(MagickTrue);
959}
960#endif
961
962/*
963%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
964% %
965% %
966% %
967% G e t M a g i c k M e m o r y M e t h o d s %
968% %
969% %
970% %
971%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
972%
973% GetMagickMemoryMethods() gets the methods to acquire, resize, and destroy
974% memory.
975%
976% The format of the GetMagickMemoryMethods() method is:
977%
978% void GetMagickMemoryMethods(AcquireMemoryHandler *acquire_memory_handler,
979% ResizeMemoryHandler *resize_memory_handler,
980% DestroyMemoryHandler *destroy_memory_handler)
981%
982% A description of each parameter follows:
983%
984% o acquire_memory_handler: method to acquire memory (e.g. malloc).
985%
986% o resize_memory_handler: method to resize memory (e.g. realloc).
987%
988% o destroy_memory_handler: method to destroy memory (e.g. free).
989%
990*/
991MagickExport void GetMagickMemoryMethods(
992 AcquireMemoryHandler *acquire_memory_handler,
993 ResizeMemoryHandler *resize_memory_handler,
994 DestroyMemoryHandler *destroy_memory_handler)
995{
996 assert(acquire_memory_handler != (AcquireMemoryHandler *) NULL);
997 assert(resize_memory_handler != (ResizeMemoryHandler *) NULL);
998 assert(destroy_memory_handler != (DestroyMemoryHandler *) NULL);
999 *acquire_memory_handler=memory_methods.acquire_memory_handler;
1000 *resize_memory_handler=memory_methods.resize_memory_handler;
1001 *destroy_memory_handler=memory_methods.destroy_memory_handler;
1002}
1003
1004/*
1005%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1006% %
1007% %
1008% %
1009+ G e t M a x M e m o r y R e q u e s t %
1010% %
1011% %
1012% %
1013%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1014%
1015% GetMaxMemoryRequest() returns the max memory request value.
1016%
1017% The format of the GetMaxMemoryRequest method is:
1018%
1019% size_t GetMaxMemoryRequest(void)
1020%
1021*/
1022static size_t GetMaxMemoryRequestFromPolicy(void)
1023{
1024#define MinMemoryRequest "16MiB"
1025
1026 char
1027 *value;
1028
1029 size_t
1030 max_memory = (size_t) MAGICK_SSIZE_MAX;
1031
1032 value=GetPolicyValue("system:max-memory-request");
1033 if (value != (char *) NULL)
1034 {
1035 /*
1036 The security policy sets a max memory request limit.
1037 */
1038 max_memory=MagickMax(StringToSizeType(value,100.0),StringToSizeType(
1039 MinMemoryRequest,100.0));
1040 value=DestroyString(value);
1041 }
1042 return(MagickMin(max_memory,(size_t) MAGICK_SSIZE_MAX));
1043}
1044
1045MagickExport size_t GetMaxMemoryRequest(void)
1046{
1047 if (max_memory_request == 0)
1048 {
1049 /*
1050 Setting this to unlimited before we check the policy value to avoid
1051 recursive calls to GetMaxMemoryRequestFromPolicy()
1052 */
1053 max_memory_request=(size_t) MAGICK_SSIZE_MAX;
1054 max_memory_request=GetMaxMemoryRequestFromPolicy();
1055 }
1056 return(max_memory_request);
1057}
1058
1059/*
1060%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1061% %
1062% %
1063% %
1064+ G e t M a x P r o f i l e S i z e %
1065% %
1066% %
1067% %
1068%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1069%
1070% GetMaxProfileSize() returns the max profile size value.
1071%
1072% The format of the GetMaxMemoryRequest method is:
1073%
1074% size_t GetMaxProfileSize(void)
1075%
1076*/
1077static size_t GetMaxProfileSizeFromPolicy(void)
1078{
1079 char
1080 *value;
1081
1082 size_t
1083 max=(size_t) MAGICK_SSIZE_MAX;
1084
1085 value=GetPolicyValue("system:max-profile-size");
1086 if (value != (char *) NULL)
1087 {
1088 /*
1089 The security policy sets a max profile size limit.
1090 */
1091 max=StringToSizeType(value,100.0);
1092 value=DestroyString(value);
1093 }
1094 return(MagickMin(max,(size_t) MAGICK_SSIZE_MAX));
1095}
1096
1097MagickExport size_t GetMaxProfileSize(void)
1098{
1099 if (max_profile_size == 0)
1100 max_profile_size=GetMaxProfileSizeFromPolicy();
1101 return(max_profile_size);
1102}
1103
1104/*
1105%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1106% %
1107% %
1108% %
1109% G e t V i r t u a l M e m o r y B l o b %
1110% %
1111% %
1112% %
1113%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1114%
1115% GetVirtualMemoryBlob() returns the virtual memory blob associated with the
1116% specified MemoryInfo structure.
1117%
1118% The format of the GetVirtualMemoryBlob method is:
1119%
1120% void *GetVirtualMemoryBlob(const MemoryInfo *memory_info)
1121%
1122% A description of each parameter follows:
1123%
1124% o memory_info: The MemoryInfo structure.
1125*/
1126MagickExport void *GetVirtualMemoryBlob(const MemoryInfo *memory_info)
1127{
1128 assert(memory_info != (const MemoryInfo *) NULL);
1129 assert(memory_info->signature == MagickCoreSignature);
1130 return(memory_info->blob);
1131}
1132
1133/*
1134%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1135% %
1136% %
1137% %
1138% R e l i n q u i s h A l i g n e d M e m o r y %
1139% %
1140% %
1141% %
1142%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1143%
1144% RelinquishAlignedMemory() frees memory acquired with AcquireAlignedMemory()
1145% or reuse.
1146%
1147% The format of the RelinquishAlignedMemory method is:
1148%
1149% void *RelinquishAlignedMemory(void *memory)
1150%
1151% A description of each parameter follows:
1152%
1153% o memory: A pointer to a block of memory to free for reuse.
1154%
1155*/
1156MagickExport void *RelinquishAlignedMemory(void *memory)
1157{
1158 if (memory == (void *) NULL)
1159 return((void *) NULL);
1160 if (memory_methods.relinquish_aligned_memory_handler != (RelinquishAlignedMemoryHandler) NULL)
1161 {
1162 memory_methods.relinquish_aligned_memory_handler(memory);
1163 return(NULL);
1164 }
1165#if defined(MAGICKCORE_HAVE_ALIGNED_MALLOC) || defined(MAGICKCORE_HAVE_POSIX_MEMALIGN)
1166 free(memory);
1167#elif defined(MAGICKCORE_HAVE__ALIGNED_MALLOC)
1168 _aligned_free(memory);
1169#else
1170 RelinquishMagickMemory(actual_base_address(memory));
1171#endif
1172 return(NULL);
1173}
1174
1175/*
1176%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1177% %
1178% %
1179% %
1180% R e l i n q u i s h M a g i c k M e m o r y %
1181% %
1182% %
1183% %
1184%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1185%
1186% RelinquishMagickMemory() frees memory acquired with AcquireMagickMemory()
1187% or AcquireQuantumMemory() for reuse.
1188%
1189% The format of the RelinquishMagickMemory method is:
1190%
1191% void *RelinquishMagickMemory(void *memory)
1192%
1193% A description of each parameter follows:
1194%
1195% o memory: A pointer to a block of memory to free for reuse.
1196%
1197*/
1198MagickExport void *RelinquishMagickMemory(void *memory)
1199{
1200 if (memory == (void *) NULL)
1201 return((void *) NULL);
1202#if !defined(MAGICKCORE_ANONYMOUS_MEMORY_SUPPORT)
1203 memory_methods.destroy_memory_handler(memory);
1204#else
1205 LockSemaphoreInfo(memory_semaphore);
1206 assert((SizeOfBlock(memory) % (4*sizeof(size_t))) == 0);
1207 assert((*BlockHeader(NextBlock(memory)) & PreviousBlockBit) != 0);
1208 if ((*BlockHeader(memory) & PreviousBlockBit) == 0)
1209 {
1210 void
1211 *previous;
1212
1213 /*
1214 Coalesce with previous adjacent block.
1215 */
1216 previous=PreviousBlock(memory);
1217 RemoveFreeBlock(previous,AllocationPolicy(SizeOfBlock(previous)));
1218 *BlockHeader(previous)=(SizeOfBlock(previous)+SizeOfBlock(memory)) |
1219 (*BlockHeader(previous) & ~SizeMask);
1220 memory=previous;
1221 }
1222 if ((*BlockHeader(NextBlock(NextBlock(memory))) & PreviousBlockBit) == 0)
1223 {
1224 void
1225 *next;
1226
1227 /*
1228 Coalesce with next adjacent block.
1229 */
1230 next=NextBlock(memory);
1231 RemoveFreeBlock(next,AllocationPolicy(SizeOfBlock(next)));
1232 *BlockHeader(memory)=(SizeOfBlock(memory)+SizeOfBlock(next)) |
1233 (*BlockHeader(memory) & ~SizeMask);
1234 }
1235 *BlockFooter(memory,SizeOfBlock(memory))=SizeOfBlock(memory);
1236 *BlockHeader(NextBlock(memory))&=(~PreviousBlockBit);
1237 InsertFreeBlock(memory,AllocationPolicy(SizeOfBlock(memory)));
1238 UnlockSemaphoreInfo(memory_semaphore);
1239#endif
1240 return((void *) NULL);
1241}
1242
1243/*
1244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1245% %
1246% %
1247% %
1248% R e l i n q u i s h V i r t u a l M e m o r y %
1249% %
1250% %
1251% %
1252%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1253%
1254% RelinquishVirtualMemory() frees memory acquired with AcquireVirtualMemory().
1255%
1256% The format of the RelinquishVirtualMemory method is:
1257%
1258% MemoryInfo *RelinquishVirtualMemory(MemoryInfo *memory_info)
1259%
1260% A description of each parameter follows:
1261%
1262% o memory_info: A pointer to a block of memory to free for reuse.
1263%
1264*/
1265MagickExport MemoryInfo *RelinquishVirtualMemory(MemoryInfo *memory_info)
1266{
1267 assert(memory_info != (MemoryInfo *) NULL);
1268 assert(memory_info->signature == MagickCoreSignature);
1269 if (memory_info->blob != (void *) NULL)
1270 switch (memory_info->type)
1271 {
1272 case AlignedVirtualMemory:
1273 {
1274 (void) ShredMagickMemory(memory_info->blob,memory_info->length);
1275 memory_info->blob=RelinquishAlignedMemory(memory_info->blob);
1276 break;
1277 }
1278 case MapVirtualMemory:
1279 {
1280 (void) UnmapBlob(memory_info->blob,memory_info->length);
1281 memory_info->blob=NULL;
1282 if (*memory_info->filename != '\0')
1283 (void) RelinquishUniqueFileResource(memory_info->filename);
1284 break;
1285 }
1286 case UnalignedVirtualMemory:
1287 default:
1288 {
1289 (void) ShredMagickMemory(memory_info->blob,memory_info->length);
1290 memory_info->blob=RelinquishMagickMemory(memory_info->blob);
1291 break;
1292 }
1293 }
1294 memory_info->signature=(~MagickCoreSignature);
1295 memory_info=(MemoryInfo *) RelinquishAlignedMemory(memory_info);
1296 return(memory_info);
1297}
1298
1299/*
1300%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1301% %
1302% %
1303% %
1304% R e s e t M a g i c k M e m o r y %
1305% %
1306% %
1307% %
1308%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1309%
1310% ResetMagickMemory() fills the first size bytes of the memory area pointed to % by memory with the constant byte c. We use a volatile pointer when
1311% updating the byte string. Most compilers will avoid optimizing away access
1312% to a volatile pointer, even if the pointer appears to be unused after the
1313% call.
1314%
1315% The format of the ResetMagickMemory method is:
1316%
1317% void *ResetMagickMemory(void *memory,int c,const size_t size)
1318%
1319% A description of each parameter follows:
1320%
1321% o memory: a pointer to a memory allocation.
1322%
1323% o c: set the memory to this value.
1324%
1325% o size: size of the memory to reset.
1326%
1327*/
1328MagickExport void *ResetMagickMemory(void *memory,int c,const size_t size)
1329{
1330 volatile unsigned char
1331 *p = (volatile unsigned char *) memory;
1332
1333 size_t
1334 n = size;
1335
1336 assert(memory != (void *) NULL);
1337 while (n-- != 0)
1338 *p++=(unsigned char) c;
1339 return(memory);
1340}
1341
1342/*
1343%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1344% %
1345% %
1346% %
1347+ R e s e t V i r t u a l A n o n y m o u s M e m o r y %
1348% %
1349% %
1350% %
1351%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1352%
1353% ResetVirtualAnonymousMemory() resets the virtual_anonymous_memory value.
1354%
1355% The format of the ResetVirtualAnonymousMemory method is:
1356%
1357% void ResetVirtualAnonymousMemory(void)
1358%
1359*/
1360MagickPrivate void ResetVirtualAnonymousMemory(void)
1361{
1362 virtual_anonymous_memory=0;
1363}
1364
1365/*
1366%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1367% %
1368% %
1369% %
1370% R e s i z e M a g i c k M e m o r y %
1371% %
1372% %
1373% %
1374%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1375%
1376% ResizeMagickMemory() changes the size of the memory and returns a pointer to
1377% the (possibly moved) block. The contents will be unchanged up to the
1378% lesser of the new and old sizes.
1379%
1380% The format of the ResizeMagickMemory method is:
1381%
1382% void *ResizeMagickMemory(void *memory,const size_t size)
1383%
1384% A description of each parameter follows:
1385%
1386% o memory: A pointer to a memory allocation.
1387%
1388% o size: the new size of the allocated memory.
1389%
1390*/
1391
1392#if defined(MAGICKCORE_ANONYMOUS_MEMORY_SUPPORT)
1393static inline void *ResizeBlock(void *block,size_t size)
1394{
1395 void
1396 *memory;
1397
1398 if (block == (void *) NULL)
1399 return(AcquireBlock(size));
1400 memory=AcquireBlock(size);
1401 if (memory == (void *) NULL)
1402 return((void *) NULL);
1403 if (size <= (SizeOfBlock(block)-sizeof(size_t)))
1404 (void) memcpy(memory,block,size);
1405 else
1406 (void) memcpy(memory,block,SizeOfBlock(block)-sizeof(size_t));
1407 memory_pool.allocation+=size;
1408 return(memory);
1409}
1410#endif
1411
1412MagickExport void *ResizeMagickMemory(void *memory,const size_t size)
1413{
1414 void
1415 *block;
1416
1417 if (memory == (void *) NULL)
1418 return(AcquireMagickMemory(size));
1419#if !defined(MAGICKCORE_ANONYMOUS_MEMORY_SUPPORT)
1420 block=memory_methods.resize_memory_handler(memory,size == 0 ? 1UL : size);
1421 if (block == (void *) NULL)
1422 memory=RelinquishMagickMemory(memory);
1423#else
1424 LockSemaphoreInfo(memory_semaphore);
1425 block=ResizeBlock(memory,size == 0 ? 1UL : size);
1426 if (block == (void *) NULL)
1427 {
1428 if (ExpandHeap(size == 0 ? 1UL : size) == MagickFalse)
1429 {
1430 UnlockSemaphoreInfo(memory_semaphore);
1431 memory=RelinquishMagickMemory(memory);
1432 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1433 }
1434 block=ResizeBlock(memory,size == 0 ? 1UL : size);
1435 assert(block != (void *) NULL);
1436 }
1437 UnlockSemaphoreInfo(memory_semaphore);
1438 memory=RelinquishMagickMemory(memory);
1439#endif
1440 return(block);
1441}
1442
1443/*
1444%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1445% %
1446% %
1447% %
1448% R e s i z e Q u a n t u m M e m o r y %
1449% %
1450% %
1451% %
1452%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1453%
1454% ResizeQuantumMemory() changes the size of the memory and returns a pointer
1455% to the (possibly moved) block. The contents will be unchanged up to the
1456% lesser of the new and old sizes.
1457%
1458% The format of the ResizeQuantumMemory method is:
1459%
1460% void *ResizeQuantumMemory(void *memory,const size_t count,
1461% const size_t quantum)
1462%
1463% A description of each parameter follows:
1464%
1465% o memory: A pointer to a memory allocation.
1466%
1467% o count: the number of objects to allocate contiguously.
1468%
1469% o quantum: the size (in bytes) of each object.
1470%
1471*/
1472MagickExport void *ResizeQuantumMemory(void *memory,const size_t count,
1473 const size_t quantum)
1474{
1475 size_t
1476 size;
1477
1478 if ((HeapOverflowSanityCheckGetSize(count,quantum,&size) != MagickFalse) ||
1479 (size > GetMaxMemoryRequest()))
1480 {
1481 errno=ENOMEM;
1482 memory=RelinquishMagickMemory(memory);
1483 return(NULL);
1484 }
1485 return(ResizeMagickMemory(memory,size));
1486}
1487
1488/*
1489%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1490% %
1491% %
1492% %
1493% S e t M a g i c k A l i g n e d M e m o r y M e t h o d s %
1494% %
1495% %
1496% %
1497%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1498%
1499% SetMagickAlignedMemoryMethods() sets the methods to acquire and relinquish
1500% aligned memory.
1501%
1502% The format of the SetMagickAlignedMemoryMethods() method is:
1503%
1504% void SetMagickAlignedMemoryMethods(
1505% AcquireAlignedMemoryHandler acquire_aligned_memory_handler,
1506% RelinquishAlignedMemoryHandler relinquish_aligned_memory_handler)
1507%
1508% A description of each parameter follows:
1509%
1510% o acquire_memory_handler: method to acquire aligned memory.
1511%
1512% o relinquish_aligned_memory_handler: method to relinquish aligned memory.
1513%
1514*/
1515MagickExport void SetMagickAlignedMemoryMethods(
1516 AcquireAlignedMemoryHandler acquire_aligned_memory_handler,
1517 RelinquishAlignedMemoryHandler relinquish_aligned_memory_handler)
1518{
1519 memory_methods.acquire_aligned_memory_handler=acquire_aligned_memory_handler;
1520 memory_methods.relinquish_aligned_memory_handler=
1521 relinquish_aligned_memory_handler;
1522}
1523
1524/*
1525%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1526% %
1527% %
1528% %
1529% S e t M a g i c k M e m o r y M e t h o d s %
1530% %
1531% %
1532% %
1533%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1534%
1535% SetMagickMemoryMethods() sets the methods to acquire, resize, and destroy
1536% memory. Your custom memory methods must be set prior to the
1537% MagickCoreGenesis() method.
1538%
1539% The format of the SetMagickMemoryMethods() method is:
1540%
1541% void SetMagickMemoryMethods(AcquireMemoryHandler acquire_memory_handler,
1542% ResizeMemoryHandler resize_memory_handler,
1543% DestroyMemoryHandler destroy_memory_handler)
1544%
1545% A description of each parameter follows:
1546%
1547% o acquire_memory_handler: method to acquire memory (e.g. malloc).
1548%
1549% o resize_memory_handler: method to resize memory (e.g. realloc).
1550%
1551% o destroy_memory_handler: method to destroy memory (e.g. free).
1552%
1553*/
1554MagickExport void SetMagickMemoryMethods(
1555 AcquireMemoryHandler acquire_memory_handler,
1556 ResizeMemoryHandler resize_memory_handler,
1557 DestroyMemoryHandler destroy_memory_handler)
1558{
1559 /*
1560 Set memory methods.
1561 */
1562 if (acquire_memory_handler != (AcquireMemoryHandler) NULL)
1563 memory_methods.acquire_memory_handler=acquire_memory_handler;
1564 if (resize_memory_handler != (ResizeMemoryHandler) NULL)
1565 memory_methods.resize_memory_handler=resize_memory_handler;
1566 if (destroy_memory_handler != (DestroyMemoryHandler) NULL)
1567 memory_methods.destroy_memory_handler=destroy_memory_handler;
1568}
1569
1570/*
1571%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1572% %
1573% %
1574% %
1575+ S e t M a x M e m o r y R e q u e s t %
1576% %
1577% %
1578% %
1579%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1580%
1581% SetMaxMemoryRequest() sets the max memory request value.
1582%
1583% The format of the SetMaxMemoryRequest method is:
1584%
1585% void SetMaxMemoryRequest(const MagickSizeType limit)
1586%
1587% A description of each parameter follows:
1588%
1589% o limit: the maximum memory request limit.
1590%
1591*/
1592MagickPrivate void SetMaxMemoryRequest(const MagickSizeType limit)
1593{
1594 max_memory_request=(size_t) MagickMin(limit,GetMaxMemoryRequestFromPolicy());
1595}
1596
1597/*
1598%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1599% %
1600% %
1601% %
1602+ S e t M a x P r o f i l e S i z e %
1603% %
1604% %
1605% %
1606%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1607%
1608% SetMaxProfileSize() sets the max profile size value.
1609%
1610% The format of the SetMaxProfileSize method is:
1611%
1612% void SetMaxProfileSize(const MagickSizeType limit)
1613%
1614% A description of each parameter follows:
1615%
1616% o limit: the maximum profile size limit.
1617%
1618*/
1619MagickPrivate void SetMaxProfileSize(const MagickSizeType limit)
1620{
1621 max_profile_size=(size_t) MagickMin(limit,GetMaxProfileSizeFromPolicy());
1622}
1623
1624/*
1625%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1626% %
1627% %
1628% %
1629% S h r e d M a g i c k M e m o r y %
1630% %
1631% %
1632% %
1633%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1634%
1635% ShredMagickMemory() overwrites the specified memory buffer with random data.
1636% The overwrite is optional and is only required to help keep the contents of
1637% the memory buffer private.
1638%
1639% The format of the ShredMagickMemory method is:
1640%
1641% MagickBooleanType ShredMagickMemory(void *memory,const size_t length)
1642%
1643% A description of each parameter follows.
1644%
1645% o memory: Specifies the memory buffer.
1646%
1647% o length: Specifies the length of the memory buffer.
1648%
1649*/
1650MagickPrivate MagickBooleanType ShredMagickMemory(void *memory,
1651 const size_t length)
1652{
1653 RandomInfo
1654 *random_info;
1655
1656 size_t
1657 quantum;
1658
1659 ssize_t
1660 i;
1661
1662 static ssize_t
1663 passes = -1;
1664
1665 StringInfo
1666 *key;
1667
1668 if ((memory == NULL) || (length == 0))
1669 return(MagickFalse);
1670 if (passes == -1)
1671 {
1672 char
1673 *property;
1674
1675 passes=0;
1676 property=GetEnvironmentValue("MAGICK_SHRED_PASSES");
1677 if (property != (char *) NULL)
1678 {
1679 passes=(ssize_t) StringToInteger(property);
1680 property=DestroyString(property);
1681 }
1682 property=GetPolicyValue("system:shred");
1683 if (property != (char *) NULL)
1684 {
1685 passes=(ssize_t) StringToInteger(property);
1686 property=DestroyString(property);
1687 }
1688 }
1689 if (passes == 0)
1690 return(MagickTrue);
1691 /*
1692 Overwrite the memory buffer with random data.
1693 */
1694 quantum=(size_t) MagickMin(length,MagickMinBufferExtent);
1695 random_info=AcquireRandomInfo();
1696 key=GetRandomKey(random_info,quantum);
1697 for (i=0; i < passes; i++)
1698 {
1699 size_t
1700 j;
1701
1702 unsigned char
1703 *p = (unsigned char *) memory;
1704
1705 for (j=0; j < length; j+=quantum)
1706 {
1707 if (i != 0)
1708 SetRandomKey(random_info,quantum,GetStringInfoDatum(key));
1709 (void) memcpy(p,GetStringInfoDatum(key),(size_t)
1710 MagickMin(quantum,length-j));
1711 p+=(ptrdiff_t) quantum;
1712 }
1713 if (j < length)
1714 break;
1715 }
1716 key=DestroyStringInfo(key);
1717 random_info=DestroyRandomInfo(random_info);
1718 return(i < passes ? MagickFalse : MagickTrue);
1719}