Merge glibc-ports into ports/ directory.
[platform/upstream/glibc.git] / hurd / hurdmalloc.c
index 1de887b..12da1f2 100644 (file)
@@ -1,90 +1,68 @@
 #include <stdlib.h>
 #include <string.h>
 
-#define bcopy(s,d,n)   memcpy ((d), (s), (n)) /* No overlap handling.  */
-
 #include "hurdmalloc.h"                /* XXX see that file */
 
 #include <mach.h>
 #define vm_allocate __vm_allocate
 #define vm_page_size __vm_page_size
 
-/* 
+/*
  * Mach Operating System
  * Copyright (c) 1991,1990,1989 Carnegie Mellon University
  * All Rights Reserved.
- * 
+ *
  * Permission to use, copy, modify and distribute this software and its
  * documentation is hereby granted, provided that both the copyright
  * notice and this permission notice appear in all copies of the
  * software, derivative works or modified versions, and any portions
  * thereof, and that both notices appear in supporting documentation.
- * 
+ *
  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
- * 
+ *
  * Carnegie Mellon requests users of this software to return to
- * 
+ *
  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  *  School of Computer Science
  *  Carnegie Mellon University
  *  Pittsburgh PA 15213-3890
- * 
+ *
  * any improvements or extensions that they make and grant Carnegie Mellon
  * the rights to redistribute these changes.
  */
 /*
- * HISTORY
- * $Log$
- * Revision 1.10  1995/02/13 22:04:34  roland
- * (malloc_init): Add self reference to avoid not only the `defined but not
- * used' warning, but also to avoid GCC optimizing out the entire function
- * (!).
- *
- * Revision 1.9  1995/02/13  16:36:08  roland
- * Include string.h; #define bcopy using memcpy.
- *
- * Revision 1.8  1995/02/03  01:54:21  roland
- * Remove bogus bcopy decl.
- *
- * Revision 1.7  1995/01/26  04:22:02  roland
- * Don't include gnu-stabs.h.
- *
- * Revision 1.6  1994/12/07  19:41:26  roland
- * (vm_allocate, vm_page_size): #define these to __ names at top.
- *
- * Revision 1.5  1994/06/04  01:48:44  roland
- * entered into RCS
+ * (pre-GNU) HISTORY
  *
  * Revision 2.7  91/05/14  17:57:34  mrt
  *     Correcting copyright
- * 
+ *
  * Revision 2.6  91/02/14  14:20:26  mrt
  *     Added new Mach copyright
  *     [91/02/13  12:41:21  mrt]
- * 
+ *
  * Revision 2.5  90/11/05  14:37:33  rpd
  *     Added malloc_fork* code.
  *     [90/11/02            rwd]
- * 
+ *
  *     Add spin_lock_t.
  *     [90/10/31            rwd]
- * 
+ *
  * Revision 2.4  90/08/07  14:31:28  rpd
  *     Removed RCS keyword nonsense.
- * 
+ *
  * Revision 2.3  90/06/02  15:14:00  rpd
  *     Converted to new IPC.
  *     [90/03/20  20:56:57  rpd]
- * 
+ *
  * Revision 2.2  89/12/08  19:53:59  rwd
  *     Removed conditionals.
  *     [89/10/23            rwd]
- * 
+ *
  * Revision 2.1  89/08/03  17:09:46  rwd
  * Created.
- * 
+ *
  *
  * 13-Sep-88  Eric Cooper (ecc) at Carnegie Mellon University
  *     Changed realloc() to copy min(old size, new size) bytes.
  */
 
 \f
+#include <assert.h>
+
 #include <cthreads.h>
-#include "cthread_internals.h"
 
+#define MCHECK
 
 /*
  * Structure of memory block header.
  * When allocated, fl points to free list.
  * Size of header is 4 bytes, so minimum usable block size is 8 bytes.
  */
