Home | History | Annotate | Line # | Download | only in ld.elf_so
xmalloc.c revision 1.7.6.1
      1  1.7.6.1      yamt /*	$NetBSD: xmalloc.c,v 1.7.6.1 2008/06/17 09:13:39 yamt Exp $	*/
      2      1.1       cgd 
      3      1.1       cgd /*
      4      1.1       cgd  * Copyright 1996 John D. Polstra.
      5      1.1       cgd  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
      6      1.1       cgd  * All rights reserved.
      7      1.1       cgd  *
      8      1.1       cgd  * Redistribution and use in source and binary forms, with or without
      9      1.1       cgd  * modification, are permitted provided that the following conditions
     10      1.1       cgd  * are met:
     11      1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     12      1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     13      1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14      1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     15      1.1       cgd  *    documentation and/or other materials provided with the distribution.
     16      1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     17      1.1       cgd  *    must display the following acknowledgement:
     18      1.1       cgd  *      This product includes software developed by John Polstra.
     19      1.1       cgd  * 4. The name of the author may not be used to endorse or promote products
     20      1.1       cgd  *    derived from this software without specific prior written permission.
     21      1.1       cgd  *
     22      1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23      1.1       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24      1.1       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25      1.1       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26      1.1       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27      1.1       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28      1.1       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29      1.1       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30      1.1       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31      1.1       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32      1.1       cgd  */
     33      1.1       cgd 
     34      1.7        ad /*
     35      1.7        ad  * Copyright (c) 1983 Regents of the University of California.
     36      1.7        ad  * All rights reserved.
     37      1.7        ad  *
     38      1.7        ad  * Redistribution and use in source and binary forms, with or without
     39      1.7        ad  * modification, are permitted provided that the following conditions
     40      1.7        ad  * are met:
     41      1.7        ad  * 1. Redistributions of source code must retain the above copyright
     42      1.7        ad  *    notice, this list of conditions and the following disclaimer.
     43      1.7        ad  * 2. Redistributions in binary form must reproduce the above copyright
     44      1.7        ad  *    notice, this list of conditions and the following disclaimer in the
     45      1.7        ad  *    documentation and/or other materials provided with the distribution.
     46      1.7        ad  * 3. Neither the name of the University nor the names of its contributors
     47      1.7        ad  *    may be used to endorse or promote products derived from this software
     48      1.7        ad  *    without specific prior written permission.
     49      1.7        ad  *
     50      1.7        ad  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     51      1.7        ad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     52      1.7        ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     53      1.7        ad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     54      1.7        ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     55      1.7        ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     56      1.7        ad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     57      1.7        ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     58      1.7        ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     59      1.7        ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     60      1.7        ad  * SUCH DAMAGE.
     61      1.7        ad  */
     62      1.7        ad 
     63      1.7        ad #if defined(LIBC_SCCS) && !defined(lint)
     64      1.7        ad /*static char *sccsid = "from: @(#)malloc.c	5.11 (Berkeley) 2/23/91";*/
     65      1.7        ad #endif /* LIBC_SCCS and not lint */
     66      1.7        ad 
     67      1.7        ad /*
     68      1.7        ad  * malloc.c (Caltech) 2/21/82
     69      1.7        ad  * Chris Kingsley, kingsley@cit-20.
     70      1.7        ad  *
     71      1.7        ad  * This is a very fast storage allocator.  It allocates blocks of a small
     72      1.7        ad  * number of different sizes, and keeps free lists of each size.  Blocks that
     73      1.7        ad  * don't exactly fit are passed up to the next larger size.  In this
     74      1.7        ad  * implementation, the available sizes are 2^n-4 (or 2^n-10) bytes long.
     75      1.7        ad  * This is designed for use in a virtual memory environment.
     76      1.7        ad  */
     77      1.7        ad 
     78      1.2  christos #include <sys/cdefs.h>
     79      1.5     skrll #ifndef lint
     80  1.7.6.1      yamt __RCSID("$NetBSD: xmalloc.c,v 1.7.6.1 2008/06/17 09:13:39 yamt Exp $");
     81      1.5     skrll #endif /* not lint */
     82      1.5     skrll 
     83      1.1       cgd #include <stdlib.h>
     84      1.1       cgd #include <string.h>
     85      1.7        ad #include <unistd.h>
     86      1.7        ad #include <errno.h>
     87      1.7        ad 
     88      1.7        ad #include <sys/types.h>
     89      1.7        ad #include <sys/param.h>
     90      1.7        ad #include <sys/mman.h>
     91      1.7        ad #include <sys/stat.h>
     92      1.7        ad 
     93      1.7        ad #include "rtld.h"
     94      1.7        ad 
     95      1.7        ad /*
     96      1.7        ad  * Pre-allocate mmap'ed pages
     97      1.7        ad  */
     98      1.7        ad #define	NPOOLPAGES	(32*1024/pagesz)
     99      1.7        ad static caddr_t		pagepool_start, pagepool_end;
    100      1.7        ad static int		morepages(int);
    101      1.7        ad 
    102      1.7        ad /*
    103      1.7        ad  * The overhead on a block is at least 4 bytes.  When free, this space
    104      1.7        ad  * contains a pointer to the next free block, and the bottom two bits must
    105      1.7        ad  * be zero.  When in use, the first byte is set to MAGIC, and the second
    106      1.7        ad  * byte is the size index.  The remaining bytes are for alignment.
    107      1.7        ad  * If range checking is enabled then a second word holds the size of the
    108      1.7        ad  * requested block, less 1, rounded up to a multiple of sizeof(RMAGIC).
    109      1.7        ad  * The order of elements is critical: ov_magic must overlay the low order
    110      1.7        ad  * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern.
    111      1.7        ad  */
    112      1.7        ad union	overhead {
    113      1.7        ad 	union	overhead *ov_next;	/* when free */
    114      1.7        ad 	struct {
    115      1.7        ad 		u_char	ovu_magic;	/* magic number */
    116      1.7        ad 		u_char	ovu_index;	/* bucket # */
    117      1.7        ad #ifdef RCHECK
    118      1.7        ad 		u_short	ovu_rmagic;	/* range magic number */
    119      1.7        ad 		u_int	ovu_size;	/* actual block size */
    120      1.7        ad #endif
    121      1.7        ad 	} ovu;
    122      1.7        ad #define	ov_magic	ovu.ovu_magic
    123      1.7        ad #define	ov_index	ovu.ovu_index
    124      1.7        ad #define	ov_rmagic	ovu.ovu_rmagic
    125      1.7        ad #define	ov_size		ovu.ovu_size
    126      1.7        ad };
    127      1.7        ad 
    128      1.7        ad static void morecore(int);
    129      1.7        ad static void *imalloc(size_t);
    130      1.7        ad 
    131      1.7        ad #define	MAGIC		0xef		/* magic # on accounting info */
    132      1.7        ad #define RMAGIC		0x5555		/* magic # on range info */
    133      1.7        ad 
    134      1.7        ad #ifdef RCHECK
    135      1.7        ad #define	RSLOP		(sizeof (u_short))
    136      1.7        ad #else
    137      1.7        ad #define	RSLOP		0
    138      1.7        ad #endif
    139      1.7        ad 
    140      1.7        ad /*
    141      1.7        ad  * nextf[i] is the pointer to the next free block of size 2^(i+3).  The
    142      1.7        ad  * smallest allocatable block is 8 bytes.  The overhead information
    143      1.7        ad  * precedes the data area returned to the user.
    144      1.7        ad  */
    145      1.7        ad #define	NBUCKETS 30
    146      1.7        ad static	union overhead *nextf[NBUCKETS];
    147      1.7        ad 
    148      1.7        ad static	int pagesz;			/* page size */
    149      1.7        ad static	int pagebucket;			/* page size bucket */
    150      1.7        ad 
    151      1.7        ad #ifdef MSTATS
    152      1.7        ad /*
    153      1.7        ad  * nmalloc[i] is the difference between the number of mallocs and frees
    154      1.7        ad  * for a given block size.
    155      1.7        ad  */
    156      1.7        ad static	u_int nmalloc[NBUCKETS];
    157      1.7        ad #endif
    158      1.7        ad 
    159      1.7        ad #if defined(MALLOC_DEBUG) || defined(RCHECK)
    160      1.7        ad #define	ASSERT(p)   if (!(p)) botch("p")
    161      1.7        ad static void
    162      1.7        ad botch(
    163      1.7        ad     const char *s)
    164      1.7        ad {
    165      1.7        ad     xwarnx("\r\nassertion botched: %s\r\n", s);
    166      1.7        ad     abort();
    167      1.7        ad }
    168      1.7        ad #else
    169      1.7        ad #define	ASSERT(p)
    170      1.7        ad #endif
    171      1.7        ad 
    172      1.7        ad #define TRACE()	xprintf("TRACE %s:%d\n", __FILE__, __LINE__)
    173      1.7        ad 
    174      1.7        ad static void *
    175      1.7        ad imalloc(size_t nbytes)
    176      1.7        ad {
    177      1.7        ad   	register union overhead *op;
    178      1.7        ad   	register int bucket;
    179      1.7        ad 	register long n;
    180      1.7        ad 	register unsigned amt;
    181      1.7        ad 
    182      1.7        ad 	/*
    183      1.7        ad 	 * First time malloc is called, setup page size and
    184      1.7        ad 	 * align break pointer so all data will be page aligned.
    185      1.7        ad 	 */
    186      1.7        ad 	if (pagesz == 0) {
    187      1.7        ad 		pagesz = n = _rtld_pagesz;
    188      1.7        ad 		if (morepages(NPOOLPAGES) == 0)
    189      1.7        ad 			return NULL;
    190      1.7        ad 		op = (union overhead *)(pagepool_start);
    191      1.7        ad   		n = n - sizeof (*op) - (((char *)op - (char *)NULL) & (n - 1));
    192      1.7        ad 		if (n < 0)
    193      1.7        ad 			n += pagesz;
    194      1.7        ad   		if (n) {
    195      1.7        ad 			pagepool_start += n;
    196      1.7        ad 		}
    197      1.7        ad 		bucket = 0;
    198      1.7        ad 		amt = sizeof(union overhead);
    199      1.7        ad 		while (pagesz > amt) {
    200      1.7        ad 			amt <<= 1;
    201      1.7        ad 			bucket++;
    202      1.7        ad 		}
    203      1.7        ad 		pagebucket = bucket;
    204      1.7        ad 	}
    205      1.7        ad 	/*
    206      1.7        ad 	 * Convert amount of memory requested into closest block size
    207      1.7        ad 	 * stored in hash buckets which satisfies request.
    208      1.7        ad 	 * Account for space used per block for accounting.
    209      1.7        ad 	 */
    210      1.7        ad 	if (nbytes <= (n = pagesz - sizeof (*op) - RSLOP)) {
    211      1.7        ad 		if (sizeof(union overhead) & (sizeof(union overhead) - 1)) {
    212      1.7        ad 		    amt = sizeof(union overhead) * 2;
    213      1.7        ad 		    bucket = 1;
    214      1.7        ad 		} else {
    215      1.7        ad 		    amt = sizeof(union overhead); /* size of first bucket */
    216      1.7        ad 		    bucket = 0;
    217      1.7        ad 		}
    218      1.7        ad 		n = -(sizeof (*op) + RSLOP);
    219      1.7        ad 	} else {
    220      1.7        ad 		amt = pagesz;
    221      1.7        ad 		bucket = pagebucket;
    222      1.7        ad 	}
    223      1.7        ad 	while (nbytes > amt + n) {
    224      1.7        ad 		amt <<= 1;
    225      1.7        ad 		if (amt == 0)
    226      1.7        ad 			return (NULL);
    227      1.7        ad 		bucket++;
    228      1.7        ad 	}
    229      1.7        ad 	/*
    230      1.7        ad 	 * If nothing in hash bucket right now,
    231      1.7        ad 	 * request more memory from the system.
    232      1.7        ad 	 */
    233      1.7        ad   	if ((op = nextf[bucket]) == NULL) {
    234      1.7        ad   		morecore(bucket);
    235      1.7        ad   		if ((op = nextf[bucket]) == NULL)
    236      1.7        ad   			return (NULL);
    237      1.7        ad 	}
    238      1.7        ad 	/* remove from linked list */
    239      1.7        ad   	nextf[bucket] = op->ov_next;
    240      1.7        ad 	op->ov_magic = MAGIC;
    241      1.7        ad 	op->ov_index = bucket;
    242      1.7        ad #ifdef MSTATS
    243      1.7        ad   	nmalloc[bucket]++;
    244      1.7        ad #endif
    245      1.7        ad #ifdef RCHECK
    246      1.7        ad 	/*
    247      1.7        ad 	 * Record allocated size of block and
    248      1.7        ad 	 * bound space with magic numbers.
    249      1.7        ad 	 */
    250      1.7        ad 	op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
    251      1.7        ad 	op->ov_rmagic = RMAGIC;
    252      1.7        ad   	*(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
    253      1.7        ad #endif
    254      1.7        ad   	return ((char *)(op + 1));
    255      1.7        ad }
    256      1.7        ad 
    257      1.7        ad /*
    258      1.7        ad  * Allocate more memory to the indicated bucket.
    259      1.7        ad  */
    260      1.7        ad static void
    261      1.7        ad morecore(int bucket)
    262      1.7        ad {
    263      1.7        ad   	register union overhead *op;
    264      1.7        ad 	register int sz;		/* size of desired block */
    265      1.7        ad   	int amt;			/* amount to allocate */
    266      1.7        ad   	int nblks;			/* how many blocks we get */
    267      1.7        ad 
    268      1.7        ad 	/*
    269      1.7        ad 	 * sbrk_size <= 0 only for big, FLUFFY, requests (about
    270      1.7        ad 	 * 2^30 bytes on a VAX, I think) or for a negative arg.
    271      1.7        ad 	 */
    272      1.7        ad 	sz = 1 << (bucket + 3);
    273      1.7        ad #ifdef MALLOC_DEBUG
    274      1.7        ad 	ASSERT(sz > 0);
    275      1.7        ad #else
    276      1.7        ad 	if (sz <= 0)
    277      1.7        ad 		return;
    278      1.7        ad #endif
    279      1.7        ad 	if (sz < pagesz) {
    280      1.7        ad 		amt = pagesz;
    281      1.7        ad   		nblks = amt / sz;
    282      1.7        ad 	} else {
    283      1.7        ad 		amt = sz + pagesz;
    284      1.7        ad 		nblks = 1;
    285      1.7        ad 	}
    286      1.7        ad 	if (amt > pagepool_end - pagepool_start)
    287      1.7        ad 		if (morepages(amt/pagesz + NPOOLPAGES) == 0)
    288      1.7        ad 			return;
    289      1.7        ad 	op = (union overhead *)pagepool_start;
    290      1.7        ad 	pagepool_start += amt;
    291      1.7        ad 
    292      1.7        ad 	/*
    293      1.7        ad 	 * Add new memory allocated to that on
    294      1.7        ad 	 * free list for this hash bucket.
    295      1.7        ad 	 */
    296      1.7        ad   	nextf[bucket] = op;
    297      1.7        ad   	while (--nblks > 0) {
    298      1.7        ad 		op->ov_next = (union overhead *)((caddr_t)op + sz);
    299      1.7        ad 		op = (union overhead *)((caddr_t)op + sz);
    300      1.7        ad   	}
    301      1.7        ad }
    302      1.7        ad 
    303      1.7        ad void
    304      1.7        ad xfree(cp)
    305      1.7        ad 	void *cp;
    306      1.7        ad {
    307      1.7        ad   	register int size;
    308      1.7        ad 	register union overhead *op;
    309      1.7        ad 
    310      1.7        ad   	if (cp == NULL)
    311      1.7        ad   		return;
    312      1.7        ad 	op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
    313      1.7        ad #ifdef MALLOC_DEBUG
    314      1.7        ad   	ASSERT(op->ov_magic == MAGIC);		/* make sure it was in use */
    315      1.7        ad #else
    316      1.7        ad 	if (op->ov_magic != MAGIC)
    317      1.7        ad 		return;				/* sanity */
    318      1.7        ad #endif
    319      1.7        ad #ifdef RCHECK
    320      1.7        ad   	ASSERT(op->ov_rmagic == RMAGIC);
    321      1.7        ad 	ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC);
    322      1.7        ad #endif
    323      1.7        ad   	size = op->ov_index;
    324      1.7        ad   	ASSERT(size < NBUCKETS);
    325      1.7        ad 	op->ov_next = nextf[size];	/* also clobbers ov_magic */
    326      1.7        ad   	nextf[size] = op;
    327      1.7        ad #ifdef MSTATS
    328      1.7        ad   	nmalloc[size]--;
    329      1.7        ad #endif
    330      1.7        ad }
    331      1.7        ad 
    332      1.7        ad static void *
    333      1.7        ad irealloc(void *cp, size_t nbytes)
    334      1.7        ad {
    335      1.7        ad   	register u_int onb;
    336      1.7        ad 	register int i;
    337      1.7        ad 	union overhead *op;
    338      1.7        ad   	char *res;
    339      1.7        ad 
    340      1.7        ad   	if (cp == NULL)
    341      1.7        ad   		return (imalloc(nbytes));
    342      1.7        ad 	op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
    343      1.7        ad 	if (op->ov_magic != MAGIC) {
    344      1.7        ad 		static const char *err_str =
    345      1.7        ad 		    "memory corruption or double free in realloc\n";
    346  1.7.6.1      yamt 		extern char *__progname;
    347      1.7        ad 	        write(STDERR_FILENO, __progname, strlen(__progname));
    348      1.7        ad 		write(STDERR_FILENO, err_str, strlen(err_str));
    349      1.7        ad 		abort();
    350      1.7        ad 	}
    351      1.7        ad 
    352      1.7        ad 	i = op->ov_index;
    353      1.7        ad 	onb = 1 << (i + 3);
    354      1.7        ad 	if (onb < pagesz)
    355      1.7        ad 		onb -= sizeof (*op) + RSLOP;
    356      1.7        ad 	else
    357      1.7        ad 		onb += pagesz - sizeof (*op) - RSLOP;
    358      1.7        ad 	/* avoid the copy if same size block */
    359      1.7        ad 	if (i) {
    360      1.7        ad 		i = 1 << (i + 2);
    361      1.7        ad 		if (i < pagesz)
    362      1.7        ad 			i -= sizeof (*op) + RSLOP;
    363      1.7        ad 		else
    364      1.7        ad 			i += pagesz - sizeof (*op) - RSLOP;
    365      1.7        ad 	}
    366      1.7        ad 	if (nbytes <= onb && nbytes > i) {
    367      1.7        ad #ifdef RCHECK
    368      1.7        ad 		op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
    369      1.7        ad 		*(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
    370      1.7        ad #endif
    371      1.7        ad 		return(cp);
    372      1.7        ad 	} else
    373      1.7        ad 		xfree(cp);
    374      1.7        ad   	if ((res = imalloc(nbytes)) == NULL)
    375      1.7        ad   		return (NULL);
    376      1.7        ad   	if (cp != res)		/* common optimization if "compacting" */
    377      1.7        ad 		memcpy(res, cp, (nbytes < onb) ? nbytes : onb);
    378      1.7        ad   	return (res);
    379      1.7        ad }
    380      1.7        ad 
    381      1.7        ad #ifdef MSTATS
    382      1.7        ad /*
    383      1.7        ad  * mstats - print out statistics about malloc
    384      1.7        ad  *
    385      1.7        ad  * Prints two lines of numbers, one showing the length of the free list
    386      1.7        ad  * for each size category, the second showing the number of mallocs -
    387      1.7        ad  * frees for each size category.
    388      1.7        ad  */
    389      1.7        ad mstats(char *s)
    390      1.7        ad {
    391      1.7        ad   	register int i, j;
    392      1.7        ad   	register union overhead *p;
    393      1.7        ad   	int totfree = 0,
    394      1.7        ad   	totused = 0;
    395      1.7        ad 
    396      1.7        ad   	xprintf("Memory allocation statistics %s\nfree:\t", s);
    397      1.7        ad   	for (i = 0; i < NBUCKETS; i++) {
    398      1.7        ad   		for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
    399      1.7        ad   			;
    400      1.7        ad   		xprintf(" %d", j);
    401      1.7        ad   		totfree += j * (1 << (i + 3));
    402      1.7        ad   	}
    403      1.7        ad   	xprintf("\nused:\t");
    404      1.7        ad   	for (i = 0; i < NBUCKETS; i++) {
    405      1.7        ad   		xprintf(" %d", nmalloc[i]);
    406      1.7        ad   		totused += nmalloc[i] * (1 << (i + 3));
    407      1.7        ad   	}
    408      1.7        ad   	xprintf("\n\tTotal in use: %d, total free: %d\n",
    409      1.7        ad 	    totused, totfree);
    410      1.7        ad }
    411      1.7        ad #endif
    412      1.7        ad 
    413      1.7        ad 
    414      1.7        ad static int
    415      1.7        ad morepages(int n)
    416      1.7        ad {
    417      1.7        ad 	int	fd = -1;
    418      1.7        ad 	int	offset;
    419      1.7        ad 
    420      1.7        ad #ifdef NEED_DEV_ZERO
    421      1.7        ad 	fd = open("/dev/zero", O_RDWR, 0);
    422      1.7        ad 	if (fd == -1)
    423      1.7        ad 		xerr(1, "/dev/zero");
    424      1.7        ad #endif
    425      1.7        ad 
    426      1.7        ad 	if (pagepool_end - pagepool_start > pagesz) {
    427      1.7        ad 		caddr_t	addr = (caddr_t)
    428      1.7        ad 			(((long)pagepool_start + pagesz - 1) & ~(pagesz - 1));
    429      1.7        ad 		if (munmap(addr, pagepool_end - addr) != 0)
    430      1.7        ad 			xwarn("morepages: munmap %p", addr);
    431      1.7        ad 	}
    432      1.7        ad 
    433      1.7        ad 	offset = (long)pagepool_start - ((long)pagepool_start & ~(pagesz - 1));
    434      1.7        ad 
    435      1.7        ad 	if ((pagepool_start = mmap(0, n * pagesz,
    436      1.7        ad 			PROT_READ|PROT_WRITE,
    437      1.7        ad 			MAP_ANON|MAP_PRIVATE, fd, 0)) == (caddr_t)-1) {
    438      1.7        ad 		xprintf("Cannot map anonymous memory");
    439      1.7        ad 		return 0;
    440      1.7        ad 	}
    441      1.7        ad 	pagepool_end = pagepool_start + n * pagesz;
    442      1.7        ad 	pagepool_start += offset;
    443      1.7        ad 
    444      1.7        ad #ifdef NEED_DEV_ZERO
    445      1.7        ad 	close(fd);
    446      1.7        ad #endif
    447      1.7        ad 	return n;
    448      1.7        ad }
    449      1.1       cgd 
    450      1.1       cgd void *
    451      1.4     skrll xcalloc(size_t size)
    452      1.1       cgd {
    453      1.3    simonb 
    454      1.2  christos 	return memset(xmalloc(size), 0, size);
    455      1.1       cgd }
    456      1.1       cgd 
    457      1.1       cgd void *
    458      1.4     skrll xmalloc(size_t size)
    459      1.1       cgd {
    460      1.7        ad 	void *p = imalloc(size);
    461      1.3    simonb 
    462      1.2  christos 	if (p == NULL)
    463      1.2  christos 		xerr(1, "%s", xstrerror(errno));
    464      1.2  christos 	return p;
    465      1.1       cgd }
    466      1.1       cgd 
    467      1.6  christos void *
    468      1.6  christos xrealloc(void *p, size_t size)
    469      1.6  christos {
    470      1.7        ad 	p = irealloc(p, size);
    471      1.6  christos 
    472      1.6  christos 	if (p == NULL)
    473      1.6  christos 		xerr(1, "%s", xstrerror(errno));
    474      1.6  christos 	return p;
    475      1.6  christos }
    476      1.6  christos 
    477      1.7        ad char *
    478      1.7        ad xstrdup(const char *str)
    479      1.1       cgd {
    480      1.7        ad 	size_t len;
    481      1.7        ad 	char *copy;
    482      1.3    simonb 
    483      1.7        ad 	len = strlen(str) + 1;
    484      1.7        ad 	copy = xmalloc(len);
    485      1.7        ad 	memcpy(copy, str, len);
    486      1.7        ad 	return (copy);
    487      1.1       cgd }
    488