remove `declare' macro
[bpt/emacs.git] / src / gmalloc.c
index f8d0cfd..ab1dfd0 100644 (file)
@@ -38,6 +38,10 @@ License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 #include <w32heap.h>   /* for sbrk */
 #endif
 
+#ifdef emacs
+extern void emacs_abort (void);
+#endif
+
 #ifdef __cplusplus
 extern "C"
 {
@@ -47,12 +51,12 @@ extern "C"
 
 
 /* Allocate SIZE bytes of memory.  */
-extern void *malloc (size_t size);
+extern void *malloc (size_t size) ATTRIBUTE_MALLOC_SIZE ((1));
 /* Re-allocate the previously allocated block
    in ptr, making the new block SIZE bytes long.  */
-extern void *realloc (void *ptr, size_t size);
+extern void *realloc (void *ptr, size_t size) ATTRIBUTE_ALLOC_SIZE ((2));
 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0.  */
-extern void *calloc (size_t nmemb, size_t size);
+extern void *calloc (size_t nmemb, size_t size) ATTRIBUTE_MALLOC_SIZE ((1,2));
 /* Free a block allocated by `malloc', `realloc' or `calloc'.  */
 extern void free (void *ptr);
 
@@ -1593,23 +1597,32 @@ aligned_alloc (size_t alignment, size_t size)
 
   /* Figure out how much we will need to pad this particular block
      to achieve the required alignment.  */
-  adj = (uintptr_t) result % alignment;
+  adj = alignment - (uintptr_t) result % alignment;
+  if (adj == alignment)
+    adj = 0;
 
-  do
+  if (adj != alignment - 1)
     {
-      /* Reallocate the block with only as much excess as it needs.  */
-      free (result);
-      result = malloc (size + alignment - adj);
-      if (result == NULL)      /* Impossible unless interrupted.  */
-       return NULL;
-
-      lastadj = adj;
-      adj = (uintptr_t) result % alignment;
-      /* It's conceivable we might have been so unlucky as to get a
-        different block with weaker alignment.  If so, this block is too
-        short to contain SIZE after alignment correction.  So we must
-        try again and get another block, slightly larger.  */
-    } while (adj < lastadj);
+      do
+       {
+         /* Reallocate the block with only as much excess as it
+            needs.  */
+         free (result);
+         result = malloc (size + adj);
+         if (result == NULL)   /* Impossible unless interrupted.  */
+           return NULL;
+
+         lastadj = adj;
+         adj = alignment - (uintptr_t) result % alignment;
+         if (adj == alignment)
+           adj = 0;
+         /* It's conceivable we might have been so unlucky as to get
+            a different block with weaker alignment.  If so, this
+            block is too short to contain SIZE after alignment
+            correction.  So we must try again and get another block,
+            slightly larger.  */
+       } while (adj > lastadj);
+    }
 
   if (adj != 0)
     {
@@ -1635,7 +1648,7 @@ aligned_alloc (size_t alignment, size_t size)
       if (l != NULL)
        {
          l->exact = result;
-         result = l->aligned = (char *) result + alignment - adj;
+         result = l->aligned = (char *) result + adj;
        }
       UNLOCK_ALIGNED_BLOCKS ();
       if (l == NULL)