File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / trafshow / recycle.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Feb 21 16:55:18 2012 UTC (12 years, 3 months ago) by misho
Branches: trafshow, MAIN
CVS tags: v5_2_3p0, v5_2_3, HEAD
trafshow

    1: /*
    2: --------------------------------------------------------------------
    3: By Bob Jenkins, September 1996.  recycle.c
    4: You may use this code in any way you wish, and it is free.  No warranty.
    5: 
    6: This manages memory for commonly-allocated structures.
    7: It allocates RESTART to REMAX items at a time.
    8: Timings have shown that, if malloc is used for every new structure,
    9:   malloc will consume about 90% of the time in a program.  This
   10:   module cuts down the number of mallocs by an order of magnitude.
   11: This also decreases memory fragmentation, and freeing structures
   12:   only requires freeing the root.
   13: --------------------------------------------------------------------
   14: */
   15: 
   16: #include <stdlib.h>
   17: #include <string.h>
   18: 
   19: #ifndef STANDARD
   20: # include "standard.h"
   21: #endif
   22: #ifndef RECYCLE
   23: # include "recycle.h"
   24: #endif
   25: 
   26: reroot *remkroot(size)
   27: size_t  size;
   28: {
   29:    reroot *r = (reroot *)malloc(sizeof(reroot));
   30:    if (r) {
   31: 	r->list = (recycle *)0;
   32: 	r->trash = (recycle *)0;
   33: 	r->size = align(size);
   34: 	r->logsize = RESTART;
   35: 	r->numleft = 0;
   36:    }
   37:    return r;
   38: }
   39: 
   40: void  refree(r)
   41: struct reroot *r;
   42: {
   43:    recycle *temp = r->list;
   44:    if (temp) while (r->list)
   45:    {
   46:       temp = r->list->next;
   47:       free((char *)r->list);
   48:       r->list = temp;
   49:    }
   50:    free((char *)r);
   51:    return;
   52: }
   53: 
   54: /* to be called from the macro renew only */
   55: char  *renewx(r)
   56: struct reroot *r;
   57: {
   58:    recycle *temp;
   59:    if (r->trash)
   60:    {  /* pull a node off the trash heap */
   61:       temp = r->trash;
   62:       r->trash = temp->next;
   63:       (void)memset((void *)temp, 0, r->size);
   64:    }
   65:    else
   66:    {  /* allocate a new block of nodes */
   67:       r->numleft = r->size*((ub4)1<<r->logsize);
   68:       if (r->numleft < REMAX) ++r->logsize;
   69:       temp = (recycle *)malloc(sizeof(recycle) + r->numleft);
   70:       if (!temp) return 0;
   71:       temp->next = r->list;
   72:       r->list = temp;
   73:       r->numleft-=r->size;
   74:       temp = (recycle *)((char *)(r->list+1)+r->numleft);
   75:    }
   76:    return (char *)temp;
   77: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>