+
+#define CHECK_BUSY  0x8a3c743e
+#define CHECK_FREE  0x66688b92
+
+#ifdef MCHECK
+
+typedef struct header {
+  long check;
+  union {
+    struct header *next;
+    struct free_list *fl;
+  } u;
+} *header_t;
+
+#define HEADER_SIZE sizeof (struct header)
+#define HEADER_NEXT(h) ((h)->u.next)
+#define HEADER_FREE(h) ((h)->u.fl)
+#define HEADER_CHECK(h) ((h)->check)
+#define MIN_SIZE       16
+#define LOG2_MIN_SIZE  4
+
+#else /* ! MCHECK */
+
 typedef union header {
        union header *next;
        struct free_list *fl;
 } *header_t;
 
+#define HEADER_SIZE sizeof (union header)
+#define HEADER_NEXT(h) ((h)->next)
+#define HEADER_FREE(h) ((h)->fl)
 #define MIN_SIZE       8       /* minimum block size */
+#define LOG2_MIN_SIZE  3
+
+#endif /* MCHECK */
 
 typedef struct free_list {
        spin_lock_t lock;       /* spin lock for mutual exclusion */
        header_t head;          /* head of free list for this size */
 #ifdef DEBUG
        int in_use;             /* # mallocs - # frees */
-#endif DEBUG
+#endif /* DEBUG */
 } *free_list_t;
 
 /*
- * Free list with index i contains blocks of size 2^(i+3) including header.
- * Smallest block size is 8, with 4 bytes available to user.
- * Size argument to malloc is a signed integer for sanity checking,
- * so largest block size is 2^31.
+ * Free list with index i contains blocks of size 2 ^ (i + LOG2_MIN_SIZE)
+ * including header.  Smallest block size is MIN_SIZE, with MIN_SIZE -
+ * HEADER_SIZE bytes available to user.  Size argument to malloc is a signed
+ * integer for sanity checking, so largest block size is 2^31.
  */
 #define NBUCKETS       29
 
@@ -160,9 +169,7 @@ malloc_init (void)
 }
 
 static void
-more_memory(size, fl)
-       int size;
-       register free_list_t fl;
+more_memory(int size, free_list_t fl)
 {
        register int amount;
        register int n;
@@ -173,18 +180,23 @@ more_memory(size, fl)
        if (size <= vm_page_size) {
                amount = vm_page_size;
                n = vm_page_size / size;
-               /*
-                * We lose vm_page_size - n*size bytes here.  */
+               /* We lose vm_page_size - n*size bytes here.  */
        } else {
                amount = size;
                n = 1;
        }
-       MACH_CALL(vm_allocate(mach_task_self(), &where, (vm_size_t) amount, TRUE), r);
+
+       r = vm_allocate(mach_task_self(), &where, (vm_size_t) amount, TRUE);
+       assert_perror (r);
+
        h = (header_t) where;
        do {
-         h->next = fl->head;
-         fl->head = h;
-         h = (header_t) ((char *) h + size);
+               HEADER_NEXT (h) = fl->head;
+#ifdef MCHECK
+               HEADER_CHECK (h) = CHECK_FREE;
+#endif
+               fl->head = h;
+               h = (header_t) ((char *) h + size);
        } while (--n != 0);
 }
 
@@ -199,7 +211,7 @@ malloc(size)
 
        if ((int) size < 0)             /* sanity check */
                return 0;
-       size += sizeof(union header);
+       size += HEADER_SIZE;
        /*
         * Find smallest power-of-two block size
         * big enough to hold requested size plus header.
@@ -232,21 +244,27 @@ malloc(size)
        /*
         * Pop block from free list.
         */
-       fl->head = h->next;
+       fl->head = HEADER_NEXT (h);
+
+#ifdef MCHECK
+       assert (HEADER_CHECK (h) == CHECK_FREE);
+       HEADER_CHECK (h) = CHECK_BUSY;
+#endif
+
 #ifdef DEBUG
        fl->in_use += 1;
-#endif DEBUG
+#endif /* DEBUG */
        spin_unlock(&fl->lock);
        /*
         * Store free list pointer in block header
         * so we can figure out where it goes
         * at free() time.
         */
-       h->fl = fl;
+       HEADER_FREE (h) = fl;
        /*
         * Return pointer past the block header.
         */
-       return ((char *) h) + sizeof(union header);
+       return ((char *) h) + HEADER_SIZE;
 }
 
 /* Declaration changed to standard one for GNU.  */
@@ -263,8 +281,13 @@ free(base)
        /*
         * Find free list for block.
         */
