alloc.c revision 1.7 1 /* $NetBSD: alloc.c,v 1.7 2006/03/21 23:40:49 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 void *
80 aresize(void *ptr, size_t size, Area *ap)
81 {
82 struct link *l, *l2, *lprev, *lnext;
83
84 if (ptr == NULL)
85 return alloc(size, ap);
86
87 l = P2L(ptr);
88 lprev = l->prev;
89 lnext = l->next;
90
91 l2 = realloc(l, sizeof(struct link) + size);
92 if (l2 == NULL)
93 internal_errorf(1, "unable to allocate memory");
94 if (lprev)
95 lprev->next = l2;
96 else
97 ap->freelist = l2;
98 if (lnext)
99 lnext->prev = l2;
100
101 return L2P(l2);
102 }
103
104 /* coverity[+free : arg-1] */
105 void
106 afree(void *ptr, Area *ap)
107 {
108 struct link *l;
109
110 if (!ptr)
111 return;
112
113 l = P2L(ptr);
114
115 if (l->prev)
116 l->prev->next = l->next;
117 else
118 ap->freelist = l->next;
119 if (l->next)
120 l->next->prev = l->prev;
121
122 free(l);
123 }
124