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, stack %p",
84 silc_mutex_lock(stack->lock);
86 /* Get stack that has block that can house our size requirement. */
87 silc_list_start(stack->stacks);
88 while ((e = silc_list_get(stack->stacks))) {
92 silc_list_del(stack->stacks, e);
93 SILC_ST_DEBUG(("Got stack blocks %p from stack %p", e->data, stack));
94 silc_mutex_unlock(stack->lock);
98 silc_mutex_unlock(stack->lock);
100 /* If we are child, get block from parent */
102 return silc_stack_ref_stack(stack->parent, size, ret_si, ret_bsize);
104 SILC_ST_DEBUG(("Allocate new stack blocks"));
106 /* Allocate new stack blocks */
107 e = silc_calloc(1, sizeof(*e));
110 e->data[si] = silc_malloc(bsize + SILC_STACK_ALIGN(sizeof(*e->data[0]),
116 e->data[si]->bytes_left = bsize;
120 SILC_ST_DEBUG(("Got stack blocks %p from stack %p", e->data, stack));
125 /* Return the `data' back to the `stack'. */
127 static void silc_stack_unref_stack(SilcStack stack, SilcStackDataEntry e)
131 SILC_LOG_DEBUG(("Release stack blocks %p to stack %p, si %d",
132 e->data, stack, e->si));
134 /* Release all blocks from allocations */
135 for (i = e->si; i < SILC_STACK_BLOCK_NUM; i++) {
139 e->data[i]->bytes_left = e->bsize;
141 e->data[i]->bytes_left = SILC_STACK_BLOCK_SIZE(stack, i);
144 silc_mutex_lock(stack->lock);
145 silc_list_add(stack->stacks, e);
146 silc_mutex_unlock(stack->lock);
149 /* Allocate memory from a specific stack block */
151 static void *silc_stack_alloc_block(SilcStack stack, SilcStackDataEntry e,
152 SilcUInt32 items, SilcUInt32 size)
157 /* Get pointer and consume the stack block */
158 asize = SILC_STACK_ALIGN(items * size, stack->alignment);
159 ptr = SILC_STACK_DATA_EXT(e->data, e->si, e->bsize, stack->alignment);
160 e->data[e->si]->bytes_left -= asize;
161 memset(ptr, 0, items * size);
166 /***************************** SilcStack API ********************************/
168 /* Allocate the stack */
170 SilcStack silc_stack_alloc(SilcUInt32 stack_size, SilcStack parent)
173 SilcStackDataEntry e;
174 SilcUInt32 si = 0, bsize = 0;
176 stack_size = stack_size ? stack_size : SILC_STACK_DEFAULT_SIZE;
177 if (stack_size < SILC_STACK_DEFAULT_SIZE)
178 stack_size = SILC_STACK_DEFAULT_SIZE;
181 /* Get stack from parent. The stack itself is allocated from the
182 parent (but does not consume parent's own stack). */
183 e = silc_stack_ref_stack(parent, stack_size, &si, &bsize);
187 /* Allocate stack from the returned stack. We allocate ourselves from
189 stack = silc_stack_alloc_block(parent, e, 1, sizeof(*stack));
191 silc_stack_unref_stack(parent, e);
195 stack->parent = parent;
196 stack->stack_size = stack_size;
197 stack->alignment = SILC_STACK_DEFAULT_ALIGN;
198 stack->oom_handler = parent->oom_handler;
199 stack->oom_context = parent->oom_context;
200 stack->lock = parent->lock;
201 silc_list_init(stack->stacks, struct SilcStackDataEntryStruct, next);
203 /* Allocate stack frames from the stack itself */
204 stack->frames = silc_stack_alloc_block(stack, e, SILC_STACK_BLOCK_NUM,
205 sizeof(*stack->frames));
206 if (!stack->frames) {
207 silc_stack_unref_stack(parent, e);
211 /* Set the initial stack */
214 /* Dynamically allocate new stack */
215 stack = silc_calloc(1, sizeof(*stack));
219 stack->stack_size = stack_size;
220 stack->alignment = SILC_STACK_DEFAULT_ALIGN;
221 silc_list_init(stack->stacks, struct SilcStackDataEntryStruct, next);
223 /* Create initial stack */
224 stack->stack = silc_calloc(1, sizeof(*stack->stack));
229 stack->stack->data[0] =
230 silc_malloc(stack->stack_size +
231 SILC_STACK_ALIGN(sizeof(*stack->stack->data[0]),
233 if (!stack->stack->data[0]) {
234 silc_free(stack->stack);
238 stack->stack->data[0]->bytes_left = stack->stack_size;
239 stack->stack->si = 0;
240 stack->stack->bsize = stack->stack_size;
242 /* Allocate stack frames from the stack itself */
243 stack->frames = silc_stack_alloc_block(stack, stack->stack,
244 SILC_STACK_DEFAULT_NUM,
245 sizeof(*stack->frames));
246 if (!stack->frames) {
247 silc_free(stack->stack->data[0]);
248 silc_free(stack->stack);
254 silc_mutex_alloc(&stack->lock);
257 /* Use the allocated stack in first stack frame */
258 stack->frame = &stack->frames[0];
259 stack->frame->prev = NULL;
260 stack->frame->bytes_used = stack->stack_size;
261 stack->frame->sp = 1;
262 stack->frame->si = si;
264 SILC_LOG_DEBUG(("New stack %p, size %d bytes", stack, stack->stack_size));
269 /* Frees the stack and all allocated memory */
271 void silc_stack_free(SilcStack stack)
273 SilcStackDataEntry e;
279 SILC_LOG_DEBUG(("Free stack %p", stack));
281 if (!stack->parent) {
282 silc_list_start(stack->stacks);
283 while ((e = silc_list_get(stack->stacks))) {
284 for (i = 0; i < SILC_STACK_BLOCK_NUM; i++)
285 silc_free(e->data[i]);
289 for (i = 0; i < SILC_STACK_BLOCK_NUM; i++)
290 silc_free(stack->stack->data[i]);
291 silc_free(stack->stack);
294 silc_mutex_free(stack->lock);
298 /* Return all stack blocks to the parent */
299 silc_list_start(stack->stacks);
300 while ((e = silc_list_get(stack->stacks)))
301 silc_stack_unref_stack(stack->parent, e);
303 silc_stack_unref_stack(stack->parent, stack->stack);
307 /* Push to next stack frame */
309 SilcUInt32 silc_stack_push(SilcStack stack, SilcStackFrame *frame)
315 if (stack->frame->sp >= SILC_STACK_ALIGN(stack->frame->sp,
316 SILC_STACK_DEFAULT_NUM)) {
317 SILC_LOG_DEBUG(("SilcStack %p running out of frames, cannot push",
319 return stack->frame->sp;
322 frame = &stack->frames[stack->frame->sp];
326 frame->prev = stack->frame;
327 frame->sp = stack->frame->sp + 1;
328 frame->si = stack->frame->si;
329 frame->bytes_used = stack->stack->data[frame->si]->bytes_left;
330 stack->frame = frame;
332 SILC_ST_DEBUG(("Push %p: sp %d -> %d, si %d", stack, frame->prev->sp,
333 frame->sp, frame->si));
335 return stack->frame->sp;
338 /* Pop to previous stack frame */
340 SilcUInt32 silc_stack_pop(SilcStack stack)
344 if (!stack || !stack->frame->prev)
348 si = stack->frame->si;
349 while (si > stack->frame->prev->si) {
350 if (stack->stack->data[si])
351 stack->stack->data[si]->bytes_left = SILC_STACK_BLOCK_SIZE(stack, si);
354 stack->stack->data[si]->bytes_left = stack->frame->bytes_used;
355 stack->frame = stack->frame->prev;
357 SILC_ST_DEBUG(("Pop %p: sp %d -> %d, si %d", stack, stack->frame->sp + 1,
358 stack->frame->sp, stack->frame->si));
360 return stack->frame->sp + 1;
363 /* Allocate memory. Returns pointer to the memory or NULL on error. */
365 void *silc_stack_malloc(SilcStack stack, SilcUInt32 size)
368 SilcUInt32 bsize, bsize2;
369 SilcUInt32 si = stack->frame->si;
371 SILC_STACK_STAT(stack, num_malloc, 1);
372 SILC_ST_DEBUG(("Allocating %d bytes from %p", size, stack));
374 if (silc_unlikely(!size)) {
375 SILC_LOG_ERROR(("Allocation by zero (0)"));
376 SILC_STACK_STAT(stack, num_errors, 1);
380 if (silc_unlikely(size > SILC_STACK_MAX_ALLOC)) {
381 SILC_LOG_ERROR(("Allocating too much"));
382 SILC_STACK_STAT(stack, num_errors, 1);
383 if (stack->oom_handler)
384 stack->oom_handler(stack, stack->oom_context);
389 size = SILC_STACK_ALIGN(size, stack->alignment);
391 /* Compute the size of current stack block */
392 bsize = SILC_STACK_BLOCK_SIZE(stack, si);
394 /* See if there is space in the current stack block */
395 if (stack->stack->data[si]->bytes_left >= size) {
396 /* Get pointer to the memory */
397 ptr = SILC_STACK_DATA(stack, si, bsize);
398 stack->stack->data[si]->bytes_left -= size;
399 SILC_STACK_STAT(stack, bytes_malloc, size);
403 /* There is not enough space in this block. Find the spot to stack
404 block that can handle this size memory. */
405 if (bsize < SILC_STACK_DEFAULT_SIZE)
406 bsize = SILC_STACK_DEFAULT_SIZE;
408 bsize2 = SILC_STACK_DEFAULT_SIZE;
410 while (bsize2 < bsize) {
414 if (silc_unlikely(si >= SILC_STACK_BLOCK_NUM)) {
415 SILC_LOG_ERROR(("Allocating too large block"));
416 SILC_STACK_STAT(stack, num_errors, 1);
417 if (stack->oom_handler)
418 stack->oom_handler(stack, stack->oom_context);
422 /* Allocate the block if it doesn't exist yet */
423 if (!stack->stack->data[si]) {
424 SILC_ST_DEBUG(("Allocating new stack block, %d bytes", bsize2));
425 stack->stack->data[si] =
427 SILC_STACK_ALIGN(sizeof(**stack->stack->data),
429 if (silc_unlikely(!stack->stack->data[si])) {
430 SILC_STACK_STAT(stack, num_errors, 1);
431 if (stack->oom_handler)
432 stack->oom_handler(stack, stack->oom_context);
435 stack->stack->data[si]->bytes_left = bsize2;
438 /* Now return memory from this new block. It is guaranteed that in this
439 block there is enough space for this memory. */
440 assert(stack->stack->data[si]->bytes_left >= size);
441 ptr = SILC_STACK_DATA(stack, si, bsize2);
442 stack->stack->data[si]->bytes_left -= size;
443 stack->frame->si = si;
444 SILC_STACK_STAT(stack, bytes_malloc, size);
449 /* Attempts to reallocate memory by changing the size of the `ptr' into
450 `size'. This routine works only if the previous allocation to `stack'
451 was `ptr'. If there is another memory allocation between allocating
452 `ptr' and this call this routine will return NULL. NULL is also returned
453 if the `size' does not fit into the current block. If NULL is returned
454 the old memory remains intact. */
456 void *silc_stack_realloc(SilcStack stack, SilcUInt32 old_size,
457 void *ptr, SilcUInt32 size)
459 SilcUInt32 si = stack->frame->si;
464 return silc_stack_malloc(stack, size);
466 SILC_STACK_STAT(stack, num_malloc, 1);
467 SILC_ST_DEBUG(("Reallocating %d bytes (%d) from %p", size, old_size, stack));
469 if (silc_unlikely(!size || !old_size)) {
470 SILC_LOG_ERROR(("Allocation by zero (0)"));
471 SILC_STACK_STAT(stack, num_errors, 1);
475 if (silc_unlikely(size > SILC_STACK_MAX_ALLOC)) {
476 SILC_LOG_ERROR(("Allocating too much"));
477 SILC_STACK_STAT(stack, num_errors, 1);
478 if (stack->oom_handler)
479 stack->oom_handler(stack, stack->oom_context);
484 old_size = SILC_STACK_ALIGN(old_size, stack->alignment);
486 /* Compute the size of current stack block */
487 bsize = SILC_STACK_BLOCK_SIZE(stack, si);
489 /* Check that `ptr' is last allocation */
490 sptr = (unsigned char *)stack->stack->data[si] +
491 SILC_STACK_ALIGN(sizeof(**stack->stack->data), stack->alignment);
492 if (stack->stack->data[si]->bytes_left + old_size +
493 ((unsigned char *)ptr - (unsigned char *)sptr) != bsize) {
494 SILC_LOG_DEBUG(("Cannot reallocate"));
495 SILC_STACK_STAT(stack, num_errors, 1);
499 /* Now check that the new size fits to this block */
500 if (stack->stack->data[si]->bytes_left >= size) {
501 /* It fits, so simply return the old pointer */
502 size = SILC_STACK_ALIGN(size, stack->alignment);
503 stack->stack->data[si]->bytes_left -= (size - old_size);
504 SILC_STACK_STAT(stack, bytes_malloc, (size - old_size));
508 SILC_LOG_DEBUG(("Cannot reallocate in this block"));
509 SILC_STACK_STAT(stack, num_errors, 1);
513 /* Set OOM handler */
515 void silc_stack_set_oom_handler(SilcStack stack,
516 SilcStackOomHandler oom_handler,
519 stack->oom_handler = oom_handler;
520 stack->oom_context = context;
523 /* Set default alignment */
525 void silc_stack_set_alignment(SilcStack stack, SilcUInt32 alignment)
527 SILC_LOG_DEBUG(("Set stack %p alignment to %d bytes", stack, alignment));
528 stack->alignment = alignment;
531 /* Get default alignment */
533 SilcUInt32 silc_stack_get_alignment(SilcStack stack)
535 return stack->alignment;
540 SilcBool silc_stack_purge(SilcStack stack)
542 SilcStackDataEntry e;
543 SilcBool ret = FALSE;
546 SILC_LOG_DEBUG(("Purge stack %p", stack));
548 /* Go through the default stack */
549 for (i = SILC_STACK_BLOCK_NUM - 1; i > 3; i--) {
550 if (stack->stack->data[i] &&
551 stack->stack->data[i]->bytes_left == SILC_STACK_BLOCK_SIZE(stack, i)) {
552 SILC_LOG_DEBUG(("Purge %d bytes",
553 SILC_STACK_BLOCK_SIZE(stack, i)));
554 silc_free(stack->stack->data[i]);
555 stack->stack->data[i] = NULL;
560 silc_mutex_lock(stack->lock);
562 /* Remove one child stack */
563 if (silc_list_count(stack->stacks) > 2) {
564 silc_list_start(stack->stacks);
565 e = silc_list_get(stack->stacks);
567 SILC_LOG_DEBUG(("Remove stack blocks %p", e->data));
568 silc_list_del(stack->stacks, e);
571 for (i = 0; i < SILC_STACK_BLOCK_NUM; i++)
572 silc_free(e->data[i]);
576 /* Go through the child stacks */
577 silc_list_start(stack->stacks);
578 while ((e = silc_list_get(stack->stacks))) {
579 for (i = SILC_STACK_BLOCK_NUM - 1; i > 3; i--) {
581 SILC_LOG_DEBUG(("Purge %d bytes",
582 SILC_STACK_BLOCK_SIZE(stack, i)));
583 silc_free(e->data[i]);
590 silc_mutex_unlock(stack->lock);
595 #ifdef SILC_DIST_INPLACE
596 /* Statistics dumping. */
598 void silc_stack_stats(SilcStack stack)
600 SilcStackDataEntry e;
601 SilcUInt32 stack_size = 0;
604 for (i = 0; i < SILC_STACK_BLOCK_NUM; i++) {
605 if (!stack->stack->data[i])
607 stack_size += SILC_STACK_BLOCK_SIZE(stack, i);
611 fprintf(stdout, "\nSilcStack %p statistics :\n\n", stack);
612 fprintf(stdout, " Size of stack : %u\n",
613 (unsigned int)stack_size);
614 fprintf(stdout, " Stack alignment : %d\n",
615 (int)stack->alignment);
616 fprintf(stdout, " Number of allocs : %u\n",
617 (unsigned int)stack->snum_malloc);
618 fprintf(stdout, " Bytes allocated : %u\n",
619 (unsigned int)stack->sbytes_malloc);
620 fprintf(stdout, " Average alloc size : %.2f\n",
621 (double)((double)stack->sbytes_malloc / (double)stack->snum_malloc));
622 fprintf(stdout, " Number of alloc errors : %u\n",
623 (unsigned int)stack->snum_errors);
624 fprintf(stdout, " Number of frames : %u\n",
625 (unsigned int)SILC_STACK_ALIGN(stack->frame->sp,
626 SILC_STACK_DEFAULT_NUM));
627 fprintf(stdout, " Number of blocks : %u\n", c);
628 fprintf(stdout, " Number of stacks : %d\n",
629 silc_list_count(stack->stacks));
631 silc_list_start(stack->stacks);
632 while ((e = silc_list_get(stack->stacks))) {
635 for (i = 0; i < SILC_STACK_BLOCK_NUM; i++) {
638 stack_size += e->data[i]->bytes_left;
641 fprintf(stdout, "\n Size of stack : %u\n",
642 (unsigned int)stack_size);
643 fprintf(stdout, " Number of blocks : %u\n", c);
646 #endif /* SILC_DIST_INPLACE */