]> granicus.if.org Git - imagemagick/blob - MagickCore/memory.c
(no commit message)
[imagemagick] / MagickCore / 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 %                                John Cristy                                  %
17 %                                 July 1998                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2013 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 %    http://www.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 %  Segregate our memory requirements from any program that calls our API.  This
37 %  should help reduce the risk of others changing our program state or causing
38 %  memory corruption.
39 %
40 %  Our custom memory allocation manager implements a best-fit allocation policy
41 %  using segregated free lists.  It uses a linear distribution of size classes
42 %  for lower sizes and a power of two distribution of size classes at higher
43 %  sizes.  It is based on the paper, "Fast Memory Allocation using Lazy Fits."
44 %  written by Yoo C. Chung.
45 %
46 %  By default, ANSI memory methods are called (e.g. malloc).  Use the
47 %  custom memory allocator by defining MAGICKCORE_ZERO_CONFIGURATION_SUPPORT
48 %  to allocate memory with private anonymous mapping rather than from the
49 %  heap.
50 %
51 */
52 \f
53 /*
54   Include declarations.
55 */
56 #include "MagickCore/studio.h"
57 #include "MagickCore/blob.h"
58 #include "MagickCore/blob-private.h"
59 #include "MagickCore/exception.h"
60 #include "MagickCore/exception-private.h"
61 #include "MagickCore/memory_.h"
62 #include "MagickCore/memory-private.h"
63 #include "MagickCore/resource_.h"
64 #include "MagickCore/semaphore.h"
65 #include "MagickCore/string_.h"
66 #include "MagickCore/utility-private.h"
67 \f
68 /*
69   Define declarations.
70 */
71 #define BlockFooter(block,size) \
72   ((size_t *) ((char *) (block)+(size)-2*sizeof(size_t)))
73 #define BlockHeader(block)  ((size_t *) (block)-1)
74 #define BlockSize  4096
75 #define BlockThreshold  1024
76 #define MaxBlockExponent  16
77 #define MaxBlocks ((BlockThreshold/(4*sizeof(size_t)))+MaxBlockExponent+1)
78 #define MaxSegments  1024
79 #define MemoryGuard  ((0xdeadbeef << 31)+0xdeafdeed)
80 #define NextBlock(block)  ((char *) (block)+SizeOfBlock(block))
81 #define NextBlockInList(block)  (*(void **) (block))
82 #define PreviousBlock(block)  ((char *) (block)-(*((size_t *) (block)-2)))
83 #define PreviousBlockBit  0x01
84 #define PreviousBlockInList(block)  (*((void **) (block)+1))
85 #define SegmentSize  (2*1024*1024)
86 #define SizeMask  (~0x01)
87 #define SizeOfBlock(block)  (*BlockHeader(block) & SizeMask)
88 \f
89 /*
90   Typedef declarations.
91 */
92 typedef struct _DataSegmentInfo
93 {
94   void
95     *allocation,
96     *bound;
97
98   MagickBooleanType
99     mapped;
100
101   size_t
102     length;
103
104   struct _DataSegmentInfo
105     *previous,
106     *next;
107 } DataSegmentInfo;
108
109 typedef struct _MagickMemoryMethods
110 {
111   AcquireMemoryHandler
112     acquire_memory_handler;
113
114   ResizeMemoryHandler
115     resize_memory_handler;
116
117   DestroyMemoryHandler
118     destroy_memory_handler;
119 } MagickMemoryMethods;
120
121 struct _MemoryInfo
122 {
123   char
124     filename[MaxTextExtent];
125
126   MagickBooleanType
127     mapped;
128
129   size_t
130     length;
131
132   void
133     *blob;
134
135   size_t
136     signature;
137 };
138
139 typedef struct _MemoryPool
140 {
141   size_t
142     allocation;
143
144   void
145     *blocks[MaxBlocks+1];
146
147   size_t
148     number_segments;
149
150   DataSegmentInfo
151     *segments[MaxSegments],
152     segment_pool[MaxSegments];
153 } MemoryPool;
154 \f
155 /*
156   Global declarations.
157 */
158 static MagickMemoryMethods
159   memory_methods =
160   {
161     (AcquireMemoryHandler) malloc,
162     (ResizeMemoryHandler) realloc,
163     (DestroyMemoryHandler) free
164   };
165
166 #if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
167 static MemoryPool
168   memory_pool;
169
170 static SemaphoreInfo
171   *memory_semaphore = (SemaphoreInfo *) NULL;
172
173 static volatile DataSegmentInfo
174   *free_segments = (DataSegmentInfo *) NULL;
175 \f
176 /*
177   Forward declarations.
178 */
179 static MagickBooleanType
180   ExpandHeap(size_t);
181 #endif
182 \f
183 /*
184 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185 %                                                                             %
186 %                                                                             %
187 %                                                                             %
188 %   A c q u i r e A l i g n e d M e m o r y                                   %
189 %                                                                             %
190 %                                                                             %
191 %                                                                             %
192 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
193 %
194 %  AcquireAlignedMemory() returns a pointer to a block of memory at least size
195 %  bytes whose address is a multiple of 16*sizeof(void *).
196 %
197 %  The format of the AcquireAlignedMemory method is:
198 %
199 %      void *AcquireAlignedMemory(const size_t count,const size_t quantum)
200 %
201 %  A description of each parameter follows:
202 %
203 %    o count: the number of quantum elements to allocate.
204 %
205 %    o quantum: the number of bytes in each quantum.
206 %
207 */
208 MagickExport void *AcquireAlignedMemory(const size_t count,const size_t quantum)
209 {
210 #define AlignedExtent(size,alignment) \
211   (((size)+((alignment)-1)) & ~((alignment)-1))
212
213   size_t
214     alignment,
215     extent,
216     size;
217
218   void
219     *memory;
220
221   size=count*quantum;
222   if ((count == 0) || (quantum != (size/count)))
223     {
224       errno=ENOMEM;
225       return((void *) NULL);
226     }
227   memory=NULL;
228   alignment=CACHE_LINE_SIZE;
229   extent=AlignedExtent(size,alignment);
230   if ((size == 0) || (alignment < sizeof(void *)) || (extent < size))
231     return((void *) NULL);
232 #if defined(MAGICKCORE_HAVE_POSIX_MEMALIGN)
233   if (posix_memalign(&memory,alignment,extent) != 0)
234     memory=NULL;
235 #elif defined(MAGICKCORE_HAVE__ALIGNED_MALLOC)
236   memory=_aligned_malloc(extent,alignment);
237 #else
238   {
239     void
240       *p;
241
242     extent=(size+alignment-1)+sizeof(void *);
243     if (extent > size)
244       {
245         p=malloc(extent);
246         if (p != NULL)
247           {
248             memory=(void *) AlignedExtent((size_t) p+sizeof(void *),alignment);
249             *((void **) memory-1)=p;
250           }
251       }
252   }
253 #endif
254   return(memory);
255 }
256 \f
257 #if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
258 /*
259 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260 %                                                                             %
261 %                                                                             %
262 %                                                                             %
263 +   A c q u i r e B l o c k                                                   %
264 %                                                                             %
265 %                                                                             %
266 %                                                                             %
267 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268 %
269 %  AcquireBlock() returns a pointer to a block of memory at least size bytes
270 %  suitably aligned for any use.
271 %
272 %  The format of the AcquireBlock method is:
273 %
274 %      void *AcquireBlock(const size_t size)
275 %
276 %  A description of each parameter follows:
277 %
278 %    o size: the size of the memory in bytes to allocate.
279 %
280 */
281
282 static inline size_t AllocationPolicy(size_t size)
283 {
284   register size_t
285     blocksize;
286
287   /*
288     The linear distribution.
289   */
290   assert(size != 0);
291   assert(size % (4*sizeof(size_t)) == 0);
292   if (size <= BlockThreshold)
293     return(size/(4*sizeof(size_t)));
294   /*
295     Check for the largest block size.
296   */
297   if (size > (size_t) (BlockThreshold*(1L << (MaxBlockExponent-1L))))
298     return(MaxBlocks-1L);
299   /*
300     Otherwise use a power of two distribution.
301   */
302   blocksize=BlockThreshold/(4*sizeof(size_t));
303   for ( ; size > BlockThreshold; size/=2)
304     blocksize++;
305   assert(blocksize > (BlockThreshold/(4*sizeof(size_t))));
306   assert(blocksize < (MaxBlocks-1L));
307   return(blocksize);
308 }
309
310 static inline void InsertFreeBlock(void *block,const size_t i)
311 {
312   register void
313     *next,
314     *previous;
315
316   size_t
317     size;
318
319   size=SizeOfBlock(block);
320   previous=(void *) NULL;
321   next=memory_pool.blocks[i];
322   while ((next != (void *) NULL) && (SizeOfBlock(next) < size))
323   {
324     previous=next;
325     next=NextBlockInList(next);
326   }
327   PreviousBlockInList(block)=previous;
328   NextBlockInList(block)=next;
329   if (previous != (void *) NULL)
330     NextBlockInList(previous)=block;
331   else
332     memory_pool.blocks[i]=block;
333   if (next != (void *) NULL)
334     PreviousBlockInList(next)=block;
335 }
336
337 static inline void RemoveFreeBlock(void *block,const size_t i)
338 {
339   register void
340     *next,
341     *previous;
342
343   next=NextBlockInList(block);
344   previous=PreviousBlockInList(block);
345   if (previous == (void *) NULL)
346     memory_pool.blocks[i]=next;
347   else
348     NextBlockInList(previous)=next;
349   if (next != (void *) NULL)
350     PreviousBlockInList(next)=previous;
351 }
352
353 static void *AcquireBlock(size_t size)
354 {
355   register size_t
356     i;
357
358   register void
359     *block;
360
361   /*
362     Find free block.
363   */
364   size=(size_t) (size+sizeof(size_t)+6*sizeof(size_t)-1) & -(4U*sizeof(size_t));
365   i=AllocationPolicy(size);
366   block=memory_pool.blocks[i];
367   while ((block != (void *) NULL) && (SizeOfBlock(block) < size))
368     block=NextBlockInList(block);
369   if (block == (void *) NULL)
370     {
371       i++;
372       while (memory_pool.blocks[i] == (void *) NULL)
373         i++;
374       block=memory_pool.blocks[i];
375       if (i >= MaxBlocks)
376         return((void *) NULL);
377     }
378   assert((*BlockHeader(NextBlock(block)) & PreviousBlockBit) == 0);
379   assert(SizeOfBlock(block) >= size);
380   RemoveFreeBlock(block,AllocationPolicy(SizeOfBlock(block)));
381   if (SizeOfBlock(block) > size)
382     {
383       size_t
384         blocksize;
385
386       void
387         *next;
388
389       /*
390         Split block.
391       */
392       next=(char *) block+size;
393       blocksize=SizeOfBlock(block)-size;
394       *BlockHeader(next)=blocksize;
395       *BlockFooter(next,blocksize)=blocksize;
396       InsertFreeBlock(next,AllocationPolicy(blocksize));
397       *BlockHeader(block)=size | (*BlockHeader(block) & ~SizeMask);
398     }
399   assert(size == SizeOfBlock(block));
400   *BlockHeader(NextBlock(block))|=PreviousBlockBit;
401   memory_pool.allocation+=size;
402   return(block);
403 }
404 #endif
405 \f
406 /*
407 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
408 %                                                                             %
409 %                                                                             %
410 %                                                                             %
411 %   A c q u i r e M a g i c k M e m o r y                                     %
412 %                                                                             %
413 %                                                                             %
414 %                                                                             %
415 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
416 %
417 %  AcquireMagickMemory() returns a pointer to a block of memory at least size
418 %  bytes suitably aligned for any use.
419 %
420 %  The format of the AcquireMagickMemory method is:
421 %
422 %      void *AcquireMagickMemory(const size_t size)
423 %
424 %  A description of each parameter follows:
425 %
426 %    o size: the size of the memory in bytes to allocate.
427 %
428 */
429 MagickExport void *AcquireMagickMemory(const size_t size)
430 {
431   register void
432     *memory;
433
434 #if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
435   memory=memory_methods.acquire_memory_handler(size == 0 ? 1UL : size);
436 #else
437   if (memory_semaphore == (SemaphoreInfo *) NULL)
438     AcquireSemaphoreInfo(&memory_semaphore);
439   if (free_segments == (DataSegmentInfo *) NULL)
440     {
441       LockSemaphoreInfo(memory_semaphore);
442       if (free_segments == (DataSegmentInfo *) NULL)
443         {
444           register ssize_t
445             i;
446
447           assert(2*sizeof(size_t) > (size_t) (~SizeMask));
448           (void) ResetMagickMemory(&memory_pool,0,sizeof(memory_pool));
449           memory_pool.allocation=SegmentSize;
450           memory_pool.blocks[MaxBlocks]=(void *) (-1);
451           for (i=0; i < MaxSegments; i++)
452           {
453             if (i != 0)
454               memory_pool.segment_pool[i].previous=
455                 (&memory_pool.segment_pool[i-1]);
456             if (i != (MaxSegments-1))
457               memory_pool.segment_pool[i].next=(&memory_pool.segment_pool[i+1]);
458           }
459           free_segments=(&memory_pool.segment_pool[0]);
460         }
461       UnlockSemaphoreInfo(memory_semaphore);
462     }
463   LockSemaphoreInfo(memory_semaphore);
464   memory=AcquireBlock(size == 0 ? 1UL : size);
465   if (memory == (void *) NULL)
466     {
467       if (ExpandHeap(size == 0 ? 1UL : size) != MagickFalse)
468         memory=AcquireBlock(size == 0 ? 1UL : size);
469     }
470   UnlockSemaphoreInfo(memory_semaphore);
471 #endif
472   return(memory);
473 }
474 \f
475 /*
476 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
477 %                                                                             %
478 %                                                                             %
479 %                                                                             %
480 %   A c q u i r e Q u a n t u m M e m o r y                                   %
481 %                                                                             %
482 %                                                                             %
483 %                                                                             %
484 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
485 %
486 %  AcquireQuantumMemory() returns a pointer to a block of memory at least
487 %  count * quantum bytes suitably aligned for any use.
488 %
489 %  The format of the AcquireQuantumMemory method is:
490 %
491 %      void *AcquireQuantumMemory(const size_t count,const size_t quantum)
492 %
493 %  A description of each parameter follows:
494 %
495 %    o count: the number of quantum elements to allocate.
496 %
497 %    o quantum: the number of bytes in each quantum.
498 %
499 */
500 MagickExport void *AcquireQuantumMemory(const size_t count,const size_t quantum)
501 {
502   size_t
503     size;
504
505   size=count*quantum;
506   if ((count == 0) || (quantum != (size/count)))
507     {
508       errno=ENOMEM;
509       return((void *) NULL);
510     }
511   return(AcquireMagickMemory(size));
512 }
513 \f
514 /*
515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
516 %                                                                             %
517 %                                                                             %
518 %                                                                             %
519 %   A c q u i r e V i r t u a l M e m o r y                                   %
520 %                                                                             %
521 %                                                                             %
522 %                                                                             %
523 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
524 %
525 %  AcquireVirtualMemory() allocates a pointer to a block of memory at least size
526 %  bytes suitably aligned for any use.
527 %
528 %  The format of the AcquireVirtualMemory method is:
529 %
530 %      MemoryInfo *AcquireVirtualMemory(const size_t count,const size_t quantum)
531 %
532 %  A description of each parameter follows:
533 %
534 %    o count: the number of quantum elements to allocate.
535 %
536 %    o quantum: the number of bytes in each quantum.
537 %
538 */
539 MagickExport MemoryInfo *AcquireVirtualMemory(const size_t count,
540   const size_t quantum)
541 {
542   MemoryInfo
543     *memory_info;
544
545   size_t
546     length;
547
548   length=count*quantum;
549   if ((count == 0) || (quantum != (length/count)))
550     {
551       errno=ENOMEM;
552       return((MemoryInfo *) NULL);
553     }
554   memory_info=(MemoryInfo *) MagickAssumeAligned(AcquireAlignedMemory(1,
555     sizeof(*memory_info)));
556   if (memory_info == (MemoryInfo *) NULL)
557     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
558   (void) ResetMagickMemory(memory_info,0,sizeof(*memory_info));
559   memory_info->length=length;
560   memory_info->signature=MagickSignature;
561   memory_info->blob=AcquireMagickMemory(length);
562   if (memory_info->blob == NULL)
563     {
564       /*
565         Heap memory failed, try anonymous memory mapping.
566       */
567       memory_info->mapped=MagickTrue;
568       memory_info->blob=MapBlob(-1,IOMode,0,length);
569     }
570   if (memory_info->blob == NULL)
571     {
572       int
573         file;
574
575       /*
576         Anonymous memory mapping failed, try file-backed memory mapping.
577       */
578       file=AcquireUniqueFileResource(memory_info->filename);
579       file=open_utf8(memory_info->filename,O_RDWR | O_CREAT | O_BINARY | O_EXCL,
580         S_MODE);
581       if (file == -1)
582         file=open_utf8(memory_info->filename,O_RDWR | O_BINARY,S_MODE);
583       if (file != -1)
584         {
585           if ((lseek(file,length-1,SEEK_SET) >= 0) && (write(file,"",1) == 1))
586             memory_info->blob=MapBlob(file,IOMode,0,length);
587           (void) close(file);
588         }
589     }
590   if (memory_info->blob == NULL)
591     return(RelinquishVirtualMemory(memory_info));
592   return(memory_info);
593 }
594 \f
595 /*
596 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
597 %                                                                             %
598 %                                                                             %
599 %                                                                             %
600 %   C o p y M a g i c k M e m o r y                                           %
601 %                                                                             %
602 %                                                                             %
603 %                                                                             %
604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
605 %
606 %  CopyMagickMemory() copies size bytes from memory area source to the
607 %  destination.  Copying between objects that overlap will take place
608 %  correctly.  It returns destination.
609 %
610 %  The format of the CopyMagickMemory method is:
611 %
612 %      void *CopyMagickMemory(void *destination,const void *source,
613 %        const size_t size)
614 %
615 %  A description of each parameter follows:
616 %
617 %    o destination: the destination.
618 %
619 %    o source: the source.
620 %
621 %    o size: the size of the memory in bytes to allocate.
622 %
623 */
624 MagickExport void *CopyMagickMemory(void *destination,const void *source,
625   const size_t size)
626 {
627   register const unsigned char
628     *p;
629
630   register unsigned char
631     *q;
632
633   assert(destination != (void *) NULL);
634   assert(source != (const void *) NULL);
635   p=(const unsigned char *) source;
636   q=(unsigned char *) destination;
637   if (((q+size) < p) || (q > (p+size)))
638     switch (size)
639     {
640       default: return(memcpy(destination,source,size));
641       case 8: *q++=(*p++);
642       case 7: *q++=(*p++);
643       case 6: *q++=(*p++);
644       case 5: *q++=(*p++);
645       case 4: *q++=(*p++);
646       case 3: *q++=(*p++);
647       case 2: *q++=(*p++);
648       case 1: *q++=(*p++);
649       case 0: return(destination);
650     }
651   return(memmove(destination,source,size));
652 }
653 \f
654 /*
655 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
656 %                                                                             %
657 %                                                                             %
658 %                                                                             %
659 +   D e s t r o y M a g i c k M e m o r y                                     %
660 %                                                                             %
661 %                                                                             %
662 %                                                                             %
663 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
664 %
665 %  DestroyMagickMemory() deallocates memory associated with the memory manager.
666 %
667 %  The format of the DestroyMagickMemory method is:
668 %
669 %      DestroyMagickMemory(void)
670 %
671 */
672 MagickExport void DestroyMagickMemory(void)
673 {
674 #if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
675   register ssize_t
676     i;
677
678   if (memory_semaphore == (SemaphoreInfo *) NULL)
679     AcquireSemaphoreInfo(&memory_semaphore);
680   LockSemaphoreInfo(memory_semaphore);
681   UnlockSemaphoreInfo(memory_semaphore);
682   for (i=0; i < (ssize_t) memory_pool.number_segments; i++)
683     if (memory_pool.segments[i]->mapped == MagickFalse)
684       memory_methods.destroy_memory_handler(
685         memory_pool.segments[i]->allocation);
686     else
687       (void) UnmapBlob(memory_pool.segments[i]->allocation,
688         memory_pool.segments[i]->length);
689   free_segments=(DataSegmentInfo *) NULL;
690   (void) ResetMagickMemory(&memory_pool,0,sizeof(memory_pool));
691   DestroySemaphoreInfo(&memory_semaphore);
692 #endif
693 }
694 \f
695 #if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
696 /*
697 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
698 %                                                                             %
699 %                                                                             %
700 %                                                                             %
701 +   E x p a n d H e a p                                                       %
702 %                                                                             %
703 %                                                                             %
704 %                                                                             %
705 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
706 %
707 %  ExpandHeap() get more memory from the system.  It returns MagickTrue on
708 %  success otherwise MagickFalse.
709 %
710 %  The format of the ExpandHeap method is:
711 %
712 %      MagickBooleanType ExpandHeap(size_t size)
713 %
714 %  A description of each parameter follows:
715 %
716 %    o size: the size of the memory in bytes we require.
717 %
718 */
719 static MagickBooleanType ExpandHeap(size_t size)
720 {
721   DataSegmentInfo
722     *segment_info;
723
724   MagickBooleanType
725     mapped;
726
727   register ssize_t
728     i;
729
730   register void
731     *block;
732
733   size_t
734     blocksize;
735
736   void
737     *segment;
738
739   blocksize=((size+12*sizeof(size_t))+SegmentSize-1) & -SegmentSize;
740   assert(memory_pool.number_segments < MaxSegments);
741   segment=MapBlob(-1,IOMode,0,blocksize);
742   mapped=segment != (void *) NULL ? MagickTrue : MagickFalse;
743   if (segment == (void *) NULL)
744     segment=(void *) memory_methods.acquire_memory_handler(blocksize);
745   if (segment == (void *) NULL)
746     return(MagickFalse);
747   segment_info=(DataSegmentInfo *) free_segments;
748   free_segments=segment_info->next;
749   segment_info->mapped=mapped;
750   segment_info->length=blocksize;
751   segment_info->allocation=segment;
752   segment_info->bound=(char *) segment+blocksize;
753   i=(ssize_t) memory_pool.number_segments-1;
754   for ( ; (i >= 0) && (memory_pool.segments[i]->allocation > segment); i--)
755     memory_pool.segments[i+1]=memory_pool.segments[i];
756   memory_pool.segments[i+1]=segment_info;
757   memory_pool.number_segments++;
758   size=blocksize-12*sizeof(size_t);
759   block=(char *) segment_info->allocation+4*sizeof(size_t);
760   *BlockHeader(block)=size | PreviousBlockBit;
761   *BlockFooter(block,size)=size;
762   InsertFreeBlock(block,AllocationPolicy(size));
763   block=NextBlock(block);
764   assert(block < segment_info->bound);
765   *BlockHeader(block)=2*sizeof(size_t);
766   *BlockHeader(NextBlock(block))=PreviousBlockBit;
767   return(MagickTrue);
768 }
769 #endif
770 \f
771 /*
772 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
773 %                                                                             %
774 %                                                                             %
775 %                                                                             %
776 %   G e t M a g i c k M e m o r y M e t h o d s                               %
777 %                                                                             %
778 %                                                                             %
779 %                                                                             %
780 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
781 %
782 %  GetMagickMemoryMethods() gets the methods to acquire, resize, and destroy
783 %  memory.
784 %
785 %  The format of the GetMagickMemoryMethods() method is:
786 %
787 %      void GetMagickMemoryMethods(AcquireMemoryHandler *acquire_memory_handler,
788 %        ResizeMemoryHandler *resize_memory_handler,
789 %        DestroyMemoryHandler *destroy_memory_handler)
790 %
791 %  A description of each parameter follows:
792 %
793 %    o acquire_memory_handler: method to acquire memory (e.g. malloc).
794 %
795 %    o resize_memory_handler: method to resize memory (e.g. realloc).
796 %
797 %    o destroy_memory_handler: method to destroy memory (e.g. free).
798 %
799 */
800 MagickExport void GetMagickMemoryMethods(
801   AcquireMemoryHandler *acquire_memory_handler,
802   ResizeMemoryHandler *resize_memory_handler,
803   DestroyMemoryHandler *destroy_memory_handler)
804 {
805   assert(acquire_memory_handler != (AcquireMemoryHandler *) NULL);
806   assert(resize_memory_handler != (ResizeMemoryHandler *) NULL);
807   assert(destroy_memory_handler != (DestroyMemoryHandler *) NULL);
808   *acquire_memory_handler=memory_methods.acquire_memory_handler;
809   *resize_memory_handler=memory_methods.resize_memory_handler;
810   *destroy_memory_handler=memory_methods.destroy_memory_handler;
811 }
812 \f
813 /*
814 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
815 %                                                                             %
816 %                                                                             %
817 %                                                                             %
818 %   G e t V i r t u a l M e m o r y B l o b                                   %
819 %                                                                             %
820 %                                                                             %
821 %                                                                             %
822 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
823 %
824 %  GetVirtualMemoryBlob() returns the virtual memory blob associated with the
825 %  specified MemoryInfo structure.
826 %
827 %  The format of the GetVirtualMemoryBlob method is:
828 %
829 %      void *GetVirtualMemoryBlob(const MemoryInfo *memory_info)
830 %
831 %  A description of each parameter follows:
832 %
833 %    o memory_info: The MemoryInfo structure.
834 */
835 MagickExport void *GetVirtualMemoryBlob(const MemoryInfo *memory_info)
836 {
837   assert(memory_info != (const MemoryInfo *) NULL);
838   assert(memory_info->signature == MagickSignature);
839   return(memory_info->blob);
840 }
841 \f
842 /*
843 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
844 %                                                                             %
845 %                                                                             %
846 %                                                                             %
847 %   R e l i n q u i s h A l i g n e d M e m o r y                             %
848 %                                                                             %
849 %                                                                             %
850 %                                                                             %
851 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
852 %
853 %  RelinquishAlignedMemory() frees memory acquired with AcquireAlignedMemory()
854 %  or reuse.
855 %
856 %  The format of the RelinquishAlignedMemory method is:
857 %
858 %      void *RelinquishAlignedMemory(void *memory)
859 %
860 %  A description of each parameter follows:
861 %
862 %    o memory: A pointer to a block of memory to free for reuse.
863 %
864 */
865 MagickExport void *RelinquishAlignedMemory(void *memory)
866 {
867   if (memory == (void *) NULL)
868     return((void *) NULL);
869 #if defined(MAGICKCORE_HAVE_POSIX_MEMALIGN)
870   free(memory);
871 #elif defined(MAGICKCORE_HAVE__ALIGNED_MALLOC)
872   _aligned_free(memory);
873 #else
874   free(*((void **) memory-1));
875 #endif
876   return(NULL);
877 }
878 \f
879 /*
880 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
881 %                                                                             %
882 %                                                                             %
883 %                                                                             %
884 %   R e l i n q u i s h M a g i c k M e m o r y                               %
885 %                                                                             %
886 %                                                                             %
887 %                                                                             %
888 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
889 %
890 %  RelinquishMagickMemory() frees memory acquired with AcquireMagickMemory()
891 %  or AcquireQuantumMemory() for reuse.
892 %
893 %  The format of the RelinquishMagickMemory method is:
894 %
895 %      void *RelinquishMagickMemory(void *memory)
896 %
897 %  A description of each parameter follows:
898 %
899 %    o memory: A pointer to a block of memory to free for reuse.
900 %
901 */
902 MagickExport void *RelinquishMagickMemory(void *memory)
903 {
904   if (memory == (void *) NULL)
905     return((void *) NULL);
906 #if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
907   memory_methods.destroy_memory_handler(memory);
908 #else
909   LockSemaphoreInfo(memory_semaphore);
910   assert((SizeOfBlock(memory) % (4*sizeof(size_t))) == 0);
911   assert((*BlockHeader(NextBlock(memory)) & PreviousBlockBit) != 0);
912   if ((*BlockHeader(memory) & PreviousBlockBit) == 0)
913     {
914       void
915         *previous;
916
917       /*
918         Coalesce with previous adjacent block.
919       */
920       previous=PreviousBlock(memory);
921       RemoveFreeBlock(previous,AllocationPolicy(SizeOfBlock(previous)));
922       *BlockHeader(previous)=(SizeOfBlock(previous)+SizeOfBlock(memory)) |
923         (*BlockHeader(previous) & ~SizeMask);
924       memory=previous;
925     }
926   if ((*BlockHeader(NextBlock(NextBlock(memory))) & PreviousBlockBit) == 0)
927     {
928       void
929         *next;
930
931       /*
932         Coalesce with next adjacent block.
933       */
934       next=NextBlock(memory);
935       RemoveFreeBlock(next,AllocationPolicy(SizeOfBlock(next)));
936       *BlockHeader(memory)=(SizeOfBlock(memory)+SizeOfBlock(next)) |
937         (*BlockHeader(memory) & ~SizeMask);
938     }
939   *BlockFooter(memory,SizeOfBlock(memory))=SizeOfBlock(memory);
940   *BlockHeader(NextBlock(memory))&=(~PreviousBlockBit);
941   InsertFreeBlock(memory,AllocationPolicy(SizeOfBlock(memory)));
942   UnlockSemaphoreInfo(memory_semaphore);
943 #endif
944   return((void *) NULL);
945 }
946 \f
947 /*
948 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
949 %                                                                             %
950 %                                                                             %
951 %                                                                             %
952 %   R e l i n q u i s h V i r t u a l M e m o r y                             %
953 %                                                                             %
954 %                                                                             %
955 %                                                                             %
956 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
957 %
958 %  RelinquishVirtualMemory() frees memory acquired with AcquireVirtualMemory().
959 %
960 %  The format of the RelinquishVirtualMemory method is:
961 %
962 %      MemoryInfo *RelinquishVirtualMemory(MemoryInfo *memory_info)
963 %
964 %  A description of each parameter follows:
965 %
966 %    o memory_info: A pointer to a block of memory to free for reuse.
967 %
968 */
969 MagickExport MemoryInfo *RelinquishVirtualMemory(MemoryInfo *memory_info)
970 {
971   assert(memory_info != (MemoryInfo *) NULL);
972   assert(memory_info->signature == MagickSignature);
973   if (memory_info->blob != (void *) NULL)
974     {
975       if (memory_info->mapped == MagickFalse)
976         memory_info->blob=RelinquishMagickMemory(memory_info->blob);
977       else
978         {
979           (void) UnmapBlob(memory_info->blob,memory_info->length);
980           memory_info->blob=NULL;
981           if (*memory_info->filename != '\0')
982             (void) RelinquishUniqueFileResource(memory_info->filename);
983         }
984     }
985   memory_info->signature=(~MagickSignature);
986   memory_info=(MemoryInfo *) RelinquishAlignedMemory(memory_info);
987   return(memory_info);
988 }
989 \f
990 /*
991 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
992 %                                                                             %
993 %                                                                             %
994 %                                                                             %
995 %   R e s e t M a g i c k M e m o r y                                         %
996 %                                                                             %
997 %                                                                             %
998 %                                                                             %
999 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1000 %
1001 %  ResetMagickMemory() fills the first size bytes of the memory area pointed to
1002 %  by memory with the constant byte c.
1003 %
1004 %  The format of the ResetMagickMemory method is:
1005 %
1006 %      void *ResetMagickMemory(void *memory,int byte,const size_t size)
1007 %
1008 %  A description of each parameter follows:
1009 %
1010 %    o memory: a pointer to a memory allocation.
1011 %
1012 %    o byte: set the memory to this value.
1013 %
1014 %    o size: size of the memory to reset.
1015 %
1016 */
1017 MagickExport void *ResetMagickMemory(void *memory,int byte,const size_t size)
1018 {
1019   assert(memory != (void *) NULL);
1020   return(memset(memory,byte,size));
1021 }
1022 \f
1023 /*
1024 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1025 %                                                                             %
1026 %                                                                             %
1027 %                                                                             %
1028 %   R e s i z e M a g i c k M e m o r y                                       %
1029 %                                                                             %
1030 %                                                                             %
1031 %                                                                             %
1032 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1033 %
1034 %  ResizeMagickMemory() changes the size of the memory and returns a pointer to
1035 %  the (possibly moved) block.  The contents will be unchanged up to the
1036 %  lesser of the new and old sizes.
1037 %
1038 %  The format of the ResizeMagickMemory method is:
1039 %
1040 %      void *ResizeMagickMemory(void *memory,const size_t size)
1041 %
1042 %  A description of each parameter follows:
1043 %
1044 %    o memory: A pointer to a memory allocation.
1045 %
1046 %    o size: the new size of the allocated memory.
1047 %
1048 */
1049
1050 #if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
1051 static inline void *ResizeBlock(void *block,size_t size)
1052 {
1053   register void
1054     *memory;
1055
1056   if (block == (void *) NULL)
1057     return(AcquireBlock(size));
1058   memory=AcquireBlock(size);
1059   if (memory == (void *) NULL)
1060     return((void *) NULL);
1061   if (size <= (SizeOfBlock(block)-sizeof(size_t)))
1062     (void) memcpy(memory,block,size);
1063   else
1064     (void) memcpy(memory,block,SizeOfBlock(block)-sizeof(size_t));
1065   memory_pool.allocation+=size;
1066   return(memory);
1067 }
1068 #endif
1069
1070 MagickExport void *ResizeMagickMemory(void *memory,const size_t size)
1071 {
1072   register void
1073     *block;
1074
1075   if (memory == (void *) NULL)
1076     return(AcquireMagickMemory(size));
1077 #if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
1078   block=memory_methods.resize_memory_handler(memory,size == 0 ? 1UL : size);
1079   if (block == (void *) NULL)
1080     memory=RelinquishMagickMemory(memory);
1081 #else
1082   LockSemaphoreInfo(memory_semaphore);
1083   block=ResizeBlock(memory,size == 0 ? 1UL : size);
1084   if (block == (void *) NULL)
1085     {
1086       if (ExpandHeap(size == 0 ? 1UL : size) == MagickFalse)
1087         {
1088           UnlockSemaphoreInfo(memory_semaphore);
1089           memory=RelinquishMagickMemory(memory);
1090           ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1091         }
1092       block=ResizeBlock(memory,size == 0 ? 1UL : size);
1093       assert(block != (void *) NULL);
1094     }
1095   UnlockSemaphoreInfo(memory_semaphore);
1096   memory=RelinquishMagickMemory(memory);
1097 #endif
1098   return(block);
1099 }
1100 \f
1101 /*
1102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1103 %                                                                             %
1104 %                                                                             %
1105 %                                                                             %
1106 %   R e s i z e Q u a n t u m M e m o r y                                     %
1107 %                                                                             %
1108 %                                                                             %
1109 %                                                                             %
1110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1111 %
1112 %  ResizeQuantumMemory() changes the size of the memory and returns a pointer
1113 %  to the (possibly moved) block.  The contents will be unchanged up to the
1114 %  lesser of the new and old sizes.
1115 %
1116 %  The format of the ResizeQuantumMemory method is:
1117 %
1118 %      void *ResizeQuantumMemory(void *memory,const size_t count,
1119 %        const size_t quantum)
1120 %
1121 %  A description of each parameter follows:
1122 %
1123 %    o memory: A pointer to a memory allocation.
1124 %
1125 %    o count: the number of quantum elements to allocate.
1126 %
1127 %    o quantum: the number of bytes in each quantum.
1128 %
1129 */
1130 MagickExport void *ResizeQuantumMemory(void *memory,const size_t count,
1131   const size_t quantum)
1132 {
1133   size_t
1134     size;
1135
1136   size=count*quantum;
1137   if ((count == 0) || (quantum != (size/count)))
1138     {
1139       memory=RelinquishMagickMemory(memory);
1140       errno=ENOMEM;
1141       return((void *) NULL);
1142     }
1143   return(ResizeMagickMemory(memory,size));
1144 }
1145 \f
1146 /*
1147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1148 %                                                                             %
1149 %                                                                             %
1150 %                                                                             %
1151 %   S e t M a g i c k M e m o r y M e t h o d s                               %
1152 %                                                                             %
1153 %                                                                             %
1154 %                                                                             %
1155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1156 %
1157 %  SetMagickMemoryMethods() sets the methods to acquire, resize, and destroy
1158 %  memory. Your custom memory methods must be set prior to the
1159 %  MagickCoreGenesis() method.
1160 %
1161 %  The format of the SetMagickMemoryMethods() method is:
1162 %
1163 %      SetMagickMemoryMethods(AcquireMemoryHandler acquire_memory_handler,
1164 %        ResizeMemoryHandler resize_memory_handler,
1165 %        DestroyMemoryHandler destroy_memory_handler)
1166 %
1167 %  A description of each parameter follows:
1168 %
1169 %    o acquire_memory_handler: method to acquire memory (e.g. malloc).
1170 %
1171 %    o resize_memory_handler: method to resize memory (e.g. realloc).
1172 %
1173 %    o destroy_memory_handler: method to destroy memory (e.g. free).
1174 %
1175 */
1176 MagickExport void SetMagickMemoryMethods(
1177   AcquireMemoryHandler acquire_memory_handler,
1178   ResizeMemoryHandler resize_memory_handler,
1179   DestroyMemoryHandler destroy_memory_handler)
1180 {
1181   /*
1182     Set memory methods.
1183   */
1184   if (acquire_memory_handler != (AcquireMemoryHandler) NULL)
1185     memory_methods.acquire_memory_handler=acquire_memory_handler;
1186   if (resize_memory_handler != (ResizeMemoryHandler) NULL)
1187     memory_methods.resize_memory_handler=resize_memory_handler;
1188   if (destroy_memory_handler != (DestroyMemoryHandler) NULL)
1189     memory_methods.destroy_memory_handler=destroy_memory_handler;
1190 }