alloc.c revision 1.6 1 /* $NetBSD: alloc.c,v 1.6 2004/07/07 19:20:09 mycroft 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 void *
62 alloc(size_t size, Area *ap)
63 {
64 struct link *l;
65
66 l = malloc(sizeof(struct link) + size);
67 if (l == NULL)
68 internal_errorf(1, "unable to allocate memory");
69 l->next = ap->freelist;
70 l->prev = NULL;
71 if (ap->freelist)
72 ap->freelist->prev = l;
73 ap->freelist = l;
74
75 return L2P(l);
76 }
77
78 void *
79 aresize(void *ptr, size_t size, Area *ap)
80 {
81 struct link *l, *l2, *lprev, *lnext;
82
83 if (ptr == NULL)
84 return alloc(size, ap);
85
86 l = P2L(ptr);
87 lprev = l->prev;
88 lnext = l->next;
89
90 l2 = realloc(l, sizeof(struct link) + size);
91 if (l2 == NULL)
92 internal_errorf(1, "unable to allocate memory");
93 if (lprev)
94 lprev->next = l2;
95 else
96 ap->freelist = l2;
97 if (lnext)
98 lnext->prev = l2;
99
100 return L2P(l2);
101 }
102
103 void
104 afree(void *ptr, Area *ap)
105 {
106 struct link *l;
107
108 if (!ptr)
109 return;
110
111 l = P2L(ptr);
112
113 if (l->prev)
114 l->prev->next = l->next;
115 else
116 ap->freelist = l->next;
117 if (l->next)
118 l->next->prev = l->prev;
119
120 free(l);
121 }
122