1 /*	$OpenBSD: mem2.c,v 1.5 2011/09/21 18:08:07 jsg Exp $	*/
2 /*	$NetBSD: mem2.c,v 1.3 1995/10/02 17:27:11 jpo Exp $	*/
3 
4 /*
5  * Copyright (c) 1994, 1995 Jochen Pohl
6  * All Rights Reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Jochen Pohl for
19  *	The NetBSD Project.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/mman.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <err.h>
41 
42 #include "lint2.h"
43 
44 /* length of new allocated memory blocks */
45 static size_t	mblklen;
46 
47 /* offset of next free byte in mbuf */
48 static size_t	nxtfree;
49 
50 /* current buffer to server memory requests from */
51 static void	*mbuf;
52 
53 void
initmem(void)54 initmem(void)
55 {
56 	int	pgsz;
57 
58 	pgsz = getpagesize();
59 	mblklen = ((MBLKSIZ + pgsz - 1) / pgsz) * pgsz;
60 
61 	nxtfree = mblklen;
62 }
63 
64 /*
65  * Allocate memory in large chunks to avoid space and time overhead of
66  * malloc(). This is possible because memory allocated by xalloc()
67  * need never to be freed.
68  */
69 void *
xalloc(size_t sz)70 xalloc(size_t sz)
71 {
72 	void	*ptr;
73 	int	prot, flags;
74 
75 	sz = ALIGN(sz);
76 	if (nxtfree + sz > mblklen) {
77 		/* use mmap() instead of malloc() to avoid malloc overhead. */
78 		prot = PROT_READ | PROT_WRITE;
79 		flags = MAP_ANON | MAP_PRIVATE;
80 		mbuf = mmap(NULL, mblklen, prot, flags, -1, (off_t)0);
81 		if (mbuf == MAP_FAILED)
82 			err(1, "can't map memory");
83 		if (ALIGN((u_long)mbuf) != (u_long)mbuf)
84 			errx(1, "mapped address is not aligned");
85 		(void)memset(mbuf, 0, mblklen);
86 		nxtfree = 0;
87 	}
88 
89 	ptr = (char *)mbuf + nxtfree;
90 	nxtfree += sz;
91 
92 	return (ptr);
93 }
94