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