From Tom Brezinski:
[obnox/wireshark/wip.git] / epan / slab.h
1 /* slab.h
2  * Definitions for very simple slab handling
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifndef __SLAB_H__
26 #define __SLAB_H__
27
28 #define NITEMS_PER_SLAB 100
29
30 /*
31  * Generate declaration of a union type containing the specified type of
32  * slab-allocated item, and a pointer to an object of that type, for use
33  * in the macros below.
34  */
35 #define SLAB_ITEM_TYPE_DEFINE(type)                     \
36         union type ## slab_item {                       \
37                 type slab_item;                         \
38                 union type ## slab_item *next_free;     \
39         };
40
41 /*
42  * Generate definition of the free list pointer.
43  */
44 #define SLAB_FREE_LIST_DEFINE(type)             \
45         union type ## slab_item *type ## _free_list = NULL;
46
47 /*
48  * Generate an external declaration of the free list pointer.
49  */
50 #define SLAB_FREE_LIST_DECLARE(type)            \
51         union type ## slab_item *type ## _free_list;
52
53 /* we never free any memory we have allocated, when it is returned to us
54    we just store it in the free list until (hopefully) it gets used again
55 */
56 #define SLAB_ALLOC(item, type)                                  \
57         if(!type ## _free_list){                                \
58                 int i;                                          \
59                 union type ## slab_item *tmp;                   \
60                 tmp=g_malloc(NITEMS_PER_SLAB*sizeof(*tmp));     \
61                 for(i=0;i<NITEMS_PER_SLAB;i++){                 \
62                         tmp[i].next_free = type ## _free_list;  \
63                         type ## _free_list = &tmp[i];           \
64                 }                                               \
65         }                                                       \
66         item = &(type ## _free_list->slab_item);                \
67         type ## _free_list = type ## _free_list->next_free;
68
69 #define SLAB_FREE(item, type)                                           \
70 {                                                                       \
71         ((union type ## slab_item *)(void *)item)->next_free = type ## _free_list;      \
72         type ## _free_list = (union type ## slab_item *)(void *)item;           \
73 }
74
75 #endif /* slab.h */