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"
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)
122 UndefinedVirtualMemory,
123 AlignedVirtualMemory,
125 UnalignedVirtualMemory
148 acquire_memory_handler;
151 resize_memory_handler;
154 destroy_memory_handler;
156 AcquireAlignedMemoryHandler
157 acquire_aligned_memory_handler;
159 RelinquishAlignedMemoryHandler
160 relinquish_aligned_memory_handler;
166 filename[MagickPathExtent];
187 *blocks[MaxBlocks+1];
193 *segments[MaxSegments],
194 segment_pool[MaxSegments];
201 max_memory_request = 0,
202 max_profile_size = 0,
203 virtual_anonymous_memory = 0;
206static void *MSCMalloc(
size_t size)
208 return(malloc(size));
211static void *MSCRealloc(
void* ptr,
size_t size)
213 return(realloc(ptr,size));
216static void MSCFree(
void* ptr)
226 (AcquireMemoryHandler) MSCMalloc,
227 (ResizeMemoryHandler) MSCRealloc,
228 (DestroyMemoryHandler) MSCFree,
230 (AcquireMemoryHandler) malloc,
231 (ResizeMemoryHandler) realloc,
232 (DestroyMemoryHandler) free,
234 (AcquireAlignedMemoryHandler) NULL,
235 (RelinquishAlignedMemoryHandler) NULL
237#if defined(MAGICKCORE_ANONYMOUS_MEMORY_SUPPORT)
250static MagickBooleanType
279#if defined(MAGICKCORE_HAVE_ALIGNED_MALLOC)
280#define AcquireAlignedMemory_Actual AcquireAlignedMemory_STDC
281static inline void *AcquireAlignedMemory_STDC(
const size_t size)
284 extent = CACHE_ALIGNED(size);
291 return(aligned_alloc(CACHE_LINE_SIZE,extent));
293#elif defined(MAGICKCORE_HAVE_POSIX_MEMALIGN)
294#define AcquireAlignedMemory_Actual AcquireAlignedMemory_POSIX
295static inline void *AcquireAlignedMemory_POSIX(
const size_t size)
300 if (posix_memalign(&memory,CACHE_LINE_SIZE,size))
304#elif defined(MAGICKCORE_HAVE__ALIGNED_MALLOC)
305#define AcquireAlignedMemory_Actual AcquireAlignedMemory_WinAPI
306static inline void *AcquireAlignedMemory_WinAPI(
const size_t size)
308 return(_aligned_malloc(size,CACHE_LINE_SIZE));
311#define ALIGNMENT_OVERHEAD \
312 (MAGICKCORE_MAX_ALIGNMENT_PADDING(CACHE_LINE_SIZE) + MAGICKCORE_SIZEOF_VOID_P)
313static inline void *reserve_space_for_actual_base_address(
void *
const p)
315 return((
void **) p+1);
318static inline void **pointer_to_space_for_actual_base_address(
void *
const p)
320 return((
void **) p-1);
323static inline void *actual_base_address(
void *
const p)
325 return(*pointer_to_space_for_actual_base_address(p));
328static inline void *align_to_cache(
void *
const p)
330 return((
void *) CACHE_ALIGNED((MagickAddressType) p));
333static inline void *adjust(
void *
const p)
335 return(align_to_cache(reserve_space_for_actual_base_address(p)));
338#define AcquireAlignedMemory_Actual AcquireAlignedMemory_Generic
339static inline void *AcquireAlignedMemory_Generic(
const size_t size)
348 #if SIZE_MAX < ALIGNMENT_OVERHEAD
349 #error "CACHE_LINE_SIZE is way too big."
351 extent=(size+ALIGNMENT_OVERHEAD);
357 p=AcquireMagickMemory(extent);
361 *pointer_to_space_for_actual_base_address(memory)=p;
366MagickExport
void *AcquireAlignedMemory(
const size_t count,
const size_t quantum)
371 if (HeapOverflowSanityCheckGetSize(count,quantum,&size) != MagickFalse)
376 if (memory_methods.acquire_aligned_memory_handler != (AcquireAlignedMemoryHandler) NULL)
377 return(memory_methods.acquire_aligned_memory_handler(size,CACHE_LINE_SIZE));
378 return(AcquireAlignedMemory_Actual(size));
381#if defined(MAGICKCORE_ANONYMOUS_MEMORY_SUPPORT)
406static inline size_t AllocationPolicy(
size_t size)
415 assert(size % (4*
sizeof(
size_t)) == 0);
416 if (size <= BlockThreshold)
417 return(size/(4*
sizeof(
size_t)));
421 if (size > (
size_t) (BlockThreshold*(1L << (MaxBlockExponent-1L))))
422 return(MaxBlocks-1L);
426 blocksize=BlockThreshold/(4*
sizeof(size_t));
427 for ( ; size > BlockThreshold; size/=2)
429 assert(blocksize > (BlockThreshold/(4*
sizeof(
size_t))));
430 assert(blocksize < (MaxBlocks-1L));
434static inline void InsertFreeBlock(
void *block,
const size_t i)
443 size=SizeOfBlock(block);
444 previous=(
void *) NULL;
445 next=memory_pool.blocks[i];
446 while ((next != (
void *) NULL) && (SizeOfBlock(next) < size))
449 next=NextBlockInList(next);
451 PreviousBlockInList(block)=previous;
452 NextBlockInList(block)=next;
453 if (previous != (
void *) NULL)
454 NextBlockInList(previous)=block;
456 memory_pool.blocks[i]=block;
457 if (next != (
void *) NULL)
458 PreviousBlockInList(next)=block;
461static inline void RemoveFreeBlock(
void *block,
const size_t i)
467 next=NextBlockInList(block);
468 previous=PreviousBlockInList(block);
469 if (previous == (
void *) NULL)
470 memory_pool.blocks[i]=next;
472 NextBlockInList(previous)=next;
473 if (next != (
void *) NULL)
474 PreviousBlockInList(next)=previous;
477static void *AcquireBlock(
size_t size)
488 size=(size_t) (size+
sizeof(
size_t)+6*
sizeof(
size_t)-1) & -(4U*
sizeof(size_t));
489 i=AllocationPolicy(size);
490 block=memory_pool.blocks[i];
491 while ((block != (
void *) NULL) && (SizeOfBlock(block) < size))
492 block=NextBlockInList(block);
493 if (block == (
void *) NULL)
496 while (memory_pool.blocks[i] == (
void *) NULL)
498 block=memory_pool.blocks[i];
500 return((
void *) NULL);
502 assert((*BlockHeader(NextBlock(block)) & PreviousBlockBit) == 0);
503 assert(SizeOfBlock(block) >= size);
504 RemoveFreeBlock(block,AllocationPolicy(SizeOfBlock(block)));
505 if (SizeOfBlock(block) > size)
516 next=(
char *) block+size;
517 blocksize=SizeOfBlock(block)-size;
518 *BlockHeader(next)=blocksize;
519 *BlockFooter(next,blocksize)=blocksize;
520 InsertFreeBlock(next,AllocationPolicy(blocksize));
521 *BlockHeader(block)=size | (*BlockHeader(block) & ~SizeMask);
523 assert(size == SizeOfBlock(block));
524 *BlockHeader(NextBlock(block))|=PreviousBlockBit;
525 memory_pool.allocation+=size;
553MagickExport
void *AcquireMagickMemory(
const size_t size)
558#if !defined(MAGICKCORE_ANONYMOUS_MEMORY_SUPPORT)
559 memory=memory_methods.acquire_memory_handler(size == 0 ? 1UL : size);
562 ActivateSemaphoreInfo(&memory_semaphore);
565 LockSemaphoreInfo(memory_semaphore);
571 assert(2*
sizeof(
size_t) > (
size_t) (~SizeMask));
572 (void) memset(&memory_pool,0,
sizeof(memory_pool));
573 memory_pool.allocation=SegmentSize;
574 memory_pool.blocks[MaxBlocks]=(
void *) (-1);
575 for (i=0; i < MaxSegments; i++)
578 memory_pool.segment_pool[i].previous=
579 (&memory_pool.segment_pool[i-1]);
580 if (i != (MaxSegments-1))
581 memory_pool.segment_pool[i].next=(&memory_pool.segment_pool[i+1]);
583 free_segments=(&memory_pool.segment_pool[0]);
585 UnlockSemaphoreInfo(memory_semaphore);
587 LockSemaphoreInfo(memory_semaphore);
588 memory=AcquireBlock(size == 0 ? 1UL : size);
589 if (memory == (
void *) NULL)
591 if (ExpandHeap(size == 0 ? 1UL : size) != MagickFalse)
592 memory=AcquireBlock(size == 0 ? 1UL : size);
594 UnlockSemaphoreInfo(memory_semaphore);
627MagickExport
void *AcquireCriticalMemory(
const size_t size)
635 memory=AcquireMagickMemory(size);
636 if (memory == (
void *) NULL)
637 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
666MagickExport
void *AcquireQuantumMemory(
const size_t count,
const size_t quantum)
671 if ((HeapOverflowSanityCheckGetSize(count,quantum,&size) != MagickFalse) ||
672 (size > GetMaxMemoryRequest()))
677 return(AcquireMagickMemory(size));
706MagickExport
MemoryInfo *AcquireVirtualMemory(
const size_t count,
707 const size_t quantum)
718 if (HeapOverflowSanityCheckGetSize(count,quantum,&size) != MagickFalse)
723 if (virtual_anonymous_memory == 0)
725 virtual_anonymous_memory=1;
726 value=GetPolicyValue(
"system:memory-map");
727 if (LocaleCompare(value,
"anonymous") == 0)
732#if defined(MAGICKCORE_HAVE_MMAP) && defined(MAP_ANONYMOUS)
733 virtual_anonymous_memory=2;
736 value=DestroyString(value);
738 memory_info=(
MemoryInfo *) MagickAssumeAligned(AcquireAlignedMemory(1,
739 sizeof(*memory_info)));
741 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
742 (void) memset(memory_info,0,
sizeof(*memory_info));
743 memory_info->length=size;
744 memory_info->signature=MagickCoreSignature;
745 if ((virtual_anonymous_memory == 1) && (size <= GetMaxMemoryRequest()))
747 memory_info->blob=AcquireAlignedMemory(1,size);
748 if (memory_info->blob != NULL)
749 memory_info->type=AlignedVirtualMemory;
751 if (memory_info->blob == NULL)
756 memory_info->blob=NULL;
757 if (size <= GetMaxMemoryRequest())
758 memory_info->blob=MapBlob(-1,IOMode,0,size);
759 if (memory_info->blob != NULL)
760 memory_info->type=MapVirtualMemory;
769 file=AcquireUniqueFileResource(memory_info->filename);
775 offset=(MagickOffsetType) lseek(file,(off_t) (size-1),SEEK_SET);
776 if ((offset == (MagickOffsetType) (size-1)) &&
777 (write(file,
"",1) == 1))
779#if !defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
780 memory_info->blob=MapBlob(file,IOMode,0,size);
782 if (posix_fallocate(file,0,(MagickOffsetType) size) == 0)
783 memory_info->blob=MapBlob(file,IOMode,0,size);
785 if (memory_info->blob != NULL)
786 memory_info->type=MapVirtualMemory;
789 (void) RelinquishUniqueFileResource(
790 memory_info->filename);
791 *memory_info->filename=
'\0';
798 if (memory_info->blob == NULL)
800 memory_info->blob=AcquireQuantumMemory(1,size);
801 if (memory_info->blob != NULL)
802 memory_info->type=UnalignedVirtualMemory;
804 if (memory_info->blob == NULL)
805 memory_info=RelinquishVirtualMemory(memory_info);
838MagickExport
void *CopyMagickMemory(
void *magick_restrict destination,
839 const void *magick_restrict source,
const size_t size)
847 assert(destination != (
void *) NULL);
848 assert(source != (
const void *) NULL);
849 p=(
const unsigned char *) source;
850 q=(
unsigned char *) destination;
851 if (((q+size) < p) || (q > (p+size)))
854 default:
return(memcpy(destination,source,size));
855 case 8: *q++=(*p++); magick_fallthrough;
856 case 7: *q++=(*p++); magick_fallthrough;
857 case 6: *q++=(*p++); magick_fallthrough;
858 case 5: *q++=(*p++); magick_fallthrough;
859 case 4: *q++=(*p++); magick_fallthrough;
860 case 3: *q++=(*p++); magick_fallthrough;
861 case 2: *q++=(*p++); magick_fallthrough;
862 case 1: *q++=(*p++); magick_fallthrough;
863 case 0:
return(destination);
865 return(memmove(destination,source,size));
886MagickExport
void DestroyMagickMemory(
void)
888#if defined(MAGICKCORE_ANONYMOUS_MEMORY_SUPPORT)
893 ActivateSemaphoreInfo(&memory_semaphore);
894 LockSemaphoreInfo(memory_semaphore);
895 for (i=0; i < (ssize_t) memory_pool.number_segments; i++)
896 if (memory_pool.segments[i]->mapped == MagickFalse)
897 memory_methods.destroy_memory_handler(
898 memory_pool.segments[i]->allocation);
900 (
void) UnmapBlob(memory_pool.segments[i]->allocation,
901 memory_pool.segments[i]->length);
903 (void) memset(&memory_pool,0,
sizeof(memory_pool));
904 UnlockSemaphoreInfo(memory_semaphore);
905 RelinquishSemaphoreInfo(&memory_semaphore);
909#if defined(MAGICKCORE_ANONYMOUS_MEMORY_SUPPORT)
933static MagickBooleanType ExpandHeap(
size_t size)
953 blocksize=((size+12*
sizeof(size_t))+SegmentSize-1) & -SegmentSize;
954 assert(memory_pool.number_segments < MaxSegments);
955 segment=MapBlob(-1,IOMode,0,blocksize);
956 mapped=segment != (
void *) NULL ? MagickTrue : MagickFalse;
957 if (segment == (
void *) NULL)
958 segment=(
void *) memory_methods.acquire_memory_handler(blocksize);
959 if (segment == (
void *) NULL)
962 free_segments=segment_info->next;
963 segment_info->mapped=mapped;
964 segment_info->length=blocksize;
965 segment_info->allocation=segment;
966 segment_info->bound=(
char *) segment+blocksize;
967 i=(ssize_t) memory_pool.number_segments-1;
968 for ( ; (i >= 0) && (memory_pool.segments[i]->allocation > segment); i--)
969 memory_pool.segments[i+1]=memory_pool.segments[i];
970 memory_pool.segments[i+1]=segment_info;
971 memory_pool.number_segments++;
972 size=blocksize-12*
sizeof(size_t);
973 block=(
char *) segment_info->allocation+4*
sizeof(
size_t);
974 *BlockHeader(block)=size | PreviousBlockBit;
975 *BlockFooter(block,size)=size;
976 InsertFreeBlock(block,AllocationPolicy(size));
977 block=NextBlock(block);
978 assert(block < segment_info->bound);
979 *BlockHeader(block)=2*
sizeof(size_t);
980 *BlockHeader(NextBlock(block))=PreviousBlockBit;
1014MagickExport
void GetMagickMemoryMethods(
1015 AcquireMemoryHandler *acquire_memory_handler,
1016 ResizeMemoryHandler *resize_memory_handler,
1017 DestroyMemoryHandler *destroy_memory_handler)
1019 assert(acquire_memory_handler != (AcquireMemoryHandler *) NULL);
1020 assert(resize_memory_handler != (ResizeMemoryHandler *) NULL);
1021 assert(destroy_memory_handler != (DestroyMemoryHandler *) NULL);
1022 *acquire_memory_handler=memory_methods.acquire_memory_handler;
1023 *resize_memory_handler=memory_methods.resize_memory_handler;
1024 *destroy_memory_handler=memory_methods.destroy_memory_handler;
1045MagickExport
size_t GetMaxMemoryRequest(
void)
1047#define MinMemoryRequest "16MiB"
1049 if (max_memory_request == 0)
1054 max_memory_request=(size_t) MAGICK_SSIZE_MAX;
1055 value=GetPolicyValue(
"system:max-memory-request");
1056 if (value != (
char *) NULL)
1061 max_memory_request=MagickMax(StringToSizeType(value,100.0),
1062 StringToSizeType(MinMemoryRequest,100.0));
1063 value=DestroyString(value);
1066 return(MagickMin(max_memory_request,(
size_t) MAGICK_SSIZE_MAX));
1086MagickExport
size_t GetMaxProfileSize(
void)
1088 if (max_profile_size == 0)
1093 max_profile_size=(size_t) MAGICK_SSIZE_MAX;
1094 value=GetPolicyValue(
"system:max-profile-size");
1095 if (value != (
char *) NULL)
1100 max_profile_size=StringToSizeType(value,100.0);
1101 value=DestroyString(value);
1104 return(MagickMin(max_profile_size,(
size_t) MAGICK_SSIZE_MAX));
1129MagickExport
void *GetVirtualMemoryBlob(
const MemoryInfo *memory_info)
1131 assert(memory_info != (
const MemoryInfo *) NULL);
1132 assert(memory_info->signature == MagickCoreSignature);
1133 return(memory_info->blob);
1159MagickExport
void *RelinquishAlignedMemory(
void *memory)
1161 if (memory == (
void *) NULL)
1162 return((
void *) NULL);
1163 if (memory_methods.relinquish_aligned_memory_handler != (RelinquishAlignedMemoryHandler) NULL)
1165 memory_methods.relinquish_aligned_memory_handler(memory);
1168#if defined(MAGICKCORE_HAVE_ALIGNED_MALLOC) || defined(MAGICKCORE_HAVE_POSIX_MEMALIGN)
1170#elif defined(MAGICKCORE_HAVE__ALIGNED_MALLOC)
1171 _aligned_free(memory);
1173 RelinquishMagickMemory(actual_base_address(memory));
1201MagickExport
void *RelinquishMagickMemory(
void *memory)
1203 if (memory == (
void *) NULL)
1204 return((
void *) NULL);
1205#if !defined(MAGICKCORE_ANONYMOUS_MEMORY_SUPPORT)
1206 memory_methods.destroy_memory_handler(memory);
1208 LockSemaphoreInfo(memory_semaphore);
1209 assert((SizeOfBlock(memory) % (4*
sizeof(
size_t))) == 0);
1210 assert((*BlockHeader(NextBlock(memory)) & PreviousBlockBit) != 0);
1211 if ((*BlockHeader(memory) & PreviousBlockBit) == 0)
1219 previous=PreviousBlock(memory);
1220 RemoveFreeBlock(previous,AllocationPolicy(SizeOfBlock(previous)));
1221 *BlockHeader(previous)=(SizeOfBlock(previous)+SizeOfBlock(memory)) |
1222 (*BlockHeader(previous) & ~SizeMask);
1225 if ((*BlockHeader(NextBlock(NextBlock(memory))) & PreviousBlockBit) == 0)
1233 next=NextBlock(memory);
1234 RemoveFreeBlock(next,AllocationPolicy(SizeOfBlock(next)));
1235 *BlockHeader(memory)=(SizeOfBlock(memory)+SizeOfBlock(next)) |
1236 (*BlockHeader(memory) & ~SizeMask);
1238 *BlockFooter(memory,SizeOfBlock(memory))=SizeOfBlock(memory);
1239 *BlockHeader(NextBlock(memory))&=(~PreviousBlockBit);
1240 InsertFreeBlock(memory,AllocationPolicy(SizeOfBlock(memory)));
1241 UnlockSemaphoreInfo(memory_semaphore);
1243 return((
void *) NULL);
1271 assert(memory_info->signature == MagickCoreSignature);
1272 if (memory_info->blob != (
void *) NULL)
1273 switch (memory_info->type)
1275 case AlignedVirtualMemory:
1277 (void) ShredMagickMemory(memory_info->blob,memory_info->length);
1278 memory_info->blob=RelinquishAlignedMemory(memory_info->blob);
1281 case MapVirtualMemory:
1283 (void) UnmapBlob(memory_info->blob,memory_info->length);
1284 memory_info->blob=NULL;
1285 if (*memory_info->filename !=
'\0')
1286 (void) RelinquishUniqueFileResource(memory_info->filename);
1289 case UnalignedVirtualMemory:
1292 (void) ShredMagickMemory(memory_info->blob,memory_info->length);
1293 memory_info->blob=RelinquishMagickMemory(memory_info->blob);
1297 memory_info->signature=(~MagickCoreSignature);
1298 memory_info=(
MemoryInfo *) RelinquishAlignedMemory(memory_info);
1299 return(memory_info);
1331MagickExport
void *ResetMagickMemory(
void *memory,
int c,
const size_t size)
1333 volatile unsigned char
1334 *p = (
volatile unsigned char *) memory;
1339 assert(memory != (
void *) NULL);
1341 *p++=(
unsigned char) c;
1363MagickPrivate
void ResetMaxMemoryRequest(
void)
1365 max_memory_request=0;
1386MagickPrivate
void ResetVirtualAnonymousMemory(
void)
1388 virtual_anonymous_memory=0;
1418#if defined(MAGICKCORE_ANONYMOUS_MEMORY_SUPPORT)
1419static inline void *ResizeBlock(
void *block,
size_t size)
1424 if (block == (
void *) NULL)
1425 return(AcquireBlock(size));
1426 memory=AcquireBlock(size);
1427 if (memory == (
void *) NULL)
1428 return((
void *) NULL);
1429 if (size <= (SizeOfBlock(block)-
sizeof(
size_t)))
1430 (void) memcpy(memory,block,size);
1432 (
void) memcpy(memory,block,SizeOfBlock(block)-
sizeof(
size_t));
1433 memory_pool.allocation+=size;
1438MagickExport
void *ResizeMagickMemory(
void *memory,
const size_t size)
1443 if (memory == (
void *) NULL)
1444 return(AcquireMagickMemory(size));
1445#if !defined(MAGICKCORE_ANONYMOUS_MEMORY_SUPPORT)
1446 block=memory_methods.resize_memory_handler(memory,size == 0 ? 1UL : size);
1447 if (block == (
void *) NULL)
1448 memory=RelinquishMagickMemory(memory);
1450 LockSemaphoreInfo(memory_semaphore);
1451 block=ResizeBlock(memory,size == 0 ? 1UL : size);
1452 if (block == (
void *) NULL)
1454 if (ExpandHeap(size == 0 ? 1UL : size) == MagickFalse)
1456 UnlockSemaphoreInfo(memory_semaphore);
1457 memory=RelinquishMagickMemory(memory);
1458 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1460 block=ResizeBlock(memory,size == 0 ? 1UL : size);
1461 assert(block != (
void *) NULL);
1463 UnlockSemaphoreInfo(memory_semaphore);
1464 memory=RelinquishMagickMemory(memory);
1498MagickExport
void *ResizeQuantumMemory(
void *memory,
const size_t count,
1499 const size_t quantum)
1504 if ((HeapOverflowSanityCheckGetSize(count,quantum,&size) != MagickFalse) ||
1505 (size > GetMaxMemoryRequest()))
1508 memory=RelinquishMagickMemory(memory);
1511 return(ResizeMagickMemory(memory,size));
1541MagickExport
void SetMagickAlignedMemoryMethods(
1542 AcquireAlignedMemoryHandler acquire_aligned_memory_handler,
1543 RelinquishAlignedMemoryHandler relinquish_aligned_memory_handler)
1545 memory_methods.acquire_aligned_memory_handler=acquire_aligned_memory_handler;
1546 memory_methods.relinquish_aligned_memory_handler=
1547 relinquish_aligned_memory_handler;
1580MagickExport
void SetMagickMemoryMethods(
1581 AcquireMemoryHandler acquire_memory_handler,
1582 ResizeMemoryHandler resize_memory_handler,
1583 DestroyMemoryHandler destroy_memory_handler)
1588 if (acquire_memory_handler != (AcquireMemoryHandler) NULL)
1589 memory_methods.acquire_memory_handler=acquire_memory_handler;
1590 if (resize_memory_handler != (ResizeMemoryHandler) NULL)
1591 memory_methods.resize_memory_handler=resize_memory_handler;
1592 if (destroy_memory_handler != (DestroyMemoryHandler) NULL)
1593 memory_methods.destroy_memory_handler=destroy_memory_handler;
1618MagickPrivate
void SetMaxMemoryRequest(
const MagickSizeType limit)
1620 max_memory_request=MagickMin(limit,GetMaxMemoryRequest());
1645MagickPrivate
void SetMaxProfileSize(
const MagickSizeType limit)
1647 max_profile_size=MagickMin(limit,GetMaxProfileSize());
1676MagickPrivate MagickBooleanType ShredMagickMemory(
void *memory,
1677 const size_t length)
1694 if ((memory == NULL) || (length == 0))
1695 return(MagickFalse);
1702 property=GetEnvironmentValue(
"MAGICK_SHRED_PASSES");
1703 if (property != (
char *) NULL)
1705 passes=(ssize_t) StringToInteger(property);
1706 property=DestroyString(property);
1708 property=GetPolicyValue(
"system:shred");
1709 if (property != (
char *) NULL)
1711 passes=(ssize_t) StringToInteger(property);
1712 property=DestroyString(property);
1720 quantum=(size_t) MagickMin(length,MagickMinBufferExtent);
1721 random_info=AcquireRandomInfo();
1722 key=GetRandomKey(random_info,quantum);
1723 for (i=0; i < passes; i++)
1729 *p = (
unsigned char *) memory;
1731 for (j=0; j < length; j+=quantum)
1734 SetRandomKey(random_info,quantum,GetStringInfoDatum(key));
1735 (void) memcpy(p,GetStringInfoDatum(key),(
size_t)
1736 MagickMin(quantum,length-j));
1742 key=DestroyStringInfo(key);
1743 random_info=DestroyRandomInfo(random_info);
1744 return(i < passes ? MagickFalse : MagickTrue);