Do not use .Xo/.Xc to work around ancient roff limits.
[netbsd-mini2440.git] / bin / ksh / alloc.c
blob9c1ccfa3a52f4ebe06d76e991dedf42bb3421462
1 /* $NetBSD: alloc.c,v 1.9 2006/04/01 23:36:28 christos Exp $ */
3 /*
4 * Copyright (c) 2002 Marc Espie.
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.
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.
29 * area-based allocation built on malloc/free
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD$");
34 #include "sh.h"
36 struct link {
37 struct link *prev;
38 struct link *next;
41 Area *
42 ainit(Area *ap)
44 ap->freelist = NULL;
45 return ap;
48 void
49 afreeall(Area *ap)
51 struct link *l, *l2;
53 for (l = ap->freelist; l != NULL; l = l2) {
54 l2 = l->next;
55 free(l);
57 ap->freelist = NULL;
60 #define L2P(l) ( (void *)(((char *)(l)) + sizeof(struct link)) )
61 #define P2L(p) ( (struct link *)(((char *)(p)) - sizeof(struct link)) )
63 /* coverity[+alloc] */
64 void *
65 alloc(size_t size, Area *ap)
67 struct link *l;
69 l = malloc(sizeof(struct link) + size);
70 if (l == NULL)
71 internal_errorf(1, "unable to allocate memory");
72 l->next = ap->freelist;
73 l->prev = NULL;
74 if (ap->freelist)
75 ap->freelist->prev = l;
76 ap->freelist = l;
78 return L2P(l);
81 /* coverity[+alloc] */
82 /* coverity[+free : arg-0] */
83 void *
84 aresize(void *ptr, size_t size, Area *ap)
86 struct link *l, *l2, *lprev, *lnext;
88 if (ptr == NULL)
89 return alloc(size, ap);
91 l = P2L(ptr);
92 lprev = l->prev;
93 lnext = l->next;
95 l2 = realloc(l, sizeof(struct link) + size);
96 if (l2 == NULL)
97 internal_errorf(1, "unable to allocate memory");
98 if (lprev)
99 lprev->next = l2;
100 else
101 ap->freelist = l2;
102 if (lnext)
103 lnext->prev = l2;
105 return L2P(l2);
108 /* coverity[+free : arg-0] */
109 void
110 afree(void *ptr, Area *ap)
112 struct link *l;
114 if (!ptr)
115 return;
117 l = P2L(ptr);
119 if (l->prev)
120 l->prev->next = l->next;
121 else
122 ap->freelist = l->next;
123 if (l->next)
124 l->next->prev = l->prev;
126 free(l);