2 * ircd-ratbox: A slightly useful ircd.
3 * balloc.c: A block allocator.
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
9 * Below are the orignal headers from the old blalloc.c
12 * Owner: Wohali (Joan Touzet)
14 * Modified 2001/11/29 for mmap() support by Aaron Sethman <androsyn@ratbox.org>
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.
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.
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
34 * About the block allocator
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.
39 * 1. mmap() anonymous pages with the MMAP_ANON flag.
40 * 2. mmap() via the /dev/zero trick.
41 * 3. HeapCreate/HeapAlloc (on win32)
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.
58 #include <libratbox_config.h>
59 #include <ratbox_lib.h>
62 #ifdef HAVE_MMAP /* We've got mmap() that is good */
67 #define MAP_ANON MAP_ANONYMOUS
73 static uintptr_t offset_pad
;
75 /* status information for an allocated block in heap */
80 unsigned long free_count
;
81 void *elems
; /* Points to allocated memory */
83 typedef struct rb_heap_block rb_heap_block
;
85 /* information for the root node of the heap */
89 size_t elemSize
; /* Size of each element to be stored */
90 unsigned long elemsPerBlock
; /* Number of elements per block */
91 rb_dlink_list block_list
;
92 rb_dlink_list free_list
;
97 static int newblock(rb_bh
*bh
);
98 static void rb_bh_gc_event(void *unused
);
99 #endif /* !NOBALLOC */
100 static rb_dlink_list
*heap_lists
;
103 static HANDLE block_heap
;
106 #define rb_bh_fail(x) _rb_bh_fail(x, __FILE__, __LINE__)
109 _rb_bh_fail(const char *reason
, const char *file
, int line
)
111 rb_lib_log("rb_heap_blockheap failure: %s (%s:%d)", reason
, file
, line
);
117 * static inline void free_block(void *ptr, size_t size)
119 * Inputs: The block and its size
121 * Side Effects: Returns memory for the block back to the OS
124 free_block(void *ptr
, size_t size
)
130 HeapFree(block_heap
, 0, ptr
);
136 #endif /* !NOBALLOC */
139 * void rb_init_bh(void)
143 * Side Effects: Initializes the block heap
149 heap_lists
= rb_malloc(sizeof(rb_dlink_list
));
150 offset_pad
= sizeof(void *);
151 /* XXX if you get SIGBUS when trying to use a long long..here is where you need to
155 if((offset_pad
% __alignof__(long long)) != 0)
157 offset_pad
+= __alignof__(long long);
158 offset_pad
&= ~(__alignof__(long long) - 1);
164 block_heap
= HeapCreate(HEAP_NO_SERIALIZE
, 0, 0);
166 rb_event_addish("rb_bh_gc_event", rb_bh_gc_event
, NULL
, 300);
167 #endif /* !NOBALLOC */
172 * static inline void *get_block(size_t size)
174 * Input: Size of block to allocate
175 * Output: Pointer to new block
179 get_block(size_t size
)
184 ptr
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_ANON
, -1, 0);
187 zero_fd
= open("/dev/zero", O_RDWR
);
189 rb_bh_fail("Failed opening /dev/zero");
190 ptr
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, zero_fd
, 0);
192 #endif /* MAP_ANON */
193 if(ptr
== MAP_FAILED
)
197 ptr
= HeapAlloc(block_heap
, 0, size
);
207 rb_bh_gc_event(void *unused
)
210 RB_DLINK_FOREACH(ptr
, heap_lists
->head
)
216 /* ************************************************************************ */
217 /* FUNCTION DOCUMENTATION: */
220 /* Allocates a new block for addition to a blockheap */
222 /* bh (IN): Pointer to parent blockheap. */
224 /* 0 if successful, 1 if not */
225 /* ************************************************************************ */
234 /* Setup the initial data structure. */
235 b
= rb_malloc(sizeof(rb_heap_block
));
237 b
->alloc_size
= bh
->elemsPerBlock
* bh
->elemSize
;
239 b
->elems
= get_block(b
->alloc_size
);
240 if(rb_unlikely(b
->elems
== NULL
))
244 offset
= (uintptr_t)b
->elems
;
245 /* Setup our blocks now */
246 for(i
= 0; i
< bh
->elemsPerBlock
; i
++, offset
+= bh
->elemSize
)
248 *((void **)offset
) = b
;
249 node
= (void *)(offset
+ offset_pad
);
250 rb_dlinkAdd((void *)offset
, node
, &bh
->free_list
);
252 rb_dlinkAdd(b
, &b
->node
, &bh
->block_list
);
253 b
->free_count
= bh
->elemsPerBlock
;
256 #endif /* !NOBALLOC */
258 /* ************************************************************************ */
259 /* FUNCTION DOCUMENTATION: */
262 /* Creates a new blockheap from which smaller blocks can be allocated. */
263 /* Intended to be used instead of multiple calls to malloc() when */
264 /* performance is an issue. */
266 /* elemsize (IN): Size of the basic element to be stored */
267 /* elemsperblock (IN): Number of elements to be stored in a single block */
268 /* of memory. When the blockheap runs out of free memory, it will */
269 /* allocate elemsize * elemsperblock more. */
271 /* Pointer to new rb_bh, or NULL if unsuccessful */
272 /* ************************************************************************ */
274 rb_bh_create(size_t elemsize
, int elemsperblock
, const char *desc
)
277 lrb_assert(elemsize
> 0 && elemsperblock
> 0);
278 lrb_assert(elemsize
>= sizeof(rb_dlink_node
));
280 /* Catch idiotic requests up front */
281 if((elemsize
== 0) || (elemsperblock
<= 0))
283 rb_bh_fail("Attempting to rb_bh_create idiotic sizes");
286 if(elemsize
< sizeof(rb_dlink_node
))
287 rb_bh_fail("Attempt to rb_bh_create smaller than sizeof(rb_dlink_node)");
289 /* Allocate our new rb_bh */
290 bh
= rb_malloc(sizeof(rb_bh
));
292 elemsize
+= offset_pad
;
293 if((elemsize
% sizeof(void *)) != 0)
295 /* Pad to even pointer boundary */
296 elemsize
+= sizeof(void *);
297 elemsize
&= ~(sizeof(void *) - 1);
301 bh
->elemSize
= elemsize
;
302 bh
->elemsPerBlock
= elemsperblock
;
304 bh
->desc
= rb_strdup(desc
);
307 /* Be sure our malloc was successful */
312 rb_lib_log("newblock() failed");
313 rb_outofmemory(); /* die.. out of memory */
315 #endif /* !NOBALLOC */
319 rb_bh_fail("bh == NULL when it shouldn't be");
321 rb_dlinkAdd(bh
, &bh
->hlist
, heap_lists
);
325 /* ************************************************************************ */
326 /* FUNCTION DOCUMENTATION: */
329 /* Returns a pointer to a struct within our rb_bh that's free for */
332 /* bh (IN): Pointer to the Blockheap. */
334 /* Pointer to a structure (void *), or NULL if unsuccessful. */
335 /* ************************************************************************ */
338 rb_bh_alloc(rb_bh
*bh
)
341 rb_dlink_node
*new_node
;
342 rb_heap_block
**block
;
345 lrb_assert(bh
!= NULL
);
346 if(rb_unlikely(bh
== NULL
))
348 rb_bh_fail("Cannot allocate if bh == NULL");
352 return (rb_malloc(bh
->elemSize
));
354 if(bh
->free_list
.head
== NULL
)
356 /* Allocate new block and assign */
357 /* newblock returns 1 if unsuccessful, 0 if not */
359 if(rb_unlikely(newblock(bh
)))
361 rb_lib_log("newblock() failed");
362 rb_outofmemory(); /* Well that didn't work either...bail */
364 if(bh
->free_list
.head
== NULL
)
366 rb_lib_log("out of memory after newblock()...");
371 new_node
= bh
->free_list
.head
;
372 block
= (rb_heap_block
**) new_node
->data
;
373 ptr
= (void *)((uintptr_t)new_node
->data
+ (uintptr_t)offset_pad
);
374 rb_dlinkDelete(new_node
, &bh
->free_list
);
375 (*block
)->free_count
--;
376 memset(ptr
, 0, bh
->elemSize
- offset_pad
);
382 /* ************************************************************************ */
383 /* FUNCTION DOCUMENTATION: */
386 /* Returns an element to the free pool, does not free() */
388 /* bh (IN): Pointer to rb_bh containing element */
389 /* ptr (in): Pointer to element to be "freed" */
391 /* 0 if successful, 1 if element not contained within rb_bh. */
392 /* ************************************************************************ */
394 rb_bh_free(rb_bh
*bh
, void *ptr
)
397 rb_heap_block
*block
;
400 lrb_assert(bh
!= NULL
);
401 lrb_assert(ptr
!= NULL
);
403 if(rb_unlikely(bh
== NULL
))
405 rb_lib_log("balloc.c:rb_bhFree() bh == NULL");
409 if(rb_unlikely(ptr
== NULL
))
411 rb_lib_log("balloc.rb_bhFree() ptr == NULL");
418 data
= (void *)((uintptr_t)ptr
- (uintptr_t)offset_pad
);
419 block
= *(rb_heap_block
**) data
;
422 (!((uintptr_t)ptr
>= (uintptr_t)block
->elems
423 && (uintptr_t)ptr
< (uintptr_t)block
->elems
+ (uintptr_t)block
->alloc_size
)))
425 rb_bh_fail("rb_bh_free() bogus pointer");
429 rb_dlinkAdd(data
, (rb_dlink_node
*)ptr
, &bh
->free_list
);
430 #endif /* !NOBALLOC */
435 /* ************************************************************************ */
436 /* FUNCTION DOCUMENTATION: */
439 /* Completely free()s a rb_bh. Use for cleanup. */
441 /* bh (IN): Pointer to the rb_bh to be destroyed. */
443 /* 0 if successful, 1 if bh == NULL */
444 /* ************************************************************************ */
446 rb_bh_destroy(rb_bh
*bh
)
449 rb_dlink_node
*ptr
, *next
;
456 RB_DLINK_FOREACH_SAFE(ptr
, next
, bh
->block_list
.head
)
459 free_block(b
->elems
, b
->alloc_size
);
462 #endif /* !NOBALLOC */
464 rb_dlinkDelete(&bh
->hlist
, heap_lists
);
472 rb_bh_usage(rb_bh
*bh
, size_t *bused
, size_t *bfree
, size_t *bmemusage
, const char **desc
)
475 size_t used
, freem
, memusage
;
482 freem
= rb_dlink_list_length(&bh
->free_list
);
483 used
= (rb_dlink_list_length(&bh
->block_list
) * bh
->elemsPerBlock
) - freem
;
484 memusage
= used
* bh
->elemSize
;
489 if(bmemusage
!= NULL
)
490 *bmemusage
= memusage
;
498 if(bmemusage
!= NULL
)
501 *desc
= "no blockheap";
506 rb_bh_usage_all(rb_bh_usage_cb
*cb
, void *data
)
510 size_t used
, freem
, memusage
, heapalloc
;
511 static const char *unnamed
= "(unnamed_heap)";
512 const char *desc
= unnamed
;
517 RB_DLINK_FOREACH(ptr
, heap_lists
->head
)
519 bh
= (rb_bh
*)ptr
->data
;
520 freem
= rb_dlink_list_length(&bh
->free_list
);
521 used
= (rb_dlink_list_length(&bh
->block_list
) * bh
->elemsPerBlock
) - freem
;
522 memusage
= used
* bh
->elemSize
;
523 heapalloc
= (freem
+ used
) * bh
->elemSize
;
526 cb(used
, freem
, memusage
, heapalloc
, desc
, data
);
532 rb_bh_total_usage(size_t *total_alloc
, size_t *total_used
)
535 size_t total_memory
= 0, used_memory
= 0, used
, freem
;
538 RB_DLINK_FOREACH(ptr
, heap_lists
->head
)
540 bh
= (rb_bh
*)ptr
->data
;
541 freem
= rb_dlink_list_length(&bh
->free_list
);
542 used
= (rb_dlink_list_length(&bh
->block_list
) * bh
->elemsPerBlock
) - freem
;
543 used_memory
+= used
* bh
->elemSize
;
544 total_memory
+= (freem
+ used
) * bh
->elemSize
;
547 if(total_alloc
!= NULL
)
548 *total_alloc
= total_memory
;
549 if(total_used
!= NULL
)
550 *total_used
= used_memory
;
558 rb_dlink_node
*ptr
, *next
;
564 /* somebody is smoking some craq..(probably lee, but don't tell him that) */
568 if((rb_dlink_list_length(&bh
->free_list
) < bh
->elemsPerBlock
)
569 || rb_dlink_list_length(&bh
->block_list
) == 1)
571 /* There couldn't possibly be an entire free block. Return. */
575 RB_DLINK_FOREACH_SAFE(ptr
, next
, bh
->block_list
.head
)
578 if(rb_dlink_list_length(&bh
->block_list
) == 1)
581 if(b
->free_count
== bh
->elemsPerBlock
)
583 /* i'm seriously going to hell for this.. */
585 offset
= (uintptr_t)b
->elems
;
586 for(i
= 0; i
< bh
->elemsPerBlock
; i
++, offset
+= (uintptr_t)bh
->elemSize
)
588 rb_dlinkDelete((rb_dlink_node
*)(offset
+ offset_pad
),
591 rb_dlinkDelete(&b
->node
, &bh
->block_list
);
592 free_block(b
->elems
, b
->alloc_size
);
599 #endif /* !NOBALLOC */