]> jfr.im git - solanum.git/blob - librb/src/balloc.c
Clean up the provider status logic.
[solanum.git] / librb / src / balloc.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * balloc.c: A block allocator.
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-2006 ircd-ratbox development team
8 *
9 * Below are the orignal headers from the old blalloc.c
10 *
11 * File: blalloc.c
12 * Owner: Wohali (Joan Touzet)
13 *
14 * Modified 2001/11/29 for mmap() support by Aaron Sethman <androsyn@ratbox.org>
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
29 * USA
30 *
31 */
32
33 /*
34 * About the block allocator
35 *
36 * Basically we have three ways of getting memory off of the operating
37 * system. Below are this list of methods and the order of preference.
38 *
39 * 1. mmap() anonymous pages with the MMAP_ANON flag.
40 * 2. mmap() via the /dev/zero trick.
41 * 3. HeapCreate/HeapAlloc (on win32)
42 * 4. malloc()
43 *
44 * The advantages of 1 and 2 are this. We can munmap() the pages which will
45 * return the pages back to the operating system, thus reducing the size
46 * of the process as the memory is unused. malloc() on many systems just keeps
47 * a heap of memory to itself, which never gets given back to the OS, except on
48 * exit. This of course is bad, if say we have an event that causes us to allocate
49 * say, 200MB of memory, while our normal memory consumption would be 15MB. In the
50 * malloc() case, the amount of memory allocated to our process never goes down, as
51 * malloc() has it locked up in its heap. With the mmap() method, we can munmap()
52 * the block and return it back to the OS, thus causing our memory consumption to go
53 * down after we no longer need it.
54 *
55 *
56 *
57 */
58 #include <librb_config.h>
59 #include <rb_lib.h>
60
61 static uintptr_t offset_pad;
62
63 /* status information for an allocated block in heap */
64 struct rb_heap_block
65 {
66 size_t alloc_size;
67 rb_dlink_node node;
68 unsigned long free_count;
69 void *elems; /* Points to allocated memory */
70 };
71 typedef struct rb_heap_block rb_heap_block;
72
73 /* information for the root node of the heap */
74 struct rb_bh
75 {
76 rb_dlink_node hlist;
77 size_t elemSize; /* Size of each element to be stored */
78 unsigned long elemsPerBlock; /* Number of elements per block */
79 rb_dlink_list block_list;
80 rb_dlink_list free_list;
81 char *desc;
82 };
83
84 static rb_dlink_list *heap_lists;
85
86 #define rb_bh_fail(x) _rb_bh_fail(x, __FILE__, __LINE__)
87
88 static void
89 _rb_bh_fail(const char *reason, const char *file, int line)
90 {
91 rb_lib_log("rb_heap_blockheap failure: %s (%s:%d)", reason, file, line);
92 abort();
93 }
94
95 /*
96 * void rb_init_bh(void)
97 *
98 * Inputs: None
99 * Outputs: None
100 * Side Effects: Initializes the block heap
101 */
102
103 void
104 rb_init_bh(void)
105 {
106 heap_lists = rb_malloc(sizeof(rb_dlink_list));
107 offset_pad = sizeof(void *);
108 /* XXX if you get SIGBUS when trying to use a long long..here is where you need to
109 * fix your shit
110 */
111 #ifdef __sparc__
112 if((offset_pad % __alignof__(long long)) != 0)
113 {
114 offset_pad += __alignof__(long long);
115 offset_pad &= ~(__alignof__(long long) - 1);
116 }
117 #endif
118 }
119
120 /* ************************************************************************ */
121 /* FUNCTION DOCUMENTATION: */
122 /* rb_bh_create */
123 /* Description: */
124 /* Creates a new blockheap from which smaller blocks can be allocated. */
125 /* Intended to be used instead of multiple calls to malloc() when */
126 /* performance is an issue. */
127 /* Parameters: */
128 /* elemsize (IN): Size of the basic element to be stored */
129 /* elemsperblock (IN): Number of elements to be stored in a single block */
130 /* of memory. When the blockheap runs out of free memory, it will */
131 /* allocate elemsize * elemsperblock more. */
132 /* Returns: */
133 /* Pointer to new rb_bh, or NULL if unsuccessful */
134 /* ************************************************************************ */
135 rb_bh *
136 rb_bh_create(size_t elemsize, int elemsperblock, const char *desc)
137 {
138 rb_bh *bh;
139 lrb_assert(elemsize > 0 && elemsperblock > 0);
140 lrb_assert(elemsize >= sizeof(rb_dlink_node));
141
142 /* Catch idiotic requests up front */
143 if((elemsize == 0) || (elemsperblock <= 0))
144 {
145 rb_bh_fail("Attempting to rb_bh_create idiotic sizes");
146 }
147
148 if(elemsize < sizeof(rb_dlink_node))
149 rb_bh_fail("Attempt to rb_bh_create smaller than sizeof(rb_dlink_node)");
150
151 /* Allocate our new rb_bh */
152 bh = rb_malloc(sizeof(rb_bh));
153 bh->elemSize = elemsize;
154 bh->elemsPerBlock = elemsperblock;
155 if(desc != NULL)
156 bh->desc = rb_strdup(desc);
157
158 if(bh == NULL)
159 {
160 rb_bh_fail("bh == NULL when it shouldn't be");
161 }
162 rb_dlinkAdd(bh, &bh->hlist, heap_lists);
163 return (bh);
164 }
165
166 /* ************************************************************************ */
167 /* FUNCTION DOCUMENTATION: */
168 /* rb_bh_alloc */
169 /* Description: */
170 /* Returns a pointer to a struct within our rb_bh that's free for */
171 /* the taking. */
172 /* Parameters: */
173 /* bh (IN): Pointer to the Blockheap. */
174 /* Returns: */
175 /* Pointer to a structure (void *), or NULL if unsuccessful. */
176 /* ************************************************************************ */
177
178 void *
179 rb_bh_alloc(rb_bh *bh)
180 {
181 lrb_assert(bh != NULL);
182 if(rb_unlikely(bh == NULL))
183 {
184 rb_bh_fail("Cannot allocate if bh == NULL");
185 }
186
187 return (rb_malloc(bh->elemSize));
188 }
189
190
191 /* ************************************************************************ */
192 /* FUNCTION DOCUMENTATION: */
193 /* rb_bh_free */
194 /* Description: */
195 /* Returns an element to the free pool, does not free() */
196 /* Parameters: */
197 /* bh (IN): Pointer to rb_bh containing element */
198 /* ptr (in): Pointer to element to be "freed" */
199 /* Returns: */
200 /* 0 if successful, 1 if element not contained within rb_bh. */
201 /* ************************************************************************ */
202 int
203 rb_bh_free(rb_bh *bh, void *ptr)
204 {
205 lrb_assert(bh != NULL);
206 lrb_assert(ptr != NULL);
207
208 if(rb_unlikely(bh == NULL))
209 {
210 rb_lib_log("balloc.c:rb_bhFree() bh == NULL");
211 return (1);
212 }
213
214 if(rb_unlikely(ptr == NULL))
215 {
216 rb_lib_log("balloc.rb_bhFree() ptr == NULL");
217 return (1);
218 }
219
220 rb_free(ptr);
221 return (0);
222 }
223
224
225 /* ************************************************************************ */
226 /* FUNCTION DOCUMENTATION: */
227 /* rb_bhDestroy */
228 /* Description: */
229 /* Completely free()s a rb_bh. Use for cleanup. */
230 /* Parameters: */
231 /* bh (IN): Pointer to the rb_bh to be destroyed. */
232 /* Returns: */
233 /* 0 if successful, 1 if bh == NULL */
234 /* ************************************************************************ */
235 int
236 rb_bh_destroy(rb_bh *bh)
237 {
238 if(bh == NULL)
239 return (1);
240
241 rb_dlinkDelete(&bh->hlist, heap_lists);
242 rb_free(bh->desc);
243 rb_free(bh);
244
245 return (0);
246 }
247
248 void
249 rb_bh_usage(rb_bh *bh, size_t *bused, size_t *bfree, size_t *bmemusage, const char **desc)
250 {
251 if(bused != NULL)
252 *bused = 0;
253 if(bfree != NULL)
254 *bfree = 0;
255 if(bmemusage != NULL)
256 *bmemusage = 0;
257 if(desc != NULL)
258 *desc = "no blockheap";
259 }
260
261 void
262 rb_bh_usage_all(rb_bh_usage_cb *cb, void *data)
263 {
264 rb_dlink_node *ptr;
265 rb_bh *bh;
266 size_t used, freem, memusage, heapalloc;
267 static const char *unnamed = "(unnamed_heap)";
268 const char *desc = unnamed;
269
270 if(cb == NULL)
271 return;
272
273 RB_DLINK_FOREACH(ptr, heap_lists->head)
274 {
275 bh = (rb_bh *)ptr->data;
276 freem = rb_dlink_list_length(&bh->free_list);
277 used = (rb_dlink_list_length(&bh->block_list) * bh->elemsPerBlock) - freem;
278 memusage = used * bh->elemSize;
279 heapalloc = (freem + used) * bh->elemSize;
280 if(bh->desc != NULL)
281 desc = bh->desc;
282 cb(used, freem, memusage, heapalloc, desc, data);
283 }
284 return;
285 }
286
287 void
288 rb_bh_total_usage(size_t *total_alloc, size_t *total_used)
289 {
290 rb_dlink_node *ptr;
291 size_t total_memory = 0, used_memory = 0, used, freem;
292 rb_bh *bh;
293
294 RB_DLINK_FOREACH(ptr, heap_lists->head)
295 {
296 bh = (rb_bh *)ptr->data;
297 freem = rb_dlink_list_length(&bh->free_list);
298 used = (rb_dlink_list_length(&bh->block_list) * bh->elemsPerBlock) - freem;
299 used_memory += used * bh->elemSize;
300 total_memory += (freem + used) * bh->elemSize;
301 }
302
303 if(total_alloc != NULL)
304 *total_alloc = total_memory;
305 if(total_used != NULL)
306 *total_used = used_memory;
307 }