Clean a bit - to be continued...
[seven-1.x.git] / libseven / balloc.h
blob9d30f533b9bdebec379d25956243ef6e3d45357e
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * balloc.h: The ircd block allocator header.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2004 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
26 #ifndef INCLUDED_blalloc_h
27 #define INCLUDED_blalloc_h
29 #include "setup.h"
30 #include "tools.h"
31 #include "memory.h"
32 #include "ircd_defs.h"
34 #define CACHEFILE_HEAP_SIZE 32
35 #define CACHELINE_HEAP_SIZE 64
38 #ifdef NOBALLOC
40 typedef struct BlockHeap BlockHeap;
41 #define initBlockHeap()
42 #define BlockHeapGarbageCollect(x)
43 #define BlockHeapCreate(es, epb) ((BlockHeap*)(es))
44 #define BlockHeapDestroy(x)
45 #define BlockHeapAlloc(x) MyMalloc((int)x)
46 #define BlockHeapFree(x,y) MyFree(y)
47 #define BlockHeapUsage(bh, bused, bfree, bmemusage) do { if (bused) (*(size_t *)bused) = 0; if (bfree) *((size_t *)bfree) = 0; if (bmemusage) *((size_t *)bmemusage) = 0; } while(0)
48 typedef struct MemBlock
50 void *dummy;
51 } MemBlock;
53 #else
55 #undef DEBUG_BALLOC
57 #ifdef DEBUG_BALLOC
58 #define BALLOC_MAGIC 0x3d3a3c3d
59 #define BALLOC_FREE_MAGIC 0xafafafaf
60 #endif
62 /* status information for an allocated block in heap */
63 struct Block
65 size_t alloc_size;
66 struct Block *next; /* Next in our chain of blocks */
67 void *elems; /* Points to allocated memory */
68 dlink_list free_list;
69 dlink_list used_list;
71 typedef struct Block Block;
73 struct MemBlock
75 #ifdef DEBUG_BALLOC
76 unsigned long magic;
77 #endif
78 dlink_node self;
79 Block *block; /* Which block we belong to */
82 typedef struct MemBlock MemBlock;
84 /* information for the root node of the heap */
85 struct BlockHeap
87 dlink_node hlist;
88 size_t elemSize; /* Size of each element to be stored */
89 unsigned long elemsPerBlock; /* Number of elements per block */
90 unsigned long blocksAllocated; /* Number of blocks allocated */
91 unsigned long freeElems; /* Number of free elements */
92 Block *base; /* Pointer to first block */
94 typedef struct BlockHeap BlockHeap;
96 extern int BlockHeapFree(BlockHeap * bh, void *ptr);
97 extern void *BlockHeapAlloc(BlockHeap * bh);
99 extern BlockHeap *BlockHeapCreate(size_t elemsize, int elemsperblock);
100 extern int BlockHeapDestroy(BlockHeap * bh);
102 extern void initBlockHeap(void);
103 extern void BlockHeapUsage(BlockHeap * bh, size_t * bused, size_t * bfree, size_t * bmemusage);
107 #endif /* NOBALLOC */
111 #endif /* INCLUDED_blalloc_h */