malloc.c revision 119255
191586Smarkm/*- 212099Sjoerg * Copyright (c) 1983 Regents of the University of California. 312099Sjoerg * All rights reserved. 491586Smarkm * 512099Sjoerg * Redistribution and use in source and binary forms, with or without 612099Sjoerg * modification, are permitted provided that the following conditions 712099Sjoerg * are met: 812099Sjoerg * 1. Redistributions of source code must retain the above copyright 912099Sjoerg * notice, this list of conditions and the following disclaimer. 1012099Sjoerg * 2. Redistributions in binary form must reproduce the above copyright 1112099Sjoerg * notice, this list of conditions and the following disclaimer in the 1212099Sjoerg * documentation and/or other materials provided with the distribution. 1312099Sjoerg * 3. All advertising materials mentioning features or use of this software 1412099Sjoerg * must display the following acknowledgement: 1512099Sjoerg * This product includes software developed by the University of 1612099Sjoerg * California, Berkeley and its contributors. 1712099Sjoerg * 4. Neither the name of the University nor the names of its contributors 1812099Sjoerg * may be used to endorse or promote products derived from this software 1912099Sjoerg * without specific prior written permission. 2012099Sjoerg * 2112099Sjoerg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2212099Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2312099Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2412099Sjoerg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2512099Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2612099Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2712099Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2812099Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2912099Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3012099Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3112099Sjoerg * SUCH DAMAGE. 3212099Sjoerg */ 3312099Sjoerg 3412099Sjoerg#if defined(LIBC_SCCS) && !defined(lint) 3591586Smarkm/*static char *sccsid = "from: @(#)malloc.c 5.11 (Berkeley) 2/23/91";*/ 3691586Smarkmstatic char *rcsid = "$FreeBSD: head/libexec/rtld-elf/malloc.c 119255 2003-08-22 02:22:59Z imp $"; 3791586Smarkm#endif /* LIBC_SCCS and not lint */ 3812099Sjoerg 39108470Sschweikh/* 4012099Sjoerg * malloc.c (Caltech) 2/21/82 4112099Sjoerg * Chris Kingsley, kingsley@cit-20. 4212099Sjoerg * 4312099Sjoerg * This is a very fast storage allocator. It allocates blocks of a small 4412099Sjoerg * number of different sizes, and keeps free lists of each size. Blocks that 4591586Smarkm * don't exactly fit are passed up to the next larger size. In this 4691586Smarkm * implementation, the available sizes are 2^n-4 (or 2^n-10) bytes long. 4712099Sjoerg * This is designed for use in a virtual memory environment. 4812099Sjoerg */ 4912099Sjoerg 5012099Sjoerg#include <sys/types.h> 5112099Sjoerg#include <err.h> 5212099Sjoerg#include <paths.h> 5312099Sjoerg#include <stdarg.h> 5412099Sjoerg#include <stddef.h> 5512099Sjoerg#include <stdio.h> 5612099Sjoerg#include <stdlib.h> 5712099Sjoerg#include <string.h> 5812099Sjoerg#include <unistd.h> 5912099Sjoerg#include <sys/param.h> 6012099Sjoerg#include <sys/mman.h> 6112099Sjoerg#ifndef BSD 6212099Sjoerg#define MAP_COPY MAP_PRIVATE 6312099Sjoerg#define MAP_FILE 0 6412099Sjoerg#define MAP_ANON 0 6512099Sjoerg#endif 6612099Sjoerg 6712099Sjoerg#ifndef BSD /* Need do better than this */ 6812099Sjoerg#define NEED_DEV_ZERO 1 6912099Sjoerg#endif 7012099Sjoerg 7112099Sjoergstatic void morecore(); 7212099Sjoergstatic int findbucket(); 7312099Sjoerg 7412099Sjoerg/* 7512099Sjoerg * Pre-allocate mmap'ed pages 7612099Sjoerg */ 7712099Sjoerg#define NPOOLPAGES (32*1024/pagesz) 7812099Sjoergstatic caddr_t pagepool_start, pagepool_end; 7912099Sjoergstatic int morepages(); 8012099Sjoerg 8112099Sjoerg/* 8212099Sjoerg * The overhead on a block is at least 4 bytes. When free, this space 8312099Sjoerg * contains a pointer to the next free block, and the bottom two bits must 8412099Sjoerg * be zero. When in use, the first byte is set to MAGIC, and the second 8512099Sjoerg * byte is the size index. The remaining bytes are for alignment. 8612099Sjoerg * If range checking is enabled then a second word holds the size of the 8712099Sjoerg * requested block, less 1, rounded up to a multiple of sizeof(RMAGIC). 8891586Smarkm * The order of elements is critical: ov_magic must overlay the low order 8912099Sjoerg * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern. 9012099Sjoerg */ 9112099Sjoergunion overhead { 9212099Sjoerg union overhead *ov_next; /* when free */ 9312099Sjoerg struct { 9412099Sjoerg u_char ovu_magic; /* magic number */ 9512099Sjoerg u_char ovu_index; /* bucket # */ 9612099Sjoerg#ifdef RCHECK 9712099Sjoerg u_short ovu_rmagic; /* range magic number */ 9812099Sjoerg u_int ovu_size; /* actual block size */ 9912099Sjoerg#endif 10012099Sjoerg } ovu; 10112099Sjoerg#define ov_magic ovu.ovu_magic 10212099Sjoerg#define ov_index ovu.ovu_index 10312099Sjoerg#define ov_rmagic ovu.ovu_rmagic 10412099Sjoerg#define ov_size ovu.ovu_size 10512099Sjoerg}; 10612099Sjoerg 10712099Sjoerg#define MAGIC 0xef /* magic # on accounting info */ 10812099Sjoerg#define RMAGIC 0x5555 /* magic # on range info */ 10912099Sjoerg 11012099Sjoerg#ifdef RCHECK 11112099Sjoerg#define RSLOP sizeof (u_short) 11212099Sjoerg#else 11312099Sjoerg#define RSLOP 0 11412099Sjoerg#endif 11512099Sjoerg 11612099Sjoerg/* 11712099Sjoerg * nextf[i] is the pointer to the next free block of size 2^(i+3). The 11812099Sjoerg * smallest allocatable block is 8 bytes. The overhead information 11912099Sjoerg * precedes the data area returned to the user. 12012099Sjoerg */ 12112099Sjoerg#define NBUCKETS 30 12212099Sjoergstatic union overhead *nextf[NBUCKETS]; 12312099Sjoerg 12412099Sjoergstatic int pagesz; /* page size */ 12512099Sjoergstatic int pagebucket; /* page size bucket */ 12612099Sjoerg 12712099Sjoerg#ifdef MSTATS 12812099Sjoerg/* 12912099Sjoerg * nmalloc[i] is the difference between the number of mallocs and frees 13012099Sjoerg * for a given block size. 13112099Sjoerg */ 13212099Sjoergstatic u_int nmalloc[NBUCKETS]; 13312099Sjoerg#include <stdio.h> 13412099Sjoerg#endif 13512099Sjoerg 13612099Sjoerg#if defined(MALLOC_DEBUG) || defined(RCHECK) 13712099Sjoerg#define ASSERT(p) if (!(p)) botch("p") 13812099Sjoerg#include <stdio.h> 13912099Sjoergstatic void 14012099Sjoergbotch(s) 14112099Sjoerg char *s; 14212099Sjoerg{ 14312099Sjoerg fprintf(stderr, "\r\nassertion botched: %s\r\n", s); 14412099Sjoerg (void) fflush(stderr); /* just in case user buffered it */ 14512099Sjoerg abort(); 14612099Sjoerg} 14712099Sjoerg#else 14812099Sjoerg#define ASSERT(p) 14912099Sjoerg#endif 15012099Sjoerg 15112099Sjoerg/* Debugging stuff */ 15212099Sjoergstatic void xprintf(const char *, ...); 15312099Sjoerg#define TRACE() xprintf("TRACE %s:%d\n", __FILE__, __LINE__) 15412099Sjoerg 15512099Sjoergvoid * 15612099Sjoergmalloc(nbytes) 15712099Sjoerg size_t nbytes; 15891586Smarkm{ 15912099Sjoerg register union overhead *op; 16012099Sjoerg register int bucket; 16112099Sjoerg register long n; 16212099Sjoerg register unsigned amt; 16312099Sjoerg 16412099Sjoerg /* 16512099Sjoerg * First time malloc is called, setup page size and 16612099Sjoerg * align break pointer so all data will be page aligned. 16712099Sjoerg */ 16812099Sjoerg if (pagesz == 0) { 16912099Sjoerg pagesz = n = getpagesize(); 17012099Sjoerg if (morepages(NPOOLPAGES) == 0) 17112099Sjoerg return NULL; 17212099Sjoerg op = (union overhead *)(pagepool_start); 17312099Sjoerg n = n - sizeof (*op) - ((long)op & (n - 1)); 17412099Sjoerg if (n < 0) 17512099Sjoerg n += pagesz; 17612099Sjoerg if (n) { 17712099Sjoerg pagepool_start += n; 17812099Sjoerg } 17912099Sjoerg bucket = 0; 18012099Sjoerg amt = 8; 18112099Sjoerg while ((unsigned)pagesz > amt) { 18212099Sjoerg amt <<= 1; 18312099Sjoerg bucket++; 18412099Sjoerg } 18512099Sjoerg pagebucket = bucket; 18612099Sjoerg } 18712099Sjoerg /* 18891586Smarkm * Convert amount of memory requested into closest block size 18912099Sjoerg * stored in hash buckets which satisfies request. 19091586Smarkm * Account for space used per block for accounting. 19191586Smarkm */ 19291586Smarkm if (nbytes <= (unsigned long)(n = pagesz - sizeof (*op) - RSLOP)) { 19391586Smarkm#ifndef RCHECK 19412099Sjoerg amt = 8; /* size of first bucket */ 19512099Sjoerg bucket = 0; 19612099Sjoerg#else 19712099Sjoerg amt = 16; /* size of first bucket */ 19812099Sjoerg bucket = 1; 19912099Sjoerg#endif 20012099Sjoerg n = -(sizeof (*op) + RSLOP); 20191586Smarkm } else { 20291586Smarkm amt = pagesz; 20391586Smarkm bucket = pagebucket; 20491586Smarkm } 20591586Smarkm while (nbytes > amt + n) { 20691586Smarkm amt <<= 1; 20712099Sjoerg if (amt == 0) 20812099Sjoerg return (NULL); 20912099Sjoerg bucket++; 21012099Sjoerg } 211108470Sschweikh /* 21212099Sjoerg * If nothing in hash bucket right now, 21312099Sjoerg * request more memory from the system. 21412099Sjoerg */ 21512099Sjoerg if ((op = nextf[bucket]) == NULL) { 21612099Sjoerg morecore(bucket); 21712099Sjoerg if ((op = nextf[bucket]) == NULL) 21891586Smarkm return (NULL); 21912099Sjoerg } 22091586Smarkm /* remove from linked list */ 22112099Sjoerg nextf[bucket] = op->ov_next; 22212099Sjoerg op->ov_magic = MAGIC; 22312099Sjoerg op->ov_index = bucket; 22412099Sjoerg#ifdef MSTATS 22512099Sjoerg nmalloc[bucket]++; 22612099Sjoerg#endif 22712099Sjoerg#ifdef RCHECK 22812099Sjoerg /* 22912099Sjoerg * Record allocated size of block and 23012099Sjoerg * bound space with magic numbers. 23112099Sjoerg */ 23212099Sjoerg op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1); 23312099Sjoerg op->ov_rmagic = RMAGIC; 23412099Sjoerg *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC; 23512099Sjoerg#endif 23612099Sjoerg return ((char *)(op + 1)); 23712099Sjoerg} 23812099Sjoerg 23912099Sjoerg/* 24012099Sjoerg * Allocate more memory to the indicated bucket. 24112099Sjoerg */ 24212099Sjoergstatic void 24312099Sjoergmorecore(bucket) 24412099Sjoerg int bucket; 24512099Sjoerg{ 24612099Sjoerg register union overhead *op; 24712099Sjoerg register int sz; /* size of desired block */ 24812099Sjoerg int amt; /* amount to allocate */ 24912099Sjoerg int nblks; /* how many blocks we get */ 25012099Sjoerg 25112099Sjoerg /* 25212099Sjoerg * sbrk_size <= 0 only for big, FLUFFY, requests (about 25312099Sjoerg * 2^30 bytes on a VAX, I think) or for a negative arg. 25412099Sjoerg */ 25512099Sjoerg sz = 1 << (bucket + 3); 25612099Sjoerg#ifdef MALLOC_DEBUG 25712099Sjoerg ASSERT(sz > 0); 25812099Sjoerg#else 25912099Sjoerg if (sz <= 0) 26012099Sjoerg return; 26112099Sjoerg#endif 26212099Sjoerg if (sz < pagesz) { 26312099Sjoerg amt = pagesz; 26412099Sjoerg nblks = amt / sz; 26512099Sjoerg } else { 26612099Sjoerg amt = sz + pagesz; 26712099Sjoerg nblks = 1; 26812099Sjoerg } 26912099Sjoerg if (amt > pagepool_end - pagepool_start) 27012099Sjoerg if (morepages(amt/pagesz + NPOOLPAGES) == 0) 27112099Sjoerg return; 27212099Sjoerg op = (union overhead *)pagepool_start; 27312099Sjoerg pagepool_start += amt; 27412099Sjoerg 27512099Sjoerg /* 27691586Smarkm * Add new memory allocated to that on 27791586Smarkm * free list for this hash bucket. 27891586Smarkm */ 27991586Smarkm nextf[bucket] = op; 28091586Smarkm while (--nblks > 0) { 28191586Smarkm op->ov_next = (union overhead *)((caddr_t)op + sz); 28212099Sjoerg op = (union overhead *)((caddr_t)op + sz); 28312099Sjoerg } 28412099Sjoerg} 28512099Sjoerg 28612099Sjoergvoid 28712099Sjoergfree(cp) 28812099Sjoerg void *cp; 28912099Sjoerg{ 29012099Sjoerg register int size; 29112099Sjoerg register union overhead *op; 29212099Sjoerg 29391586Smarkm if (cp == NULL) 29412099Sjoerg return; 29512099Sjoerg op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); 29612099Sjoerg#ifdef MALLOC_DEBUG 29712099Sjoerg ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */ 29812099Sjoerg#else 29912099Sjoerg if (op->ov_magic != MAGIC) 30012099Sjoerg return; /* sanity */ 30112099Sjoerg#endif 30212099Sjoerg#ifdef RCHECK 30312099Sjoerg ASSERT(op->ov_rmagic == RMAGIC); 30412099Sjoerg ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC); 30512099Sjoerg#endif 30612099Sjoerg size = op->ov_index; 30712099Sjoerg ASSERT(size < NBUCKETS); 30812099Sjoerg op->ov_next = nextf[size]; /* also clobbers ov_magic */ 30912099Sjoerg nextf[size] = op; 31012099Sjoerg#ifdef MSTATS 31112099Sjoerg nmalloc[size]--; 31212099Sjoerg#endif 31312099Sjoerg} 31412099Sjoerg 31512099Sjoerg/* 31612099Sjoerg * When a program attempts "storage compaction" as mentioned in the 31712099Sjoerg * old malloc man page, it realloc's an already freed block. Usually 31812099Sjoerg * this is the last block it freed; occasionally it might be farther 31912099Sjoerg * back. We have to search all the free lists for the block in order 32012099Sjoerg * to determine its bucket: 1st we make one pass thru the lists 32112099Sjoerg * checking only the first block in each; if that fails we search 32212099Sjoerg * ``realloc_srchlen'' blocks in each list for a match (the variable 32312099Sjoerg * is extern so the caller can modify it). If that fails we just copy 32412099Sjoerg * however many bytes was given to realloc() and hope it's not huge. 32512099Sjoerg */ 32612099Sjoergint realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */ 32712099Sjoerg 32812099Sjoergvoid * 32912099Sjoergrealloc(cp, nbytes) 33012099Sjoerg void *cp; 33112099Sjoerg size_t nbytes; 33212099Sjoerg{ 33312099Sjoerg register u_int onb; 33412099Sjoerg register int i; 33512099Sjoerg union overhead *op; 33612099Sjoerg char *res; 33712099Sjoerg int was_alloced = 0; 33812099Sjoerg 33912099Sjoerg if (cp == NULL) 34012099Sjoerg return (malloc(nbytes)); 34112099Sjoerg op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); 34212099Sjoerg if (op->ov_magic == MAGIC) { 34312099Sjoerg was_alloced++; 34412099Sjoerg i = op->ov_index; 34512099Sjoerg } else { 34612099Sjoerg /* 34712099Sjoerg * Already free, doing "compaction". 34812099Sjoerg * 34912099Sjoerg * Search for the old block of memory on the 35012099Sjoerg * free list. First, check the most common 35112099Sjoerg * case (last element free'd), then (this failing) 35212099Sjoerg * the last ``realloc_srchlen'' items free'd. 35312099Sjoerg * If all lookups fail, then assume the size of 35412099Sjoerg * the memory block being realloc'd is the 35512099Sjoerg * largest possible (so that all "nbytes" of new 35612099Sjoerg * memory are copied into). Note that this could cause 35712099Sjoerg * a memory fault if the old area was tiny, and the moon 35812099Sjoerg * is gibbous. However, that is very unlikely. 35912099Sjoerg */ 36012099Sjoerg if ((i = findbucket(op, 1)) < 0 && 36112099Sjoerg (i = findbucket(op, realloc_srchlen)) < 0) 36212099Sjoerg i = NBUCKETS; 36312099Sjoerg } 36412099Sjoerg onb = 1 << (i + 3); 36591586Smarkm if (onb < (u_int)pagesz) 36691586Smarkm onb -= sizeof (*op) + RSLOP; 36791586Smarkm else 36891586Smarkm onb += pagesz - sizeof (*op) - RSLOP; 36991586Smarkm /* avoid the copy if same size block */ 37091586Smarkm if (was_alloced) { 37112099Sjoerg if (i) { 37212099Sjoerg i = 1 << (i + 2); 37312099Sjoerg if (i < pagesz) 37412099Sjoerg i -= sizeof (*op) + RSLOP; 37512099Sjoerg else 37612099Sjoerg i += pagesz - sizeof (*op) - RSLOP; 37712099Sjoerg } 37812099Sjoerg if (nbytes <= onb && nbytes > (size_t)i) { 37912099Sjoerg#ifdef RCHECK 38012099Sjoerg op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1); 38112099Sjoerg *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC; 38212099Sjoerg#endif 38312099Sjoerg return(cp); 38412099Sjoerg } else 38512099Sjoerg free(cp); 38612099Sjoerg } 38712099Sjoerg if ((res = malloc(nbytes)) == NULL) 38812099Sjoerg return (NULL); 38912099Sjoerg if (cp != res) /* common optimization if "compacting" */ 39012099Sjoerg bcopy(cp, res, (nbytes < onb) ? nbytes : onb); 39112099Sjoerg return (res); 39212099Sjoerg} 39312099Sjoerg 39412099Sjoerg/* 39591586Smarkm * Search ``srchlen'' elements of each free list for a block whose 39612099Sjoerg * header starts at ``freep''. If srchlen is -1 search the whole list. 39712099Sjoerg * Return bucket number, or -1 if not found. 39812099Sjoerg */ 39991586Smarkmstatic int 40012099Sjoergfindbucket(freep, srchlen) 40112099Sjoerg union overhead *freep; 40212099Sjoerg int srchlen; 40312099Sjoerg{ 40412099Sjoerg register union overhead *p; 40512099Sjoerg register int i, j; 40612099Sjoerg 40712099Sjoerg for (i = 0; i < NBUCKETS; i++) { 40812099Sjoerg j = 0; 40912099Sjoerg for (p = nextf[i]; p && j != srchlen; p = p->ov_next) { 41012099Sjoerg if (p == freep) 41112099Sjoerg return (i); 41212099Sjoerg j++; 41312099Sjoerg } 41412099Sjoerg } 41512099Sjoerg return (-1); 41612099Sjoerg} 41712099Sjoerg 41812099Sjoerg#ifdef MSTATS 41912099Sjoerg/* 42012099Sjoerg * mstats - print out statistics about malloc 42112099Sjoerg * 42212099Sjoerg * Prints two lines of numbers, one showing the length of the free list 42312099Sjoerg * for each size category, the second showing the number of mallocs - 42412099Sjoerg * frees for each size category. 42512099Sjoerg */ 42612099Sjoergmstats(s) 42791586Smarkm char *s; 42891586Smarkm{ 42912099Sjoerg register int i, j; 43012099Sjoerg register union overhead *p; 43112099Sjoerg int totfree = 0, 43212099Sjoerg totused = 0; 43312099Sjoerg 43412099Sjoerg fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s); 43512099Sjoerg for (i = 0; i < NBUCKETS; i++) { 43612099Sjoerg for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) 43712099Sjoerg ; 43812099Sjoerg fprintf(stderr, " %d", j); 43912099Sjoerg totfree += j * (1 << (i + 3)); 44012099Sjoerg } 44112099Sjoerg fprintf(stderr, "\nused:\t"); 44212099Sjoerg for (i = 0; i < NBUCKETS; i++) { 44312099Sjoerg fprintf(stderr, " %d", nmalloc[i]); 44412099Sjoerg totused += nmalloc[i] * (1 << (i + 3)); 44512099Sjoerg } 44612099Sjoerg fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n", 44712099Sjoerg totused, totfree); 44812099Sjoerg} 44912099Sjoerg#endif 45012099Sjoerg 45112099Sjoerg 45212099Sjoergstatic int 45312099Sjoergmorepages(n) 45412099Sjoergint n; 45512099Sjoerg{ 45612099Sjoerg int fd = -1; 45712099Sjoerg int offset; 45812099Sjoerg 45912099Sjoerg#ifdef NEED_DEV_ZERO 46012099Sjoerg fd = open(_PATH_DEVZERO, O_RDWR, 0); 46112099Sjoerg if (fd == -1) 46212099Sjoerg perror(_PATH_DEVZERO); 46312099Sjoerg#endif 46412099Sjoerg 46512099Sjoerg if (pagepool_end - pagepool_start > pagesz) { 46612099Sjoerg caddr_t addr = (caddr_t) 46712099Sjoerg (((long)pagepool_start + pagesz - 1) & ~(pagesz - 1)); 46812099Sjoerg if (munmap(addr, pagepool_end - addr) != 0) 46991586Smarkm warn("morepages: munmap %p", addr); 47091586Smarkm } 47112099Sjoerg 47212099Sjoerg offset = (long)pagepool_start - ((long)pagepool_start & ~(pagesz - 1)); 47312099Sjoerg 47412099Sjoerg if ((pagepool_start = mmap(0, n * pagesz, 47512099Sjoerg PROT_READ|PROT_WRITE, 47612099Sjoerg MAP_ANON|MAP_COPY, fd, 0)) == (caddr_t)-1) { 47712099Sjoerg xprintf("Cannot map anonymous memory"); 47812099Sjoerg return 0; 47912099Sjoerg } 48012099Sjoerg pagepool_end = pagepool_start + n * pagesz; 48112099Sjoerg pagepool_start += offset; 48291586Smarkm 48312099Sjoerg#ifdef NEED_DEV_ZERO 48412099Sjoerg close(fd); 48512099Sjoerg#endif 48612099Sjoerg return n; 48712099Sjoerg} 48812099Sjoerg 48912099Sjoerg/* 49012099Sjoerg * Non-mallocing printf, for use by malloc itself. 49112099Sjoerg */ 49212099Sjoergstatic void 49312099Sjoergxprintf(const char *fmt, ...) 49412099Sjoerg{ 49512099Sjoerg char buf[256]; 49612099Sjoerg va_list ap; 49712099Sjoerg 49812099Sjoerg va_start(ap, fmt); 49912099Sjoerg vsprintf(buf, fmt, ap); 50012099Sjoerg (void)write(STDOUT_FILENO, buf, strlen(buf)); 50112099Sjoerg va_end(ap); 50212099Sjoerg} 50312099Sjoerg