-       h = (header_t) (base - sizeof(union header));
-       fl = h->fl;
+       h = (header_t) (base - HEADER_SIZE);
+
+#ifdef MCHECK
+       assert (HEADER_CHECK (h) == CHECK_BUSY);
+#endif
+
+       fl = HEADER_FREE (h);
        i = fl - malloc_free_list;
        /*
         * Sanity checks.
@@ -281,11 +304,14 @@ free(base)
         * Push block on free list.
         */
        spin_lock(&fl->lock);
-       h->next = fl->head;
+       HEADER_NEXT (h) = fl->head;
+#ifdef MCHECK
+       HEADER_CHECK (h) = CHECK_FREE;
+#endif
        fl->head = h;
 #ifdef DEBUG
        fl->in_use -= 1;
-#endif DEBUG
+#endif /* DEBUG */
        spin_unlock(&fl->lock);
        return;
 }
@@ -308,8 +334,11 @@ realloc(old_base, new_size)
        /*
         * Find size of old block.
         */
-       h = (header_t) (old_base - sizeof(union header));
-       fl = h->fl;
+       h = (header_t) (old_base - HEADER_SIZE);
+#ifdef MCHECK
+       assert (HEADER_CHECK (h) == CHECK_BUSY);
+#endif
+       fl = HEADER_FREE (h);
        i = fl - malloc_free_list;
        /*
         * Sanity checks.
@@ -323,16 +352,29 @@ realloc(old_base, new_size)
                return 0;
        }
        /*
-        * Free list with index i contains blocks of size 2^(i+3) including header.
+        * Free list with index i contains blocks of size
+        * 2 ^ (i + * LOG2_MIN_SIZE) including header.
         */
-       old_size = (1 << (i+3)) - sizeof(union header);
+       old_size = (1 << (i + LOG2_MIN_SIZE)) - HEADER_SIZE;
+
+       if (new_size <= old_size
+           && new_size > (((old_size + HEADER_SIZE) >> 1) - HEADER_SIZE))
+         /* The new size still fits in the same block, and wouldn't fit in
+            the next smaller block!  */
+         return old_base;
+
        /*
         * Allocate new block, copy old bytes, and free old block.
         */
        new_base = malloc(new_size);
-       if (new_base != 0)
-               bcopy(old_base, new_base, (int) (old_size < new_size ? old_size : new_size));
-       free(old_base);
+       if (new_base)
+         memcpy (new_base, old_base,
+                 (int) (old_size < new_size ? old_size : new_size));
+
+       if (new_base || new_size == 0)
+         /* Free OLD_BASE, but only if the malloc didn't fail.  */
+         free (old_base);
+
        return new_base;
 }
 
@@ -354,7 +396,7 @@ print_malloc_free_list()
                spin_lock(&fl->lock);
                if (fl->in_use != 0 || fl->head != 0) {
                        total_used += fl->in_use * size;
-                       for (n = 0, h = fl->head; h != 0; h = h->next, n += 1)
+                       for (n = 0, h = fl->head; h != 0; h = HEADER_NEXT (h), n += 1)
                                ;
                        total_free += n * size;
                        fprintf(stderr, "%10d %10d %10d %10d\n",
@@ -365,22 +407,24 @@ print_malloc_free_list()
        fprintf(stderr, " all sizes %10d %10d %10d\n",
                total_used, total_free, total_used + total_free);
 }
-#endif DEBUG
+#endif /* DEBUG */
 
-static void malloc_fork_prepare()
+static void
+malloc_fork_prepare(void)
 /*
  * Prepare the malloc module for a fork by insuring that no thread is in a
  * malloc critical section.
  */
 {
     register int i;
-    
+
     for (i = 0; i < NBUCKETS; i++) {
        spin_lock(&malloc_free_list[i].lock);
     }
 }
 
-static void malloc_fork_parent()
+static void
+malloc_fork_parent(void)
 /*
  * Called in the parent process after a fork() to resume normal operation.
  */
@@ -392,7 +436,8 @@ static void malloc_fork_parent()
     }
 }
 
-static void malloc_fork_child()
+static void
+malloc_fork_child(void)
 /*
  * Called in the child process after a fork() to resume normal operation.
  */