]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/memory.c
(no commit message)
[imagemagick] / MagickCore / memory.c
index 126b9087f9f144c714b87d1f944b7bfcbfc4cddf..1edb4ad96328af6cfe9e3faf2505183064983c63 100644 (file)
 %                     MagickCore Memory Allocation Methods                    %
 %                                                                             %
 %                              Software Design                                %
-%                                John Cristy                                  %
+%                                   Cristy                                    %
 %                                 July 1998                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2014 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  %
 /*
   Typedef declarations.
 */
+typedef enum
+{
+  UndefinedVirtualMemory,
+  AlignedVirtualMemory,
+  MapVirtualMemory,
+  UnalignedVirtualMemory
+} VirtualMemoryType;
+
 typedef struct _DataSegmentInfo
 {
   void
@@ -123,8 +131,8 @@ struct _MemoryInfo
   char
     filename[MaxTextExtent];
 
-  MagickBooleanType
-    mapped;
+  VirtualMemoryType
+    type;
 
   size_t
     length;
@@ -155,14 +163,34 @@ typedef struct _MemoryPool
 /*
   Global declarations.
 */
+#if defined _MSC_VER
+static void* MSCMalloc(size_t size)
+{
+  return malloc(size);
+}
+static void* MSCRealloc(void* ptr, size_t size)
+{
+  return realloc(ptr, size);
+}
+static void MSCFree(void* ptr)
+{
+  free(ptr);
+}
+#endif
+
 static MagickMemoryMethods
   memory_methods =
   {
+#if defined _MSC_VER
+    (AcquireMemoryHandler) MSCMalloc,
+    (ResizeMemoryHandler) MSCRealloc,
+    (DestroyMemoryHandler) MSCFree
+#else
     (AcquireMemoryHandler) malloc,
     (ResizeMemoryHandler) realloc,
     (DestroyMemoryHandler) free
+#endif
   };
-
 #if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
 static MemoryPool
   memory_pool;
@@ -435,7 +463,7 @@ MagickExport void *AcquireMagickMemory(const size_t size)
   memory=memory_methods.acquire_memory_handler(size == 0 ? 1UL : size);
 #else
   if (memory_semaphore == (SemaphoreInfo *) NULL)
-    AcquireSemaphoreInfo(&memory_semaphore);
+    ActivateSemaphoreInfo(&memory_semaphore);
   if (free_segments == (DataSegmentInfo *) NULL)
     {
       LockSemaphoreInfo(memory_semaphore);
@@ -560,8 +588,10 @@ MagickExport MemoryInfo *AcquireVirtualMemory(const size_t count,
   memory_info->signature=MagickSignature;
   if (AcquireMagickResource(MemoryResource,length) != MagickFalse)
     {
-      memory_info->blob=AcquireMagickMemory(length);
-      if (memory_info->blob == NULL)
+      memory_info->blob=AcquireAlignedMemory(1,length);
+      if (memory_info->blob != NULL)
+        memory_info->type=AlignedVirtualMemory;
+      else
         RelinquishMagickResource(MemoryResource,length);
     }
   if ((memory_info->blob == NULL) &&
@@ -570,9 +600,10 @@ MagickExport MemoryInfo *AcquireVirtualMemory(const size_t count,
       /*
         Heap memory failed, try anonymous memory mapping.
       */
-      memory_info->mapped=MagickTrue;
       memory_info->blob=MapBlob(-1,IOMode,0,length);
-      if (memory_info->blob == NULL)
+      if (memory_info->blob != NULL)
+        memory_info->type=MapVirtualMemory;
+      else
         RelinquishMagickResource(MapResource,length);
     }
   if (memory_info->blob == NULL)
@@ -584,25 +615,28 @@ MagickExport MemoryInfo *AcquireVirtualMemory(const size_t count,
         Anonymous memory mapping failed, try file-backed memory mapping.
       */
       file=AcquireUniqueFileResource(memory_info->filename);
-      file=open_utf8(memory_info->filename,O_RDWR | O_CREAT | O_BINARY | O_EXCL,
-        S_MODE);
-      if (file == -1)
-        file=open_utf8(memory_info->filename,O_RDWR | O_BINARY,S_MODE);
       if (file != -1)
         {
           if ((lseek(file,length-1,SEEK_SET) >= 0) && (write(file,"",1) == 1))
             {
-              (void) AcquireMagickResource(MapResource,length);
-              memory_info->mapped=MagickTrue;
               memory_info->blob=MapBlob(file,IOMode,0,length);
-              if (memory_info->blob == NULL)
-                RelinquishMagickResource(MapResource,length);
+              if (memory_info->blob != NULL)
+                {
+                  memory_info->type=MapVirtualMemory;
+                  (void) AcquireMagickResource(MapResource,length);
+                }
             }
           (void) close(file);
         }
     }
   if (memory_info->blob == NULL)
-    return(RelinquishVirtualMemory(memory_info));
+    {
+      memory_info->blob=AcquireMagickMemory(length);
+      if (memory_info->blob != NULL)
+        memory_info->type=UnalignedVirtualMemory;
+    }
+  if (memory_info->blob == NULL)
+    memory_info=RelinquishVirtualMemory(memory_info);
   return(memory_info);
 }
 \f
@@ -690,7 +724,7 @@ MagickExport void DestroyMagickMemory(void)
     i;
 
   if (memory_semaphore == (SemaphoreInfo *) NULL)
-    AcquireSemaphoreInfo(&memory_semaphore);
+    ActivateSemaphoreInfo(&memory_semaphore);
   LockSemaphoreInfo(memory_semaphore);
   UnlockSemaphoreInfo(memory_semaphore);
   for (i=0; i < (ssize_t) memory_pool.number_segments; i++)
@@ -702,7 +736,7 @@ MagickExport void DestroyMagickMemory(void)
         memory_pool.segments[i]->length);
   free_segments=(DataSegmentInfo *) NULL;
   (void) ResetMagickMemory(&memory_pool,0,sizeof(memory_pool));
-  DestroySemaphoreInfo(&memory_semaphore);
+  RelinquishSemaphoreInfo(&memory_semaphore);
 #endif
 }
 \f
@@ -985,20 +1019,29 @@ MagickExport MemoryInfo *RelinquishVirtualMemory(MemoryInfo *memory_info)
   assert(memory_info != (MemoryInfo *) NULL);
   assert(memory_info->signature == MagickSignature);
   if (memory_info->blob != (void *) NULL)
+    switch (memory_info->type)
     {
-      if (memory_info->mapped == MagickFalse)
-        {
-          memory_info->blob=RelinquishMagickMemory(memory_info->blob);
-          RelinquishMagickResource(MemoryResource,memory_info->length);
-        }
-      else
-        {
-          (void) UnmapBlob(memory_info->blob,memory_info->length);
-          RelinquishMagickResource(MapResource,memory_info->length);
-          memory_info->blob=NULL;
-          if (*memory_info->filename != '\0')
-            (void) RelinquishUniqueFileResource(memory_info->filename);
-        }
+      case AlignedVirtualMemory:
+      {
+        memory_info->blob=RelinquishAlignedMemory(memory_info->blob);
+        RelinquishMagickResource(MemoryResource,memory_info->length);
+        break;
+      }
+      case MapVirtualMemory:
+      {
+        (void) UnmapBlob(memory_info->blob,memory_info->length);
+        memory_info->blob=NULL;
+        RelinquishMagickResource(MapResource,memory_info->length);
+        if (*memory_info->filename != '\0')
+          (void) RelinquishUniqueFileResource(memory_info->filename);
+        break;
+      }
+      case UnalignedVirtualMemory:
+      default:
+      {
+        memory_info->blob=RelinquishMagickMemory(memory_info->blob);
+        break;
+      }
     }
   memory_info->signature=(~MagickSignature);
   memory_info=(MemoryInfo *) RelinquishAlignedMemory(memory_info);