Annotation of embedaddon/trafshow/recycle.h, revision 1.1

1.1     ! misho       1: /*
        !             2: --------------------------------------------------------------------
        !             3: By Bob Jenkins, September 1996.  recycle.h
        !             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 all structures
        !            12:   only requires freeing the root.
        !            13: --------------------------------------------------------------------
        !            14: */
        !            15: 
        !            16: #ifndef STANDARD
        !            17: #include "standard.h"
        !            18: #endif
        !            19: 
        !            20: #ifndef RECYCLE
        !            21: #define RECYCLE
        !            22: 
        !            23: #define RESTART    0
        !            24: #define REMAX      32000
        !            25: 
        !            26: struct recycle
        !            27: {
        !            28:    struct recycle *next;
        !            29: };
        !            30: typedef  struct recycle  recycle;
        !            31: 
        !            32: struct reroot
        !            33: {
        !            34:    struct recycle *list;     /* list of malloced blocks */
        !            35:    struct recycle *trash;    /* list of deleted items */
        !            36:    size_t          size;     /* size of an item */
        !            37:    size_t          logsize;  /* log_2 of number of items in a block */
        !            38:    word            numleft;  /* number of items left in this block */
        !            39: };
        !            40: typedef  struct reroot  reroot;
        !            41: 
        !            42: /* make a new recycling root */
        !            43: reroot  *remkroot(size_t mysize);
        !            44: 
        !            45: /* free a recycling root and all the items it has made */
        !            46: void     refree(struct reroot *r);
        !            47: 
        !            48: /* get a new (cleared) item from the root */
        !            49: #define renew(r) ((r)->numleft ? \
        !            50:    (((char *)((r)->list+1))+((r)->numleft-=(r)->size)) : renewx(r))
        !            51: 
        !            52: char    *renewx(struct reroot *r);
        !            53: 
        !            54: /* delete an item; let the root recycle it */
        !            55: /* void     redel(/o_ struct reroot *r, struct recycle *item _o/); */
        !            56: #define redel(root,item) { \
        !            57:    ((recycle *)item)->next=(root)->trash; \
        !            58:    (root)->trash=(recycle *)(item); \
        !            59: }
        !            60: 
        !            61: #endif  /* RECYCLE */

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