% July 1998 %
% %
% %
-% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
+% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
% dedicated to making software imaging solutions freely available. %
% %
% You may not use this file except in compliance with the License. You may %
% written by Yoo C. Chung.
%
% By default, ANSI memory methods are called (e.g. malloc). Use the
-% custom memory allocator by defining MAGICKCORE_EMBEDDABLE_SUPPORT
+% custom memory allocator by defining MAGICKCORE_ZERO_CONFIGURATION_SUPPORT
% to allocate memory with private anonymous mapping rather than from the
% heap.
%
#include "MagickCore/exception.h"
#include "MagickCore/exception-private.h"
#include "MagickCore/memory_.h"
+#include "MagickCore/memory-private.h"
#include "MagickCore/semaphore.h"
#include "MagickCore/string_.h"
\f
#define BlockHeader(block) ((size_t *) (block)-1)
#define BlockSize 4096
#define BlockThreshold 1024
-#define AlignedSize (16*sizeof(void *))
#define MaxBlockExponent 16
#define MaxBlocks ((BlockThreshold/(4*sizeof(size_t)))+MaxBlockExponent+1)
#define MaxSegments 1024
(DestroyMemoryHandler) free
};
-#if defined(MAGICKCORE_EMBEDDABLE_SUPPORT)
+#if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
static MemoryInfo
memory_info;
% o quantum: the number of bytes in each quantum.
%
*/
-
-static inline size_t MagickMax(const size_t x,const size_t y)
-{
- if (x > y)
- return(x);
- return(y);
-}
-
MagickExport void *AcquireAlignedMemory(const size_t count,const size_t quantum)
{
+#define AlignedExtent(size,alignment) \
+ (((size)+((alignment)-1)) & ~((alignment)-1))
+
size_t
+ alignment,
+ extent,
size;
+ void
+ *memory;
+
size=count*quantum;
if ((count == 0) || (quantum != (size/count)))
{
errno=ENOMEM;
return((void *) NULL);
}
+ memory=NULL;
+ alignment=CACHE_LINE_SIZE;
+ extent=AlignedExtent(size,alignment);
+ if ((size == 0) || (alignment < sizeof(void *)) || (extent < size))
+ return((void *) NULL);
#if defined(MAGICKCORE_HAVE_POSIX_MEMALIGN)
+ if (posix_memalign(&memory,alignment,extent) != 0)
+ memory=NULL;
+#elif defined(MAGICKCORE_HAVE__ALIGNED_MALLOC)
+ memory=_aligned_malloc(extent,alignment);
+#else
{
void
- *memory;
+ *p;
- if (posix_memalign(&memory,AlignedSize,MagickMax(size,AlignedSize)) == 0)
- return(memory);
+ extent=(size+alignment-1)+sizeof(void *);
+ if (extent > size)
+ {
+ p=malloc(extent);
+ if (p != NULL)
+ {
+ memory=(void *) AlignedExtent((size_t) p+sizeof(void *),alignment);
+ *((void **) memory-1)=p;
+ }
+ }
}
#endif
- return(malloc(MagickMax(size,AlignedSize)));
+ return(memory);
}
\f
-#if defined(MAGICKCORE_EMBEDDABLE_SUPPORT)
+#if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
register void
*memory;
-#if !defined(MAGICKCORE_EMBEDDABLE_SUPPORT)
+#if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
memory=memory_methods.acquire_memory_handler(size == 0 ? 1UL : size);
#else
if (memory_semaphore == (SemaphoreInfo *) NULL)
*/
MagickExport void DestroyMagickMemory(void)
{
-#if defined(MAGICKCORE_EMBEDDABLE_SUPPORT)
+#if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
register ssize_t
i;
#endif
}
\f
-#if defined(MAGICKCORE_EMBEDDABLE_SUPPORT)
+#if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
{
if (memory == (void *) NULL)
return((void *) NULL);
+#if defined(MAGICKCORE_HAVE_POSIX_MEMALIGN)
free(memory);
- return((void *) NULL);
+#elif defined(MAGICKCORE_HAVE__ALIGNED_MALLOC)
+ _aligned_free(memory);
+#else
+ free(*((void **) memory-1));
+#endif
+ return(NULL);
}
\f
/*
{
if (memory == (void *) NULL)
return((void *) NULL);
-#if !defined(MAGICKCORE_EMBEDDABLE_SUPPORT)
+#if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
memory_methods.destroy_memory_handler(memory);
#else
+ LockSemaphoreInfo(memory_semaphore);
assert((SizeOfBlock(memory) % (4*sizeof(size_t))) == 0);
assert((*BlockHeader(NextBlock(memory)) & PreviousBlockBit) != 0);
- LockSemaphoreInfo(memory_semaphore);
if ((*BlockHeader(memory) & PreviousBlockBit) == 0)
{
void
%
*/
-#if defined(MAGICKCORE_EMBEDDABLE_SUPPORT)
+#if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
static inline void *ResizeBlock(void *block,size_t size)
{
register void
if (memory == (void *) NULL)
return(AcquireMagickMemory(size));
-#if !defined(MAGICKCORE_EMBEDDABLE_SUPPORT)
+#if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
block=memory_methods.resize_memory_handler(memory,size == 0 ? 1UL : size);
if (block == (void *) NULL)
memory=RelinquishMagickMemory(memory);