191592Smarkm/* $NetBSD: mem2.c,v 1.6 2002/01/21 19:49:52 tv Exp $ */ 212099Sjoerg 312099Sjoerg/* 412099Sjoerg * Copyright (c) 1994, 1995 Jochen Pohl 512099Sjoerg * All Rights Reserved. 612099Sjoerg * 712099Sjoerg * Redistribution and use in source and binary forms, with or without 812099Sjoerg * modification, are permitted provided that the following conditions 912099Sjoerg * are met: 1012099Sjoerg * 1. Redistributions of source code must retain the above copyright 1112099Sjoerg * notice, this list of conditions and the following disclaimer. 1212099Sjoerg * 2. Redistributions in binary form must reproduce the above copyright 1312099Sjoerg * notice, this list of conditions and the following disclaimer in the 1412099Sjoerg * documentation and/or other materials provided with the distribution. 1512099Sjoerg * 3. All advertising materials mentioning features or use of this software 1612099Sjoerg * must display the following acknowledgement: 1712099Sjoerg * This product includes software developed by Jochen Pohl for 1812099Sjoerg * The NetBSD Project. 1912099Sjoerg * 4. The name of the author may not be used to endorse or promote products 2012099Sjoerg * derived from this software without specific prior written permission. 2112099Sjoerg * 2212099Sjoerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2312099Sjoerg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2412099Sjoerg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2512099Sjoerg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2612099Sjoerg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2712099Sjoerg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2812099Sjoerg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2912099Sjoerg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3012099Sjoerg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3112099Sjoerg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3212099Sjoerg */ 3312099Sjoerg 3491592Smarkm#include <sys/cdefs.h> 3591592Smarkm#if defined(__RCSID) && !defined(lint) 3691592Smarkm__RCSID("$NetBSD: mem2.c,v 1.6 2002/01/21 19:49:52 tv Exp $"); 3712099Sjoerg#endif 3891592Smarkm__FBSDID("$FreeBSD$"); 3912099Sjoerg 4012099Sjoerg#include <sys/param.h> 4112099Sjoerg#include <sys/types.h> 4212099Sjoerg#include <sys/mman.h> 43148723Sstefanf#include <err.h> 4412099Sjoerg#include <unistd.h> 4512099Sjoerg#include <string.h> 4612099Sjoerg 4712099Sjoerg#include "lint2.h" 4812099Sjoerg 4912099Sjoerg/* length of new allocated memory blocks */ 5012099Sjoergstatic size_t mblklen; 5112099Sjoerg 5212099Sjoerg/* offset of next free byte in mbuf */ 5312099Sjoergstatic size_t nxtfree; 5412099Sjoerg 5512099Sjoerg/* current buffer to server memory requests from */ 5612099Sjoergstatic void *mbuf; 5712099Sjoerg 5812099Sjoergvoid 5991592Smarkminitmem(void) 6012099Sjoerg{ 6112099Sjoerg int pgsz; 6212099Sjoerg 6312099Sjoerg pgsz = getpagesize(); 6412099Sjoerg mblklen = ((MBLKSIZ + pgsz - 1) / pgsz) * pgsz; 6512099Sjoerg 6612099Sjoerg nxtfree = mblklen; 6712099Sjoerg} 6812099Sjoerg 6912099Sjoerg/* 7012099Sjoerg * Allocate memory in large chunks to avoid space and time overhead of 7112099Sjoerg * malloc(). This is possible because memory allocated by xalloc() 7212099Sjoerg * need never to be freed. 7312099Sjoerg */ 7412099Sjoergvoid * 7591592Smarkmxalloc(size_t sz) 7612099Sjoerg{ 7712099Sjoerg void *ptr; 7812099Sjoerg int prot, flags; 7912099Sjoerg 8091592Smarkm /* Align to at least 8 bytes. */ 8191592Smarkm sz = (sz + 7) & ~7L; 8212099Sjoerg if (nxtfree + sz > mblklen) { 8312099Sjoerg /* use mmap() instead of malloc() to avoid malloc overhead. */ 8412099Sjoerg prot = PROT_READ | PROT_WRITE; 8512099Sjoerg flags = MAP_ANON | MAP_PRIVATE; 8612099Sjoerg mbuf = mmap(NULL, mblklen, prot, flags, -1, (off_t)0); 8721786Salex if (mbuf == (void *)MAP_FAILED) 8812099Sjoerg err(1, "can't map memory"); 8912099Sjoerg (void)memset(mbuf, 0, mblklen); 9012099Sjoerg nxtfree = 0; 9112099Sjoerg } 9212099Sjoerg 9312099Sjoerg ptr = (char *)mbuf + nxtfree; 9412099Sjoerg nxtfree += sz; 9512099Sjoerg 9612099Sjoerg return (ptr); 9712099Sjoerg} 98