Deleted Added
full compact
mem2.c (75831) mem2.c (91592)
1/* $NetBSD: mem2.c,v 1.3 1995/10/02 17:27:11 jpo Exp $ */
1/* $NetBSD: mem2.c,v 1.6 2002/01/21 19:49:52 tv Exp $ */
2
3/*
4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:

--- 16 unchanged lines hidden (view full) ---

26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
2
3/*
4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:

--- 16 unchanged lines hidden (view full) ---

26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char rcsid[] =
36 "$FreeBSD: head/usr.bin/xlint/lint2/mem2.c 75831 2001-04-22 17:06:12Z asmodai $";
34#include <sys/cdefs.h>
35#if defined(__RCSID) && !defined(lint)
36__RCSID("$NetBSD: mem2.c,v 1.6 2002/01/21 19:49:52 tv Exp $");
37#endif
37#endif
38__FBSDID("$FreeBSD: head/usr.bin/xlint/lint2/mem2.c 91592 2002-03-03 15:12:50Z markm $");
38
39#include <sys/param.h>
40#include <sys/types.h>
41#include <sys/mman.h>
42#include <unistd.h>
43#include <string.h>
39
40#include <sys/param.h>
41#include <sys/types.h>
42#include <sys/mman.h>
43#include <unistd.h>
44#include <string.h>
44#include <err.h>
45
46#include "lint2.h"
47
48/* length of new allocated memory blocks */
49static size_t mblklen;
50
51/* offset of next free byte in mbuf */
52static size_t nxtfree;
53
54/* current buffer to server memory requests from */
55static void *mbuf;
56
57void
45
46#include "lint2.h"
47
48/* length of new allocated memory blocks */
49static size_t mblklen;
50
51/* offset of next free byte in mbuf */
52static size_t nxtfree;
53
54/* current buffer to server memory requests from */
55static void *mbuf;
56
57void
58initmem()
58initmem(void)
59{
60 int pgsz;
61
62 pgsz = getpagesize();
63 mblklen = ((MBLKSIZ + pgsz - 1) / pgsz) * pgsz;
64
65 nxtfree = mblklen;
66}
67
68/*
69 * Allocate memory in large chunks to avoid space and time overhead of
70 * malloc(). This is possible because memory allocated by xalloc()
71 * need never to be freed.
72 */
73void *
59{
60 int pgsz;
61
62 pgsz = getpagesize();
63 mblklen = ((MBLKSIZ + pgsz - 1) / pgsz) * pgsz;
64
65 nxtfree = mblklen;
66}
67
68/*
69 * Allocate memory in large chunks to avoid space and time overhead of
70 * malloc(). This is possible because memory allocated by xalloc()
71 * need never to be freed.
72 */
73void *
74xalloc(sz)
75 size_t sz;
74xalloc(size_t sz)
76{
77 void *ptr;
78 int prot, flags;
79
75{
76 void *ptr;
77 int prot, flags;
78
80 sz = ALIGN(sz);
79 /* Align to at least 8 bytes. */
80 sz = (sz + 7) & ~7L;
81 if (nxtfree + sz > mblklen) {
82 /* use mmap() instead of malloc() to avoid malloc overhead. */
83 prot = PROT_READ | PROT_WRITE;
84 flags = MAP_ANON | MAP_PRIVATE;
85 mbuf = mmap(NULL, mblklen, prot, flags, -1, (off_t)0);
86 if (mbuf == (void *)MAP_FAILED)
87 err(1, "can't map memory");
81 if (nxtfree + sz > mblklen) {
82 /* use mmap() instead of malloc() to avoid malloc overhead. */
83 prot = PROT_READ | PROT_WRITE;
84 flags = MAP_ANON | MAP_PRIVATE;
85 mbuf = mmap(NULL, mblklen, prot, flags, -1, (off_t)0);
86 if (mbuf == (void *)MAP_FAILED)
87 err(1, "can't map memory");
88 if (ALIGN((u_long)mbuf) != (u_long)mbuf)
89 errx(1, "mapped address is not aligned");
90 (void)memset(mbuf, 0, mblklen);
91 nxtfree = 0;
92 }
93
94 ptr = (char *)mbuf + nxtfree;
95 nxtfree += sz;
96
97 return (ptr);
98}
88 (void)memset(mbuf, 0, mblklen);
89 nxtfree = 0;
90 }
91
92 ptr = (char *)mbuf + nxtfree;
93 nxtfree += sz;
94
95 return (ptr);
96}