]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/semaphore.c
(no commit message)
[imagemagick] / MagickCore / semaphore.c
index b39e56b5e4e1a22e8da251644fc6a657a744e8a3..25cfcb260f9652af6b4608b22a112da78869fa40 100644 (file)
@@ -18,7 +18,7 @@
 %                                 June 2000                                   %
 %                                                                             %
 %                                                                             %
-%  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  %
@@ -45,6 +45,7 @@
 #include "MagickCore/exception.h"
 #include "MagickCore/exception-private.h"
 #include "MagickCore/memory_.h"
+#include "MagickCore/memory-private.h"
 #include "MagickCore/semaphore.h"
 #include "MagickCore/semaphore-private.h"
 #include "MagickCore/string_.h"
@@ -121,6 +122,70 @@ MagickExport void AcquireSemaphoreInfo(SemaphoreInfo **semaphore_info)
 %      SemaphoreInfo *AllocateSemaphoreInfo(void)
 %
 */
+
+static void *AcquireSemaphoreMemory(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
+      *p;
+
+    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(memory);
+}
+
+static void *RelinquishSemaphoreMemory(void *memory)
+{
+  if (memory == (void *) NULL)
+    return((void *) NULL);
+#if defined(MAGICKCORE_HAVE_POSIX_MEMALIGN)
+  free(memory);
+#elif defined(MAGICKCORE_HAVE__ALIGNED_MALLOC)
+  _aligned_free(memory);
+#else
+  free(*((void **) memory-1));
+#endif
+  return(NULL);
+}
+
 MagickExport SemaphoreInfo *AllocateSemaphoreInfo(void)
 {
   SemaphoreInfo
@@ -129,7 +194,8 @@ MagickExport SemaphoreInfo *AllocateSemaphoreInfo(void)
   /*
     Allocate semaphore.
   */
-  semaphore_info=(SemaphoreInfo *) malloc(sizeof(SemaphoreInfo));
+  semaphore_info=(SemaphoreInfo *) AcquireSemaphoreMemory(1,
+    sizeof(*semaphore_info));
   if (semaphore_info == (SemaphoreInfo *) NULL)
     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
   (void) ResetMagickMemory(semaphore_info,0,sizeof(SemaphoreInfo));
@@ -241,8 +307,7 @@ MagickExport void DestroySemaphoreInfo(SemaphoreInfo **semaphore_info)
   DeleteCriticalSection(&(*semaphore_info)->mutex);
 #endif
   (*semaphore_info)->signature=(~MagickSignature);
-  free(*semaphore_info);
-  *semaphore_info=(SemaphoreInfo *) NULL;
+  *semaphore_info=(SemaphoreInfo *) RelinquishSemaphoreMemory(*semaphore_info);
   UnlockMagickMutex();
 }
 \f