Home | History | Annotate | Line # | Download | only in ld.elf_so
xmalloc.c revision 1.10
      1  1.10     joerg /*	$NetBSD: xmalloc.c,v 1.10 2010/12/03 23:07:49 joerg 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.10     joerg __RCSID("$NetBSD: xmalloc.c,v 1.10 2010/12/03 23:07:49 joerg 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.9  christos static char 		*pagepool_start, *pagepool_end;
    100   1.7        ad static int		morepages(int);
    101   1.9  christos #define PAGEPOOL_SIZE	(size_t)(pagepool_end - pagepool_start)
    102   1.7        ad 
    103   1.7        ad /*
    104   1.7        ad  * The overhead on a block is at least 4 bytes.  When free, this space
    105   1.7        ad  * contains a pointer to the next free block, and the bottom two bits must
    106   1.7        ad  * be zero.  When in use, the first byte is set to MAGIC, and the second
    107   1.7        ad  * byte is the size index.  The remaining bytes are for alignment.
    108   1.7        ad  * If range checking is enabled then a second word holds the size of the
    109   1.7        ad  * requested block, less 1, rounded up to a multiple of sizeof(RMAGIC).
    110   1.7        ad  * The order of elements is critical: ov_magic must overlay the low order
    111   1.7        ad  * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern.
    112   1.7        ad  */
    113   1.7        ad union	overhead {
    114   1.7        ad 	union	overhead *ov_next;	/* when free */
    115   1.7        ad 	struct {
    116   1.7        ad 		u_char	ovu_magic;	/* magic number */
    117   1.7        ad 		u_char	ovu_index;	/* bucket # */
    118   1.7        ad #ifdef RCHECK
    119   1.7        ad 		u_short	ovu_rmagic;	/* range magic number */
    120   1.7        ad 		u_int	ovu_size;	/* actual block size */
    121   1.7        ad #endif
    122   1.7        ad 	} ovu;
    123   1.7        ad #define	ov_magic	ovu.ovu_magic
    124   1.7        ad #define	ov_index	ovu.ovu_index
    125   1.7        ad #define	ov_rmagic	ovu.ovu_rmagic
    126   1.7        ad #define	ov_size		ovu.ovu_size
    127   1.7        ad };
    128   1.7        ad 
    129   1.9  christos static void morecore(size_t);
    130   1.7        ad static void *imalloc(size_t);
    131   1.7        ad 
    132   1.7        ad #define	MAGIC		0xef		/* magic # on accounting info */
    133   1.7        ad #define RMAGIC		0x5555		/* magic # on range info */
    134   1.7        ad 
    135   1.7        ad #ifdef RCHECK
    136   1.7        ad #define	RSLOP		(sizeof (u_short))
    137   1.7        ad #else
    138   1.7        ad #define	RSLOP		0
    139   1.7        ad #endif
    140   1.7        ad 
    141   1.7        ad /*
    142   1.7        ad  * nextf[i] is the pointer to the next free block of size 2^(i+3).  The
    143   1.7        ad  * smallest allocatable block is 8 bytes.  The overhead information
    144   1.7        ad  * precedes the data area returned to the user.
    145   1.7        ad  */
    146   1.7        ad #define	NBUCKETS 30
    147   1.7        ad static	union overhead *nextf[NBUCKETS];
    148   1.7        ad 
    149   1.9  christos static	size_t pagesz;			/* page size */
    150   1.9  christos static	size_t pagebucket;		/* page size bucket */
    151   1.7        ad 
    152   1.7        ad #ifdef MSTATS
    153   1.7        ad /*
    154   1.7        ad  * nmalloc[i] is the difference between the number of mallocs and frees
    155   1.7        ad  * for a given block size.
    156   1.7        ad  */
    157   1.7        ad static	u_int nmalloc[NBUCKETS];
    158   1.7        ad #endif
    159   1.7        ad 
    160   1.7        ad #if defined(MALLOC_DEBUG) || defined(RCHECK)
    161   1.7        ad #define	ASSERT(p)   if (!(p)) botch("p")
    162   1.7        ad static void
    163  1.10     joerg botch(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.9  christos   	union overhead *op;
    178   1.9  christos   	size_t bucket;
    179   1.9  christos 	size_t n, m;
    180   1.9  christos 	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.9  christos 		m = sizeof (*op) - (((char *)op - (char *)NULL) & (n - 1));
    192   1.9  christos 		if (n < m)
    193   1.9  christos 			n += pagesz - m;
    194   1.9  christos 		else
    195   1.9  christos 			n -= m;
    196   1.7        ad   		if (n) {
    197   1.7        ad 			pagepool_start += n;
    198   1.7        ad 		}
    199   1.7        ad 		bucket = 0;
    200   1.7        ad 		amt = sizeof(union overhead);
    201   1.7        ad 		while (pagesz > amt) {
    202   1.7        ad 			amt <<= 1;
    203   1.7        ad 			bucket++;
    204   1.7        ad 		}
    205   1.7        ad 		pagebucket = bucket;
    206   1.7        ad 	}
    207   1.7        ad 	/*
    208   1.7        ad 	 * Convert amount of memory requested into closest block size
    209   1.7        ad 	 * stored in hash buckets which satisfies request.
    210   1.7        ad 	 * Account for space used per block for accounting.
    211   1.7        ad 	 */
    212   1.7        ad 	if (nbytes <= (n = pagesz - sizeof (*op) - RSLOP)) {
    213   1.7        ad 		if (sizeof(union overhead) & (sizeof(union overhead) - 1)) {
    214   1.7        ad 		    amt = sizeof(union overhead) * 2;
    215   1.7        ad 		    bucket = 1;
    216   1.7        ad 		} else {
    217   1.7        ad 		    amt = sizeof(union overhead); /* size of first bucket */
    218   1.7        ad 		    bucket = 0;
    219   1.7        ad 		}
    220   1.7        ad 		n = -(sizeof (*op) + RSLOP);
    221   1.7        ad 	} else {
    222   1.7        ad 		amt = pagesz;
    223   1.7        ad 		bucket = pagebucket;
    224   1.7        ad 	}
    225   1.7        ad 	while (nbytes > amt + n) {
    226   1.7        ad 		amt <<= 1;
    227   1.7        ad 		if (amt == 0)
    228   1.7        ad 			return (NULL);
    229   1.7        ad 		bucket++;
    230   1.7        ad 	}
    231   1.7        ad 	/*
    232   1.7        ad 	 * If nothing in hash bucket right now,
    233   1.7        ad 	 * request more memory from the system.
    234   1.7        ad 	 */
    235   1.7        ad   	if ((op = nextf[bucket]) == NULL) {
    236   1.7        ad   		morecore(bucket);
    237   1.7        ad   		if ((op = nextf[bucket]) == NULL)
    238   1.7        ad   			return (NULL);
    239   1.7        ad 	}
    240   1.7        ad 	/* remove from linked list */
    241   1.7        ad   	nextf[bucket] = op->ov_next;
    242   1.7        ad 	op->ov_magic = MAGIC;
    243   1.7        ad 	op->ov_index = bucket;
    244   1.7        ad #ifdef MSTATS
    245   1.7        ad   	nmalloc[bucket]++;
    246   1.7        ad #endif
    247   1.7        ad #ifdef RCHECK
    248   1.7        ad 	/*
    249   1.7        ad 	 * Record allocated size of block and
    250   1.7        ad 	 * bound space with magic numbers.
    251   1.7        ad 	 */
    252   1.7        ad 	op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
    253   1.7        ad 	op->ov_rmagic = RMAGIC;
    254   1.7        ad   	*(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
    255   1.7        ad #endif
    256   1.7        ad   	return ((char *)(op + 1));
    257   1.7        ad }
    258   1.7        ad 
    259   1.7        ad /*
    260   1.7        ad  * Allocate more memory to the indicated bucket.
    261   1.7        ad  */
    262   1.7        ad static void
    263   1.9  christos morecore(size_t bucket)
    264   1.7        ad {
    265   1.9  christos   	union overhead *op;
    266   1.9  christos 	size_t sz;		/* size of desired block */
    267   1.9  christos   	size_t amt;		/* amount to allocate */
    268   1.9  christos   	size_t nblks;		/* how many blocks we get */
    269   1.7        ad 
    270   1.7        ad 	/*
    271   1.7        ad 	 * sbrk_size <= 0 only for big, FLUFFY, requests (about
    272   1.7        ad 	 * 2^30 bytes on a VAX, I think) or for a negative arg.
    273   1.7        ad 	 */
    274   1.7        ad 	sz = 1 << (bucket + 3);
    275   1.7        ad #ifdef MALLOC_DEBUG
    276   1.7        ad 	ASSERT(sz > 0);
    277   1.7        ad #endif
    278   1.7        ad 	if (sz < pagesz) {
    279   1.7        ad 		amt = pagesz;
    280   1.7        ad   		nblks = amt / sz;
    281   1.7        ad 	} else {
    282   1.7        ad 		amt = sz + pagesz;
    283   1.7        ad 		nblks = 1;
    284   1.7        ad 	}
    285   1.9  christos 	if (amt > PAGEPOOL_SIZE)
    286   1.7        ad 		if (morepages(amt/pagesz + NPOOLPAGES) == 0)
    287   1.7        ad 			return;
    288   1.7        ad 	op = (union overhead *)pagepool_start;
    289   1.7        ad 	pagepool_start += amt;
    290   1.7        ad 
    291   1.7        ad 	/*
    292   1.7        ad 	 * Add new memory allocated to that on
    293   1.7        ad 	 * free list for this hash bucket.
    294   1.7        ad 	 */
    295   1.7        ad   	nextf[bucket] = op;
    296   1.7        ad   	while (--nblks > 0) {
    297   1.7        ad 		op->ov_next = (union overhead *)((caddr_t)op + sz);
    298   1.7        ad 		op = (union overhead *)((caddr_t)op + sz);
    299   1.7        ad   	}
    300   1.7        ad }
    301   1.7        ad 
    302   1.7        ad void
    303  1.10     joerg xfree(void *cp)
    304   1.7        ad {
    305   1.9  christos   	int size;
    306   1.9  christos 	union overhead *op;
    307   1.7        ad 
    308   1.7        ad   	if (cp == NULL)
    309   1.7        ad   		return;
    310   1.7        ad 	op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
    311   1.7        ad #ifdef MALLOC_DEBUG
    312   1.7        ad   	ASSERT(op->ov_magic == MAGIC);		/* make sure it was in use */
    313   1.7        ad #else
    314   1.7        ad 	if (op->ov_magic != MAGIC)
    315   1.7        ad 		return;				/* sanity */
    316   1.7        ad #endif
    317   1.7        ad #ifdef RCHECK
    318   1.7        ad   	ASSERT(op->ov_rmagic == RMAGIC);
    319   1.7        ad 	ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC);
    320   1.7        ad #endif
    321   1.7        ad   	size = op->ov_index;
    322   1.7        ad   	ASSERT(size < NBUCKETS);
    323   1.7        ad 	op->ov_next = nextf[size];	/* also clobbers ov_magic */
    324   1.7        ad   	nextf[size] = op;
    325   1.7        ad #ifdef MSTATS
    326   1.7        ad   	nmalloc[size]--;
    327   1.7        ad #endif
    328   1.7        ad }
    329   1.7        ad 
    330   1.7        ad static void *
    331   1.7        ad irealloc(void *cp, size_t nbytes)
    332   1.7        ad {
    333   1.9  christos   	size_t onb;
    334   1.9  christos 	size_t i;
    335   1.7        ad 	union overhead *op;
    336   1.7        ad   	char *res;
    337   1.7        ad 
    338   1.7        ad   	if (cp == NULL)
    339   1.7        ad   		return (imalloc(nbytes));
    340   1.7        ad 	op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
    341   1.7        ad 	if (op->ov_magic != MAGIC) {
    342   1.7        ad 		static const char *err_str =
    343   1.7        ad 		    "memory corruption or double free in realloc\n";
    344   1.8        ad 		extern char *__progname;
    345   1.7        ad 	        write(STDERR_FILENO, __progname, strlen(__progname));
    346   1.7        ad 		write(STDERR_FILENO, err_str, strlen(err_str));
    347   1.7        ad 		abort();
    348   1.7        ad 	}
    349   1.7        ad 
    350   1.7        ad 	i = op->ov_index;
    351   1.7        ad 	onb = 1 << (i + 3);
    352   1.7        ad 	if (onb < pagesz)
    353   1.7        ad 		onb -= sizeof (*op) + RSLOP;
    354   1.7        ad 	else
    355   1.7        ad 		onb += pagesz - sizeof (*op) - RSLOP;
    356   1.7        ad 	/* avoid the copy if same size block */
    357   1.7        ad 	if (i) {
    358   1.7        ad 		i = 1 << (i + 2);
    359   1.7        ad 		if (i < pagesz)
    360   1.7        ad 			i -= sizeof (*op) + RSLOP;
    361   1.7        ad 		else
    362   1.7        ad 			i += pagesz - sizeof (*op) - RSLOP;
    363   1.7        ad 	}
    364   1.7        ad 	if (nbytes <= onb && nbytes > i) {
    365   1.7        ad #ifdef RCHECK
    366   1.7        ad 		op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
    367   1.7        ad 		*(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
    368   1.7        ad #endif
    369   1.7        ad 		return(cp);
    370   1.7        ad 	} else
    371   1.7        ad 		xfree(cp);
    372   1.7        ad   	if ((res = imalloc(nbytes)) == NULL)
    373   1.7        ad   		return (NULL);
    374   1.7        ad   	if (cp != res)		/* common optimization if "compacting" */
    375   1.7        ad 		memcpy(res, cp, (nbytes < onb) ? nbytes : onb);
    376   1.7        ad   	return (res);
    377   1.7        ad }
    378   1.7        ad 
    379   1.7        ad #ifdef MSTATS
    380   1.7        ad /*
    381   1.7        ad  * mstats - print out statistics about malloc
    382   1.7        ad  *
    383   1.7        ad  * Prints two lines of numbers, one showing the length of the free list
    384   1.7        ad  * for each size category, the second showing the number of mallocs -
    385   1.7        ad  * frees for each size category.
    386   1.7        ad  */
    387  1.10     joerg void
    388   1.7        ad mstats(char *s)
    389   1.7        ad {
    390   1.9  christos   	int i, j;
    391   1.9  christos   	union overhead *p;
    392   1.7        ad   	int totfree = 0,
    393   1.7        ad   	totused = 0;
    394   1.7        ad 
    395   1.7        ad   	xprintf("Memory allocation statistics %s\nfree:\t", s);
    396   1.7        ad   	for (i = 0; i < NBUCKETS; i++) {
    397   1.7        ad   		for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
    398   1.7        ad   			;
    399   1.7        ad   		xprintf(" %d", j);
    400   1.7        ad   		totfree += j * (1 << (i + 3));
    401   1.7        ad   	}
    402   1.7        ad   	xprintf("\nused:\t");
    403   1.7        ad   	for (i = 0; i < NBUCKETS; i++) {
    404   1.7        ad   		xprintf(" %d", nmalloc[i]);
    405   1.7        ad   		totused += nmalloc[i] * (1 << (i + 3));
    406   1.7        ad   	}
    407   1.7        ad   	xprintf("\n\tTotal in use: %d, total free: %d\n",
    408   1.7        ad 	    totused, totfree);
    409   1.7        ad }
    410   1.7        ad #endif
    411   1.7        ad 
    412   1.7        ad 
    413   1.7        ad static int
    414   1.7        ad morepages(int n)
    415   1.7        ad {
    416   1.7        ad 	int	fd = -1;
    417   1.7        ad 	int	offset;
    418   1.7        ad 
    419   1.7        ad #ifdef NEED_DEV_ZERO
    420   1.7        ad 	fd = open("/dev/zero", O_RDWR, 0);
    421   1.7        ad 	if (fd == -1)
    422   1.7        ad 		xerr(1, "/dev/zero");
    423   1.7        ad #endif
    424   1.7        ad 
    425   1.9  christos 	if (PAGEPOOL_SIZE > pagesz) {
    426   1.7        ad 		caddr_t	addr = (caddr_t)
    427   1.7        ad 			(((long)pagepool_start + pagesz - 1) & ~(pagesz - 1));
    428   1.7        ad 		if (munmap(addr, pagepool_end - addr) != 0)
    429   1.7        ad 			xwarn("morepages: munmap %p", addr);
    430   1.7        ad 	}
    431   1.7        ad 
    432   1.7        ad 	offset = (long)pagepool_start - ((long)pagepool_start & ~(pagesz - 1));
    433   1.7        ad 
    434   1.7        ad 	if ((pagepool_start = mmap(0, n * pagesz,
    435   1.7        ad 			PROT_READ|PROT_WRITE,
    436   1.7        ad 			MAP_ANON|MAP_PRIVATE, fd, 0)) == (caddr_t)-1) {
    437   1.7        ad 		xprintf("Cannot map anonymous memory");
    438   1.7        ad 		return 0;
    439   1.7        ad 	}
    440   1.7        ad 	pagepool_end = pagepool_start + n * pagesz;
    441   1.7        ad 	pagepool_start += offset;
    442   1.7        ad 
    443   1.7        ad #ifdef NEED_DEV_ZERO
    444   1.7        ad 	close(fd);
    445   1.7        ad #endif
    446   1.7        ad 	return n;
    447   1.7        ad }
    448   1.1       cgd 
    449   1.1       cgd void *
    450   1.4     skrll xcalloc(size_t size)
    451   1.1       cgd {
    452   1.3    simonb 
    453   1.2  christos 	return memset(xmalloc(size), 0, size);
    454   1.1       cgd }
    455   1.1       cgd 
    456   1.1       cgd void *
    457   1.4     skrll xmalloc(size_t size)
    458   1.1       cgd {
    459   1.7        ad 	void *p = imalloc(size);
    460   1.3    simonb 
    461   1.2  christos 	if (p == NULL)
    462   1.2  christos 		xerr(1, "%s", xstrerror(errno));
    463   1.2  christos 	return p;
    464   1.1       cgd }
    465   1.1       cgd 
    466   1.6  christos void *
    467   1.6  christos xrealloc(void *p, size_t size)
    468   1.6  christos {
    469   1.7        ad 	p = irealloc(p, size);
    470   1.6  christos 
    471   1.6  christos 	if (p == NULL)
    472   1.6  christos 		xerr(1, "%s", xstrerror(errno));
    473   1.6  christos 	return p;
    474   1.6  christos }
    475   1.6  christos 
    476   1.7        ad char *
    477   1.7        ad xstrdup(const char *str)
    478   1.1       cgd {
    479   1.7        ad 	size_t len;
    480   1.7        ad 	char *copy;
    481   1.3    simonb 
    482   1.7        ad 	len = strlen(str) + 1;
    483   1.7        ad 	copy = xmalloc(len);
    484   1.7        ad 	memcpy(copy, str, len);
    485   1.7        ad 	return (copy);
    486   1.1       cgd }
    487