Home | History | Annotate | Line # | Download | only in libbsdmalloc
malloc.c revision 1.3
      1  1.3  riastrad /*	$NetBSD: malloc.c,v 1.3 2023/07/04 15:08:55 riastradh Exp $	*/
      2  1.1     elric 
      3  1.1     elric /*
      4  1.1     elric  * Copyright (c) 1983, 1993
      5  1.1     elric  *	The Regents of the University of California.  All rights reserved.
      6  1.1     elric  *
      7  1.1     elric  * Redistribution and use in source and binary forms, with or without
      8  1.1     elric  * modification, are permitted provided that the following conditions
      9  1.1     elric  * are met:
     10  1.1     elric  * 1. Redistributions of source code must retain the above copyright
     11  1.1     elric  *    notice, this list of conditions and the following disclaimer.
     12  1.1     elric  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1     elric  *    notice, this list of conditions and the following disclaimer in the
     14  1.1     elric  *    documentation and/or other materials provided with the distribution.
     15  1.2       agc  * 3. Neither the name of the University nor the names of its contributors
     16  1.1     elric  *    may be used to endorse or promote products derived from this software
     17  1.1     elric  *    without specific prior written permission.
     18  1.1     elric  *
     19  1.1     elric  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.1     elric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1     elric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1     elric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.1     elric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1     elric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1     elric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1     elric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1     elric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1     elric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1     elric  * SUCH DAMAGE.
     30  1.1     elric  */
     31  1.1     elric 
     32  1.1     elric #include <sys/cdefs.h>
     33  1.1     elric #if defined(LIBC_SCCS) && !defined(lint)
     34  1.1     elric #if 0
     35  1.1     elric static char sccsid[] = "@(#)malloc.c	8.1 (Berkeley) 6/4/93";
     36  1.1     elric #else
     37  1.3  riastrad __RCSID("$NetBSD: malloc.c,v 1.3 2023/07/04 15:08:55 riastradh Exp $");
     38  1.1     elric #endif
     39  1.1     elric #endif /* LIBC_SCCS and not lint */
     40  1.1     elric 
     41  1.1     elric /*
     42  1.1     elric  * malloc.c (Caltech) 2/21/82
     43  1.1     elric  * Chris Kingsley, kingsley@cit-20.
     44  1.1     elric  *
     45  1.1     elric  * This is a very fast storage allocator.  It allocates blocks of a small
     46  1.1     elric  * number of different sizes, and keeps free lists of each size.  Blocks that
     47  1.1     elric  * don't exactly fit are passed up to the next larger size.  In this
     48  1.1     elric  * implementation, the available sizes are 2^n-4 (or 2^n-10) bytes long.
     49  1.1     elric  * This is designed for use in a virtual memory environment.
     50  1.1     elric  */
     51  1.1     elric 
     52  1.1     elric #include <sys/types.h>
     53  1.1     elric #if defined(DEBUG) || defined(RCHECK)
     54  1.1     elric #include <sys/uio.h>
     55  1.1     elric #endif
     56  1.3  riastrad 
     57  1.3  riastrad #include <errno.h>
     58  1.3  riastrad #include <limits.h>
     59  1.3  riastrad #include <stddef.h>
     60  1.3  riastrad #include <stdint.h>
     61  1.1     elric #if defined(RCHECK) || defined(MSTATS)
     62  1.1     elric #include <stdio.h>
     63  1.1     elric #endif
     64  1.1     elric #include <stdlib.h>
     65  1.1     elric #include <string.h>
     66  1.1     elric #include <unistd.h>
     67  1.3  riastrad 
     68  1.1     elric #include "reentrant.h"
     69  1.1     elric 
     70  1.1     elric 
     71  1.1     elric /*
     72  1.1     elric  * The overhead on a block is at least 4 bytes.  When free, this space
     73  1.1     elric  * contains a pointer to the next free block, and the bottom two bits must
     74  1.1     elric  * be zero.  When in use, the first byte is set to MAGIC, and the second
     75  1.1     elric  * byte is the size index.  The remaining bytes are for alignment.
     76  1.1     elric  * If range checking is enabled then a second word holds the size of the
     77  1.1     elric  * requested block, less 1, rounded up to a multiple of sizeof(RMAGIC).
     78  1.1     elric  * The order of elements is critical: ov_magic must overlay the low order
     79  1.1     elric  * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern.
     80  1.1     elric  */
     81  1.1     elric union	overhead {
     82  1.1     elric 	union	overhead *ov_next;	/* when free */
     83  1.1     elric 	struct {
     84  1.1     elric 		u_char	ovu_magic;	/* magic number */
     85  1.1     elric 		u_char	ovu_index;	/* bucket # */
     86  1.1     elric #ifdef RCHECK
     87  1.1     elric 		u_short	ovu_rmagic;	/* range magic number */
     88  1.1     elric 		u_long	ovu_size;	/* actual block size */
     89  1.1     elric #endif
     90  1.1     elric 	} ovu;
     91  1.1     elric #define	ov_magic	ovu.ovu_magic
     92  1.1     elric #define	ov_index	ovu.ovu_index
     93  1.1     elric #define	ov_rmagic	ovu.ovu_rmagic
     94  1.1     elric #define	ov_size		ovu.ovu_size
     95  1.1     elric };
     96  1.1     elric 
     97  1.1     elric #define	MAGIC		0xef		/* magic # on accounting info */
     98  1.1     elric #ifdef RCHECK
     99  1.1     elric #define RMAGIC		0x5555		/* magic # on range info */
    100  1.1     elric #endif
    101  1.1     elric 
    102  1.1     elric #ifdef RCHECK
    103  1.1     elric #define	RSLOP		sizeof (u_short)
    104  1.1     elric #else
    105  1.1     elric #define	RSLOP		0
    106  1.1     elric #endif
    107  1.1     elric 
    108  1.1     elric /*
    109  1.1     elric  * nextf[i] is the pointer to the next free block of size 2^(i+3).  The
    110  1.1     elric  * smallest allocatable block is 8 bytes.  The overhead information
    111  1.1     elric  * precedes the data area returned to the user.
    112  1.1     elric  */
    113  1.1     elric #define	NBUCKETS 30
    114  1.1     elric static	union overhead *nextf[NBUCKETS];
    115  1.1     elric 
    116  1.1     elric static	long pagesz;			/* page size */
    117  1.1     elric static	int pagebucket;			/* page size bucket */
    118  1.1     elric 
    119  1.1     elric #ifdef MSTATS
    120  1.1     elric /*
    121  1.1     elric  * nmalloc[i] is the difference between the number of mallocs and frees
    122  1.1     elric  * for a given block size.
    123  1.1     elric  */
    124  1.1     elric static	u_int nmalloc[NBUCKETS];
    125  1.1     elric #endif
    126  1.1     elric 
    127  1.1     elric #ifdef _REENT
    128  1.1     elric static	mutex_t malloc_mutex = MUTEX_INITIALIZER;
    129  1.1     elric #endif
    130  1.1     elric 
    131  1.1     elric static void morecore __P((int));
    132  1.1     elric static int findbucket __P((union overhead *, int));
    133  1.1     elric #ifdef MSTATS
    134  1.1     elric void mstats __P((const char *));
    135  1.1     elric #endif
    136  1.1     elric 
    137  1.1     elric #if defined(DEBUG) || defined(RCHECK)
    138  1.1     elric #define	ASSERT(p)   if (!(p)) botch(__STRING(p))
    139  1.1     elric 
    140  1.1     elric static void botch __P((const char *));
    141  1.1     elric 
    142  1.1     elric /*
    143  1.1     elric  * NOTE: since this may be called while malloc_mutex is locked, stdio must not
    144  1.1     elric  *       be used in this function.
    145  1.1     elric  */
    146  1.1     elric static void
    147  1.1     elric botch(s)
    148  1.1     elric 	const char *s;
    149  1.1     elric {
    150  1.1     elric 	struct iovec iov[3];
    151  1.1     elric 
    152  1.1     elric 	iov[0].iov_base	= "\nassertion botched: ";
    153  1.1     elric 	iov[0].iov_len	= 20;
    154  1.1     elric 	iov[1].iov_base	= (void *)s;
    155  1.1     elric 	iov[1].iov_len	= strlen(s);
    156  1.1     elric 	iov[2].iov_base	= "\n";
    157  1.1     elric 	iov[2].iov_len	= 1;
    158  1.1     elric 
    159  1.1     elric 	/*
    160  1.1     elric 	 * This place deserves a word of warning: a cancellation point will
    161  1.1     elric 	 * occur when executing writev(), and we might be still owning
    162  1.1     elric 	 * malloc_mutex.  At this point we need to disable cancellation
    163  1.1     elric 	 * until `after' abort() because i) establishing a cancellation handler
    164  1.1     elric 	 * might, depending on the implementation, result in another malloc()
    165  1.1     elric 	 * to be executed, and ii) it is really not desirable to let execution
    166  1.1     elric 	 * continue.  `Fix me.'
    167  1.1     elric 	 *
    168  1.1     elric 	 * Note that holding mutex_lock during abort() is safe.
    169  1.1     elric 	 */
    170  1.1     elric 
    171  1.1     elric 	(void)writev(STDERR_FILENO, iov, 3);
    172  1.1     elric 	abort();
    173  1.1     elric }
    174  1.1     elric #else
    175  1.3  riastrad #define	ASSERT(p)	((void)sizeof((long)(p)))
    176  1.1     elric #endif
    177  1.1     elric 
    178  1.1     elric void *
    179  1.1     elric malloc(nbytes)
    180  1.1     elric 	size_t nbytes;
    181  1.1     elric {
    182  1.1     elric   	union overhead *op;
    183  1.1     elric 	int bucket;
    184  1.1     elric   	long n;
    185  1.1     elric 	unsigned amt;
    186  1.1     elric 
    187  1.1     elric 	mutex_lock(&malloc_mutex);
    188  1.1     elric 
    189  1.1     elric 	/*
    190  1.1     elric 	 * First time malloc is called, setup page size and
    191  1.1     elric 	 * align break pointer so all data will be page aligned.
    192  1.1     elric 	 */
    193  1.1     elric 	if (pagesz == 0) {
    194  1.1     elric 		pagesz = n = getpagesize();
    195  1.1     elric 		ASSERT(pagesz > 0);
    196  1.1     elric 		op = (union overhead *)(void *)sbrk(0);
    197  1.1     elric   		n = n - sizeof (*op) - ((long)op & (n - 1));
    198  1.1     elric 		if (n < 0)
    199  1.1     elric 			n += pagesz;
    200  1.1     elric 		if (n) {
    201  1.1     elric 			if (sbrk((int)n) == (void *)-1) {
    202  1.1     elric 				mutex_unlock(&malloc_mutex);
    203  1.1     elric 				return (NULL);
    204  1.1     elric 			}
    205  1.1     elric 		}
    206  1.1     elric 		bucket = 0;
    207  1.1     elric 		amt = 8;
    208  1.1     elric 		while (pagesz > amt) {
    209  1.1     elric 			amt <<= 1;
    210  1.1     elric 			bucket++;
    211  1.1     elric 		}
    212  1.1     elric 		pagebucket = bucket;
    213  1.1     elric 	}
    214  1.1     elric 	/*
    215  1.1     elric 	 * Convert amount of memory requested into closest block size
    216  1.1     elric 	 * stored in hash buckets which satisfies request.
    217  1.1     elric 	 * Account for space used per block for accounting.
    218  1.1     elric 	 */
    219  1.1     elric 	if (nbytes <= (n = pagesz - sizeof (*op) - RSLOP)) {
    220  1.1     elric #ifndef RCHECK
    221  1.1     elric 		amt = 8;	/* size of first bucket */
    222  1.1     elric 		bucket = 0;
    223  1.1     elric #else
    224  1.1     elric 		amt = 16;	/* size of first bucket */
    225  1.1     elric 		bucket = 1;
    226  1.1     elric #endif
    227  1.1     elric 		n = -((long)sizeof (*op) + RSLOP);
    228  1.1     elric 	} else {
    229  1.1     elric 		amt = (unsigned)pagesz;
    230  1.1     elric 		bucket = pagebucket;
    231  1.1     elric 	}
    232  1.1     elric 	while (nbytes > amt + n) {
    233  1.1     elric 		amt <<= 1;
    234  1.1     elric 		if (amt == 0)
    235  1.1     elric 			return (NULL);
    236  1.1     elric 		bucket++;
    237  1.1     elric 	}
    238  1.1     elric 	/*
    239  1.1     elric 	 * If nothing in hash bucket right now,
    240  1.1     elric 	 * request more memory from the system.
    241  1.1     elric 	 */
    242  1.1     elric   	if ((op = nextf[bucket]) == NULL) {
    243  1.1     elric   		morecore(bucket);
    244  1.1     elric   		if ((op = nextf[bucket]) == NULL) {
    245  1.1     elric 			mutex_unlock(&malloc_mutex);
    246  1.1     elric   			return (NULL);
    247  1.1     elric 		}
    248  1.1     elric 	}
    249  1.1     elric 	/* remove from linked list */
    250  1.1     elric   	nextf[bucket] = op->ov_next;
    251  1.1     elric 	op->ov_magic = MAGIC;
    252  1.1     elric 	op->ov_index = bucket;
    253  1.1     elric #ifdef MSTATS
    254  1.1     elric   	nmalloc[bucket]++;
    255  1.1     elric #endif
    256  1.1     elric 	mutex_unlock(&malloc_mutex);
    257  1.1     elric #ifdef RCHECK
    258  1.1     elric 	/*
    259  1.1     elric 	 * Record allocated size of block and
    260  1.1     elric 	 * bound space with magic numbers.
    261  1.1     elric 	 */
    262  1.1     elric 	op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
    263  1.1     elric 	op->ov_rmagic = RMAGIC;
    264  1.1     elric   	*(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
    265  1.1     elric #endif
    266  1.1     elric   	return ((void *)(op + 1));
    267  1.1     elric }
    268  1.1     elric 
    269  1.1     elric /*
    270  1.1     elric  * Allocate more memory to the indicated bucket.
    271  1.1     elric  */
    272  1.1     elric static void
    273  1.1     elric morecore(bucket)
    274  1.1     elric 	int bucket;
    275  1.1     elric {
    276  1.1     elric   	union overhead *op;
    277  1.1     elric 	long sz;		/* size of desired block */
    278  1.1     elric   	long amt;			/* amount to allocate */
    279  1.1     elric   	long nblks;			/* how many blocks we get */
    280  1.1     elric 
    281  1.1     elric 	/*
    282  1.1     elric 	 * sbrk_size <= 0 only for big, FLUFFY, requests (about
    283  1.1     elric 	 * 2^30 bytes on a VAX, I think) or for a negative arg.
    284  1.1     elric 	 */
    285  1.1     elric 	sz = 1 << (bucket + 3);
    286  1.1     elric #ifdef DEBUG
    287  1.1     elric 	ASSERT(sz > 0);
    288  1.1     elric #else
    289  1.1     elric 	if (sz <= 0)
    290  1.1     elric 		return;
    291  1.1     elric #endif
    292  1.1     elric 	if (sz < pagesz) {
    293  1.1     elric 		amt = pagesz;
    294  1.1     elric   		nblks = amt / sz;
    295  1.1     elric 	} else {
    296  1.1     elric 		amt = sz + pagesz;
    297  1.1     elric 		nblks = 1;
    298  1.1     elric 	}
    299  1.1     elric 	op = (union overhead *)(void *)sbrk((int)amt);
    300  1.1     elric 	/* no more room! */
    301  1.1     elric   	if ((long)op == -1)
    302  1.1     elric   		return;
    303  1.1     elric 	/*
    304  1.1     elric 	 * Add new memory allocated to that on
    305  1.1     elric 	 * free list for this hash bucket.
    306  1.1     elric 	 */
    307  1.1     elric   	nextf[bucket] = op;
    308  1.1     elric   	while (--nblks > 0) {
    309  1.1     elric 		op->ov_next =
    310  1.1     elric 		    (union overhead *)(void *)((caddr_t)(void *)op+(size_t)sz);
    311  1.1     elric 		op = op->ov_next;
    312  1.1     elric   	}
    313  1.1     elric }
    314  1.1     elric 
    315  1.1     elric void
    316  1.1     elric free(cp)
    317  1.1     elric 	void *cp;
    318  1.1     elric {
    319  1.1     elric 	long size;
    320  1.1     elric 	union overhead *op;
    321  1.1     elric 
    322  1.1     elric   	if (cp == NULL)
    323  1.1     elric   		return;
    324  1.1     elric 	op = (union overhead *)(void *)((caddr_t)cp - sizeof (union overhead));
    325  1.1     elric #ifdef DEBUG
    326  1.1     elric   	ASSERT(op->ov_magic == MAGIC);		/* make sure it was in use */
    327  1.1     elric #else
    328  1.1     elric 	if (op->ov_magic != MAGIC)
    329  1.1     elric 		return;				/* sanity */
    330  1.1     elric #endif
    331  1.1     elric #ifdef RCHECK
    332  1.1     elric   	ASSERT(op->ov_rmagic == RMAGIC);
    333  1.1     elric 	ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC);
    334  1.1     elric #endif
    335  1.1     elric   	size = op->ov_index;
    336  1.1     elric   	ASSERT(size < NBUCKETS);
    337  1.1     elric 	mutex_lock(&malloc_mutex);
    338  1.1     elric 	op->ov_next = nextf[(unsigned int)size];/* also clobbers ov_magic */
    339  1.1     elric   	nextf[(unsigned int)size] = op;
    340  1.1     elric #ifdef MSTATS
    341  1.1     elric   	nmalloc[(size_t)size]--;
    342  1.1     elric #endif
    343  1.1     elric 	mutex_unlock(&malloc_mutex);
    344  1.1     elric }
    345  1.1     elric 
    346  1.1     elric /*
    347  1.1     elric  * When a program attempts "storage compaction" as mentioned in the
    348  1.1     elric  * old malloc man page, it realloc's an already freed block.  Usually
    349  1.1     elric  * this is the last block it freed; occasionally it might be farther
    350  1.1     elric  * back.  We have to search all the free lists for the block in order
    351  1.1     elric  * to determine its bucket: 1st we make one pass thru the lists
    352  1.1     elric  * checking only the first block in each; if that fails we search
    353  1.1     elric  * ``__realloc_srchlen'' blocks in each list for a match (the variable
    354  1.1     elric  * is extern so the caller can modify it).  If that fails we just copy
    355  1.1     elric  * however many bytes was given to realloc() and hope it's not huge.
    356  1.1     elric  */
    357  1.1     elric int __realloc_srchlen = 4;	/* 4 should be plenty, -1 =>'s whole list */
    358  1.1     elric 
    359  1.1     elric void *
    360  1.1     elric realloc(cp, nbytes)
    361  1.1     elric 	void *cp;
    362  1.1     elric 	size_t nbytes;
    363  1.1     elric {
    364  1.1     elric   	u_long onb;
    365  1.1     elric 	long i;
    366  1.1     elric 	union overhead *op;
    367  1.1     elric 	char *res;
    368  1.1     elric 	int was_alloced = 0;
    369  1.1     elric 
    370  1.1     elric   	if (cp == NULL)
    371  1.1     elric   		return (malloc(nbytes));
    372  1.1     elric 	if (nbytes == 0) {
    373  1.1     elric 		free (cp);
    374  1.1     elric 		return (NULL);
    375  1.1     elric 	}
    376  1.1     elric 	op = (union overhead *)(void *)((caddr_t)cp - sizeof (union overhead));
    377  1.1     elric 	mutex_lock(&malloc_mutex);
    378  1.1     elric 	if (op->ov_magic == MAGIC) {
    379  1.1     elric 		was_alloced++;
    380  1.1     elric 		i = op->ov_index;
    381  1.1     elric 	} else {
    382  1.1     elric 		/*
    383  1.1     elric 		 * Already free, doing "compaction".
    384  1.1     elric 		 *
    385  1.1     elric 		 * Search for the old block of memory on the
    386  1.1     elric 		 * free list.  First, check the most common
    387  1.1     elric 		 * case (last element free'd), then (this failing)
    388  1.1     elric 		 * the last ``__realloc_srchlen'' items free'd.
    389  1.1     elric 		 * If all lookups fail, then assume the size of
    390  1.1     elric 		 * the memory block being realloc'd is the
    391  1.1     elric 		 * largest possible (so that all "nbytes" of new
    392  1.1     elric 		 * memory are copied into).  Note that this could cause
    393  1.1     elric 		 * a memory fault if the old area was tiny, and the moon
    394  1.1     elric 		 * is gibbous.  However, that is very unlikely.
    395  1.1     elric 		 */
    396  1.1     elric 		if ((i = findbucket(op, 1)) < 0 &&
    397  1.1     elric 		    (i = findbucket(op, __realloc_srchlen)) < 0)
    398  1.1     elric 			i = NBUCKETS;
    399  1.1     elric 	}
    400  1.1     elric 	onb = (u_long)1 << (u_long)(i + 3);
    401  1.1     elric 	if (onb < pagesz)
    402  1.1     elric 		onb -= sizeof (*op) + RSLOP;
    403  1.1     elric 	else
    404  1.1     elric 		onb += pagesz - sizeof (*op) - RSLOP;
    405  1.1     elric 	/* avoid the copy if same size block */
    406  1.1     elric 	if (was_alloced) {
    407  1.1     elric 		if (i) {
    408  1.1     elric 			i = (long)1 << (long)(i + 2);
    409  1.1     elric 			if (i < pagesz)
    410  1.1     elric 				i -= sizeof (*op) + RSLOP;
    411  1.1     elric 			else
    412  1.1     elric 				i += pagesz - sizeof (*op) - RSLOP;
    413  1.1     elric 		}
    414  1.1     elric 		if (nbytes <= onb && nbytes > i) {
    415  1.1     elric #ifdef RCHECK
    416  1.1     elric 			op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
    417  1.1     elric 			*(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
    418  1.1     elric #endif
    419  1.1     elric 			mutex_unlock(&malloc_mutex);
    420  1.1     elric 			return (cp);
    421  1.1     elric 
    422  1.1     elric 		}
    423  1.1     elric #ifndef _REENT
    424  1.1     elric 		else
    425  1.1     elric 			free(cp);
    426  1.1     elric #endif
    427  1.1     elric 	}
    428  1.1     elric 	mutex_unlock(&malloc_mutex);
    429  1.1     elric 	if ((res = malloc(nbytes)) == NULL) {
    430  1.1     elric #ifdef _REENT
    431  1.1     elric 		free(cp);
    432  1.1     elric #endif
    433  1.1     elric 		return (NULL);
    434  1.1     elric 	}
    435  1.1     elric #ifndef _REENT
    436  1.1     elric 	if (cp != res)		/* common optimization if "compacting" */
    437  1.1     elric 		(void)memmove(res, cp, (size_t)((nbytes < onb) ? nbytes : onb));
    438  1.1     elric #else
    439  1.1     elric 	(void)memmove(res, cp, (size_t)((nbytes < onb) ? nbytes : onb));
    440  1.1     elric 	free(cp);
    441  1.1     elric #endif
    442  1.1     elric   	return (res);
    443  1.1     elric }
    444  1.1     elric 
    445  1.1     elric /*
    446  1.1     elric  * Search ``srchlen'' elements of each free list for a block whose
    447  1.1     elric  * header starts at ``freep''.  If srchlen is -1 search the whole list.
    448  1.1     elric  * Return bucket number, or -1 if not found.
    449  1.1     elric  */
    450  1.1     elric static int
    451  1.1     elric findbucket(freep, srchlen)
    452  1.1     elric 	union overhead *freep;
    453  1.1     elric 	int srchlen;
    454  1.1     elric {
    455  1.1     elric 	union overhead *p;
    456  1.1     elric 	int i, j;
    457  1.1     elric 
    458  1.1     elric 	for (i = 0; i < NBUCKETS; i++) {
    459  1.1     elric 		j = 0;
    460  1.1     elric 		for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
    461  1.1     elric 			if (p == freep)
    462  1.1     elric 				return (i);
    463  1.1     elric 			j++;
    464  1.1     elric 		}
    465  1.1     elric 	}
    466  1.1     elric 	return (-1);
    467  1.1     elric }
    468  1.1     elric 
    469  1.1     elric #ifdef MSTATS
    470  1.1     elric /*
    471  1.1     elric  * mstats - print out statistics about malloc
    472  1.1     elric  *
    473  1.1     elric  * Prints two lines of numbers, one showing the length of the free list
    474  1.1     elric  * for each size category, the second showing the number of mallocs -
    475  1.1     elric  * frees for each size category.
    476  1.1     elric  */
    477  1.1     elric void
    478  1.1     elric mstats(s)
    479  1.1     elric 	char *s;
    480  1.1     elric {
    481  1.1     elric   	int i, j;
    482  1.1     elric   	union overhead *p;
    483  1.1     elric   	int totfree = 0,
    484  1.1     elric   	totused = 0;
    485  1.1     elric 
    486  1.1     elric   	fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s);
    487  1.1     elric   	for (i = 0; i < NBUCKETS; i++) {
    488  1.1     elric   		for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
    489  1.1     elric   			;
    490  1.1     elric   		fprintf(stderr, " %d", j);
    491  1.1     elric   		totfree += j * (1 << (i + 3));
    492  1.1     elric   	}
    493  1.1     elric   	fprintf(stderr, "\nused:\t");
    494  1.1     elric   	for (i = 0; i < NBUCKETS; i++) {
    495  1.1     elric   		fprintf(stderr, " %d", nmalloc[i]);
    496  1.1     elric   		totused += nmalloc[i] * (1 << (i + 3));
    497  1.1     elric   	}
    498  1.1     elric   	fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n",
    499  1.1     elric 	    totused, totfree);
    500  1.1     elric }
    501  1.1     elric #endif
    502  1.3  riastrad 
    503  1.3  riastrad /*
    504  1.3  riastrad  * Additional front ends:
    505  1.3  riastrad  * - aligned_alloc (C11)
    506  1.3  riastrad  * - calloc(n,m) = malloc(n*m) without overflow
    507  1.3  riastrad  * - posix_memalign (POSIX)
    508  1.3  riastrad  *
    509  1.3  riastrad  * These must all be in the same compilation unit as malloc, realloc,
    510  1.3  riastrad  * and free (or -lbsdmalloc must be surrounded by -Wl,--whole-archive
    511  1.3  riastrad  * -lbsdmalloc -Wl,--no-whole-archive) in order to override the libc
    512  1.3  riastrad  * built-in malloc implementation.
    513  1.3  riastrad  *
    514  1.3  riastrad  * Allocations of size n, up to and including the page size, are
    515  1.3  riastrad  * already aligned by malloc on multiples of n.  Larger alignment is
    516  1.3  riastrad  * not supported.
    517  1.3  riastrad  */
    518  1.3  riastrad 
    519  1.3  riastrad static long __constfunc
    520  1.3  riastrad cachedpagesize(void)
    521  1.3  riastrad {
    522  1.3  riastrad 	long n;
    523  1.3  riastrad 
    524  1.3  riastrad 	/* XXX atomic_load_relaxed, but that's not defined in userland atm */
    525  1.3  riastrad 	if (__predict_false((n = pagesz) == 0)) {
    526  1.3  riastrad 		mutex_lock(&malloc_mutex);
    527  1.3  riastrad 		if ((n = pagesz) == 0)
    528  1.3  riastrad 			n = pagesz = getpagesize();
    529  1.3  riastrad 		mutex_unlock(&malloc_mutex);
    530  1.3  riastrad 	}
    531  1.3  riastrad 
    532  1.3  riastrad 	return n;
    533  1.3  riastrad }
    534  1.3  riastrad 
    535  1.3  riastrad void *
    536  1.3  riastrad aligned_alloc(size_t alignment, size_t size)
    537  1.3  riastrad {
    538  1.3  riastrad 	char *p;
    539  1.3  riastrad 
    540  1.3  riastrad 	if (alignment == 0 ||
    541  1.3  riastrad 	    (alignment & (alignment - 1)) != 0 ||
    542  1.3  riastrad 	    alignment > cachedpagesize() ||
    543  1.3  riastrad 	    (size & (alignment - 1)) != 0) {
    544  1.3  riastrad 		errno = EINVAL;
    545  1.3  riastrad 		return NULL;
    546  1.3  riastrad 	}
    547  1.3  riastrad 	p = malloc(size);
    548  1.3  riastrad 	if (__predict_false(p == NULL))
    549  1.3  riastrad 		ASSERT((uintptr_t)p % alignment == 0);
    550  1.3  riastrad 	return p;
    551  1.3  riastrad }
    552  1.3  riastrad 
    553  1.3  riastrad void *
    554  1.3  riastrad calloc(size_t nmemb, size_t size)
    555  1.3  riastrad {
    556  1.3  riastrad 	void *p;
    557  1.3  riastrad 	size_t n;
    558  1.3  riastrad 
    559  1.3  riastrad 	if (__builtin_mul_overflow_p(nmemb, size, (size_t)0)) {
    560  1.3  riastrad 		errno = ENOMEM;
    561  1.3  riastrad 		return NULL;
    562  1.3  riastrad 	}
    563  1.3  riastrad 	n = nmemb * size;
    564  1.3  riastrad 	p = malloc(n);
    565  1.3  riastrad 	if (__predict_false(p == NULL))
    566  1.3  riastrad 		return NULL;
    567  1.3  riastrad 	memset(p, 0, n);
    568  1.3  riastrad 	return p;
    569  1.3  riastrad }
    570  1.3  riastrad 
    571  1.3  riastrad int
    572  1.3  riastrad posix_memalign(void **memptr, size_t alignment, size_t size)
    573  1.3  riastrad {
    574  1.3  riastrad 	char *p;
    575  1.3  riastrad 
    576  1.3  riastrad 	if (alignment < sizeof(void *) ||
    577  1.3  riastrad 	    (alignment & (alignment - 1)) != 0 ||
    578  1.3  riastrad 	    alignment > cachedpagesize())
    579  1.3  riastrad 		return EINVAL;
    580  1.3  riastrad 	p = malloc(size < alignment ? alignment : size);
    581  1.3  riastrad 	if (__predict_false(p == NULL))
    582  1.3  riastrad 		return ENOMEM;
    583  1.3  riastrad 	ASSERT((uintptr_t)p % alignment == 0);
    584  1.3  riastrad 	*memptr = p;
    585  1.3  riastrad 	return 0;
    586  1.3  riastrad }
    587  1.3  riastrad 
    588  1.3  riastrad /*
    589  1.3  riastrad  * libc hooks required by fork
    590  1.3  riastrad  */
    591  1.3  riastrad 
    592  1.3  riastrad #include "../libc/include/extern.h"
    593  1.3  riastrad 
    594  1.3  riastrad void
    595  1.3  riastrad _malloc_prefork(void)
    596  1.3  riastrad {
    597  1.3  riastrad 
    598  1.3  riastrad 	mutex_lock(&malloc_mutex);
    599  1.3  riastrad }
    600  1.3  riastrad 
    601  1.3  riastrad void
    602  1.3  riastrad _malloc_postfork(void)
    603  1.3  riastrad {
    604  1.3  riastrad 
    605  1.3  riastrad 	mutex_unlock(&malloc_mutex);
    606  1.3  riastrad }
    607  1.3  riastrad 
    608  1.3  riastrad void
    609  1.3  riastrad _malloc_postfork_child(void)
    610  1.3  riastrad {
    611  1.3  riastrad 
    612  1.3  riastrad 	mutex_unlock(&malloc_mutex);
    613  1.3  riastrad }
    614