]> jfr.im git - irc/rqf/shadowircd.git/blob - libcharybdis/balloc.h
[svn] - the new plan:
[irc/rqf/shadowircd.git] / libcharybdis / balloc.h
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * balloc.h: The ircd block allocator header.
4 *
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
8 *
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.
13 *
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.
18 *
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
23 *
24 * $Id: balloc.h 1781 2006-07-30 18:07:38Z jilles $
25 */
26
27 #ifndef INCLUDED_blalloc_h
28 #define INCLUDED_blalloc_h
29
30 #include "setup.h"
31 #include "tools.h"
32 #include "memory.h"
33 #include "ircd_defs.h"
34
35 #define CACHEFILE_HEAP_SIZE 32
36 #define CACHELINE_HEAP_SIZE 64
37
38
39 #ifdef NOBALLOC
40
41 typedef struct BlockHeap BlockHeap;
42 #define initBlockHeap()
43 #define BlockHeapGarbageCollect(x)
44 #define BlockHeapCreate(es, epb) ((BlockHeap*)(es))
45 #define BlockHeapDestroy(x)
46 #define BlockHeapAlloc(x) MyMalloc((int)x)
47 #define BlockHeapFree(x,y) MyFree(y)
48 #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)
49 typedef struct MemBlock
50 {
51 void *dummy;
52 } MemBlock;
53
54 #else
55
56 #undef DEBUG_BALLOC
57
58 #ifdef DEBUG_BALLOC
59 #define BALLOC_MAGIC 0x3d3a3c3d
60 #define BALLOC_FREE_MAGIC 0xafafafaf
61 #endif
62
63 /* status information for an allocated block in heap */
64 struct Block
65 {
66 size_t alloc_size;
67 struct Block *next; /* Next in our chain of blocks */
68 void *elems; /* Points to allocated memory */
69 dlink_list free_list;
70 dlink_list used_list;
71 };
72 typedef struct Block Block;
73
74 struct MemBlock
75 {
76 #ifdef DEBUG_BALLOC
77 unsigned long magic;
78 #endif
79 dlink_node self;
80 Block *block; /* Which block we belong to */
81 };
82
83 typedef struct MemBlock MemBlock;
84
85 /* information for the root node of the heap */
86 struct BlockHeap
87 {
88 dlink_node hlist;
89 size_t elemSize; /* Size of each element to be stored */
90 unsigned long elemsPerBlock; /* Number of elements per block */
91 unsigned long blocksAllocated; /* Number of blocks allocated */
92 unsigned long freeElems; /* Number of free elements */
93 Block *base; /* Pointer to first block */
94 };
95 typedef struct BlockHeap BlockHeap;
96
97 extern int BlockHeapFree(BlockHeap * bh, void *ptr);
98 extern void *BlockHeapAlloc(BlockHeap * bh);
99
100 extern BlockHeap *BlockHeapCreate(size_t elemsize, int elemsperblock);
101 extern int BlockHeapDestroy(BlockHeap * bh);
102
103 extern void initBlockHeap(void);
104 extern void BlockHeapUsage(BlockHeap * bh, size_t * bused, size_t * bfree, size_t * bmemusage);
105
106
107
108 #endif /* NOBALLOC */
109
110
111
112 #endif /* INCLUDED_blalloc_h */