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
31 * $Id: balloc.c 26100 2008-09-20 01:27:19Z androsyn $
35 * About the block allocator
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.
40 * 1. mmap() anonymous pages with the MMAP_ANON flag.
41 * 2. mmap() via the /dev/zero trick.
42 * 3. HeapCreate/HeapAlloc (on win32)
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.
59 #include <libratbox_config.h>
60 #include <ratbox_lib.h>
63 #ifdef HAVE_MMAP /* We've got mmap() that is good */
68 #define MAP_ANON MAP_ANONYMOUS
74 static uintptr_t offset_pad
;
76 /* status information for an allocated block in heap */
81 unsigned long free_count
;
82 void *elems
; /* Points to allocated memory */
84 typedef struct rb_heap_block rb_heap_block
;
86 /* information for the root node of the heap */
90 size_t elemSize
; /* Size of each element to be stored */
91 unsigned long elemsPerBlock
; /* Number of elements per block */
92 rb_dlink_list block_list
;
93 rb_dlink_list free_list
;
98 static int newblock(rb_bh
*bh
);
99 static void rb_bh_gc_event(void *unused
);
100 #endif /* !NOBALLOC */
101 static rb_dlink_list
*heap_lists
;
104 static HANDLE block_heap
;
107 #define rb_bh_fail(x) _rb_bh_fail(x, __FILE__, __LINE__)
110 _rb_bh_fail(const char *reason
, const char *file
, int line
)
112 rb_lib_log("rb_heap_blockheap failure: %s (%s:%d)", reason
, file
, line
);
118 * static inline void free_block(void *ptr, size_t size)
120 * Inputs: The block and its size
122 * Side Effects: Returns memory for the block back to the OS
125 free_block(void *ptr
, size_t size
)
131 HeapFree(block_heap
, 0, ptr
);
137 #endif /* !NOBALLOC */
140 * void rb_init_bh(void)
144 * Side Effects: Initializes the block heap
150 heap_lists
= rb_malloc(sizeof(rb_dlink_list
));
151 offset_pad
= sizeof(void *);
152 /* XXX if you get SIGBUS when trying to use a long long..here is where you need to
156 if((offset_pad
% __alignof__(long long)) != 0)
158 offset_pad
+= __alignof__(long long);
159 offset_pad
&= ~(__alignof__(long long) - 1);
165 block_heap
= HeapCreate(HEAP_NO_SERIALIZE
, 0, 0);
167 rb_event_addish("rb_bh_gc_event", rb_bh_gc_event
, NULL
, 300);
168 #endif /* !NOBALLOC */
173 * static inline void *get_block(size_t size)
175 * Input: Size of block to allocate
176 * Output: Pointer to new block
180 get_block(size_t size
)
185 ptr
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_ANON
, -1, 0);
188 zero_fd
= open("/dev/zero", O_RDWR
);
190 rb_bh_fail("Failed opening /dev/zero");
191 ptr
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, zero_fd
, 0);
193 #endif /* MAP_ANON */
194 if(ptr
== MAP_FAILED
)
198 ptr
= HeapAlloc(block_heap
, 0, size
);
208 rb_bh_gc_event(void *unused
)
211 RB_DLINK_FOREACH(ptr
, heap_lists
->head
)
217 /* ************************************************************************ */
218 /* FUNCTION DOCUMENTATION: */
221 /* Allocates a new block for addition to a blockheap */
223 /* bh (IN): Pointer to parent blockheap. */
225 /* 0 if successful, 1 if not */
226 /* ************************************************************************ */
235 /* Setup the initial data structure. */
236 b
= rb_malloc(sizeof(rb_heap_block
));
238 b
->alloc_size
= bh
->elemsPerBlock
* bh
->elemSize
;
240 b
->elems
= get_block(b
->alloc_size
);
241 if(rb_unlikely(b
->elems
== NULL
))
245 offset
= (uintptr_t)b
->elems
;
246 /* Setup our blocks now */
247 for(i
= 0; i
< bh
->elemsPerBlock
; i
++, offset
+= bh
->elemSize
)
249 *((void **)offset
) = b
;
250 node
= (void *)(offset
+ offset_pad
);
251 rb_dlinkAdd((void *)offset
, node
, &bh
->free_list
);
253 rb_dlinkAdd(b
, &b
->node
, &bh
->block_list
);
254 b
->free_count
= bh
->elemsPerBlock
;
257 #endif /* !NOBALLOC */
259 /* ************************************************************************ */
260 /* FUNCTION DOCUMENTATION: */
263 /* Creates a new blockheap from which smaller blocks can be allocated. */
264 /* Intended to be used instead of multiple calls to malloc() when */
265 /* performance is an issue. */
267 /* elemsize (IN): Size of the basic element to be stored */
268 /* elemsperblock (IN): Number of elements to be stored in a single block */
269 /* of memory. When the blockheap runs out of free memory, it will */
270 /* allocate elemsize * elemsperblock more. */
272 /* Pointer to new rb_bh, or NULL if unsuccessful */
273 /* ************************************************************************ */
275 rb_bh_create(size_t elemsize
, int elemsperblock
, const char *desc
)
278 lrb_assert(elemsize
> 0 && elemsperblock
> 0);
279 lrb_assert(elemsize
>= sizeof(rb_dlink_node
));
281 /* Catch idiotic requests up front */
282 if((elemsize
== 0) || (elemsperblock
<= 0))
284 rb_bh_fail("Attempting to rb_bh_create idiotic sizes");
287 if(elemsize
< sizeof(rb_dlink_node
))
288 rb_bh_fail("Attempt to rb_bh_create smaller than sizeof(rb_dlink_node)");
290 /* Allocate our new rb_bh */
291 bh
= rb_malloc(sizeof(rb_bh
));
293 elemsize
+= offset_pad
;
294 if((elemsize
% sizeof(void *)) != 0)
296 /* Pad to even pointer boundary */
297 elemsize
+= sizeof(void *);
298 elemsize
&= ~(sizeof(void *) - 1);
302 bh
->elemSize
= elemsize
;
303 bh
->elemsPerBlock
= elemsperblock
;
305 bh
->desc
= rb_strdup(desc
);
308 /* Be sure our malloc was successful */
313 rb_lib_log("newblock() failed");
314 rb_outofmemory(); /* die.. out of memory */
316 #endif /* !NOBALLOC */
320 rb_bh_fail("bh == NULL when it shouldn't be");
322 rb_dlinkAdd(bh
, &bh
->hlist
, heap_lists
);
326 /* ************************************************************************ */
327 /* FUNCTION DOCUMENTATION: */
330 /* Returns a pointer to a struct within our rb_bh that's free for */
333 /* bh (IN): Pointer to the Blockheap. */
335 /* Pointer to a structure (void *), or NULL if unsuccessful. */
336 /* ************************************************************************ */
339 rb_bh_alloc(rb_bh
*bh
)
342 rb_dlink_node
*new_node
;
343 rb_heap_block
**block
;
346 lrb_assert(bh
!= NULL
);
347 if(rb_unlikely(bh
== NULL
))
349 rb_bh_fail("Cannot allocate if bh == NULL");
353 return (rb_malloc(bh
->elemSize
));
355 if(bh
->free_list
.head
== NULL
)
357 /* Allocate new block and assign */
358 /* newblock returns 1 if unsuccessful, 0 if not */
360 if(rb_unlikely(newblock(bh
)))
362 rb_lib_log("newblock() failed");
363 rb_outofmemory(); /* Well that didn't work either...bail */
365 if(bh
->free_list
.head
== NULL
)
367 rb_lib_log("out of memory after newblock()...");
372 new_node
= bh
->free_list
.head
;
373 block
= (rb_heap_block
**) new_node
->data
;
374 ptr
= (void *)((uintptr_t)new_node
->data
+ (uintptr_t)offset_pad
);
375 rb_dlinkDelete(new_node
, &bh
->free_list
);
376 (*block
)->free_count
--;
377 memset(ptr
, 0, bh
->elemSize
- offset_pad
);
383 /* ************************************************************************ */
384 /* FUNCTION DOCUMENTATION: */
387 /* Returns an element to the free pool, does not free() */
389 /* bh (IN): Pointer to rb_bh containing element */
390 /* ptr (in): Pointer to element to be "freed" */
392 /* 0 if successful, 1 if element not contained within rb_bh. */
393 /* ************************************************************************ */
395 rb_bh_free(rb_bh
*bh
, void *ptr
)
398 rb_heap_block
*block
;
401 lrb_assert(bh
!= NULL
);
402 lrb_assert(ptr
!= NULL
);
404 if(rb_unlikely(bh
== NULL
))
406 rb_lib_log("balloc.c:rb_bhFree() bh == NULL");
410 if(rb_unlikely(ptr
== NULL
))
412 rb_lib_log("balloc.rb_bhFree() ptr == NULL");
419 data
= (void *)((uintptr_t)ptr
- (uintptr_t)offset_pad
);
420 block
= *(rb_heap_block
**) data
;
423 (!((uintptr_t)ptr
>= (uintptr_t)block
->elems
424 && (uintptr_t)ptr
< (uintptr_t)block
->elems
+ (uintptr_t)block
->alloc_size
)))
426 rb_bh_fail("rb_bh_free() bogus pointer");
430 rb_dlinkAdd(data
, (rb_dlink_node
*)ptr
, &bh
->free_list
);
431 #endif /* !NOBALLOC */
436 /* ************************************************************************ */
437 /* FUNCTION DOCUMENTATION: */
440 /* Completely free()s a rb_bh. Use for cleanup. */
442 /* bh (IN): Pointer to the rb_bh to be destroyed. */
444 /* 0 if successful, 1 if bh == NULL */
445 /* ************************************************************************ */
447 rb_bh_destroy(rb_bh
*bh
)
450 rb_dlink_node
*ptr
, *next
;
457 RB_DLINK_FOREACH_SAFE(ptr
, next
, bh
->block_list
.head
)
460 free_block(b
->elems
, b
->alloc_size
);
463 #endif /* !NOBALLOC */
465 rb_dlinkDelete(&bh
->hlist
, heap_lists
);
473 rb_bh_usage(rb_bh
*bh
, size_t *bused
, size_t *bfree
, size_t *bmemusage
, const char **desc
)
476 size_t used
, freem
, memusage
;
483 freem
= rb_dlink_list_length(&bh
->free_list
);
484 used
= (rb_dlink_list_length(&bh
->block_list
) * bh
->elemsPerBlock
) - freem
;
485 memusage
= used
* bh
->elemSize
;
490 if(bmemusage
!= NULL
)
491 *bmemusage
= memusage
;
499 if(bmemusage
!= NULL
)
502 *desc
= "no blockheap";
507 rb_bh_usage_all(rb_bh_usage_cb
*cb
, void *data
)
511 size_t used
, freem
, memusage
, heapalloc
;
512 static const char *unnamed
= "(unnamed_heap)";
513 const char *desc
= unnamed
;
518 RB_DLINK_FOREACH(ptr
, heap_lists
->head
)
520 bh
= (rb_bh
*)ptr
->data
;
521 freem
= rb_dlink_list_length(&bh
->free_list
);
522 used
= (rb_dlink_list_length(&bh
->block_list
) * bh
->elemsPerBlock
) - freem
;
523 memusage
= used
* bh
->elemSize
;
524 heapalloc
= (freem
+ used
) * bh
->elemSize
;
527 cb(used
, freem
, memusage
, heapalloc
, desc
, data
);
533 rb_bh_total_usage(size_t *total_alloc
, size_t *total_used
)
536 size_t total_memory
= 0, used_memory
= 0, used
, freem
;
539 RB_DLINK_FOREACH(ptr
, heap_lists
->head
)
541 bh
= (rb_bh
*)ptr
->data
;
542 freem
= rb_dlink_list_length(&bh
->free_list
);
543 used
= (rb_dlink_list_length(&bh
->block_list
) * bh
->elemsPerBlock
) - freem
;
544 used_memory
+= used
* bh
->elemSize
;
545 total_memory
+= (freem
+ used
) * bh
->elemSize
;
548 if(total_alloc
!= NULL
)
549 *total_alloc
= total_memory
;
550 if(total_used
!= NULL
)
551 *total_used
= used_memory
;
559 rb_dlink_node
*ptr
, *next
;
565 /* somebody is smoking some craq..(probably lee, but don't tell him that) */
569 if((rb_dlink_list_length(&bh
->free_list
) < bh
->elemsPerBlock
)
570 || rb_dlink_list_length(&bh
->block_list
) == 1)
572 /* There couldn't possibly be an entire free block. Return. */
576 RB_DLINK_FOREACH_SAFE(ptr
, next
, bh
->block_list
.head
)
579 if(rb_dlink_list_length(&bh
->block_list
) == 1)
582 if(b
->free_count
== bh
->elemsPerBlock
)
584 /* i'm seriously going to hell for this.. */
586 offset
= (uintptr_t)b
->elems
;
587 for(i
= 0; i
< bh
->elemsPerBlock
; i
++, offset
+= (uintptr_t)bh
->elemSize
)
589 rb_dlinkDelete((rb_dlink_node
*)(offset
+ offset_pad
),
592 rb_dlinkDelete(&b
->node
, &bh
->block_list
);
593 free_block(b
->elems
, b
->alloc_size
);
600 #endif /* !NOBALLOC */