xref: /openbsd/bin/ksh/alloc.c (revision 322c3a8a)
1 /*	$OpenBSD: alloc.c,v 1.9 2015/10/16 03:17:56 mmcc Exp $	*/
2 /*
3  * Copyright (c) 2002 Marc Espie.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
15  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
18  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /*
28  * area-based allocation built on malloc/free
29  */
30 
31 #include <stdint.h>
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 /*
79  * Copied from calloc().
80  *
81  * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
82  * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
83  */
84 #define MUL_NO_OVERFLOW	(1UL << (sizeof(size_t) * 4))
85 
86 void *
87 allocarray(size_t nmemb, size_t size, Area *ap)
88 {
89 	/* condition logic cloned from calloc() */
90 	if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
91 	    nmemb > 0 && SIZE_MAX / nmemb < size) {
92 		internal_errorf(1, "unable to allocate memory");
93 	}
94 
95 	/* additional check because alloc() allocates space for link */
96 	if (nmemb * size > SIZE_MAX - sizeof(struct link))
97 		internal_errorf(1, "unable to allocate memory");
98 
99 	return alloc(nmemb * size, ap);
100 }
101 
102 void *
103 aresize(void *ptr, size_t size, Area *ap)
104 {
105 	struct link *l, *l2, *lprev, *lnext;
106 
107 	if (ptr == NULL)
108 		return alloc(size, ap);
109 
110 	l = P2L(ptr);
111 	lprev = l->prev;
112 	lnext = l->next;
113 
114 	l2 = realloc(l, sizeof(struct link) + size);
115 	if (l2 == NULL)
116 		internal_errorf(1, "unable to allocate memory");
117 	if (lprev)
118 		lprev->next = l2;
119 	else
120 		ap->freelist = l2;
121 	if (lnext)
122 		lnext->prev = l2;
123 
124 	return L2P(l2);
125 }
126 
127 void
128 afree(void *ptr, Area *ap)
129 {
130 	struct link *l, *l2;
131 
132 	if (!ptr)
133 		return;
134 
135 	l = P2L(ptr);
136 
137 	for (l2 = ap->freelist; l2 != NULL; l2 = l2->next) {
138 		if (l == l2)
139 			break;
140 	}
141 	if (l2 == NULL)
142 		internal_errorf(1, "afree: %p not present in area %p", ptr, ap);
143 
144 	if (l->prev)
145 		l->prev->next = l->next;
146 	else
147 		ap->freelist = l->next;
148 	if (l->next)
149 		l->next->prev = l->prev;
150 
151 	free(l);
152 }
153