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