alloc.c revision 1.9 1 /* $NetBSD: alloc.c,v 1.9 2006/04/01 23:36:28 christos Exp $ */
2
3 /*
4 * Copyright (c) 2002 Marc Espie.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
19 * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*
29 * area-based allocation built on malloc/free
30 */
31
32 #include "sh.h"
33
34 struct link {
35 struct link *prev;
36 struct link *next;
37 };
38
39 Area *
40 ainit(Area *ap)
41 {
42 ap->freelist = NULL;
43 return ap;
44 }
45
46 void
47 afreeall(Area *ap)
48 {
49 struct link *l, *l2;
50
51 for (l = ap->freelist; l != NULL; l = l2) {
52 l2 = l->next;
53 free(l);
54 }
55 ap->freelist = NULL;
56 }
57
58 #define L2P(l) ( (void *)(((char *)(l)) + sizeof(struct link)) )
59 #define P2L(p) ( (struct link *)(((char *)(p)) - sizeof(struct link)) )
60
61 /* coverity[+alloc] */
62 void *
63 alloc(size_t size, Area *ap)
64 {
65 struct link *l;
66
67 l = malloc(sizeof(struct link) + size);
68 if (l == NULL)
69 internal_errorf(1, "unable to allocate memory");
70 l->next = ap->freelist;
71 l->prev = NULL;
72 if (ap->freelist)
73 ap->freelist->prev = l;
74 ap->freelist = l;
75
76 return L2P(l);
77 }
78
79 /* coverity[+alloc] */
80 /* coverity[+free : arg-0] */
81 void *
82 aresize(void *ptr, size_t size, Area *ap)
83 {
84 struct link *l, *l2, *lprev, *lnext;
85
86 if (ptr == NULL)
87 return alloc(size, ap);
88
89 l = P2L(ptr);
90 lprev = l->prev;
91 lnext = l->next;
92
93 l2 = realloc(l, sizeof(struct link) + size);
94 if (l2 == NULL)
95 internal_errorf(1, "unable to allocate memory");
96 if (lprev)
97 lprev->next = l2;
98 else
99 ap->freelist = l2;
100 if (lnext)
101 lnext->prev = l2;
102
103 return L2P(l2);
104 }
105
106 /* coverity[+free : arg-0] */
107 void
108 afree(void *ptr, Area *ap)
109 {
110 struct link *l;
111
112 if (!ptr)
113 return;
114
115 l = P2L(ptr);
116
117 if (l->prev)
118 l->prev->next = l->next;
119 else
120 ap->freelist = l->next;
121 if (l->next)
122 l->next->prev = l->prev;
123
124 free(l);
125 }
126