alloc.c revision 1.1.1.1 1 1.1 jtc /*
2 1.1 jtc * area-based allocation built on malloc/free
3 1.1 jtc */
4 1.1 jtc
5 1.1 jtc #include "sh.h"
6 1.1 jtc #ifdef MEM_DEBUG
7 1.1 jtc # undef alloc
8 1.1 jtc # undef aresize
9 1.1 jtc # undef afree
10 1.1 jtc #endif /* MEM_DEBUG */
11 1.1 jtc
12 1.1 jtc #define ICELLS 100 /* number of Cells in small Block */
13 1.1 jtc
14 1.1 jtc typedef union Cell Cell;
15 1.1 jtc typedef struct Block Block;
16 1.1 jtc
17 1.1 jtc /*
18 1.1 jtc * The Cells in a Block are organized as a set of objects.
19 1.1 jtc * Each object (pointed to by dp) begins with a size in (dp-1)->size,
20 1.1 jtc * followed with "size" data Cells. Free objects are
21 1.1 jtc * linked together via dp->next.
22 1.1 jtc */
23 1.1 jtc
24 1.1 jtc union Cell {
25 1.1 jtc size_t size;
26 1.1 jtc Cell *next;
27 1.1 jtc struct {int _;} junk; /* alignment */
28 1.1 jtc };
29 1.1 jtc
30 1.1 jtc struct Block {
31 1.1 jtc Block *next; /* list of Blocks in Area */
32 1.1 jtc Cell *freelist; /* object free list */
33 1.1 jtc Cell *last; /* &b.cell[size] */
34 1.1 jtc Cell cell [1]; /* [size] Cells for allocation */
35 1.1 jtc };
36 1.1 jtc
37 1.1 jtc static Block aempty = {&aempty, aempty.cell, aempty.cell};
38 1.1 jtc
39 1.1 jtc /* create empty Area */
40 1.1 jtc Area *
41 1.1 jtc ainit(ap)
42 1.1 jtc register Area *ap;
43 1.1 jtc {
44 1.1 jtc ap->freelist = &aempty;
45 1.1 jtc return ap;
46 1.1 jtc }
47 1.1 jtc
48 1.1 jtc /* free all object in Area */
49 1.1 jtc void
50 1.1 jtc afreeall(ap)
51 1.1 jtc register Area *ap;
52 1.1 jtc {
53 1.1 jtc register Block *bp;
54 1.1 jtc register Block *tmp;
55 1.1 jtc
56 1.1 jtc bp = ap->freelist;
57 1.1 jtc if (bp != NULL && bp != &aempty) {
58 1.1 jtc do {
59 1.1 jtc tmp = bp->next;
60 1.1 jtc free((void*)bp);
61 1.1 jtc bp = tmp;
62 1.1 jtc } while (bp != ap->freelist);
63 1.1 jtc ap->freelist = &aempty;
64 1.1 jtc }
65 1.1 jtc }
66 1.1 jtc
67 1.1 jtc /* allocate object from Area */
68 1.1 jtc void *
69 1.1 jtc alloc(size, ap)
70 1.1 jtc size_t size;
71 1.1 jtc register Area *ap;
72 1.1 jtc {
73 1.1 jtc int cells, split;
74 1.1 jtc register Block *bp;
75 1.1 jtc register Cell *dp, *fp, *fpp;
76 1.1 jtc
77 1.1 jtc if (size <= 0) {
78 1.1 jtc aerror(ap, "allocate bad size");
79 1.1 jtc return NULL;
80 1.1 jtc }
81 1.1 jtc cells = (unsigned)(size - 1) / sizeof(Cell) + 1;
82 1.1 jtc
83 1.1 jtc /* find Cell large enough */
84 1.1 jtc for (bp = ap->freelist; ; bp = bp->next) {
85 1.1 jtc for (fpp = NULL, fp = bp->freelist;
86 1.1 jtc fp != bp->last; fpp = fp, fp = fpp->next)
87 1.1 jtc if ((fp-1)->size >= cells)
88 1.1 jtc goto Found;
89 1.1 jtc
90 1.1 jtc /* wrapped around Block list, create new Block */
91 1.1 jtc if (bp->next == ap->freelist) {
92 1.1 jtc bp = (Block*) malloc(offsetof(Block, cell[ICELLS])
93 1.1 jtc + sizeof(bp->cell[0]) * cells);
94 1.1 jtc if (bp == NULL) {
95 1.1 jtc aerror(ap, "cannot allocate");
96 1.1 jtc return NULL;
97 1.1 jtc }
98 1.1 jtc if (ap->freelist == &aempty)
99 1.1 jtc bp->next = bp;
100 1.1 jtc else {
101 1.1 jtc bp->next = ap->freelist->next;
102 1.1 jtc ap->freelist->next = bp;
103 1.1 jtc }
104 1.1 jtc bp->last = bp->cell + ICELLS + cells;
105 1.1 jtc fp = bp->freelist = bp->cell + 1; /* initial free list */
106 1.1 jtc (fp-1)->size = ICELLS + cells - 1;
107 1.1 jtc fp->next = bp->last;
108 1.1 jtc fpp = NULL;
109 1.1 jtc break;
110 1.1 jtc }
111 1.1 jtc }
112 1.1 jtc Found:
113 1.1 jtc ap->freelist = bp;
114 1.1 jtc dp = fp; /* allocated object */
115 1.1 jtc split = (dp-1)->size - cells;
116 1.1 jtc if (split < 0)
117 1.1 jtc aerror(ap, "allocated object too small");
118 1.1 jtc if (--split <= 0) { /* allocate all */
119 1.1 jtc fp = fp->next;
120 1.1 jtc } else { /* allocate head, free tail */
121 1.1 jtc (fp-1)->size = cells;
122 1.1 jtc fp += cells + 1;
123 1.1 jtc (fp-1)->size = split;
124 1.1 jtc fp->next = dp->next;
125 1.1 jtc }
126 1.1 jtc if (fpp == NULL)
127 1.1 jtc bp->freelist = fp;
128 1.1 jtc else
129 1.1 jtc fpp->next = fp;
130 1.1 jtc return (void*) dp;
131 1.1 jtc }
132 1.1 jtc
133 1.1 jtc /* change size of object -- like realloc */
134 1.1 jtc void *
135 1.1 jtc aresize(ptr, size, ap)
136 1.1 jtc register void *ptr;
137 1.1 jtc size_t size;
138 1.1 jtc Area *ap;
139 1.1 jtc {
140 1.1 jtc int cells;
141 1.1 jtc register Cell *dp = (Cell*) ptr;
142 1.1 jtc
143 1.1 jtc if (size <= 0) {
144 1.1 jtc aerror(ap, "allocate bad size");
145 1.1 jtc return NULL;
146 1.1 jtc }
147 1.1 jtc cells = (unsigned)(size - 1) / sizeof(Cell) + 1;
148 1.1 jtc
149 1.1 jtc if (dp == NULL || (dp-1)->size < cells) { /* enlarge object */
150 1.1 jtc /* XXX check for available adjacent free block */
151 1.1 jtc ptr = alloc(size, ap);
152 1.1 jtc if (dp != NULL) {
153 1.1 jtc memcpy(ptr, dp, (dp-1)->size * sizeof(Cell));
154 1.1 jtc afree((void *) dp, ap);
155 1.1 jtc }
156 1.1 jtc } else { /* shrink object */
157 1.1 jtc int split;
158 1.1 jtc
159 1.1 jtc split = (dp-1)->size - cells;
160 1.1 jtc if (--split <= 0) /* cannot split */
161 1.1 jtc ;
162 1.1 jtc else { /* shrink head, free tail */
163 1.1 jtc (dp-1)->size = cells;
164 1.1 jtc dp += cells + 1;
165 1.1 jtc (dp-1)->size = split;
166 1.1 jtc afree((void*)dp, ap);
167 1.1 jtc }
168 1.1 jtc }
169 1.1 jtc return (void*) ptr;
170 1.1 jtc }
171 1.1 jtc
172 1.1 jtc void
173 1.1 jtc afree(ptr, ap)
174 1.1 jtc void *ptr;
175 1.1 jtc register Area *ap;
176 1.1 jtc {
177 1.1 jtc register Block *bp;
178 1.1 jtc register Cell *fp, *fpp;
179 1.1 jtc register Cell *dp = (Cell*)ptr;
180 1.1 jtc
181 1.1 jtc /* find Block containing Cell */
182 1.1 jtc for (bp = ap->freelist; ; bp = bp->next) {
183 1.1 jtc if (bp->cell <= dp && dp < bp->last)
184 1.1 jtc break;
185 1.1 jtc if (bp->next == ap->freelist) {
186 1.1 jtc aerror(ap, "freeing with invalid area");
187 1.1 jtc return;
188 1.1 jtc }
189 1.1 jtc }
190 1.1 jtc
191 1.1 jtc /* find position in free list */
192 1.1 jtc for (fpp = NULL, fp = bp->freelist; fp < dp; fpp = fp, fp = fpp->next)
193 1.1 jtc ;
194 1.1 jtc
195 1.1 jtc if (fp == dp) {
196 1.1 jtc aerror(ap, "freeing free object");
197 1.1 jtc return;
198 1.1 jtc }
199 1.1 jtc
200 1.1 jtc /* join object with next */
201 1.1 jtc if (dp + (dp-1)->size == fp-1) { /* adjacent */
202 1.1 jtc (dp-1)->size += (fp-1)->size + 1;
203 1.1 jtc dp->next = fp->next;
204 1.1 jtc } else /* non-adjacent */
205 1.1 jtc dp->next = fp;
206 1.1 jtc
207 1.1 jtc /* join previous with object */
208 1.1 jtc if (fpp == NULL)
209 1.1 jtc bp->freelist = dp;
210 1.1 jtc else if (fpp + (fpp-1)->size == dp-1) { /* adjacent */
211 1.1 jtc (fpp-1)->size += (dp-1)->size + 1;
212 1.1 jtc fpp->next = dp->next;
213 1.1 jtc } else /* non-adjacent */
214 1.1 jtc fpp->next = dp;
215 1.1 jtc }
216 1.1 jtc
217 1.1 jtc #if DEBUG_ALLOC
218 1.1 jtc void
219 1.1 jtc aprint(ap, ptr, size)
220 1.1 jtc register Area *ap;
221 1.1 jtc void *ptr;
222 1.1 jtc size_t size;
223 1.1 jtc {
224 1.1 jtc Block *bp;
225 1.1 jtc
226 1.1 jtc if (!ap)
227 1.1 jtc shellf("aprint: null area pointer\n");
228 1.1 jtc else if (!(bp = ap->freelist))
229 1.1 jtc shellf("aprint: null area freelist\n");
230 1.1 jtc else if (bp == &aempty)
231 1.1 jtc shellf("aprint: area is empty\n");
232 1.1 jtc else {
233 1.1 jtc int i;
234 1.1 jtc Cell *fp;
235 1.1 jtc
236 1.1 jtc for (i = 0; !i || bp != ap->freelist; bp = bp->next, i++) {
237 1.1 jtc if (ptr) {
238 1.1 jtc void *eptr = (void *) (((char *) ptr) + size);
239 1.1 jtc /* print block only if it overlaps ptr/size */
240 1.1 jtc if (!((ptr >= (void *) bp
241 1.1 jtc && ptr <= (void *) bp->last)
242 1.1 jtc || (eptr >= (void *) bp
243 1.1 jtc && eptr <= (void *) bp->last)))
244 1.1 jtc continue;
245 1.1 jtc shellf("aprint: overlap of 0x%p .. 0x%p\n",
246 1.1 jtc ptr, eptr);
247 1.1 jtc }
248 1.1 jtc shellf("aprint: block %2d: 0x%p .. 0x%p (%d)\n", i,
249 1.1 jtc bp->cell, bp->last,
250 1.1 jtc (char *) bp->last - (char *) bp->cell);
251 1.1 jtc for (fp = bp->freelist; fp != bp->last; fp = fp->next)
252 1.1 jtc shellf(
253 1.1 jtc "aprint: 0x%p .. 0x%p (%d) free\n",
254 1.1 jtc (fp-1), (fp-1) + (fp-1)->size,
255 1.1 jtc (fp-1)->size * sizeof(Cell));
256 1.1 jtc }
257 1.1 jtc }
258 1.1 jtc }
259 1.1 jtc #endif /* DEBUG_ALLOC */
260 1.1 jtc
261 1.1 jtc
262 1.1 jtc #ifdef TEST_ALLOC
263 1.1 jtc
264 1.1 jtc Area a;
265 1.1 jtc
266 1.1 jtc main(int argc, char **argv) {
267 1.1 jtc int i;
268 1.1 jtc char *p [9];
269 1.1 jtc
270 1.1 jtc ainit(&a);
271 1.1 jtc for (i = 0; i < 9; i++) {
272 1.1 jtc p[i] = alloc(124, &a);
273 1.1 jtc printf("alloc: %x\n", p[i]);
274 1.1 jtc }
275 1.1 jtc for (i = 1; i < argc; i++)
276 1.1 jtc afree(p[atoi(argv[i])], &a);
277 1.1 jtc afreeall(&a);
278 1.1 jtc return 0;
279 1.1 jtc }
280 1.1 jtc
281 1.1 jtc void aerror(Area *ap, const char *msg) {
282 1.1 jtc abort();
283 1.1 jtc }
284 1.1 jtc
285 1.1 jtc #endif
286 1.1 jtc
287