1 /* $NetBSD: alloc.c,v 1.9 2006/04/01 23:36:28 christos Exp $ */
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
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>
53 for (l
= ap
->freelist
; l
!= NULL
; l
= l2
) {
60 #define L2P(l) ( (void *)(((char *)(l)) + sizeof(struct link)) )
61 #define P2L(p) ( (struct link *)(((char *)(p)) - sizeof(struct link)) )
63 /* coverity[+alloc] */
65 alloc(size_t size
, Area
*ap
)
69 l
= malloc(sizeof(struct link
) + size
);
71 internal_errorf(1, "unable to allocate memory");
72 l
->next
= ap
->freelist
;
75 ap
->freelist
->prev
= l
;
81 /* coverity[+alloc] */
82 /* coverity[+free : arg-0] */
84 aresize(void *ptr
, size_t size
, Area
*ap
)
86 struct link
*l
, *l2
, *lprev
, *lnext
;
89 return alloc(size
, ap
);
95 l2
= realloc(l
, sizeof(struct link
) + size
);
97 internal_errorf(1, "unable to allocate memory");
108 /* coverity[+free : arg-0] */
110 afree(void *ptr
, Area
*ap
)
120 l
->prev
->next
= l
->next
;
122 ap
->freelist
= l
->next
;
124 l
->next
->prev
= l
->prev
;