malloc.c revision 154248
134192Sjdp/*-
234192Sjdp * Copyright (c) 1983 Regents of the University of California.
334192Sjdp * All rights reserved.
434192Sjdp *
534192Sjdp * Redistribution and use in source and binary forms, with or without
634192Sjdp * modification, are permitted provided that the following conditions
734192Sjdp * are met:
834192Sjdp * 1. Redistributions of source code must retain the above copyright
934192Sjdp *    notice, this list of conditions and the following disclaimer.
1034192Sjdp * 2. Redistributions in binary form must reproduce the above copyright
1134192Sjdp *    notice, this list of conditions and the following disclaimer in the
1234192Sjdp *    documentation and/or other materials provided with the distribution.
1334192Sjdp * 3. All advertising materials mentioning features or use of this software
1434192Sjdp *    must display the following acknowledgement:
1534192Sjdp *	This product includes software developed by the University of
1634192Sjdp *	California, Berkeley and its contributors.
1734192Sjdp * 4. Neither the name of the University nor the names of its contributors
1834192Sjdp *    may be used to endorse or promote products derived from this software
1934192Sjdp *    without specific prior written permission.
2034192Sjdp *
2134192Sjdp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2234192Sjdp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2334192Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2434192Sjdp * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2534192Sjdp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2634192Sjdp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2734192Sjdp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2834192Sjdp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2934192Sjdp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3034192Sjdp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3134192Sjdp * SUCH DAMAGE.
3234192Sjdp */
3334192Sjdp
3434192Sjdp#if defined(LIBC_SCCS) && !defined(lint)
3534192Sjdp/*static char *sccsid = "from: @(#)malloc.c	5.11 (Berkeley) 2/23/91";*/
3650476Speterstatic char *rcsid = "$FreeBSD: head/libexec/rtld-elf/malloc.c 154248 2006-01-12 07:28:21Z jasone $";
3734192Sjdp#endif /* LIBC_SCCS and not lint */
3834192Sjdp
3934192Sjdp/*
4034192Sjdp * malloc.c (Caltech) 2/21/82
4134192Sjdp * Chris Kingsley, kingsley@cit-20.
4234192Sjdp *
4334192Sjdp * This is a very fast storage allocator.  It allocates blocks of a small
4434192Sjdp * number of different sizes, and keeps free lists of each size.  Blocks that
4534192Sjdp * don't exactly fit are passed up to the next larger size.  In this
4634192Sjdp * implementation, the available sizes are 2^n-4 (or 2^n-10) bytes long.
4734192Sjdp * This is designed for use in a virtual memory environment.
4834192Sjdp */
4934192Sjdp
5034192Sjdp#include <sys/types.h>
5134192Sjdp#include <err.h>
5269793Sobrien#include <paths.h>
53110803Skan#include <stdarg.h>
54119255Simp#include <stddef.h>
55110803Skan#include <stdio.h>
5634192Sjdp#include <stdlib.h>
5734192Sjdp#include <string.h>
5834192Sjdp#include <unistd.h>
5934192Sjdp#include <sys/param.h>
6034192Sjdp#include <sys/mman.h>
6134192Sjdp#ifndef BSD
6234192Sjdp#define MAP_COPY	MAP_PRIVATE
6334192Sjdp#define MAP_FILE	0
6434192Sjdp#define MAP_ANON	0
6534192Sjdp#endif
6634192Sjdp
6734192Sjdp#ifndef BSD		/* Need do better than this */
6834192Sjdp#define NEED_DEV_ZERO	1
6934192Sjdp#endif
7034192Sjdp
7134192Sjdpstatic void morecore();
7234192Sjdpstatic int findbucket();
7334192Sjdp
7434192Sjdp/*
7534192Sjdp * Pre-allocate mmap'ed pages
7634192Sjdp */
7734192Sjdp#define	NPOOLPAGES	(32*1024/pagesz)
7834192Sjdpstatic caddr_t		pagepool_start, pagepool_end;
7934192Sjdpstatic int		morepages();
8034192Sjdp
8134192Sjdp/*
8234192Sjdp * The overhead on a block is at least 4 bytes.  When free, this space
8334192Sjdp * contains a pointer to the next free block, and the bottom two bits must
8434192Sjdp * be zero.  When in use, the first byte is set to MAGIC, and the second
8534192Sjdp * byte is the size index.  The remaining bytes are for alignment.
8634192Sjdp * If range checking is enabled then a second word holds the size of the
8734192Sjdp * requested block, less 1, rounded up to a multiple of sizeof(RMAGIC).
8834192Sjdp * The order of elements is critical: ov_magic must overlay the low order
8934192Sjdp * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern.
9034192Sjdp */
9134192Sjdpunion	overhead {
9234192Sjdp	union	overhead *ov_next;	/* when free */
9334192Sjdp	struct {
9434192Sjdp		u_char	ovu_magic;	/* magic number */
9534192Sjdp		u_char	ovu_index;	/* bucket # */
9634192Sjdp#ifdef RCHECK
9734192Sjdp		u_short	ovu_rmagic;	/* range magic number */
9834192Sjdp		u_int	ovu_size;	/* actual block size */
9934192Sjdp#endif
10034192Sjdp	} ovu;
10134192Sjdp#define	ov_magic	ovu.ovu_magic
10234192Sjdp#define	ov_index	ovu.ovu_index
10334192Sjdp#define	ov_rmagic	ovu.ovu_rmagic
10434192Sjdp#define	ov_size		ovu.ovu_size
10534192Sjdp};
10634192Sjdp
10734192Sjdp#define	MAGIC		0xef		/* magic # on accounting info */
10834192Sjdp#define RMAGIC		0x5555		/* magic # on range info */
10934192Sjdp
11034192Sjdp#ifdef RCHECK
11134192Sjdp#define	RSLOP		sizeof (u_short)
11234192Sjdp#else
11334192Sjdp#define	RSLOP		0
11434192Sjdp#endif
11534192Sjdp
11634192Sjdp/*
11734192Sjdp * nextf[i] is the pointer to the next free block of size 2^(i+3).  The
11834192Sjdp * smallest allocatable block is 8 bytes.  The overhead information
11934192Sjdp * precedes the data area returned to the user.
12034192Sjdp */
12134192Sjdp#define	NBUCKETS 30
12234192Sjdpstatic	union overhead *nextf[NBUCKETS];
12334192Sjdp
12434192Sjdpstatic	int pagesz;			/* page size */
12534192Sjdpstatic	int pagebucket;			/* page size bucket */
12634192Sjdp
12734192Sjdp#ifdef MSTATS
12834192Sjdp/*
12934192Sjdp * nmalloc[i] is the difference between the number of mallocs and frees
13034192Sjdp * for a given block size.
13134192Sjdp */
13234192Sjdpstatic	u_int nmalloc[NBUCKETS];
13334192Sjdp#include <stdio.h>
13434192Sjdp#endif
13534192Sjdp
13634192Sjdp#if defined(MALLOC_DEBUG) || defined(RCHECK)
13734192Sjdp#define	ASSERT(p)   if (!(p)) botch("p")
13834192Sjdp#include <stdio.h>
13934192Sjdpstatic void
14034192Sjdpbotch(s)
14134192Sjdp	char *s;
14234192Sjdp{
14334192Sjdp	fprintf(stderr, "\r\nassertion botched: %s\r\n", s);
14434192Sjdp 	(void) fflush(stderr);		/* just in case user buffered it */
14534192Sjdp	abort();
14634192Sjdp}
14734192Sjdp#else
14834192Sjdp#define	ASSERT(p)
14934192Sjdp#endif
15034192Sjdp
15134192Sjdp/* Debugging stuff */
152110801Skanstatic void xprintf(const char *, ...);
15334192Sjdp#define TRACE()	xprintf("TRACE %s:%d\n", __FILE__, __LINE__)
15434192Sjdp
15534192Sjdpvoid *
15634192Sjdpmalloc(nbytes)
15734192Sjdp	size_t nbytes;
15834192Sjdp{
15934192Sjdp  	register union overhead *op;
16038816Sdfr  	register int bucket;
16138816Sdfr	register long n;
16234192Sjdp	register unsigned amt;
16334192Sjdp
16434192Sjdp	/*
16534192Sjdp	 * First time malloc is called, setup page size and
16634192Sjdp	 * align break pointer so all data will be page aligned.
16734192Sjdp	 */
16834192Sjdp	if (pagesz == 0) {
16934192Sjdp		pagesz = n = getpagesize();
17034192Sjdp		if (morepages(NPOOLPAGES) == 0)
17134192Sjdp			return NULL;
17234192Sjdp		op = (union overhead *)(pagepool_start);
17338816Sdfr  		n = n - sizeof (*op) - ((long)op & (n - 1));
17434192Sjdp		if (n < 0)
17534192Sjdp			n += pagesz;
17634192Sjdp  		if (n) {
17734192Sjdp			pagepool_start += n;
17834192Sjdp		}
17934192Sjdp		bucket = 0;
18034192Sjdp		amt = 8;
181114625Sobrien		while ((unsigned)pagesz > amt) {
18234192Sjdp			amt <<= 1;
18334192Sjdp			bucket++;
18434192Sjdp		}
18534192Sjdp		pagebucket = bucket;
18634192Sjdp	}
18734192Sjdp	/*
18834192Sjdp	 * Convert amount of memory requested into closest block size
18934192Sjdp	 * stored in hash buckets which satisfies request.
19034192Sjdp	 * Account for space used per block for accounting.
19134192Sjdp	 */
192114625Sobrien	if (nbytes <= (unsigned long)(n = pagesz - sizeof (*op) - RSLOP)) {
19334192Sjdp#ifndef RCHECK
19434192Sjdp		amt = 8;	/* size of first bucket */
19534192Sjdp		bucket = 0;
19634192Sjdp#else
19734192Sjdp		amt = 16;	/* size of first bucket */
19834192Sjdp		bucket = 1;
19934192Sjdp#endif
20034192Sjdp		n = -(sizeof (*op) + RSLOP);
20134192Sjdp	} else {
20234192Sjdp		amt = pagesz;
20334192Sjdp		bucket = pagebucket;
20434192Sjdp	}
20534192Sjdp	while (nbytes > amt + n) {
20634192Sjdp		amt <<= 1;
20734192Sjdp		if (amt == 0)
20834192Sjdp			return (NULL);
20934192Sjdp		bucket++;
21034192Sjdp	}
21134192Sjdp	/*
21234192Sjdp	 * If nothing in hash bucket right now,
21334192Sjdp	 * request more memory from the system.
21434192Sjdp	 */
21534192Sjdp  	if ((op = nextf[bucket]) == NULL) {
21634192Sjdp  		morecore(bucket);
21734192Sjdp  		if ((op = nextf[bucket]) == NULL)
21834192Sjdp  			return (NULL);
21934192Sjdp	}
22034192Sjdp	/* remove from linked list */
22134192Sjdp  	nextf[bucket] = op->ov_next;
22234192Sjdp	op->ov_magic = MAGIC;
22334192Sjdp	op->ov_index = bucket;
22434192Sjdp#ifdef MSTATS
22534192Sjdp  	nmalloc[bucket]++;
22634192Sjdp#endif
22734192Sjdp#ifdef RCHECK
22834192Sjdp	/*
22934192Sjdp	 * Record allocated size of block and
23034192Sjdp	 * bound space with magic numbers.
23134192Sjdp	 */
23234192Sjdp	op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
23334192Sjdp	op->ov_rmagic = RMAGIC;
23434192Sjdp  	*(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
23534192Sjdp#endif
23634192Sjdp  	return ((char *)(op + 1));
23734192Sjdp}
23834192Sjdp
239154248Sjasonevoid *
240154248Sjasonecalloc(size_t num, size_t size)
241154248Sjasone{
242154248Sjasone	void *ret;
243154248Sjasone
244154248Sjasone	if (size != 0 && (num * size) / size != num) {
245154248Sjasone		/* size_t overflow. */
246154248Sjasone		return (NULL);
247154248Sjasone	}
248154248Sjasone
249154248Sjasone	if ((ret = malloc(num * size)) != NULL)
250154248Sjasone		memset(ret, 0, num * size);
251154248Sjasone
252154248Sjasone	return (ret);
253154248Sjasone}
254154248Sjasone
25534192Sjdp/*
25634192Sjdp * Allocate more memory to the indicated bucket.
25734192Sjdp */
25834192Sjdpstatic void
25934192Sjdpmorecore(bucket)
26034192Sjdp	int bucket;
26134192Sjdp{
26234192Sjdp  	register union overhead *op;
26334192Sjdp	register int sz;		/* size of desired block */
26434192Sjdp  	int amt;			/* amount to allocate */
26534192Sjdp  	int nblks;			/* how many blocks we get */
26634192Sjdp
26734192Sjdp	/*
26834192Sjdp	 * sbrk_size <= 0 only for big, FLUFFY, requests (about
26934192Sjdp	 * 2^30 bytes on a VAX, I think) or for a negative arg.
27034192Sjdp	 */
27134192Sjdp	sz = 1 << (bucket + 3);
27234192Sjdp#ifdef MALLOC_DEBUG
27334192Sjdp	ASSERT(sz > 0);
27434192Sjdp#else
27534192Sjdp	if (sz <= 0)
27634192Sjdp		return;
27734192Sjdp#endif
27834192Sjdp	if (sz < pagesz) {
27934192Sjdp		amt = pagesz;
28034192Sjdp  		nblks = amt / sz;
28134192Sjdp	} else {
28234192Sjdp		amt = sz + pagesz;
28334192Sjdp		nblks = 1;
28434192Sjdp	}
28534192Sjdp	if (amt > pagepool_end - pagepool_start)
28634192Sjdp		if (morepages(amt/pagesz + NPOOLPAGES) == 0)
28734192Sjdp			return;
28834192Sjdp	op = (union overhead *)pagepool_start;
28934192Sjdp	pagepool_start += amt;
29034192Sjdp
29134192Sjdp	/*
29234192Sjdp	 * Add new memory allocated to that on
29334192Sjdp	 * free list for this hash bucket.
29434192Sjdp	 */
29534192Sjdp  	nextf[bucket] = op;
29634192Sjdp  	while (--nblks > 0) {
29734192Sjdp		op->ov_next = (union overhead *)((caddr_t)op + sz);
29834192Sjdp		op = (union overhead *)((caddr_t)op + sz);
29934192Sjdp  	}
30034192Sjdp}
30134192Sjdp
30234192Sjdpvoid
30334192Sjdpfree(cp)
30434192Sjdp	void *cp;
30534192Sjdp{
30634192Sjdp  	register int size;
30734192Sjdp	register union overhead *op;
30834192Sjdp
30934192Sjdp  	if (cp == NULL)
31034192Sjdp  		return;
31134192Sjdp	op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
31234192Sjdp#ifdef MALLOC_DEBUG
31334192Sjdp  	ASSERT(op->ov_magic == MAGIC);		/* make sure it was in use */
31434192Sjdp#else
31534192Sjdp	if (op->ov_magic != MAGIC)
31634192Sjdp		return;				/* sanity */
31734192Sjdp#endif
31834192Sjdp#ifdef RCHECK
31934192Sjdp  	ASSERT(op->ov_rmagic == RMAGIC);
32034192Sjdp	ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC);
32134192Sjdp#endif
32234192Sjdp  	size = op->ov_index;
32334192Sjdp  	ASSERT(size < NBUCKETS);
32434192Sjdp	op->ov_next = nextf[size];	/* also clobbers ov_magic */
32534192Sjdp  	nextf[size] = op;
32634192Sjdp#ifdef MSTATS
32734192Sjdp  	nmalloc[size]--;
32834192Sjdp#endif
32934192Sjdp}
33034192Sjdp
33134192Sjdp/*
33234192Sjdp * When a program attempts "storage compaction" as mentioned in the
33334192Sjdp * old malloc man page, it realloc's an already freed block.  Usually
33434192Sjdp * this is the last block it freed; occasionally it might be farther
33534192Sjdp * back.  We have to search all the free lists for the block in order
33634192Sjdp * to determine its bucket: 1st we make one pass thru the lists
33734192Sjdp * checking only the first block in each; if that fails we search
33834192Sjdp * ``realloc_srchlen'' blocks in each list for a match (the variable
33934192Sjdp * is extern so the caller can modify it).  If that fails we just copy
34034192Sjdp * however many bytes was given to realloc() and hope it's not huge.
34134192Sjdp */
34234192Sjdpint realloc_srchlen = 4;	/* 4 should be plenty, -1 =>'s whole list */
34334192Sjdp
34434192Sjdpvoid *
34534192Sjdprealloc(cp, nbytes)
34634192Sjdp	void *cp;
34734192Sjdp	size_t nbytes;
34834192Sjdp{
34934192Sjdp  	register u_int onb;
35034192Sjdp	register int i;
35134192Sjdp	union overhead *op;
35234192Sjdp  	char *res;
35334192Sjdp	int was_alloced = 0;
35434192Sjdp
35534192Sjdp  	if (cp == NULL)
35634192Sjdp  		return (malloc(nbytes));
35734192Sjdp	op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
35834192Sjdp	if (op->ov_magic == MAGIC) {
35934192Sjdp		was_alloced++;
36034192Sjdp		i = op->ov_index;
36134192Sjdp	} else {
36234192Sjdp		/*
36334192Sjdp		 * Already free, doing "compaction".
36434192Sjdp		 *
36534192Sjdp		 * Search for the old block of memory on the
36634192Sjdp		 * free list.  First, check the most common
36734192Sjdp		 * case (last element free'd), then (this failing)
36834192Sjdp		 * the last ``realloc_srchlen'' items free'd.
36934192Sjdp		 * If all lookups fail, then assume the size of
37034192Sjdp		 * the memory block being realloc'd is the
37134192Sjdp		 * largest possible (so that all "nbytes" of new
37234192Sjdp		 * memory are copied into).  Note that this could cause
37334192Sjdp		 * a memory fault if the old area was tiny, and the moon
37434192Sjdp		 * is gibbous.  However, that is very unlikely.
37534192Sjdp		 */
37634192Sjdp		if ((i = findbucket(op, 1)) < 0 &&
37734192Sjdp		    (i = findbucket(op, realloc_srchlen)) < 0)
37834192Sjdp			i = NBUCKETS;
37934192Sjdp	}
38034192Sjdp	onb = 1 << (i + 3);
381114625Sobrien	if (onb < (u_int)pagesz)
38234192Sjdp		onb -= sizeof (*op) + RSLOP;
38334192Sjdp	else
38434192Sjdp		onb += pagesz - sizeof (*op) - RSLOP;
38534192Sjdp	/* avoid the copy if same size block */
38634192Sjdp	if (was_alloced) {
38734192Sjdp		if (i) {
38834192Sjdp			i = 1 << (i + 2);
38934192Sjdp			if (i < pagesz)
39034192Sjdp				i -= sizeof (*op) + RSLOP;
39134192Sjdp			else
39234192Sjdp				i += pagesz - sizeof (*op) - RSLOP;
39334192Sjdp		}
394114625Sobrien		if (nbytes <= onb && nbytes > (size_t)i) {
39534192Sjdp#ifdef RCHECK
39634192Sjdp			op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
39734192Sjdp			*(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
39834192Sjdp#endif
39934192Sjdp			return(cp);
40034192Sjdp		} else
40134192Sjdp			free(cp);
40234192Sjdp	}
40334192Sjdp  	if ((res = malloc(nbytes)) == NULL)
40434192Sjdp  		return (NULL);
40534192Sjdp  	if (cp != res)		/* common optimization if "compacting" */
40634192Sjdp		bcopy(cp, res, (nbytes < onb) ? nbytes : onb);
40734192Sjdp  	return (res);
40834192Sjdp}
40934192Sjdp
41034192Sjdp/*
41134192Sjdp * Search ``srchlen'' elements of each free list for a block whose
41234192Sjdp * header starts at ``freep''.  If srchlen is -1 search the whole list.
41334192Sjdp * Return bucket number, or -1 if not found.
41434192Sjdp */
41534192Sjdpstatic int
41634192Sjdpfindbucket(freep, srchlen)
41734192Sjdp	union overhead *freep;
41834192Sjdp	int srchlen;
41934192Sjdp{
42034192Sjdp	register union overhead *p;
42134192Sjdp	register int i, j;
42234192Sjdp
42334192Sjdp	for (i = 0; i < NBUCKETS; i++) {
42434192Sjdp		j = 0;
42534192Sjdp		for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
42634192Sjdp			if (p == freep)
42734192Sjdp				return (i);
42834192Sjdp			j++;
42934192Sjdp		}
43034192Sjdp	}
43134192Sjdp	return (-1);
43234192Sjdp}
43334192Sjdp
43434192Sjdp#ifdef MSTATS
43534192Sjdp/*
43634192Sjdp * mstats - print out statistics about malloc
43734192Sjdp *
43834192Sjdp * Prints two lines of numbers, one showing the length of the free list
43934192Sjdp * for each size category, the second showing the number of mallocs -
44034192Sjdp * frees for each size category.
44134192Sjdp */
44234192Sjdpmstats(s)
44334192Sjdp	char *s;
44434192Sjdp{
44534192Sjdp  	register int i, j;
44634192Sjdp  	register union overhead *p;
44734192Sjdp  	int totfree = 0,
44834192Sjdp  	totused = 0;
44934192Sjdp
45034192Sjdp  	fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s);
45134192Sjdp  	for (i = 0; i < NBUCKETS; i++) {
45234192Sjdp  		for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
45334192Sjdp  			;
45434192Sjdp  		fprintf(stderr, " %d", j);
45534192Sjdp  		totfree += j * (1 << (i + 3));
45634192Sjdp  	}
45734192Sjdp  	fprintf(stderr, "\nused:\t");
45834192Sjdp  	for (i = 0; i < NBUCKETS; i++) {
45934192Sjdp  		fprintf(stderr, " %d", nmalloc[i]);
46034192Sjdp  		totused += nmalloc[i] * (1 << (i + 3));
46134192Sjdp  	}
46234192Sjdp  	fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n",
46334192Sjdp	    totused, totfree);
46434192Sjdp}
46534192Sjdp#endif
46634192Sjdp
46734192Sjdp
46834192Sjdpstatic int
46934192Sjdpmorepages(n)
47034192Sjdpint	n;
47134192Sjdp{
47234192Sjdp	int	fd = -1;
47334192Sjdp	int	offset;
47434192Sjdp
47534192Sjdp#ifdef NEED_DEV_ZERO
47669793Sobrien	fd = open(_PATH_DEVZERO, O_RDWR, 0);
47734192Sjdp	if (fd == -1)
47869793Sobrien		perror(_PATH_DEVZERO);
47934192Sjdp#endif
48034192Sjdp
48134192Sjdp	if (pagepool_end - pagepool_start > pagesz) {
48234192Sjdp		caddr_t	addr = (caddr_t)
48338816Sdfr			(((long)pagepool_start + pagesz - 1) & ~(pagesz - 1));
48434192Sjdp		if (munmap(addr, pagepool_end - addr) != 0)
48534192Sjdp			warn("morepages: munmap %p", addr);
48634192Sjdp	}
48734192Sjdp
48838816Sdfr	offset = (long)pagepool_start - ((long)pagepool_start & ~(pagesz - 1));
48934192Sjdp
49034192Sjdp	if ((pagepool_start = mmap(0, n * pagesz,
49134192Sjdp			PROT_READ|PROT_WRITE,
49234192Sjdp			MAP_ANON|MAP_COPY, fd, 0)) == (caddr_t)-1) {
49334192Sjdp		xprintf("Cannot map anonymous memory");
49434192Sjdp		return 0;
49534192Sjdp	}
49634192Sjdp	pagepool_end = pagepool_start + n * pagesz;
49734192Sjdp	pagepool_start += offset;
49834192Sjdp
49934192Sjdp#ifdef NEED_DEV_ZERO
50034192Sjdp	close(fd);
50134192Sjdp#endif
50234192Sjdp	return n;
50334192Sjdp}
504110801Skan
505110801Skan/*
506110801Skan * Non-mallocing printf, for use by malloc itself.
507110801Skan */
508110801Skanstatic void
509110801Skanxprintf(const char *fmt, ...)
510110801Skan{
511110801Skan    char buf[256];
512110801Skan    va_list ap;
513110801Skan
514110801Skan    va_start(ap, fmt);
515110801Skan    vsprintf(buf, fmt, ap);
516110801Skan    (void)write(STDOUT_FILENO, buf, strlen(buf));
517110801Skan    va_end(ap);
518110801Skan}
519