]> granicus.if.org Git - imagemagick/commitdiff
(no commit message)
authorcristy <urban-warrior@git.imagemagick.org>
Sat, 28 Jul 2012 21:01:00 +0000 (21:01 +0000)
committercristy <urban-warrior@git.imagemagick.org>
Sat, 28 Jul 2012 21:01:00 +0000 (21:01 +0000)
MagickCore/semaphore.c

index 3b3b4befafaa33f940c394f66ccea16adfae056c..755f0c799f373f5f01eb97e7fff89990ef360db7 100644 (file)
@@ -123,18 +123,61 @@ MagickExport void AcquireSemaphoreInfo(SemaphoreInfo **semaphore_info)
 %
 */
 
-static void *AcquireSemaphoreMemory(const size_t size)
+static void *AcquireSemaphoreMemory(const size_t count,const size_t quantum)
 {
+  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=(size+alignment-1)+sizeof(void *);
+  if ((size == 0) || (alignment < sizeof(void *)) || (extent <= size))
+    return((void *) NULL);
 #if defined(MAGICKCORE_HAVE_POSIX_MEMALIGN)
+  if (posix_memalign(&memory,alignment,size) != 0)
+    memory=NULL;
+#elif defined(MAGICKCORE_HAVE__ALIGNED_MALLOC)
+  memory=_aligned_malloc(size,alignment);
+#else
   {
     void
-      *memory;
+      *p;
 
-    if (posix_memalign(&memory,CACHE_LINE_SIZE,CacheAlign(size)) == 0)
-      return(memory);
+    p=malloc(extent);
+    if (p != NULL)
+      {
+        memory=(void *) (((size_t) p+sizeof(void *)+alignment-1) &
+          ~(alignment-1));
+        *((void **) memory-1)=p;
+      }
   }
 #endif
-  return(malloc(CacheAlign(size)));
+  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)
@@ -145,7 +188,7 @@ MagickExport SemaphoreInfo *AllocateSemaphoreInfo(void)
   /*
     Allocate semaphore.
   */
-  semaphore_info=(SemaphoreInfo *) AcquireSemaphoreMemory(
+  semaphore_info=(SemaphoreInfo *) AcquireSemaphoreMemory(1,
     sizeof(*semaphore_info));
   if (semaphore_info == (SemaphoreInfo *) NULL)
     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
@@ -258,8 +301,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=RelinquishSemaphoreMemory(*semaphore_info);
   UnlockMagickMutex();
 }
 \f