Home | History | Annotate | Line # | Download | only in ksh
      1 /*	$NetBSD: alloc.c,v 1.10 2007/12/12 22:55:42 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2002 Marc Espie.
      5  *
      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.
     14  *
     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.
     26  */
     27 
     28 /*
     29  * area-based allocation built on malloc/free
     30  */
     31 #include <sys/cdefs.h>
     32 __RCSID("$NetBSD: alloc.c,v 1.10 2007/12/12 22:55:42 lukem Exp $");
     33 
     34 #include "sh.h"
     35 
     36 struct link {
     37 	struct link *prev;
     38 	struct link *next;
     39 };
     40 
     41 Area *
     42 ainit(Area *ap)
     43 {
     44 	ap->freelist = NULL;
     45 	return ap;
     46 }
     47 
     48 void
     49 afreeall(Area *ap)
     50 {
     51 	struct link *l, *l2;
     52 
     53 	for (l = ap->freelist; l != NULL; l = l2) {
     54 		l2 = l->next;
     55 		free(l);
     56 	}
     57 	ap->freelist = NULL;
     58 }
     59 
     60 #define L2P(l)	( (void *)(((char *)(l)) + sizeof(struct link)) )
     61 #define P2L(p)	( (struct link *)(((char *)(p)) - sizeof(struct link)) )
     62 
     63 /* coverity[+alloc] */
     64 void *
     65 alloc(size_t size, Area *ap)
     66 {
     67 	struct link *l;
     68 
     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;
     77 
     78 	return L2P(l);
     79 }
     80 
     81 /* coverity[+alloc] */
     82 /* coverity[+free : arg-0] */
     83 void *
     84 aresize(void *ptr, size_t size, Area *ap)
     85 {
     86 	struct link *l, *l2, *lprev, *lnext;
     87 
     88 	if (ptr == NULL)
     89 		return alloc(size, ap);
     90 
     91 	l = P2L(ptr);
     92 	lprev = l->prev;
     93 	lnext = l->next;
     94 
     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;
    104 
    105 	return L2P(l2);
    106 }
    107 
    108 /* coverity[+free : arg-0] */
    109 void
    110 afree(void *ptr, Area *ap)
    111 {
    112 	struct link *l;
    113 
    114 	if (!ptr)
    115 		return;
    116 
    117 	l = P2L(ptr);
    118 
    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;
    125 
    126 	free(l);
    127 }
    128