]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/memory.c
(no commit message)
[imagemagick] / MagickCore / memory.c
index b29ea0f3565548befa813974dcaac977e94422d2..b6d34cad9b68985574ae921c14f310fd1542245b 100644 (file)
@@ -188,25 +188,51 @@ static MagickBooleanType
 */
 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,CACHE_LINE_SIZE,CacheAlign(size)) == 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(CacheAlign(size)));
+  return(memory);
 }
 \f
 #if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
@@ -711,8 +737,14 @@ MagickExport void *RelinquishAlignedMemory(void *memory)
 {
   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
 /*