mem2.c revision 12099
112099Sjoerg/* $NetBSD: mem2.c,v 1.3 1995/10/02 17:27:11 jpo 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 3412099Sjoerg#ifndef lint 3512099Sjoergstatic char rcsid[] = "$NetBSD: mem2.c,v 1.3 1995/10/02 17:27:11 jpo Exp $"; 3612099Sjoerg#endif 3712099Sjoerg 3812099Sjoerg#include <sys/param.h> 3912099Sjoerg#include <sys/types.h> 4012099Sjoerg#include <sys/mman.h> 4112099Sjoerg#include <unistd.h> 4212099Sjoerg#include <string.h> 4312099Sjoerg#include <err.h> 4412099Sjoerg 4512099Sjoerg#include "lint2.h" 4612099Sjoerg 4712099Sjoerg/* length of new allocated memory blocks */ 4812099Sjoergstatic size_t mblklen; 4912099Sjoerg 5012099Sjoerg/* offset of next free byte in mbuf */ 5112099Sjoergstatic size_t nxtfree; 5212099Sjoerg 5312099Sjoerg/* current buffer to server memory requests from */ 5412099Sjoergstatic void *mbuf; 5512099Sjoerg 5612099Sjoergvoid 5712099Sjoerginitmem() 5812099Sjoerg{ 5912099Sjoerg int pgsz; 6012099Sjoerg 6112099Sjoerg pgsz = getpagesize(); 6212099Sjoerg mblklen = ((MBLKSIZ + pgsz - 1) / pgsz) * pgsz; 6312099Sjoerg 6412099Sjoerg nxtfree = mblklen; 6512099Sjoerg} 6612099Sjoerg 6712099Sjoerg/* 6812099Sjoerg * Allocate memory in large chunks to avoid space and time overhead of 6912099Sjoerg * malloc(). This is possible because memory allocated by xalloc() 7012099Sjoerg * need never to be freed. 7112099Sjoerg */ 7212099Sjoergvoid * 7312099Sjoergxalloc(sz) 7412099Sjoerg size_t sz; 7512099Sjoerg{ 7612099Sjoerg void *ptr; 7712099Sjoerg int prot, flags; 7812099Sjoerg 7912099Sjoerg sz = ALIGN(sz); 8012099Sjoerg if (nxtfree + sz > mblklen) { 8112099Sjoerg /* use mmap() instead of malloc() to avoid malloc overhead. */ 8212099Sjoerg prot = PROT_READ | PROT_WRITE; 8312099Sjoerg flags = MAP_ANON | MAP_PRIVATE; 8412099Sjoerg mbuf = mmap(NULL, mblklen, prot, flags, -1, (off_t)0); 8512099Sjoerg if (mbuf == (void *)-1) 8612099Sjoerg err(1, "can't map memory"); 8712099Sjoerg if (ALIGN((u_long)mbuf) != (u_long)mbuf) 8812099Sjoerg errx(1, "mapped address is not aligned"); 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