2 * Definitions for very simple slab handling
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
28 #define NITEMS_PER_SLAB 100
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.
35 #define SLAB_ITEM_TYPE_DEFINE(type) \
36 union type ## slab_item { \
38 union type ## slab_item *next_free; \
42 * Generate definition of the free list pointer.
44 #define SLAB_FREE_LIST_DEFINE(type) \
45 union type ## slab_item *type ## _free_list = NULL;
48 * Generate an external declaration of the free list pointer.
50 #define SLAB_FREE_LIST_DECLARE(type) \
51 union type ## slab_item *type ## _free_list;
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
56 #define SLAB_ALLOC(item, type) \
57 if(!type ## _free_list){ \
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]; \
66 item = &(type ## _free_list->slab_item); \
67 type ## _free_list = type ## _free_list->next_free;
69 #define SLAB_FREE(item, type) \
71 ((union type ## slab_item *)item)->next_free = type ## _free_list; \
72 type ## _free_list = (union type ## slab_item *)item; \