5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2003 - 2007 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
22 /************************** Types and definitions ***************************/
24 /* The SilcStack context */
25 struct SilcStackStruct {
26 SilcStack parent; /* Parent stack */
27 SilcMutex lock; /* Stack lock */
28 SilcList stacks; /* List of stacks for childs */
29 SilcStackDataEntry stack; /* The allocated stack */
30 SilcStackFrame *frames; /* Allocated stack frames */
31 SilcStackFrame *frame; /* Current stack frame */
32 SilcStackOomHandler oom_handler; /* OOM handler */
33 void *oom_context; /* OOM handler context */
34 SilcUInt32 stack_size; /* Default stack size */
35 SilcUInt32 alignment; /* Memory alignment */
36 #ifdef SILC_DIST_INPLACE
38 SilcUInt32 snum_malloc;
39 SilcUInt32 sbytes_malloc;
40 SilcUInt32 snum_errors;
41 #endif /* SILC_DIST_INPLACE */
44 /************************ Static utility functions **************************/
46 /* Compute stack block index for the `size'. */
48 static SilcUInt32 silc_stack_get_index(SilcUInt32 size, SilcUInt32 *ret_bsize)
52 if (size < SILC_STACK_DEFAULT_SIZE)
53 size = SILC_STACK_DEFAULT_SIZE;
55 bsize = SILC_STACK_DEFAULT_SIZE;
56 while (bsize < size) {
66 /* Get stack from `stack' or allocate new one. */
68 static SilcStackDataEntry silc_stack_ref_stack(SilcStack stack,
71 SilcUInt32 *ret_bsize)
76 /* Get stack block index and block size for requested size */
77 si = silc_stack_get_index(size, &bsize);
81 SILC_ST_DEBUG(("Get stack block, si %d, size %lu", si, bsize));
83 silc_mutex_lock(stack->lock);
85 /* Get stack that has block that can house our size requirement. */
86 silc_list_start(stack->stacks);
87 while ((e = silc_list_get(stack->stacks))) {
91 silc_list_del(stack->stacks, e);
92 SILC_ST_DEBUG(("Got stack blocks %p from stack %p", e->data, stack));
93 silc_mutex_unlock(stack->lock);
97 SILC_ST_DEBUG(("Allocate new stack blocks"));
99 /* Allocate new stack blocks */
100 e = silc_calloc(1, sizeof(*e));
102 silc_mutex_unlock(stack->lock);
105 e->data[si] = silc_malloc(bsize + SILC_STACK_ALIGN(sizeof(*e->data[0]),
109 silc_mutex_unlock(stack->lock);
112 e->data[si]->bytes_left = bsize;
116 SILC_ST_DEBUG(("Got stack blocks %p from stack %p", e->data, stack));
118 silc_mutex_unlock(stack->lock);
122 /* Return the `data' back to the `stack'. */
124 static void silc_stack_unref_stack(SilcStack stack, SilcStackDataEntry e)
128 SILC_LOG_DEBUG(("Release stack blocks %p to stack %p, si %d",
129 e->data, stack, e->si));
131 /* Release all blocks from allocations */
132 for (i = e->si; i < SILC_STACK_BLOCK_NUM; i++) {
136 e->data[i]->bytes_left = e->bsize;
138 e->data[i]->bytes_left = SILC_STACK_BLOCK_SIZE(stack, i);
141 silc_mutex_lock(stack->lock);
142 silc_list_add(stack->stacks, e);
143 silc_mutex_unlock(stack->lock);
146 /* Allocate memory from a specific stack block */
148 static void *silc_stack_alloc_block(SilcStack stack, SilcStackDataEntry e,
149 SilcUInt32 items, SilcUInt32 size)
154 /* Get pointer and consume the stack block */
155 asize = SILC_STACK_ALIGN(items * size, stack->alignment);
156 ptr = SILC_STACK_DATA_EXT(e->data, e->si, e->bsize, stack->alignment);
157 e->data[e->si]->bytes_left -= asize;
158 memset(ptr, 0, items * size);
163 /***************************** SilcStack API ********************************/
165 /* Allocate the stack */
167 SilcStack silc_stack_alloc(SilcUInt32 stack_size, SilcStack parent)
170 SilcStackDataEntry e;
171 SilcUInt32 si = 0, bsize = 0;
173 stack_size = stack_size ? stack_size : SILC_STACK_DEFAULT_SIZE;
174 if (stack_size < SILC_STACK_DEFAULT_SIZE)
175 stack_size = SILC_STACK_DEFAULT_SIZE;
178 /* Get stack from parent. The stack itself is allocated from the
180 e = silc_stack_ref_stack(parent, stack_size, &si, &bsize);
184 /* Allocate stack from the parent */
185 stack = silc_stack_alloc_block(parent, e, 1, sizeof(*stack));
187 silc_stack_unref_stack(parent, e);
191 stack->parent = parent;
192 stack->stack_size = stack_size;
193 stack->alignment = SILC_STACK_DEFAULT_ALIGN;
194 silc_list_init(stack->stacks, struct SilcStackDataEntryStruct, next);
196 /* Allocate stack frames from the parent */
197 stack->frames = silc_stack_alloc_block(parent, e, SILC_STACK_BLOCK_NUM,
198 sizeof(*stack->frames));
199 if (!stack->frames) {
200 silc_stack_unref_stack(parent, e);
204 /* Set the initial stack */
207 /* Dynamically allocate new stack */
208 stack = silc_calloc(1, sizeof(*stack));
212 stack->stack_size = stack_size;
213 stack->alignment = SILC_STACK_DEFAULT_ALIGN;
214 silc_list_init(stack->stacks, struct SilcStackDataEntryStruct, next);
216 /* Create initial stack */
217 stack->stack = silc_calloc(1, sizeof(*stack->stack));
222 stack->stack->data[0] =
223 silc_malloc(stack->stack_size +
224 SILC_STACK_ALIGN(sizeof(*stack->stack->data[0]),
226 if (!stack->stack->data[0]) {
227 silc_free(stack->stack);
231 stack->stack->data[0]->bytes_left = stack->stack_size;
232 stack->stack->si = 0;
233 stack->stack->bsize = stack->stack_size;
235 /* Allocate stack frames from the stack itself */
236 stack->frames = silc_stack_alloc_block(stack, stack->stack,
237 SILC_STACK_DEFAULT_NUM,
238 sizeof(*stack->frames));
239 if (!stack->frames) {
240 silc_free(stack->stack->data[0]);
241 silc_free(stack->stack);
248 silc_mutex_alloc(&stack->lock);
250 /* Use the allocated stack in first stack frame */
251 stack->frame = &stack->frames[0];
252 stack->frame->prev = NULL;
253 stack->frame->bytes_used = stack->stack_size;
254 stack->frame->sp = 1;
255 stack->frame->si = si;
257 SILC_LOG_DEBUG(("New stack %p, size %d bytes", stack, stack->stack_size));
262 /* Frees the stack and all allocated memory */
264 void silc_stack_free(SilcStack stack)
266 SilcStackDataEntry e;
272 SILC_LOG_DEBUG(("Free stack %p", stack));
275 silc_mutex_free(stack->lock);
277 if (!stack->parent) {
278 silc_list_start(stack->stacks);
279 while ((e = silc_list_get(stack->stacks))) {
280 for (i = 0; i < SILC_STACK_BLOCK_NUM; i++)
281 silc_free(e->data[i]);
285 for (i = 0; i < SILC_STACK_BLOCK_NUM; i++)
286 silc_free(stack->stack->data[i]);
287 silc_free(stack->stack);
291 /* Return all stack blocks to the parent */
292 silc_list_start(stack->stacks);
293 while ((e = silc_list_get(stack->stacks)))
294 silc_stack_unref_stack(stack->parent, e);
296 silc_stack_unref_stack(stack->parent, stack->stack);
300 /* Push to next stack frame */
302 SilcUInt32 silc_stack_push(SilcStack stack, SilcStackFrame *frame)
308 if (stack->frame->sp >= SILC_STACK_ALIGN(stack->frame->sp,
309 SILC_STACK_DEFAULT_NUM)) {
310 SILC_LOG_DEBUG(("SilcStack %p running out of frames, cannot push",
312 return stack->frame->sp;
315 frame = &stack->frames[stack->frame->sp];
319 frame->prev = stack->frame;
320 frame->sp = stack->frame->sp + 1;
321 frame->si = stack->frame->si;
322 frame->bytes_used = stack->stack->data[frame->si]->bytes_left;
323 stack->frame = frame;
325 SILC_ST_DEBUG(("Push %p: sp %d -> %d, si %d", stack, frame->prev->sp,
326 frame->sp, frame->si));
328 return stack->frame->sp;
331 /* Pop to previous stack frame */
333 SilcUInt32 silc_stack_pop(SilcStack stack)
337 if (!stack || !stack->frame->prev)
341 si = stack->frame->si;
342 while (si > stack->frame->prev->si) {
343 if (stack->stack->data[si])
344 stack->stack->data[si]->bytes_left = SILC_STACK_BLOCK_SIZE(stack, si);
347 stack->stack->data[si]->bytes_left = stack->frame->bytes_used;
348 stack->frame = stack->frame->prev;
350 SILC_ST_DEBUG(("Pop %p: sp %d -> %d, si %d", stack, stack->frame->sp + 1,
351 stack->frame->sp, stack->frame->si));
353 return stack->frame->sp + 1;
356 /* Allocate memory. Returns pointer to the memory or NULL on error. */
358 void *silc_stack_malloc(SilcStack stack, SilcUInt32 size)
361 SilcUInt32 bsize, bsize2;
362 SilcUInt32 si = stack->frame->si;
364 SILC_STACK_STAT(stack, num_malloc, 1);
365 SILC_ST_DEBUG(("Allocating %d bytes from %p", size, stack));
367 if (silc_unlikely(!size)) {
368 SILC_LOG_ERROR(("Allocation by zero (0)"));
369 SILC_STACK_STAT(stack, num_errors, 1);
373 if (silc_unlikely(size > SILC_STACK_MAX_ALLOC)) {
374 SILC_LOG_ERROR(("Allocating too much"));
375 SILC_STACK_STAT(stack, num_errors, 1);
376 if (stack->oom_handler)
377 stack->oom_handler(stack, stack->oom_context);
382 size = SILC_STACK_ALIGN(size, stack->alignment);
384 /* Compute the size of current stack block */
385 bsize = SILC_STACK_BLOCK_SIZE(stack, si);
387 /* See if there is space in the current stack block */
388 if (stack->stack->data[si]->bytes_left >= size) {
389 /* Get pointer to the memory */
390 ptr = SILC_STACK_DATA(stack, si, bsize);
391 stack->stack->data[si]->bytes_left -= size;
392 SILC_STACK_STAT(stack, bytes_malloc, size);
396 /* There is not enough space in this block. Find the spot to stack
397 block that can handle this size memory. */
398 if (bsize < SILC_STACK_DEFAULT_SIZE)
399 bsize = SILC_STACK_DEFAULT_SIZE;
401 bsize2 = SILC_STACK_DEFAULT_SIZE;
403 while (bsize2 < bsize) {
407 if (silc_unlikely(si >= SILC_STACK_BLOCK_NUM)) {
408 SILC_LOG_ERROR(("Allocating too large block"));
409 SILC_STACK_STAT(stack, num_errors, 1);
410 if (stack->oom_handler)
411 stack->oom_handler(stack, stack->oom_context);
415 /* Allocate the block if it doesn't exist yet */
416 if (!stack->stack->data[si]) {
417 SILC_ST_DEBUG(("Allocating new stack block, %d bytes", bsize2));
418 stack->stack->data[si] =
420 SILC_STACK_ALIGN(sizeof(**stack->stack->data),
422 if (silc_unlikely(!stack->stack->data[si])) {
423 SILC_STACK_STAT(stack, num_errors, 1);
424 if (stack->oom_handler)
425 stack->oom_handler(stack, stack->oom_context);
428 stack->stack->data[si]->bytes_left = bsize2;
431 /* Now return memory from this new block. It is guaranteed that in this
432 block there is enough space for this memory. */
433 assert(stack->stack->data[si]->bytes_left >= size);
434 ptr = SILC_STACK_DATA(stack, si, bsize2);
435 stack->stack->data[si]->bytes_left -= size;
436 stack->frame->si = si;
437 SILC_STACK_STAT(stack, bytes_malloc, size);
442 /* Attempts to reallocate memory by changing the size of the `ptr' into
443 `size'. This routine works only if the previous allocation to `stack'
444 was `ptr'. If there is another memory allocation between allocating
445 `ptr' and this call this routine will return NULL. NULL is also returned
446 if the `size' does not fit into the current block. If NULL is returned
447 the old memory remains intact. */
449 void *silc_stack_realloc(SilcStack stack, SilcUInt32 old_size,
450 void *ptr, SilcUInt32 size)
452 SilcUInt32 si = stack->frame->si;
457 return silc_stack_malloc(stack, size);
459 SILC_STACK_STAT(stack, num_malloc, 1);
460 SILC_ST_DEBUG(("Reallocating %d bytes (%d) from %p", size, old_size, stack));
462 if (silc_unlikely(!size || !old_size)) {
463 SILC_LOG_ERROR(("Allocation by zero (0)"));
464 SILC_STACK_STAT(stack, num_errors, 1);
468 if (silc_unlikely(size > SILC_STACK_MAX_ALLOC)) {
469 SILC_LOG_ERROR(("Allocating too much"));
470 SILC_STACK_STAT(stack, num_errors, 1);
471 if (stack->oom_handler)
472 stack->oom_handler(stack, stack->oom_context);
477 old_size = SILC_STACK_ALIGN(old_size, stack->alignment);
479 /* Compute the size of current stack block */
480 bsize = SILC_STACK_BLOCK_SIZE(stack, si);
482 /* Check that `ptr' is last allocation */
483 sptr = (unsigned char *)stack->stack->data[si] +
484 SILC_STACK_ALIGN(sizeof(**stack->stack->data), stack->alignment);
485 if (stack->stack->data[si]->bytes_left + old_size +
486 ((unsigned char *)ptr - (unsigned char *)sptr) != bsize) {
487 SILC_LOG_DEBUG(("Cannot reallocate"));
488 SILC_STACK_STAT(stack, num_errors, 1);
492 /* Now check that the new size fits to this block */
493 if (stack->stack->data[si]->bytes_left >= size) {
494 /* It fits, so simply return the old pointer */
495 size = SILC_STACK_ALIGN(size, stack->alignment);
496 stack->stack->data[si]->bytes_left -= (size - old_size);
497 SILC_STACK_STAT(stack, bytes_malloc, (size - old_size));
501 SILC_LOG_DEBUG(("Cannot reallocate in this block"));
502 SILC_STACK_STAT(stack, num_errors, 1);
506 /* Set OOM handler */
508 void silc_stack_set_oom_handler(SilcStack stack,
509 SilcStackOomHandler oom_handler,
512 stack->oom_handler = oom_handler;
513 stack->oom_context = context;
516 /* Set default alignment */
518 void silc_stack_set_alignment(SilcStack stack, SilcUInt32 alignment)
520 SILC_LOG_DEBUG(("Set stack %p alignment to %d bytes", stack, alignment));
521 stack->alignment = alignment;
524 /* Get default alignment */
526 SilcUInt32 silc_stack_get_alignment(SilcStack stack)
528 return stack->alignment;
531 #ifdef SILC_DIST_INPLACE
532 /* Statistics dumping. */
534 void silc_stack_stats(SilcStack stack)
536 SilcStackDataEntry e;
537 SilcUInt32 stack_size = 0;
540 for (i = 0; i < SILC_STACK_BLOCK_NUM; i++) {
541 if (!stack->stack->data[i])
543 stack_size += SILC_STACK_BLOCK_SIZE(stack, i);
547 fprintf(stdout, "\nSilcStack %p statistics :\n\n", stack);
548 fprintf(stdout, " Size of stack : %u\n",
549 (unsigned int)stack_size);
550 fprintf(stdout, " Stack alignment : %d\n",
551 (int)stack->alignment);
552 fprintf(stdout, " Number of allocs : %u\n",
553 (unsigned int)stack->snum_malloc);
554 fprintf(stdout, " Bytes allocated : %u\n",
555 (unsigned int)stack->sbytes_malloc);
556 fprintf(stdout, " Average alloc size : %.2f\n",
557 (double)((double)stack->sbytes_malloc / (double)stack->snum_malloc));
558 fprintf(stdout, " Number of alloc errors : %u\n",
559 (unsigned int)stack->snum_errors);
560 fprintf(stdout, " Number of frames : %u\n",
561 (unsigned int)SILC_STACK_ALIGN(stack->frame->sp,
562 SILC_STACK_DEFAULT_NUM));
563 fprintf(stdout, " Number of blocks : %u\n", c);
564 fprintf(stdout, " Number of stacks : %d\n",
565 silc_list_count(stack->stacks));
567 silc_list_start(stack->stacks);
568 while ((e = silc_list_get(stack->stacks))) {
571 for (i = 0; i < SILC_STACK_BLOCK_NUM; i++) {
574 stack_size += e->data[i]->bytes_left;
577 fprintf(stdout, "\n Size of stack : %u\n",
578 (unsigned int)stack_size);
579 fprintf(stdout, " Number of blocks : %u\n", c);
582 #endif /* SILC_DIST_INPLACE */