Home | History | Annotate | Line # | Download | only in stdlib
malloc.c revision 1.46
      1  1.46     perry /*	$NetBSD: malloc.c,v 1.46 2005/12/24 21:42:02 perry Exp $	*/
      2  1.18   thorpej 
      3   1.1       cgd /*
      4  1.17       tls  * ----------------------------------------------------------------------------
      5  1.17       tls  * "THE BEER-WARE LICENSE" (Revision 42):
      6  1.17       tls  * <phk (at) FreeBSD.ORG> wrote this file.  As long as you retain this notice you
      7  1.17       tls  * can do whatever you want with this stuff. If we meet some day, and you think
      8  1.17       tls  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
      9  1.17       tls  * ----------------------------------------------------------------------------
     10   1.1       cgd  *
     11  1.17       tls  * From FreeBSD: malloc.c,v 1.43 1998/09/30 06:13:59 jb
     12   1.1       cgd  *
     13  1.17       tls  */
     14  1.17       tls 
     15  1.17       tls /*
     16  1.19   thorpej  * Defining MALLOC_EXTRA_SANITY will enable extra checks which are related
     17  1.17       tls  * to internal conditions and consistency in malloc.c. This has a
     18  1.17       tls  * noticeable runtime performance hit, and generally will not do you
     19  1.17       tls  * any good unless you fiddle with the internals of malloc or want
     20  1.17       tls  * to catch random pointer corruption as early as possible.
     21  1.17       tls  */
     22  1.17       tls #ifndef MALLOC_EXTRA_SANITY
     23  1.17       tls #undef MALLOC_EXTRA_SANITY
     24   1.5   thorpej #endif
     25   1.1       cgd 
     26   1.1       cgd /*
     27  1.17       tls  * What to use for Junk.  This is the byte value we use to fill with
     28  1.17       tls  * when the 'J' option is enabled.
     29  1.17       tls  */
     30  1.17       tls #define SOME_JUNK	0xd0		/* as in "Duh" :-) */
     31  1.17       tls 
     32  1.17       tls /*
     33  1.17       tls  * The basic parameters you can tweak.
     34  1.17       tls  *
     35  1.17       tls  * malloc_minsize	minimum size of an allocation in bytes.
     36  1.17       tls  *			If this is too small it's too much work
     37  1.17       tls  *			to manage them.  This is also the smallest
     38  1.17       tls  *			unit of alignment used for the storage
     39  1.17       tls  *			returned by malloc/realloc.
     40   1.1       cgd  *
     41   1.1       cgd  */
     42   1.1       cgd 
     43  1.17       tls #if defined(__FreeBSD__)
     44  1.17       tls #   if defined(__i386__)
     45  1.17       tls #       define malloc_minsize		16U
     46  1.17       tls #   endif
     47  1.17       tls #   if defined(__alpha__)
     48  1.17       tls #       define malloc_minsize		16U
     49  1.17       tls #   endif
     50  1.35  jdolecek #   define HAS_UTRACE
     51  1.35  jdolecek #   define UTRACE_LABEL
     52  1.35  jdolecek 
     53  1.35  jdolecek #include <sys/cdefs.h>
     54  1.43  junyoung void utrace(struct ut *, int);
     55  1.35  jdolecek 
     56  1.17       tls     /*
     57  1.17       tls      * Make malloc/free/realloc thread-safe in libc for use with
     58  1.17       tls      * kernel threads.
     59  1.17       tls      */
     60  1.17       tls #   include "libc_private.h"
     61  1.17       tls #   include "spinlock.h"
     62  1.17       tls     static spinlock_t thread_lock	= _SPINLOCK_INITIALIZER;
     63  1.17       tls #   define THREAD_LOCK()		if (__isthreaded) _SPINLOCK(&thread_lock);
     64  1.17       tls #   define THREAD_UNLOCK()		if (__isthreaded) _SPINUNLOCK(&thread_lock);
     65  1.17       tls #endif /* __FreeBSD__ */
     66  1.17       tls 
     67  1.17       tls #if defined(__NetBSD__)
     68  1.17       tls #   define malloc_minsize               16U
     69  1.35  jdolecek #   define HAS_UTRACE
     70  1.35  jdolecek #   define UTRACE_LABEL "malloc",
     71  1.35  jdolecek #include <sys/cdefs.h>
     72  1.44     lukem #if defined(LIBC_SCCS) && !defined(lint)
     73  1.46     perry __RCSID("$NetBSD: malloc.c,v 1.46 2005/12/24 21:42:02 perry Exp $");
     74  1.44     lukem #endif /* LIBC_SCCS and not lint */
     75  1.35  jdolecek #include <sys/types.h>
     76  1.43  junyoung int utrace(const char *, void *, size_t);
     77  1.41   thorpej 
     78  1.41   thorpej #include <reentrant.h>
     79  1.41   thorpej extern int __isthreaded;
     80  1.41   thorpej static mutex_t thread_lock = MUTEX_INITIALIZER;
     81  1.41   thorpej #define THREAD_LOCK()	if (__isthreaded) mutex_lock(&thread_lock);
     82  1.41   thorpej #define THREAD_UNLOCK()	if (__isthreaded) mutex_unlock(&thread_lock);
     83  1.17       tls #endif /* __NetBSD__ */
     84  1.17       tls 
     85  1.17       tls #if defined(__sparc__) && defined(sun)
     86  1.17       tls #   define malloc_minsize		16U
     87  1.17       tls #   define MAP_ANON			(0)
     88  1.17       tls     static int fdzero;
     89  1.17       tls #   define MMAP_FD	fdzero
     90  1.17       tls #   define INIT_MMAP() \
     91  1.17       tls 	{ if ((fdzero=open("/dev/zero", O_RDWR, 0000)) == -1) \
     92  1.17       tls 	    wrterror("open of /dev/zero"); }
     93  1.17       tls #endif /* __sparc__ */
     94  1.17       tls 
     95  1.17       tls /* Insert your combination here... */
     96  1.17       tls #if defined(__FOOCPU__) && defined(__BAROS__)
     97  1.17       tls #   define malloc_minsize		16U
     98  1.17       tls #endif /* __FOOCPU__ && __BAROS__ */
     99  1.17       tls 
    100  1.17       tls 
    101  1.17       tls /*
    102  1.17       tls  * No user serviceable parts behind this point.
    103  1.17       tls  */
    104  1.25    kleink #include "namespace.h"
    105  1.16    kleink #include <sys/types.h>
    106  1.17       tls #include <sys/mman.h>
    107  1.17       tls #include <errno.h>
    108  1.17       tls #include <fcntl.h>
    109  1.17       tls #include <stddef.h>
    110   1.9  christos #include <stdio.h>
    111   1.1       cgd #include <stdlib.h>
    112   1.1       cgd #include <string.h>
    113   1.1       cgd #include <unistd.h>
    114   1.1       cgd 
    115  1.17       tls /*
    116  1.17       tls  * This structure describes a page worth of chunks.
    117  1.17       tls  */
    118  1.17       tls 
    119  1.17       tls struct pginfo {
    120  1.17       tls     struct pginfo	*next;	/* next on the free list */
    121  1.17       tls     void		*page;	/* Pointer to the page */
    122  1.17       tls     u_short		size;	/* size of this page's chunks */
    123  1.17       tls     u_short		shift;	/* How far to shift for this size chunks */
    124  1.17       tls     u_short		free;	/* How many free chunks */
    125  1.17       tls     u_short		total;	/* How many chunk */
    126  1.17       tls     u_int		bits[1]; /* Which chunks are free */
    127  1.17       tls };
    128   1.1       cgd 
    129   1.1       cgd /*
    130  1.17       tls  * This structure describes a number of free pages.
    131  1.17       tls  */
    132  1.17       tls 
    133  1.17       tls struct pgfree {
    134  1.17       tls     struct pgfree	*next;	/* next run of free pages */
    135  1.17       tls     struct pgfree	*prev;	/* prev run of free pages */
    136  1.17       tls     void		*page;	/* pointer to free pages */
    137  1.17       tls     void		*end;	/* pointer to end of free pages */
    138  1.17       tls     size_t		size;	/* number of bytes free */
    139   1.1       cgd };
    140   1.1       cgd 
    141  1.17       tls /*
    142  1.17       tls  * How many bits per u_int in the bitmap.
    143  1.17       tls  * Change only if not 8 bits/byte
    144  1.17       tls  */
    145  1.39   thorpej #define	MALLOC_BITS	((int)(8*sizeof(u_int)))
    146  1.17       tls 
    147  1.17       tls /*
    148  1.17       tls  * Magic values to put in the page_directory
    149  1.17       tls  */
    150  1.17       tls #define MALLOC_NOT_MINE	((struct pginfo*) 0)
    151  1.17       tls #define MALLOC_FREE 	((struct pginfo*) 1)
    152  1.17       tls #define MALLOC_FIRST	((struct pginfo*) 2)
    153  1.17       tls #define MALLOC_FOLLOW	((struct pginfo*) 3)
    154  1.17       tls #define MALLOC_MAGIC	((struct pginfo*) 4)
    155  1.17       tls 
    156  1.20   thorpej /*
    157  1.20   thorpej  * Page size related parameters, computed at run-time.
    158  1.20   thorpej  */
    159  1.20   thorpej static size_t malloc_pagesize;
    160  1.20   thorpej static size_t malloc_pageshift;
    161  1.20   thorpej static size_t malloc_pagemask;
    162  1.17       tls 
    163  1.17       tls #ifndef malloc_minsize
    164  1.17       tls #define malloc_minsize			16U
    165  1.17       tls #endif
    166  1.17       tls 
    167  1.17       tls #ifndef malloc_maxsize
    168  1.17       tls #define malloc_maxsize			((malloc_pagesize)>>1)
    169  1.17       tls #endif
    170  1.17       tls 
    171  1.17       tls #define pageround(foo) (((foo) + (malloc_pagemask))&(~(malloc_pagemask)))
    172  1.38  christos #define ptr2idx(foo) \
    173  1.38  christos     (((size_t)(uintptr_t)(foo) >> malloc_pageshift)-malloc_origo)
    174   1.1       cgd 
    175  1.17       tls #ifndef THREAD_LOCK
    176  1.17       tls #define THREAD_LOCK()
    177  1.17       tls #endif
    178   1.1       cgd 
    179  1.17       tls #ifndef THREAD_UNLOCK
    180  1.17       tls #define THREAD_UNLOCK()
    181  1.16    kleink #endif
    182  1.16    kleink 
    183  1.17       tls #ifndef MMAP_FD
    184  1.17       tls #define MMAP_FD (-1)
    185   1.1       cgd #endif
    186   1.1       cgd 
    187  1.17       tls #ifndef INIT_MMAP
    188  1.17       tls #define INIT_MMAP()
    189  1.18   thorpej #endif
    190  1.18   thorpej 
    191  1.18   thorpej #ifndef MADV_FREE
    192  1.18   thorpej #define MADV_FREE MADV_DONTNEED
    193   1.9  christos #endif
    194   1.9  christos 
    195  1.17       tls /* Set when initialization has been done */
    196  1.17       tls static unsigned malloc_started;
    197  1.17       tls 
    198  1.17       tls /* Recusion flag for public interface. */
    199  1.17       tls static int malloc_active;
    200  1.17       tls 
    201  1.17       tls /* Number of free pages we cache */
    202  1.17       tls static unsigned malloc_cache = 16;
    203  1.17       tls 
    204  1.17       tls /* The offset from pagenumber to index into the page directory */
    205  1.30     enami static size_t malloc_origo;
    206  1.17       tls 
    207  1.17       tls /* The last index in the page directory we care about */
    208  1.30     enami static size_t last_idx;
    209  1.17       tls 
    210  1.17       tls /* Pointer to page directory. Allocated "as if with" malloc */
    211  1.17       tls static struct	pginfo **page_dir;
    212  1.17       tls 
    213  1.17       tls /* How many slots in the page directory */
    214  1.17       tls static unsigned	malloc_ninfo;
    215  1.17       tls 
    216  1.17       tls /* Free pages line up here */
    217  1.17       tls static struct pgfree free_list;
    218  1.17       tls 
    219  1.17       tls /* Abort(), user doesn't handle problems.  */
    220  1.17       tls static int malloc_abort;
    221  1.17       tls 
    222  1.17       tls /* Are we trying to die ?  */
    223  1.17       tls static int suicide;
    224  1.17       tls 
    225  1.17       tls /* always realloc ?  */
    226  1.17       tls static int malloc_realloc;
    227   1.9  christos 
    228  1.17       tls /* pass the kernel a hint on free pages ?  */
    229  1.32    simonb static int malloc_hint = 0;
    230  1.17       tls 
    231  1.17       tls /* xmalloc behaviour ?  */
    232  1.17       tls static int malloc_xmalloc;
    233  1.17       tls 
    234  1.17       tls /* sysv behaviour for malloc(0) ?  */
    235  1.17       tls static int malloc_sysv;
    236  1.17       tls 
    237  1.17       tls /* zero fill ?  */
    238  1.17       tls static int malloc_zero;
    239  1.17       tls 
    240  1.17       tls /* junk fill ?  */
    241  1.17       tls static int malloc_junk;
    242  1.17       tls 
    243  1.17       tls #ifdef HAS_UTRACE
    244  1.17       tls 
    245  1.17       tls /* utrace ?  */
    246  1.17       tls static int malloc_utrace;
    247  1.17       tls 
    248  1.17       tls struct ut { void *p; size_t s; void *r; };
    249  1.17       tls 
    250  1.17       tls #define UTRACE(a, b, c) \
    251  1.35  jdolecek 	if (malloc_utrace) {			\
    252  1.35  jdolecek 		struct ut u;			\
    253  1.35  jdolecek 		u.p=a; u.s = b; u.r=c;		\
    254  1.35  jdolecek 		utrace(UTRACE_LABEL (void *) &u, sizeof u);	\
    255  1.35  jdolecek 	}
    256  1.17       tls #else /* !HAS_UTRACE */
    257  1.17       tls #define UTRACE(a,b,c)
    258  1.17       tls #endif /* HAS_UTRACE */
    259  1.17       tls 
    260  1.17       tls /* my last break. */
    261  1.17       tls static void *malloc_brk;
    262  1.17       tls 
    263  1.17       tls /* one location cache for free-list holders */
    264  1.17       tls static struct pgfree *px;
    265  1.17       tls 
    266  1.17       tls /* compile-time options */
    267  1.17       tls char *malloc_options;
    268  1.17       tls 
    269  1.17       tls /* Name of the current public function */
    270  1.45  christos static const char *malloc_func;
    271  1.17       tls 
    272  1.17       tls /* Macro for mmap */
    273  1.17       tls #define MMAP(size) \
    274  1.17       tls 	mmap(0, (size), PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, \
    275  1.30     enami 	    MMAP_FD, (off_t)0);
    276   1.9  christos 
    277  1.16    kleink /*
    278  1.17       tls  * Necessary function declarations
    279  1.16    kleink  */
    280  1.30     enami static int extend_pgdir(size_t idx);
    281  1.17       tls static void *imalloc(size_t size);
    282  1.17       tls static void ifree(void *ptr);
    283  1.17       tls static void *irealloc(void *ptr, size_t size);
    284  1.17       tls 
    285  1.17       tls static void
    286  1.45  christos wrterror(const char *p)
    287  1.17       tls {
    288  1.36       cgd     const char *progname = getprogname();
    289  1.45  christos     const char *q = " error: ";
    290  1.36       cgd     write(STDERR_FILENO, progname, strlen(progname));
    291  1.17       tls     write(STDERR_FILENO, malloc_func, strlen(malloc_func));
    292  1.17       tls     write(STDERR_FILENO, q, strlen(q));
    293  1.17       tls     write(STDERR_FILENO, p, strlen(p));
    294  1.17       tls     suicide = 1;
    295  1.17       tls     abort();
    296  1.17       tls }
    297  1.17       tls 
    298  1.16    kleink static void
    299  1.45  christos wrtwarning(const char *p)
    300   1.1       cgd {
    301  1.36       cgd     const char *progname = getprogname();
    302  1.45  christos     const char *q = " warning: ";
    303  1.17       tls     if (malloc_abort)
    304  1.17       tls 	wrterror(p);
    305  1.36       cgd     write(STDERR_FILENO, progname, strlen(progname));
    306  1.17       tls     write(STDERR_FILENO, malloc_func, strlen(malloc_func));
    307  1.17       tls     write(STDERR_FILENO, q, strlen(q));
    308  1.17       tls     write(STDERR_FILENO, p, strlen(p));
    309  1.17       tls }
    310  1.17       tls 
    311  1.17       tls /*
    312  1.17       tls  * Allocate a number of pages from the OS
    313  1.17       tls  */
    314  1.17       tls static void *
    315  1.30     enami map_pages(size_t pages)
    316  1.17       tls {
    317  1.38  christos     caddr_t result, rresult, tail;
    318  1.38  christos     intptr_t bytes = pages << malloc_pageshift;
    319  1.16    kleink 
    320  1.38  christos     if (bytes < 0 || (size_t)bytes < pages) {
    321  1.38  christos 	errno = ENOMEM;
    322  1.38  christos 	return NULL;
    323  1.38  christos     }
    324  1.17       tls 
    325  1.38  christos     if ((result = sbrk(bytes)) == (void *)-1)
    326  1.38  christos 	return NULL;
    327  1.38  christos 
    328  1.38  christos     /*
    329  1.38  christos      * Round to a page, in case sbrk(2) did not do this for us
    330  1.38  christos      */
    331  1.38  christos     rresult = (caddr_t)pageround((size_t)(uintptr_t)result);
    332  1.38  christos     if (result < rresult) {
    333  1.38  christos 	/* make sure we have enough space to fit bytes */
    334  1.38  christos 	if (sbrk((intptr_t)(rresult - result)) == (void *) -1) {
    335  1.38  christos 	    /* we failed, put everything back */
    336  1.38  christos 	    if (brk(result)) {
    337  1.38  christos 		wrterror("brk(2) failed [internal error]\n");
    338  1.38  christos 	    }
    339  1.38  christos 	}
    340  1.17       tls     }
    341  1.38  christos     tail = rresult + (size_t)bytes;
    342  1.38  christos 
    343  1.23   thorpej     last_idx = ptr2idx(tail) - 1;
    344  1.17       tls     malloc_brk = tail;
    345  1.17       tls 
    346  1.38  christos     if ((last_idx+1) >= malloc_ninfo && !extend_pgdir(last_idx)) {
    347  1.38  christos 	malloc_brk = result;
    348  1.38  christos 	last_idx = ptr2idx(malloc_brk) - 1;
    349  1.38  christos 	/* Put back break point since we failed. */
    350  1.38  christos 	if (brk(malloc_brk))
    351  1.38  christos 	    wrterror("brk(2) failed [internal error]\n");
    352  1.38  christos 	return 0;
    353  1.38  christos     }
    354  1.16    kleink 
    355  1.38  christos     return rresult;
    356   1.1       cgd }
    357   1.1       cgd 
    358  1.17       tls /*
    359  1.17       tls  * Extend page directory
    360  1.17       tls  */
    361  1.17       tls static int
    362  1.30     enami extend_pgdir(size_t idx)
    363   1.1       cgd {
    364  1.17       tls     struct  pginfo **new, **old;
    365  1.30     enami     size_t newlen, oldlen;
    366  1.37  christos 
    367  1.37  christos     /* check for overflow */
    368  1.37  christos     if ((((~(1UL << ((sizeof(size_t) * NBBY) - 1)) / sizeof(*page_dir)) + 1)
    369  1.37  christos 	+ (malloc_pagesize / sizeof *page_dir)) < idx) {
    370  1.37  christos 	errno = ENOMEM;
    371  1.37  christos 	return 0;
    372  1.37  christos     }
    373   1.1       cgd 
    374  1.17       tls     /* Make it this many pages */
    375  1.30     enami     newlen = pageround(idx * sizeof *page_dir) + malloc_pagesize;
    376  1.17       tls 
    377  1.17       tls     /* remember the old mapping size */
    378  1.17       tls     oldlen = malloc_ninfo * sizeof *page_dir;
    379  1.17       tls 
    380  1.17       tls     /*
    381  1.17       tls      * NOTE: we allocate new pages and copy the directory rather than tempt
    382  1.17       tls      * fate by trying to "grow" the region.. There is nothing to prevent
    383  1.17       tls      * us from accidently re-mapping space that's been allocated by our caller
    384  1.17       tls      * via dlopen() or other mmap().
    385  1.17       tls      *
    386  1.17       tls      * The copy problem is not too bad, as there is 4K of page index per
    387  1.17       tls      * 4MB of malloc arena.
    388  1.17       tls      *
    389  1.17       tls      * We can totally avoid the copy if we open a file descriptor to associate
    390  1.17       tls      * the anon mappings with.  Then, when we remap the pages at the new
    391  1.17       tls      * address, the old pages will be "magically" remapped..  But this means
    392  1.17       tls      * keeping open a "secret" file descriptor.....
    393  1.17       tls      */
    394  1.17       tls 
    395  1.17       tls     /* Get new pages */
    396  1.30     enami     new = (struct pginfo**) MMAP(newlen);
    397  1.43  junyoung     if (new == MAP_FAILED)
    398  1.17       tls 	return 0;
    399  1.17       tls 
    400  1.17       tls     /* Copy the old stuff */
    401  1.30     enami     memcpy(new, page_dir, oldlen);
    402  1.17       tls 
    403  1.17       tls     /* register the new size */
    404  1.30     enami     malloc_ninfo = newlen / sizeof *page_dir;
    405  1.17       tls 
    406  1.17       tls     /* swap the pointers */
    407  1.17       tls     old = page_dir;
    408  1.17       tls     page_dir = new;
    409  1.17       tls 
    410  1.17       tls     /* Now free the old stuff */
    411  1.17       tls     munmap(old, oldlen);
    412  1.17       tls     return 1;
    413  1.17       tls }
    414  1.16    kleink 
    415  1.17       tls /*
    416  1.17       tls  * Initialize the world
    417  1.17       tls  */
    418  1.17       tls static void
    419  1.17       tls malloc_init (void)
    420  1.17       tls {
    421  1.17       tls     char *p, b[64];
    422  1.17       tls     int i, j;
    423  1.17       tls     int errnosave;
    424  1.20   thorpej 
    425  1.20   thorpej     /*
    426  1.20   thorpej      * Compute page-size related variables.
    427  1.20   thorpej      */
    428  1.30     enami     malloc_pagesize = (size_t)sysconf(_SC_PAGESIZE);
    429  1.20   thorpej     malloc_pagemask = malloc_pagesize - 1;
    430  1.20   thorpej     for (malloc_pageshift = 0;
    431  1.20   thorpej 	 (1UL << malloc_pageshift) != malloc_pagesize;
    432  1.20   thorpej 	 malloc_pageshift++)
    433  1.20   thorpej 	/* nothing */ ;
    434  1.17       tls 
    435  1.17       tls     INIT_MMAP();
    436  1.17       tls 
    437  1.19   thorpej #ifdef MALLOC_EXTRA_SANITY
    438  1.17       tls     malloc_junk = 1;
    439  1.19   thorpej #endif /* MALLOC_EXTRA_SANITY */
    440  1.17       tls 
    441  1.17       tls     for (i = 0; i < 3; i++) {
    442  1.17       tls 	if (i == 0) {
    443  1.17       tls 	    errnosave = errno;
    444  1.17       tls 	    j = readlink("/etc/malloc.conf", b, sizeof b - 1);
    445  1.17       tls 	    errno = errnosave;
    446  1.17       tls 	    if (j <= 0)
    447  1.17       tls 		continue;
    448  1.17       tls 	    b[j] = '\0';
    449  1.17       tls 	    p = b;
    450  1.17       tls 	} else if (i == 1) {
    451  1.17       tls 	    p = getenv("MALLOC_OPTIONS");
    452  1.17       tls 	} else {
    453  1.17       tls 	    p = malloc_options;
    454   1.1       cgd 	}
    455  1.43  junyoung 	for (; p != NULL && *p != '\0'; p++) {
    456  1.17       tls 	    switch (*p) {
    457  1.17       tls 		case '>': malloc_cache   <<= 1; break;
    458  1.17       tls 		case '<': malloc_cache   >>= 1; break;
    459  1.17       tls 		case 'a': malloc_abort   = 0; break;
    460  1.17       tls 		case 'A': malloc_abort   = 1; break;
    461  1.17       tls 		case 'h': malloc_hint    = 0; break;
    462  1.17       tls 		case 'H': malloc_hint    = 1; break;
    463  1.17       tls 		case 'r': malloc_realloc = 0; break;
    464  1.17       tls 		case 'R': malloc_realloc = 1; break;
    465  1.17       tls 		case 'j': malloc_junk    = 0; break;
    466  1.17       tls 		case 'J': malloc_junk    = 1; break;
    467  1.17       tls #ifdef HAS_UTRACE
    468  1.17       tls 		case 'u': malloc_utrace  = 0; break;
    469  1.17       tls 		case 'U': malloc_utrace  = 1; break;
    470  1.17       tls #endif
    471  1.17       tls 		case 'v': malloc_sysv    = 0; break;
    472  1.17       tls 		case 'V': malloc_sysv    = 1; break;
    473  1.17       tls 		case 'x': malloc_xmalloc = 0; break;
    474  1.17       tls 		case 'X': malloc_xmalloc = 1; break;
    475  1.17       tls 		case 'z': malloc_zero    = 0; break;
    476  1.17       tls 		case 'Z': malloc_zero    = 1; break;
    477  1.17       tls 		default:
    478  1.17       tls 		    j = malloc_abort;
    479  1.17       tls 		    malloc_abort = 0;
    480  1.43  junyoung 		    wrtwarning("unknown char in MALLOC_OPTIONS.\n");
    481  1.17       tls 		    malloc_abort = j;
    482  1.17       tls 		    break;
    483  1.17       tls 	    }
    484   1.1       cgd 	}
    485  1.17       tls     }
    486  1.17       tls 
    487  1.17       tls     UTRACE(0, 0, 0);
    488  1.17       tls 
    489  1.17       tls     /*
    490  1.17       tls      * We want junk in the entire allocation, and zero only in the part
    491  1.17       tls      * the user asked for.
    492  1.17       tls      */
    493  1.17       tls     if (malloc_zero)
    494  1.17       tls 	malloc_junk=1;
    495  1.17       tls 
    496  1.17       tls     /*
    497  1.17       tls      * If we run with junk (or implicitly from above: zero), we want to
    498  1.17       tls      * force realloc() to get new storage, so we can DTRT with it.
    499  1.17       tls      */
    500  1.17       tls     if (malloc_junk)
    501  1.17       tls 	malloc_realloc=1;
    502  1.17       tls 
    503  1.17       tls     /* Allocate one page for the page directory */
    504  1.17       tls     page_dir = (struct pginfo **) MMAP(malloc_pagesize);
    505  1.17       tls 
    506  1.43  junyoung     if (page_dir == MAP_FAILED)
    507  1.17       tls 	wrterror("mmap(2) failed, check limits.\n");
    508  1.17       tls 
    509  1.17       tls     /*
    510  1.17       tls      * We need a maximum of malloc_pageshift buckets, steal these from the
    511  1.17       tls      * front of the page_directory;
    512  1.17       tls      */
    513  1.38  christos     malloc_origo = pageround((size_t)(uintptr_t)sbrk((intptr_t)0))
    514  1.34  christos 	>> malloc_pageshift;
    515  1.17       tls     malloc_origo -= malloc_pageshift;
    516  1.17       tls 
    517  1.17       tls     malloc_ninfo = malloc_pagesize / sizeof *page_dir;
    518  1.17       tls 
    519  1.17       tls     /* Recalculate the cache size in bytes, and make sure it's nonzero */
    520  1.17       tls 
    521  1.17       tls     if (!malloc_cache)
    522  1.17       tls 	malloc_cache++;
    523  1.17       tls 
    524  1.17       tls     malloc_cache <<= malloc_pageshift;
    525  1.17       tls 
    526  1.17       tls     /*
    527  1.17       tls      * This is a nice hack from Kaleb Keithly (kaleb (at) x.org).
    528  1.17       tls      * We can sbrk(2) further back when we keep this on a low address.
    529  1.17       tls      */
    530  1.17       tls     px = (struct pgfree *) imalloc (sizeof *px);
    531  1.17       tls 
    532  1.17       tls     /* Been here, done that */
    533  1.17       tls     malloc_started++;
    534  1.17       tls }
    535  1.17       tls 
    536  1.17       tls /*
    537  1.17       tls  * Allocate a number of complete pages
    538  1.17       tls  */
    539  1.17       tls static void *
    540  1.17       tls malloc_pages(size_t size)
    541  1.17       tls {
    542  1.43  junyoung     void *p, *delay_free = NULL;
    543  1.38  christos     size_t i;
    544  1.17       tls     struct pgfree *pf;
    545  1.30     enami     size_t idx;
    546  1.17       tls 
    547  1.38  christos     idx = pageround(size);
    548  1.38  christos     if (idx < size) {
    549  1.38  christos 	errno = ENOMEM;
    550  1.38  christos 	return NULL;
    551  1.38  christos     } else
    552  1.38  christos 	size = idx;
    553  1.17       tls 
    554  1.43  junyoung     p = NULL;
    555  1.17       tls 
    556  1.17       tls     /* Look for free pages before asking for more */
    557  1.17       tls     for(pf = free_list.next; pf; pf = pf->next) {
    558  1.17       tls 
    559  1.19   thorpej #ifdef MALLOC_EXTRA_SANITY
    560  1.17       tls 	if (pf->size & malloc_pagemask)
    561  1.43  junyoung 	    wrterror("(ES): junk length entry on free_list.\n");
    562  1.17       tls 	if (!pf->size)
    563  1.43  junyoung 	    wrterror("(ES): zero length entry on free_list.\n");
    564  1.17       tls 	if (pf->page == pf->end)
    565  1.43  junyoung 	    wrterror("(ES): zero entry on free_list.\n");
    566  1.43  junyoung 	if (pf->page > pf->end)
    567  1.43  junyoung 	    wrterror("(ES): sick entry on free_list.\n");
    568  1.17       tls 	if ((void*)pf->page >= (void*)sbrk(0))
    569  1.43  junyoung 	    wrterror("(ES): entry on free_list past brk.\n");
    570  1.43  junyoung 	if (page_dir[ptr2idx(pf->page)] != MALLOC_FREE)
    571  1.43  junyoung 	    wrterror("(ES): non-free first page on free-list.\n");
    572  1.23   thorpej 	if (page_dir[ptr2idx(pf->end)-1] != MALLOC_FREE)
    573  1.43  junyoung 	    wrterror("(ES): non-free last page on free-list.\n");
    574  1.19   thorpej #endif /* MALLOC_EXTRA_SANITY */
    575  1.17       tls 
    576  1.17       tls 	if (pf->size < size)
    577  1.17       tls 	    continue;
    578  1.17       tls 
    579  1.17       tls 	if (pf->size == size) {
    580  1.17       tls 	    p = pf->page;
    581  1.43  junyoung 	    if (pf->next != NULL)
    582  1.17       tls 		    pf->next->prev = pf->prev;
    583  1.17       tls 	    pf->prev->next = pf->next;
    584  1.17       tls 	    delay_free = pf;
    585  1.17       tls 	    break;
    586  1.17       tls 	}
    587  1.17       tls 
    588  1.17       tls 	p = pf->page;
    589  1.17       tls 	pf->page = (char *)pf->page + size;
    590  1.17       tls 	pf->size -= size;
    591  1.17       tls 	break;
    592  1.17       tls     }
    593  1.17       tls 
    594  1.19   thorpej #ifdef MALLOC_EXTRA_SANITY
    595  1.43  junyoung     if (p != NULL && page_dir[ptr2idx(p)] != MALLOC_FREE)
    596  1.43  junyoung 	wrterror("(ES): allocated non-free page on free-list.\n");
    597  1.19   thorpej #endif /* MALLOC_EXTRA_SANITY */
    598  1.17       tls 
    599  1.17       tls     size >>= malloc_pageshift;
    600  1.17       tls 
    601  1.17       tls     /* Map new pages */
    602  1.43  junyoung     if (p == NULL)
    603  1.17       tls 	p = map_pages(size);
    604  1.17       tls 
    605  1.43  junyoung     if (p != NULL) {
    606  1.17       tls 
    607  1.23   thorpej 	idx = ptr2idx(p);
    608  1.23   thorpej 	page_dir[idx] = MALLOC_FIRST;
    609  1.17       tls 	for (i=1;i<size;i++)
    610  1.23   thorpej 	    page_dir[idx+i] = MALLOC_FOLLOW;
    611  1.17       tls 
    612  1.17       tls 	if (malloc_junk)
    613  1.17       tls 	    memset(p, SOME_JUNK, size << malloc_pageshift);
    614  1.17       tls     }
    615  1.17       tls 
    616  1.17       tls     if (delay_free) {
    617  1.43  junyoung 	if (px == NULL)
    618  1.17       tls 	    px = delay_free;
    619  1.17       tls 	else
    620  1.17       tls 	    ifree(delay_free);
    621  1.17       tls     }
    622  1.17       tls 
    623  1.17       tls     return p;
    624  1.17       tls }
    625  1.17       tls 
    626  1.17       tls /*
    627  1.17       tls  * Allocate a page of fragments
    628  1.17       tls  */
    629  1.17       tls 
    630  1.46     perry static inline int
    631  1.17       tls malloc_make_chunks(int bits)
    632  1.17       tls {
    633  1.17       tls     struct  pginfo *bp;
    634  1.17       tls     void *pp;
    635  1.31     enami     int i, k, l;
    636  1.17       tls 
    637  1.17       tls     /* Allocate a new bucket */
    638  1.17       tls     pp = malloc_pages(malloc_pagesize);
    639  1.43  junyoung     if (pp == NULL)
    640  1.17       tls 	return 0;
    641  1.17       tls 
    642  1.17       tls     /* Find length of admin structure */
    643  1.33  christos     l = (int)offsetof(struct pginfo, bits[0]);
    644  1.17       tls     l += sizeof bp->bits[0] *
    645  1.17       tls 	(((malloc_pagesize >> bits)+MALLOC_BITS-1) / MALLOC_BITS);
    646  1.17       tls 
    647  1.17       tls     /* Don't waste more than two chunks on this */
    648  1.17       tls     if ((1<<(bits)) <= l+l) {
    649  1.17       tls 	bp = (struct  pginfo *)pp;
    650  1.17       tls     } else {
    651  1.31     enami 	bp = (struct  pginfo *)imalloc((size_t)l);
    652  1.43  junyoung 	if (bp == NULL) {
    653  1.17       tls 	    ifree(pp);
    654  1.17       tls 	    return 0;
    655   1.1       cgd 	}
    656  1.17       tls     }
    657  1.17       tls 
    658  1.17       tls     bp->size = (1<<bits);
    659  1.17       tls     bp->shift = bits;
    660  1.17       tls     bp->total = bp->free = malloc_pagesize >> bits;
    661  1.17       tls     bp->page = pp;
    662  1.17       tls 
    663  1.17       tls     /* set all valid bits in the bitmap */
    664  1.17       tls     k = bp->total;
    665  1.17       tls     i = 0;
    666  1.17       tls 
    667  1.17       tls     /* Do a bunch at a time */
    668  1.17       tls     for(;k-i >= MALLOC_BITS; i += MALLOC_BITS)
    669  1.30     enami 	bp->bits[i / MALLOC_BITS] = ~0U;
    670  1.17       tls 
    671  1.17       tls     for(; i < k; i++)
    672  1.17       tls         bp->bits[i/MALLOC_BITS] |= 1<<(i%MALLOC_BITS);
    673  1.17       tls 
    674  1.17       tls     if (bp == bp->page) {
    675  1.17       tls 	/* Mark the ones we stole for ourselves */
    676  1.17       tls 	for(i=0;l > 0;i++) {
    677  1.17       tls 	    bp->bits[i/MALLOC_BITS] &= ~(1<<(i%MALLOC_BITS));
    678  1.17       tls 	    bp->free--;
    679  1.17       tls 	    bp->total--;
    680  1.17       tls 	    l -= (1 << bits);
    681   1.1       cgd 	}
    682  1.17       tls     }
    683  1.17       tls 
    684  1.17       tls     /* MALLOC_LOCK */
    685  1.17       tls 
    686  1.23   thorpej     page_dir[ptr2idx(pp)] = bp;
    687  1.17       tls 
    688  1.17       tls     bp->next = page_dir[bits];
    689  1.17       tls     page_dir[bits] = bp;
    690  1.17       tls 
    691  1.17       tls     /* MALLOC_UNLOCK */
    692  1.17       tls 
    693  1.17       tls     return 1;
    694   1.1       cgd }
    695   1.1       cgd 
    696   1.1       cgd /*
    697  1.17       tls  * Allocate a fragment
    698   1.1       cgd  */
    699  1.17       tls static void *
    700  1.17       tls malloc_bytes(size_t size)
    701   1.1       cgd {
    702  1.30     enami     size_t i;
    703  1.30     enami     int j;
    704  1.17       tls     u_int u;
    705  1.17       tls     struct  pginfo *bp;
    706  1.17       tls     int k;
    707  1.17       tls     u_int *lp;
    708  1.17       tls 
    709  1.17       tls     /* Don't bother with anything less than this */
    710  1.17       tls     if (size < malloc_minsize)
    711  1.17       tls 	size = malloc_minsize;
    712  1.17       tls 
    713  1.17       tls     /* Find the right bucket */
    714  1.17       tls     j = 1;
    715  1.17       tls     i = size-1;
    716  1.17       tls     while (i >>= 1)
    717  1.17       tls 	j++;
    718  1.17       tls 
    719  1.17       tls     /* If it's empty, make a page more of that size chunks */
    720  1.43  junyoung     if (page_dir[j] == NULL && !malloc_make_chunks(j))
    721  1.43  junyoung 	return NULL;
    722  1.17       tls 
    723  1.17       tls     bp = page_dir[j];
    724  1.17       tls 
    725  1.17       tls     /* Find first word of bitmap which isn't empty */
    726  1.17       tls     for (lp = bp->bits; !*lp; lp++)
    727  1.17       tls 	;
    728  1.17       tls 
    729  1.17       tls     /* Find that bit, and tweak it */
    730  1.17       tls     u = 1;
    731  1.17       tls     k = 0;
    732  1.17       tls     while (!(*lp & u)) {
    733  1.17       tls 	u += u;
    734  1.17       tls 	k++;
    735  1.17       tls     }
    736  1.17       tls     *lp ^= u;
    737  1.17       tls 
    738  1.17       tls     /* If there are no more free, remove from free-list */
    739  1.17       tls     if (!--bp->free) {
    740  1.17       tls 	page_dir[j] = bp->next;
    741  1.43  junyoung 	bp->next = NULL;
    742  1.17       tls     }
    743  1.17       tls 
    744  1.17       tls     /* Adjust to the real offset of that chunk */
    745  1.17       tls     k += (lp-bp->bits)*MALLOC_BITS;
    746  1.17       tls     k <<= bp->shift;
    747  1.17       tls 
    748  1.17       tls     if (malloc_junk)
    749  1.30     enami 	memset((u_char*)bp->page + k, SOME_JUNK, (size_t)bp->size);
    750   1.1       cgd 
    751  1.17       tls     return (u_char *)bp->page + k;
    752   1.1       cgd }
    753   1.1       cgd 
    754  1.17       tls /*
    755  1.17       tls  * Allocate a piece of memory
    756  1.17       tls  */
    757  1.17       tls static void *
    758  1.17       tls imalloc(size_t size)
    759  1.17       tls {
    760  1.17       tls     void *result;
    761  1.17       tls 
    762  1.17       tls     if (suicide)
    763  1.17       tls 	abort();
    764  1.17       tls 
    765  1.17       tls     if ((size + malloc_pagesize) < size)	/* Check for overflow */
    766  1.43  junyoung 	result = NULL;
    767  1.17       tls     else if (size <= malloc_maxsize)
    768  1.43  junyoung 	result = malloc_bytes(size);
    769  1.17       tls     else
    770  1.43  junyoung 	result = malloc_pages(size);
    771  1.17       tls 
    772  1.43  junyoung     if (malloc_abort && result == NULL)
    773  1.17       tls 	wrterror("allocation failed.\n");
    774  1.17       tls 
    775  1.43  junyoung     if (malloc_zero && result != NULL)
    776  1.17       tls 	memset(result, 0, size);
    777  1.17       tls 
    778  1.17       tls     return result;
    779   1.1       cgd }
    780   1.1       cgd 
    781   1.1       cgd /*
    782  1.17       tls  * Change the size of an allocation.
    783   1.1       cgd  */
    784  1.17       tls static void *
    785  1.17       tls irealloc(void *ptr, size_t size)
    786  1.17       tls {
    787  1.17       tls     void *p;
    788  1.30     enami     size_t osize, idx;
    789  1.17       tls     struct pginfo **mp;
    790  1.30     enami     size_t i;
    791  1.17       tls 
    792  1.17       tls     if (suicide)
    793  1.17       tls 	abort();
    794  1.17       tls 
    795  1.23   thorpej     idx = ptr2idx(ptr);
    796   1.1       cgd 
    797  1.23   thorpej     if (idx < malloc_pageshift) {
    798  1.17       tls 	wrtwarning("junk pointer, too low to make sense.\n");
    799  1.17       tls 	return 0;
    800  1.17       tls     }
    801  1.17       tls 
    802  1.23   thorpej     if (idx > last_idx) {
    803  1.17       tls 	wrtwarning("junk pointer, too high to make sense.\n");
    804  1.17       tls 	return 0;
    805  1.17       tls     }
    806  1.17       tls 
    807  1.23   thorpej     mp = &page_dir[idx];
    808  1.17       tls 
    809  1.17       tls     if (*mp == MALLOC_FIRST) {			/* Page allocation */
    810  1.17       tls 
    811  1.17       tls 	/* Check the pointer */
    812  1.38  christos 	if ((size_t)(uintptr_t)ptr & malloc_pagemask) {
    813  1.17       tls 	    wrtwarning("modified (page-) pointer.\n");
    814  1.43  junyoung 	    return NULL;
    815  1.17       tls 	}
    816  1.17       tls 
    817  1.17       tls 	/* Find the size in bytes */
    818  1.17       tls 	for (osize = malloc_pagesize; *++mp == MALLOC_FOLLOW;)
    819  1.17       tls 	    osize += malloc_pagesize;
    820  1.17       tls 
    821  1.17       tls         if (!malloc_realloc && 			/* unless we have to, */
    822  1.17       tls 	  size <= osize && 			/* .. or are too small, */
    823  1.17       tls 	  size > (osize - malloc_pagesize)) {	/* .. or can free a page, */
    824  1.17       tls 	    return ptr;				/* don't do anything. */
    825   1.6       jtc 	}
    826  1.17       tls 
    827  1.17       tls     } else if (*mp >= MALLOC_MAGIC) {		/* Chunk allocation */
    828  1.17       tls 
    829  1.17       tls 	/* Check the pointer for sane values */
    830  1.38  christos 	if (((size_t)(uintptr_t)ptr & ((*mp)->size-1))) {
    831  1.17       tls 	    wrtwarning("modified (chunk-) pointer.\n");
    832  1.43  junyoung 	    return NULL;
    833   1.1       cgd 	}
    834  1.17       tls 
    835  1.17       tls 	/* Find the chunk index in the page */
    836  1.38  christos 	i = ((size_t)(uintptr_t)ptr & malloc_pagemask) >> (*mp)->shift;
    837  1.17       tls 
    838  1.17       tls 	/* Verify that it isn't a free chunk already */
    839  1.17       tls         if ((*mp)->bits[i/MALLOC_BITS] & (1<<(i%MALLOC_BITS))) {
    840  1.17       tls 	    wrtwarning("chunk is already free.\n");
    841  1.43  junyoung 	    return NULL;
    842  1.16    kleink 	}
    843  1.17       tls 
    844  1.17       tls 	osize = (*mp)->size;
    845  1.17       tls 
    846  1.17       tls 	if (!malloc_realloc &&		/* Unless we have to, */
    847  1.17       tls 	  size < osize && 		/* ..or are too small, */
    848  1.17       tls 	  (size > osize/2 ||	 	/* ..or could use a smaller size, */
    849  1.17       tls 	  osize == malloc_minsize)) {	/* ..(if there is one) */
    850  1.17       tls 	    return ptr;			/* ..Don't do anything */
    851   1.1       cgd 	}
    852  1.17       tls 
    853  1.17       tls     } else {
    854  1.17       tls 	wrtwarning("pointer to wrong page.\n");
    855  1.43  junyoung 	return NULL;
    856  1.17       tls     }
    857  1.17       tls 
    858  1.17       tls     p = imalloc(size);
    859  1.17       tls 
    860  1.43  junyoung     if (p != NULL) {
    861  1.17       tls 	/* copy the lesser of the two sizes, and free the old one */
    862  1.17       tls 	if (!size || !osize)
    863  1.17       tls 	    ;
    864  1.17       tls 	else if (osize < size)
    865  1.17       tls 	    memcpy(p, ptr, osize);
    866  1.17       tls 	else
    867  1.17       tls 	    memcpy(p, ptr, size);
    868  1.17       tls 	ifree(ptr);
    869  1.17       tls     }
    870  1.17       tls     return p;
    871   1.1       cgd }
    872   1.1       cgd 
    873   1.1       cgd /*
    874  1.17       tls  * Free a sequence of pages
    875   1.1       cgd  */
    876  1.17       tls 
    877  1.46     perry static inline void
    878  1.30     enami free_pages(void *ptr, size_t idx, struct pginfo *info)
    879  1.17       tls {
    880  1.30     enami     size_t i;
    881  1.43  junyoung     struct pgfree *pf, *pt=NULL;
    882  1.30     enami     size_t l;
    883  1.17       tls     void *tail;
    884  1.17       tls 
    885  1.17       tls     if (info == MALLOC_FREE) {
    886  1.17       tls 	wrtwarning("page is already free.\n");
    887  1.17       tls 	return;
    888  1.17       tls     }
    889  1.17       tls 
    890  1.17       tls     if (info != MALLOC_FIRST) {
    891  1.17       tls 	wrtwarning("pointer to wrong page.\n");
    892  1.17       tls 	return;
    893  1.17       tls     }
    894  1.17       tls 
    895  1.38  christos     if ((size_t)(uintptr_t)ptr & malloc_pagemask) {
    896  1.17       tls 	wrtwarning("modified (page-) pointer.\n");
    897  1.17       tls 	return;
    898  1.17       tls     }
    899  1.17       tls 
    900  1.17       tls     /* Count how many pages and mark them free at the same time */
    901  1.23   thorpej     page_dir[idx] = MALLOC_FREE;
    902  1.23   thorpej     for (i = 1; page_dir[idx+i] == MALLOC_FOLLOW; i++)
    903  1.23   thorpej 	page_dir[idx + i] = MALLOC_FREE;
    904  1.17       tls 
    905  1.17       tls     l = i << malloc_pageshift;
    906  1.17       tls 
    907  1.17       tls     if (malloc_junk)
    908  1.17       tls 	memset(ptr, SOME_JUNK, l);
    909  1.17       tls 
    910  1.17       tls     if (malloc_hint)
    911  1.17       tls 	madvise(ptr, l, MADV_FREE);
    912  1.17       tls 
    913  1.17       tls     tail = (char *)ptr+l;
    914  1.17       tls 
    915  1.17       tls     /* add to free-list */
    916  1.43  junyoung     if (px == NULL)
    917  1.17       tls 	px = imalloc(sizeof *pt);	/* This cannot fail... */
    918  1.17       tls     px->page = ptr;
    919  1.17       tls     px->end =  tail;
    920  1.17       tls     px->size = l;
    921  1.43  junyoung     if (free_list.next == NULL) {
    922  1.17       tls 
    923  1.17       tls 	/* Nothing on free list, put this at head */
    924  1.17       tls 	px->next = free_list.next;
    925  1.17       tls 	px->prev = &free_list;
    926  1.17       tls 	free_list.next = px;
    927  1.17       tls 	pf = px;
    928  1.43  junyoung 	px = NULL;
    929  1.17       tls 
    930  1.17       tls     } else {
    931  1.17       tls 
    932  1.17       tls 	/* Find the right spot, leave pf pointing to the modified entry. */
    933  1.17       tls 	tail = (char *)ptr+l;
    934  1.17       tls 
    935  1.43  junyoung 	for(pf = free_list.next; pf->end < ptr && pf->next != NULL;
    936  1.43  junyoung 	    pf = pf->next)
    937  1.17       tls 	    ; /* Race ahead here */
    938  1.17       tls 
    939  1.17       tls 	if (pf->page > tail) {
    940  1.17       tls 	    /* Insert before entry */
    941  1.17       tls 	    px->next = pf;
    942  1.17       tls 	    px->prev = pf->prev;
    943  1.17       tls 	    pf->prev = px;
    944  1.17       tls 	    px->prev->next = px;
    945  1.17       tls 	    pf = px;
    946  1.43  junyoung 	    px = NULL;
    947  1.17       tls 	} else if (pf->end == ptr ) {
    948  1.17       tls 	    /* Append to the previous entry */
    949  1.17       tls 	    pf->end = (char *)pf->end + l;
    950  1.17       tls 	    pf->size += l;
    951  1.43  junyoung 	    if (pf->next != NULL && pf->end == pf->next->page ) {
    952  1.17       tls 		/* And collapse the next too. */
    953  1.17       tls 		pt = pf->next;
    954  1.17       tls 		pf->end = pt->end;
    955  1.17       tls 		pf->size += pt->size;
    956  1.17       tls 		pf->next = pt->next;
    957  1.43  junyoung 		if (pf->next != NULL)
    958  1.17       tls 		    pf->next->prev = pf;
    959  1.17       tls 	    }
    960  1.17       tls 	} else if (pf->page == tail) {
    961  1.17       tls 	    /* Prepend to entry */
    962  1.17       tls 	    pf->size += l;
    963  1.17       tls 	    pf->page = ptr;
    964  1.43  junyoung 	} else if (pf->next == NULL) {
    965  1.17       tls 	    /* Append at tail of chain */
    966  1.43  junyoung 	    px->next = NULL;
    967  1.17       tls 	    px->prev = pf;
    968  1.17       tls 	    pf->next = px;
    969  1.17       tls 	    pf = px;
    970  1.43  junyoung 	    px = NULL;
    971  1.17       tls 	} else {
    972  1.17       tls 	    wrterror("freelist is destroyed.\n");
    973   1.1       cgd 	}
    974  1.17       tls     }
    975  1.17       tls 
    976  1.17       tls     /* Return something to OS ? */
    977  1.43  junyoung     if (pf->next == NULL &&			/* If we're the last one, */
    978  1.17       tls       pf->size > malloc_cache &&		/* ..and the cache is full, */
    979  1.17       tls       pf->end == malloc_brk &&			/* ..and none behind us, */
    980  1.34  christos       malloc_brk == sbrk((intptr_t)0)) {	/* ..and it's OK to do... */
    981  1.17       tls 
    982  1.17       tls 	/*
    983  1.17       tls 	 * Keep the cache intact.  Notice that the '>' above guarantees that
    984  1.17       tls 	 * the pf will always have at least one page afterwards.
    985  1.17       tls 	 */
    986  1.17       tls 	pf->end = (char *)pf->page + malloc_cache;
    987  1.17       tls 	pf->size = malloc_cache;
    988  1.17       tls 
    989  1.17       tls 	brk(pf->end);
    990  1.17       tls 	malloc_brk = pf->end;
    991  1.17       tls 
    992  1.23   thorpej 	idx = ptr2idx(pf->end);
    993  1.23   thorpej 	last_idx = idx - 1;
    994  1.17       tls 
    995  1.23   thorpej 	for(i=idx;i <= last_idx;)
    996  1.17       tls 	    page_dir[i++] = MALLOC_NOT_MINE;
    997  1.17       tls 
    998  1.17       tls 	/* XXX: We could realloc/shrink the pagedir here I guess. */
    999  1.17       tls     }
   1000  1.43  junyoung     if (pt != NULL)
   1001  1.17       tls 	ifree(pt);
   1002   1.1       cgd }
   1003   1.1       cgd 
   1004   1.1       cgd /*
   1005  1.17       tls  * Free a chunk, and possibly the page it's on, if the page becomes empty.
   1006   1.1       cgd  */
   1007  1.17       tls 
   1008  1.46     perry static inline void
   1009  1.30     enami free_bytes(void *ptr, size_t idx, struct pginfo *info)
   1010  1.17       tls {
   1011  1.30     enami     size_t i;
   1012  1.17       tls     struct pginfo **mp;
   1013  1.17       tls     void *vp;
   1014  1.17       tls 
   1015  1.17       tls     /* Find the chunk number on the page */
   1016  1.38  christos     i = ((size_t)(uintptr_t)ptr & malloc_pagemask) >> info->shift;
   1017  1.17       tls 
   1018  1.38  christos     if (((size_t)(uintptr_t)ptr & (info->size-1))) {
   1019  1.17       tls 	wrtwarning("modified (chunk-) pointer.\n");
   1020  1.17       tls 	return;
   1021  1.17       tls     }
   1022  1.17       tls 
   1023  1.17       tls     if (info->bits[i/MALLOC_BITS] & (1<<(i%MALLOC_BITS))) {
   1024  1.17       tls 	wrtwarning("chunk is already free.\n");
   1025  1.17       tls 	return;
   1026  1.17       tls     }
   1027  1.17       tls 
   1028  1.17       tls     if (malloc_junk)
   1029  1.30     enami 	memset(ptr, SOME_JUNK, (size_t)info->size);
   1030  1.17       tls 
   1031  1.17       tls     info->bits[i/MALLOC_BITS] |= 1<<(i%MALLOC_BITS);
   1032  1.17       tls     info->free++;
   1033  1.17       tls 
   1034  1.17       tls     mp = page_dir + info->shift;
   1035  1.17       tls 
   1036  1.17       tls     if (info->free == 1) {
   1037  1.17       tls 
   1038  1.17       tls 	/* Page became non-full */
   1039  1.17       tls 
   1040  1.17       tls 	mp = page_dir + info->shift;
   1041  1.17       tls 	/* Insert in address order */
   1042  1.17       tls 	while (*mp && (*mp)->next && (*mp)->next->page < info->page)
   1043  1.17       tls 	    mp = &(*mp)->next;
   1044  1.17       tls 	info->next = *mp;
   1045  1.17       tls 	*mp = info;
   1046  1.17       tls 	return;
   1047  1.17       tls     }
   1048  1.17       tls 
   1049  1.17       tls     if (info->free != info->total)
   1050  1.17       tls 	return;
   1051  1.17       tls 
   1052  1.17       tls     /* Find & remove this page in the queue */
   1053  1.17       tls     while (*mp != info) {
   1054  1.17       tls 	mp = &((*mp)->next);
   1055  1.19   thorpej #ifdef MALLOC_EXTRA_SANITY
   1056  1.17       tls 	if (!*mp)
   1057  1.43  junyoung 		wrterror("(ES): Not on queue.\n");
   1058  1.19   thorpej #endif /* MALLOC_EXTRA_SANITY */
   1059  1.17       tls     }
   1060  1.17       tls     *mp = info->next;
   1061  1.17       tls 
   1062  1.17       tls     /* Free the page & the info structure if need be */
   1063  1.30     enami     page_dir[idx] = MALLOC_FIRST;
   1064  1.17       tls     vp = info->page;		/* Order is important ! */
   1065  1.17       tls     if(vp != (void*)info)
   1066  1.17       tls 	ifree(info);
   1067  1.17       tls     ifree(vp);
   1068  1.17       tls }
   1069  1.17       tls 
   1070  1.17       tls static void
   1071  1.17       tls ifree(void *ptr)
   1072  1.17       tls {
   1073  1.17       tls     struct pginfo *info;
   1074  1.30     enami     size_t idx;
   1075  1.17       tls 
   1076  1.17       tls     /* This is legal */
   1077  1.43  junyoung     if (ptr == NULL)
   1078  1.17       tls 	return;
   1079  1.17       tls 
   1080  1.17       tls     /* If we're already sinking, don't make matters any worse. */
   1081  1.17       tls     if (suicide)
   1082  1.17       tls 	return;
   1083  1.17       tls 
   1084  1.23   thorpej     idx = ptr2idx(ptr);
   1085  1.17       tls 
   1086  1.23   thorpej     if (idx < malloc_pageshift) {
   1087  1.17       tls 	wrtwarning("junk pointer, too low to make sense.\n");
   1088  1.17       tls 	return;
   1089  1.17       tls     }
   1090  1.17       tls 
   1091  1.23   thorpej     if (idx > last_idx) {
   1092  1.17       tls 	wrtwarning("junk pointer, too high to make sense.\n");
   1093  1.17       tls 	return;
   1094  1.17       tls     }
   1095  1.17       tls 
   1096  1.23   thorpej     info = page_dir[idx];
   1097  1.17       tls 
   1098  1.17       tls     if (info < MALLOC_MAGIC)
   1099  1.23   thorpej         free_pages(ptr, idx, info);
   1100  1.17       tls     else
   1101  1.23   thorpej 	free_bytes(ptr, idx, info);
   1102  1.17       tls     return;
   1103  1.17       tls }
   1104  1.17       tls 
   1105  1.17       tls /*
   1106  1.17       tls  * These are the public exported interface routines.
   1107  1.17       tls  */
   1108  1.17       tls 
   1109  1.17       tls 
   1110  1.17       tls void *
   1111  1.17       tls malloc(size_t size)
   1112  1.17       tls {
   1113  1.43  junyoung     void *r;
   1114  1.17       tls 
   1115  1.17       tls     THREAD_LOCK();
   1116  1.17       tls     malloc_func = " in malloc():";
   1117  1.17       tls     if (malloc_active++) {
   1118  1.17       tls 	wrtwarning("recursive call.\n");
   1119  1.17       tls         malloc_active--;
   1120  1.40     chris 	THREAD_UNLOCK();
   1121  1.43  junyoung 	return (NULL);
   1122  1.17       tls     }
   1123  1.17       tls     if (!malloc_started)
   1124  1.17       tls 	malloc_init();
   1125  1.17       tls     if (malloc_sysv && !size)
   1126  1.43  junyoung 	r = NULL;
   1127  1.17       tls     else
   1128  1.17       tls 	r = imalloc(size);
   1129  1.17       tls     UTRACE(0, size, r);
   1130  1.17       tls     malloc_active--;
   1131  1.17       tls     THREAD_UNLOCK();
   1132  1.26    kleink     if (r == NULL && (size != 0 || !malloc_sysv)) {
   1133  1.24   thorpej 	if (malloc_xmalloc)
   1134  1.24   thorpej 	    wrterror("out of memory.\n");
   1135  1.24   thorpej 	errno = ENOMEM;
   1136  1.24   thorpej     }
   1137  1.17       tls     return (r);
   1138  1.17       tls }
   1139  1.17       tls 
   1140   1.9  christos void
   1141  1.17       tls free(void *ptr)
   1142   1.1       cgd {
   1143  1.17       tls     THREAD_LOCK();
   1144  1.17       tls     malloc_func = " in free():";
   1145  1.17       tls     if (malloc_active++) {
   1146  1.17       tls 	wrtwarning("recursive call.\n");
   1147  1.17       tls 	malloc_active--;
   1148  1.38  christos 	THREAD_UNLOCK();
   1149  1.17       tls 	return;
   1150  1.17       tls     }
   1151  1.42    itojun     if (!malloc_started)
   1152  1.42    itojun 	malloc_init();
   1153  1.42    itojun     ifree(ptr);
   1154  1.42    itojun     UTRACE(ptr, 0, 0);
   1155  1.17       tls     malloc_active--;
   1156  1.17       tls     THREAD_UNLOCK();
   1157  1.17       tls     return;
   1158  1.17       tls }
   1159  1.17       tls 
   1160  1.17       tls void *
   1161  1.17       tls realloc(void *ptr, size_t size)
   1162  1.17       tls {
   1163  1.43  junyoung     void *r;
   1164  1.17       tls 
   1165  1.17       tls     THREAD_LOCK();
   1166  1.17       tls     malloc_func = " in realloc():";
   1167  1.17       tls     if (malloc_active++) {
   1168  1.17       tls 	wrtwarning("recursive call.\n");
   1169  1.17       tls         malloc_active--;
   1170  1.40     chris 	THREAD_UNLOCK();
   1171  1.43  junyoung 	return (NULL);
   1172  1.17       tls     }
   1173  1.17       tls     if (!malloc_started)
   1174  1.17       tls 	malloc_init();
   1175  1.17       tls     if (malloc_sysv && !size) {
   1176  1.17       tls 	ifree(ptr);
   1177  1.43  junyoung 	r = NULL;
   1178  1.17       tls     } else if (!ptr) {
   1179  1.17       tls 	r = imalloc(size);
   1180  1.17       tls     } else {
   1181  1.17       tls         r = irealloc(ptr, size);
   1182  1.17       tls     }
   1183  1.17       tls     UTRACE(ptr, size, r);
   1184  1.17       tls     malloc_active--;
   1185  1.17       tls     THREAD_UNLOCK();
   1186  1.26    kleink     if (r == NULL && (size != 0 || !malloc_sysv)) {
   1187  1.24   thorpej 	if (malloc_xmalloc)
   1188  1.24   thorpej 	    wrterror("out of memory.\n");
   1189  1.24   thorpej 	errno = ENOMEM;
   1190  1.24   thorpej     }
   1191  1.17       tls     return (r);
   1192   1.1       cgd }
   1193