]> jfr.im git - solanum.git/blob - librb/src/balloc.c
Revert "Accept expired certificates"
[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. malloc()
42 *
43 * The advantages of 1 and 2 are this. We can munmap() the pages which will
44 * return the pages back to the operating system, thus reducing the size
45 * of the process as the memory is unused. malloc() on many systems just keeps
46 * a heap of memory to itself, which never gets given back to the OS, except on
47 * exit. This of course is bad, if say we have an event that causes us to allocate
48 * say, 200MB of memory, while our normal memory consumption would be 15MB. In the
49 * malloc() case, the amount of memory allocated to our process never goes down, as
50 * malloc() has it locked up in its heap. With the mmap() method, we can munmap()
51 * the block and return it back to the OS, thus causing our memory consumption to go
52 * down after we no longer need it.
53 *
54 *
55 *
56 */
57 #include <librb_config.h>
58 #include <rb_lib.h>
59
60 static void _rb_bh_fail(const char *reason, const char *file, int line) __attribute__((noreturn));
61
62 static uintptr_t offset_pad;
63
64 /* information for the root node of the heap */
65 struct rb_bh
66 {
67 rb_dlink_node hlist;
68 size_t elemSize; /* Size of each element to be stored */
69 unsigned long elemsPerBlock; /* Number of elements per block */
70 rb_dlink_list block_list;
71 rb_dlink_list free_list;
72 char *desc;
73 };
74
75 static rb_dlink_list *heap_lists;
76
77 #define rb_bh_fail(x) _rb_bh_fail(x, __FILE__, __LINE__)
78
79 static void
80 _rb_bh_fail(const char *reason, const char *file, int line)
81 {
82 rb_lib_log("rb_heap_blockheap failure: %s (%s:%d)", reason, file, line);
83 abort();
84 }
85
86 /*
87 * void rb_init_bh(void)
88 *
89 * Inputs: None
90 * Outputs: None
91 * Side Effects: Initializes the block heap
92 */
93
94 void
95 rb_init_bh(void)
96 {
97 heap_lists = rb_malloc(sizeof(rb_dlink_list));
98 offset_pad = sizeof(void *);
99 /* XXX if you get SIGBUS when trying to use a long long..here is where you need to
100 * fix your shit
101 */
102 #ifdef __sparc__
103 if((offset_pad % __alignof__(long long)) != 0)
104 {
105 offset_pad += __alignof__(long long);
106 offset_pad &= ~(__alignof__(long long) - 1);
107 }
108 #endif
109 }
110
111 /* ************************************************************************ */
112 /* FUNCTION DOCUMENTATION: */
113 /* rb_bh_create */
114 /* Description: */
115 /* Creates a new blockheap from which smaller blocks can be allocated. */
116 /* Intended to be used instead of multiple calls to malloc() when */
117 /* performance is an issue. */
118 /* Parameters: */
119 /* elemsize (IN): Size of the basic element to be stored */
120 /* elemsperblock (IN): Number of elements to be stored in a single block */
121 /* of memory. When the blockheap runs out of free memory, it will */
122 /* allocate elemsize * elemsperblock more. */
123 /* Returns: */
124 /* Pointer to new rb_bh, or NULL if unsuccessful */
125 /* ************************************************************************ */
126 rb_bh *
127 rb_bh_create(size_t elemsize, int elemsperblock, const char *desc)
128 {
129 rb_bh *bh;
130 lrb_assert(elemsize > 0 && elemsperblock > 0);
131 lrb_assert(elemsize >= sizeof(rb_dlink_node));
132
133 /* Catch idiotic requests up front */
134 if((elemsize == 0) || (elemsperblock <= 0))
135 {
136 rb_bh_fail("Attempting to rb_bh_create idiotic sizes");
137 }
138
139 if(elemsize < sizeof(rb_dlink_node))
140 rb_bh_fail("Attempt to rb_bh_create smaller than sizeof(rb_dlink_node)");
141
142 /* Allocate our new rb_bh */
143 bh = rb_malloc(sizeof(rb_bh));
144 bh->elemSize = elemsize;
145 bh->elemsPerBlock = elemsperblock;
146 if(desc != NULL)
147 bh->desc = rb_strdup(desc);
148
149 if(bh == NULL)
150 {
151 rb_bh_fail("bh == NULL when it shouldn't be");
152 }
153 rb_dlinkAdd(bh, &bh->hlist, heap_lists);
154 return (bh);
155 }
156
157 /* ************************************************************************ */
158 /* FUNCTION DOCUMENTATION: */
159 /* rb_bh_alloc */
160 /* Description: */
161 /* Returns a pointer to a struct within our rb_bh that's free for */
162 /* the taking. */
163 /* Parameters: */
164 /* bh (IN): Pointer to the Blockheap. */
165 /* Returns: */
166 /* Pointer to a structure (void *), or NULL if unsuccessful. */
167 /* ************************************************************************ */
168
169 void *
170 rb_bh_alloc(rb_bh *bh)
171 {
172 lrb_assert(bh != NULL);
173 if(rb_unlikely(bh == NULL))
174 {
175 rb_bh_fail("Cannot allocate if bh == NULL");
176 }
177
178 return (rb_malloc(bh->elemSize));
179 }
180
181
182 /* ************************************************************************ */
183 /* FUNCTION DOCUMENTATION: */
184 /* rb_bh_free */
185 /* Description: */
186 /* Returns an element to the free pool, does not free() */
187 /* Parameters: */
188 /* bh (IN): Pointer to rb_bh containing element */
189 /* ptr (in): Pointer to element to be "freed" */
190 /* Returns: */
191 /* 0 if successful, 1 if element not contained within rb_bh. */
192 /* ************************************************************************ */
193 int
194 rb_bh_free(rb_bh *bh, void *ptr)
195 {
196 lrb_assert(bh != NULL);
197 lrb_assert(ptr != NULL);
198
199 if(rb_unlikely(bh == NULL))
200 {
201 rb_lib_log("balloc.c:rb_bhFree() bh == NULL");
202 return (1);
203 }
204
205 if(rb_unlikely(ptr == NULL))
206 {
207 rb_lib_log("balloc.rb_bhFree() ptr == NULL");
208 return (1);
209 }
210
211 rb_free(ptr);
212 return (0);
213 }
214
215
216 /* ************************************************************************ */
217 /* FUNCTION DOCUMENTATION: */
218 /* rb_bhDestroy */
219 /* Description: */
220 /* Completely free()s a rb_bh. Use for cleanup. */
221 /* Parameters: */
222 /* bh (IN): Pointer to the rb_bh to be destroyed. */
223 /* Returns: */
224 /* 0 if successful, 1 if bh == NULL */
225 /* ************************************************************************ */
226 int
227 rb_bh_destroy(rb_bh *bh)
228 {
229 if(bh == NULL)
230 return (1);
231
232 rb_dlinkDelete(&bh->hlist, heap_lists);
233 rb_free(bh->desc);
234 rb_free(bh);
235
236 return (0);
237 }
238
239 void
240 rb_bh_usage(rb_bh *bh __attribute__((unused)), size_t *bused, size_t *bfree, size_t *bmemusage, const char **desc)
241 {
242 if(bused != NULL)
243 *bused = 0;
244 if(bfree != NULL)
245 *bfree = 0;
246 if(bmemusage != NULL)
247 *bmemusage = 0;
248 if(desc != NULL)
249 *desc = "no blockheap";
250 }
251
252 void
253 rb_bh_usage_all(rb_bh_usage_cb *cb, void *data)
254 {
255 rb_dlink_node *ptr;
256 rb_bh *bh;
257 size_t used, freem, memusage, heapalloc;
258 static const char *unnamed = "(unnamed_heap)";
259 const char *desc = unnamed;
260
261 if(cb == NULL)
262 return;
263
264 RB_DLINK_FOREACH(ptr, heap_lists->head)
265 {
266 bh = (rb_bh *)ptr->data;
267 freem = rb_dlink_list_length(&bh->free_list);
268 used = (rb_dlink_list_length(&bh->block_list) * bh->elemsPerBlock) - freem;
269 memusage = used * bh->elemSize;
270 heapalloc = (freem + used) * bh->elemSize;
271 if(bh->desc != NULL)
272 desc = bh->desc;
273 cb(used, freem, memusage, heapalloc, desc, data);
274 }
275 return;
276 }
277
278 void
279 rb_bh_total_usage(size_t *total_alloc, size_t *total_used)
280 {
281 rb_dlink_node *ptr;
282 size_t total_memory = 0, used_memory = 0, used, freem;
283 rb_bh *bh;
284
285 RB_DLINK_FOREACH(ptr, heap_lists->head)
286 {
287 bh = (rb_bh *)ptr->data;
288 freem = rb_dlink_list_length(&bh->free_list);
289 used = (rb_dlink_list_length(&bh->block_list) * bh->elemsPerBlock) - freem;
290 used_memory += used * bh->elemSize;
291 total_memory += (freem + used) * bh->elemSize;
292 }
293
294 if(total_alloc != NULL)
295 *total_alloc = total_memory;
296 if(total_used != NULL)
297 *total_used = used_memory;
298 }