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