]> jfr.im git - irc/rqf/shadowircd.git/blob - libratbox/src/balloc.c
7c06e307034fe0ee6e62be5e4640050d0e296703
[irc/rqf/shadowircd.git] / libratbox / 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 * $Id: balloc.c 25861 2008-08-06 19:51:44Z androsyn $
32 */
33
34 /*
35 * About the block allocator
36 *
37 * Basically we have three ways of getting memory off of the operating
38 * system. Below are this list of methods and the order of preference.
39 *
40 * 1. mmap() anonymous pages with the MMAP_ANON flag.
41 * 2. mmap() via the /dev/zero trick.
42 * 3. HeapCreate/HeapAlloc (on win32)
43 * 4. malloc()
44 *
45 * The advantages of 1 and 2 are this. We can munmap() the pages which will
46 * return the pages back to the operating system, thus reducing the size
47 * of the process as the memory is unused. malloc() on many systems just keeps
48 * a heap of memory to itself, which never gets given back to the OS, except on
49 * exit. This of course is bad, if say we have an event that causes us to allocate
50 * say, 200MB of memory, while our normal memory consumption would be 15MB. In the
51 * malloc() case, the amount of memory allocated to our process never goes down, as
52 * malloc() has it locked up in its heap. With the mmap() method, we can munmap()
53 * the block and return it back to the OS, thus causing our memory consumption to go
54 * down after we no longer need it.
55 *
56 *
57 *
58 */
59 #include <libratbox_config.h>
60 #include <ratbox_lib.h>
61
62 #ifdef HAVE_MMAP /* We've got mmap() that is good */
63 #include <sys/mman.h>
64 /* HP-UX sucks */
65 #ifdef MAP_ANONYMOUS
66 #ifndef MAP_ANON
67 #define MAP_ANON MAP_ANONYMOUS
68 #endif
69 #endif
70 #endif
71
72 static uintptr_t offset_pad;
73
74 /* status information for an allocated block in heap */
75 struct rb_heap_block
76 {
77 size_t alloc_size;
78 rb_dlink_node node;
79 unsigned long free_count;
80 void *elems; /* Points to allocated memory */
81 };
82 typedef struct rb_heap_block rb_heap_block;
83
84 /* information for the root node of the heap */
85 struct rb_bh
86 {
87 rb_dlink_node hlist;
88 size_t elemSize; /* Size of each element to be stored */
89 unsigned long elemsPerBlock; /* Number of elements per block */
90 rb_dlink_list block_list;
91 rb_dlink_list free_list;
92 char *desc;
93 };
94
95 #ifndef NOBALLOC
96 static int newblock(rb_bh * bh);
97 static void rb_bh_gc_event(void *unused);
98 #endif /* !NOBALLOC */
99 static rb_dlink_list *heap_lists;
100
101 #if defined(WIN32)
102 static HANDLE block_heap;
103 #endif
104
105 #define rb_bh_fail(x) _rb_bh_fail(x, __FILE__, __LINE__)
106
107 static void
108 _rb_bh_fail(const char *reason, const char *file, int line)
109 {
110 rb_lib_log("rb_heap_blockheap failure: %s (%s:%d)", reason, file, line);
111 abort();
112 }
113
114 #ifndef NOBALLOC
115 /*
116 * static inline void free_block(void *ptr, size_t size)
117 *
118 * Inputs: The block and its size
119 * Output: None
120 * Side Effects: Returns memory for the block back to the OS
121 */
122 static inline void
123 free_block(void *ptr, size_t size)
124 {
125 #ifdef HAVE_MMAP
126 munmap(ptr, size);
127 #else
128 #ifdef WIN32
129 HeapFree(block_heap, 0, ptr);
130 #else
131 free(ptr);
132 #endif
133 #endif
134 }
135 #endif /* !NOBALLOC */
136
137 /*
138 * void rb_init_bh(void)
139 *
140 * Inputs: None
141 * Outputs: None
142 * Side Effects: Initializes the block heap
143 */
144
145 void
146 rb_init_bh(void)
147 {
148 heap_lists = rb_malloc(sizeof(rb_dlink_list));
149 offset_pad = sizeof(void *);
150 /* XXX if you get SIGBUS when trying to use a long long..here is where you need to
151 * fix your shit
152 */
153 #ifdef __sparc__
154 if((offset_pad % __alignof__(long long)) != 0)
155 {
156 offset_pad += __alignof__(long long);
157 offset_pad &= ~(__alignof__(long long) - 1);
158 }
159 #endif
160
161 #ifndef NOBALLOC
162 #ifdef WIN32
163 block_heap = HeapCreate(HEAP_NO_SERIALIZE, 0, 0);
164 #endif
165 rb_event_addish("rb_bh_gc_event", rb_bh_gc_event, NULL, 300);
166 #endif /* !NOBALLOC */
167 }
168
169 #ifndef NOBALLOC
170 /*
171 * static inline void *get_block(size_t size)
172 *
173 * Input: Size of block to allocate
174 * Output: Pointer to new block
175 * Side Effects: None
176 */
177 static inline void *
178 get_block(size_t size)
179 {
180 void *ptr;
181 #ifdef HAVE_MMAP
182 #ifdef MAP_ANON
183 ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
184 #else
185 int zero_fd;
186 zero_fd = open("/dev/zero", O_RDWR);
187 if(zero_fd < 0)
188 rb_bh_fail("Failed opening /dev/zero");
189 ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, zero_fd, 0);
190 close(zero_fd);
191 #endif /* MAP_ANON */
192 if(ptr == MAP_FAILED)
193 ptr = NULL;
194 #else
195 #ifdef WIN32
196 ptr = HeapAlloc(block_heap, 0, size);
197 #else
198 ptr = malloc(size);
199 #endif
200 #endif
201 return(ptr);
202 }
203
204
205 static void
206 rb_bh_gc_event(void *unused)
207 {
208 rb_dlink_node *ptr;
209 RB_DLINK_FOREACH(ptr, heap_lists->head)
210 {
211 rb_bh_gc(ptr->data);
212 }
213 }
214
215 /* ************************************************************************ */
216 /* FUNCTION DOCUMENTATION: */
217 /* newblock */
218 /* Description: */
219 /* Allocates a new block for addition to a blockheap */
220 /* Parameters: */
221 /* bh (IN): Pointer to parent blockheap. */
222 /* Returns: */
223 /* 0 if successful, 1 if not */
224 /* ************************************************************************ */
225
226 static int
227 newblock(rb_bh * bh)
228 {
229 rb_heap_block *b;
230 unsigned long i;
231 uintptr_t offset;
232 rb_dlink_node *node;
233 /* Setup the initial data structure. */
234 b = rb_malloc(sizeof(rb_heap_block));
235
236 b->alloc_size = bh->elemsPerBlock * bh->elemSize;
237
238 b->elems = get_block(b->alloc_size);
239 if(rb_unlikely(b->elems == NULL))
240 {
241 return (1);
242 }
243 offset = (uintptr_t)b->elems;
244 /* Setup our blocks now */
245 for (i = 0; i < bh->elemsPerBlock; i++, offset += bh->elemSize)
246 {
247 *((void **)offset) = b;
248 node = (void *)(offset + offset_pad);
249 rb_dlinkAdd((void *)offset, node, &bh->free_list);
250 }
251 rb_dlinkAdd(b, &b->node, &bh->block_list);
252 b->free_count = bh->elemsPerBlock;
253 return (0);
254 }
255 #endif /* !NOBALLOC */
256
257 /* ************************************************************************ */
258 /* FUNCTION DOCUMENTATION: */
259 /* rb_bh_create */
260 /* Description: */
261 /* Creates a new blockheap from which smaller blocks can be allocated. */
262 /* Intended to be used instead of multiple calls to malloc() when */
263 /* performance is an issue. */
264 /* Parameters: */
265 /* elemsize (IN): Size of the basic element to be stored */
266 /* elemsperblock (IN): Number of elements to be stored in a single block */
267 /* of memory. When the blockheap runs out of free memory, it will */
268 /* allocate elemsize * elemsperblock more. */
269 /* Returns: */
270 /* Pointer to new rb_bh, or NULL if unsuccessful */
271 /* ************************************************************************ */
272 rb_bh *
273 rb_bh_create(size_t elemsize, int elemsperblock, const char *desc)
274 {
275 rb_bh *bh;
276 lrb_assert(elemsize > 0 && elemsperblock > 0);
277 lrb_assert(elemsize >= sizeof(rb_dlink_node));
278
279 /* Catch idiotic requests up front */
280 if((elemsize == 0) || (elemsperblock <= 0))
281 {
282 rb_bh_fail("Attempting to rb_bh_create idiotic sizes");
283 }
284
285 if(elemsize < sizeof(rb_dlink_node))
286 rb_bh_fail("Attempt to rb_bh_create smaller than sizeof(rb_dlink_node)");
287
288 /* Allocate our new rb_bh */
289 bh = rb_malloc(sizeof(rb_bh));
290 #ifndef NOBALLOC
291 elemsize += offset_pad;
292 if((elemsize % sizeof(void *)) != 0)
293 {
294 /* Pad to even pointer boundary */
295 elemsize += sizeof(void *);
296 elemsize &= ~(sizeof(void *) - 1);
297 }
298 #endif
299
300 bh->elemSize = elemsize;
301 bh->elemsPerBlock = elemsperblock;
302 if(desc != NULL)
303 bh->desc = rb_strdup(desc);
304
305 #ifndef NOBALLOC
306 /* Be sure our malloc was successful */
307 if(newblock(bh))
308 {
309 if(bh != NULL)
310 free(bh);
311 rb_lib_log("newblock() failed");
312 rb_outofmemory(); /* die.. out of memory */
313 }
314 #endif /* !NOBALLOC */
315
316 if(bh == NULL)
317 {
318 rb_bh_fail("bh == NULL when it shouldn't be");
319 }
320 rb_dlinkAdd(bh, &bh->hlist, heap_lists);
321 return (bh);
322 }
323
324 /* ************************************************************************ */
325 /* FUNCTION DOCUMENTATION: */
326 /* rb_bh_alloc */
327 /* Description: */
328 /* Returns a pointer to a struct within our rb_bh that's free for */
329 /* the taking. */
330 /* Parameters: */
331 /* bh (IN): Pointer to the Blockheap. */
332 /* Returns: */
333 /* Pointer to a structure (void *), or NULL if unsuccessful. */
334 /* ************************************************************************ */
335
336 void *
337 rb_bh_alloc(rb_bh * bh)
338 {
339 #ifndef NOBALLOC
340 rb_dlink_node *new_node;
341 rb_heap_block *block;
342 void *ptr;
343 #endif
344 lrb_assert(bh != NULL);
345 if(rb_unlikely(bh == NULL))
346 {
347 rb_bh_fail("Cannot allocate if bh == NULL");
348 }
349
350 #ifdef NOBALLOC
351 return(rb_malloc(bh->elemSize));
352 #else
353 if(bh->free_list.head == NULL)
354 {
355 /* Allocate new block and assign */
356 /* newblock returns 1 if unsuccessful, 0 if not */
357
358 if(rb_unlikely(newblock(bh)))
359 {
360 rb_lib_log("newblock() failed");
361 rb_outofmemory(); /* Well that didn't work either...bail */
362 }
363 if(bh->free_list.head == NULL)
364 {
365 rb_lib_log("out of memory after newblock()...");
366 rb_outofmemory();
367 }
368 }
369
370 new_node = bh->free_list.head;
371 block = new_node->data;
372 ptr = new_node->data + offset_pad;
373 rb_dlinkDelete(new_node, &bh->free_list);
374 memset(ptr, 0, bh->elemSize - offset_pad);
375 return(ptr);
376 #endif
377 }
378
379
380 /* ************************************************************************ */
381 /* FUNCTION DOCUMENTATION: */
382 /* rb_bh_free */
383 /* Description: */
384 /* Returns an element to the free pool, does not free() */
385 /* Parameters: */
386 /* bh (IN): Pointer to rb_bh containing element */
387 /* ptr (in): Pointer to element to be "freed" */
388 /* Returns: */
389 /* 0 if successful, 1 if element not contained within rb_bh. */
390 /* ************************************************************************ */
391 int
392 rb_bh_free(rb_bh * bh, void *ptr)
393 {
394 #ifndef NOBALLOC
395 rb_heap_block *block;
396 void *data;
397 #endif
398 lrb_assert(bh != NULL);
399 lrb_assert(ptr != NULL);
400
401 if(rb_unlikely(bh == NULL))
402 {
403 rb_lib_log("balloc.c:rb_bhFree() bh == NULL");
404 return (1);
405 }
406
407 if(rb_unlikely(ptr == NULL))
408 {
409 rb_lib_log("balloc.rb_bhFree() ptr == NULL");
410 return (1);
411 }
412
413 #ifdef NOBALLOC
414 rb_free(ptr);
415 #else
416 data = (void *)(ptr - offset_pad);
417 block = *(rb_heap_block **)data;
418 /* XXX */
419 if(rb_unlikely(!((uintptr_t)ptr >= (uintptr_t)block->elems && (uintptr_t)ptr < (uintptr_t)block->elems + (uintptr_t)block->alloc_size)))
420 {
421 rb_bh_fail("rb_bh_free() bogus pointer");
422 }
423 block->free_count++;
424 rb_dlinkAdd(data, (rb_dlink_node *)ptr, &bh->free_list);
425 #endif /* !NOBALLOC */
426 return (0);
427 }
428
429
430 /* ************************************************************************ */
431 /* FUNCTION DOCUMENTATION: */
432 /* rb_bhDestroy */
433 /* Description: */
434 /* Completely free()s a rb_bh. Use for cleanup. */
435 /* Parameters: */
436 /* bh (IN): Pointer to the rb_bh to be destroyed. */
437 /* Returns: */
438 /* 0 if successful, 1 if bh == NULL */
439 /* ************************************************************************ */
440 int
441 rb_bh_destroy(rb_bh * bh)
442 {
443 #ifndef NOBALLOC
444 rb_dlink_node *ptr, *next;
445 rb_heap_block *b;
446 #endif
447 if(bh == NULL)
448 return (1);
449
450 #ifndef NOBALLOC
451 RB_DLINK_FOREACH_SAFE(ptr, next, bh->block_list.head)
452 {
453 b = ptr->data;
454 free_block(b->elems, b->alloc_size);
455 rb_free(b);
456 }
457 #endif /* !NOBALLOC */
458
459 rb_dlinkDelete(&bh->hlist, heap_lists);
460 rb_free(bh->desc);
461 rb_free(bh);
462
463 return (0);
464 }
465
466 void
467 rb_bh_usage(rb_bh * bh, size_t * bused, size_t * bfree, size_t * bmemusage, const char **desc)
468 {
469 size_t used, freem, memusage;
470
471 if(bh == NULL)
472 {
473 return;
474 }
475
476 freem = rb_dlink_list_length(&bh->free_list);
477 used = (rb_dlink_list_length(&bh->block_list) * bh->elemsPerBlock) - freem;
478 memusage = used * bh->elemSize;
479 if(bused != NULL)
480 *bused = used;
481 if(bfree != NULL)
482 *bfree = freem;
483 if(bmemusage != NULL)
484 *bmemusage = memusage;
485 if(desc != NULL)
486 *desc = bh->desc;
487 }
488
489 void rb_bh_usage_all(rb_bh_usage_cb *cb, void *data)
490 {
491 rb_dlink_node *ptr;
492 rb_bh *bh;
493 size_t used, freem, memusage, heapalloc;
494 static const char *unnamed = "(unnamed_heap)";
495 const char *desc = unnamed;
496
497 if(cb == NULL)
498 return;
499
500 RB_DLINK_FOREACH(ptr, heap_lists->head)
501 {
502 bh = (rb_bh *)ptr->data;
503 freem = rb_dlink_list_length(&bh->free_list);
504 used = (rb_dlink_list_length(&bh->block_list) * bh->elemsPerBlock) - freem;
505 memusage = used * bh->elemSize;
506 heapalloc = (freem + used) * bh->elemSize;
507 if(bh->desc != NULL)
508 desc = bh->desc;
509 cb(used, freem, memusage, heapalloc, desc, data);
510 }
511 return;
512 }
513
514 void
515 rb_bh_total_usage(size_t *total_alloc, size_t *total_used)
516 {
517 rb_dlink_node *ptr;
518 size_t total_memory = 0, used_memory = 0, used, freem;
519 rb_bh *bh;
520
521 RB_DLINK_FOREACH(ptr, heap_lists->head)
522 {
523 bh = (rb_bh *)ptr->data;
524 freem = rb_dlink_list_length(&bh->free_list);
525 used = (rb_dlink_list_length(&bh->block_list) * bh->elemsPerBlock) - freem;
526 used_memory += used * bh->elemSize;
527 total_memory += (freem + used) * bh->elemSize;
528 }
529
530 if(total_alloc != NULL)
531 *total_alloc = total_memory;
532 if(total_used != NULL)
533 *total_used = used_memory;
534 }
535
536 #ifndef NOBALLOC
537 int
538 rb_bh_gc(rb_bh * bh)
539 {
540 rb_heap_block *b;
541 rb_dlink_node *ptr, *next;
542 unsigned long i;
543 uintptr_t offset;
544
545 if(bh == NULL)
546 {
547 /* somebody is smoking some craq..(probably lee, but don't tell him that) */
548 return (1);
549 }
550
551 if((rb_dlink_list_length(&bh->free_list) < bh->elemsPerBlock) || rb_dlink_list_length(&bh->block_list) == 1)
552 {
553 /* There couldn't possibly be an entire free block. Return. */
554 return (0);
555 }
556
557 RB_DLINK_FOREACH_SAFE(ptr, next, bh->block_list.head)
558 {
559 b = ptr->data;
560 if(rb_dlink_list_length(&bh->block_list) == 1)
561 return (0);
562
563 if(b->free_count == bh->elemsPerBlock)
564 {
565 /* i'm seriously going to hell for this.. */
566
567 offset = (uintptr_t)b->elems;
568 for (i = 0; i < bh->elemsPerBlock; i++, offset += (uintptr_t)bh->elemSize)
569 {
570 rb_dlinkDelete(((rb_dlink_node *)offset), &bh->free_list);
571 }
572 rb_dlinkDelete(&b->node, &bh->block_list);
573 free_block(b->elems, b->alloc_size);
574 rb_free(b);
575 }
576
577 }
578 return (0);
579 }
580 #endif /* !NOBALLOC */