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
183 e = silc_stack_ref_stack(parent, stack_size, &si, &bsize);
187 /* Allocate stack from the parent */
188 stack = silc_stack_alloc_block(parent, e, 1, sizeof(*stack));
190 silc_stack_unref_stack(parent, e);
194 stack->parent = parent;
195 stack->stack_size = stack_size;
196 stack->alignment = SILC_STACK_DEFAULT_ALIGN;
197 stack->oom_handler = parent->oom_handler;
198 stack->oom_context = parent->oom_context;
199 stack->lock = parent->lock;
200 silc_list_init(stack->stacks, struct SilcStackDataEntryStruct, next);
202 /* Allocate stack frames from the parent */
203 stack->frames = silc_stack_alloc_block(parent, e, SILC_STACK_BLOCK_NUM,
204 sizeof(*stack->frames));
205 if (!stack->frames) {
206 silc_stack_unref_stack(parent, e);
210 /* Set the initial stack */
213 /* Dynamically allocate new stack */
214 stack = silc_calloc(1, sizeof(*stack));
218 stack->stack_size = stack_size;
219 stack->alignment = SILC_STACK_DEFAULT_ALIGN;
220 silc_list_init(stack->stacks, struct SilcStackDataEntryStruct, next);
222 /* Create initial stack */
223 stack->stack = silc_calloc(1, sizeof(*stack->stack));
228 stack->stack->data[0] =
229 silc_malloc(stack->stack_size +
230 SILC_STACK_ALIGN(sizeof(*stack->stack->data[0]),
232 if (!stack->stack->data[0]) {
233 silc_free(stack->stack);
237 stack->stack->data[0]->bytes_left = stack->stack_size;
238 stack->stack->si = 0;
239 stack->stack->bsize = stack->stack_size;
241 /* Allocate stack frames from the stack itself */
242 stack->frames = silc_stack_alloc_block(stack, stack->stack,
243 SILC_STACK_DEFAULT_NUM,
244 sizeof(*stack->frames));
245 if (!stack->frames) {
246 silc_free(stack->stack->data[0]);
247 silc_free(stack->stack);
253 silc_mutex_alloc(&stack->lock);
256 /* Use the allocated stack in first stack frame */
257 stack->frame = &stack->frames[0];
258 stack->frame->prev = NULL;
259 stack->frame->bytes_used = stack->stack_size;
260 stack->frame->sp = 1;
261 stack->frame->si = si;
263 SILC_LOG_DEBUG(("New stack %p, size %d bytes", stack, stack->stack_size));
268 /* Frees the stack and all allocated memory */
270 void silc_stack_free(SilcStack stack)
272 SilcStackDataEntry e;
278 SILC_LOG_DEBUG(("Free stack %p", stack));
280 if (!stack->parent) {
281 silc_list_start(stack->stacks);
282 while ((e = silc_list_get(stack->stacks))) {
283 for (i = 0; i < SILC_STACK_BLOCK_NUM; i++)
284 silc_free(e->data[i]);
288 for (i = 0; i < SILC_STACK_BLOCK_NUM; i++)
289 silc_free(stack->stack->data[i]);
290 silc_free(stack->stack);
293 silc_mutex_free(stack->lock);
297 /* Return all stack blocks to the parent */
298 silc_list_start(stack->stacks);
299 while ((e = silc_list_get(stack->stacks)))
300 silc_stack_unref_stack(stack->parent, e);
302 silc_stack_unref_stack(stack->parent, stack->stack);
306 /* Push to next stack frame */
308 SilcUInt32 silc_stack_push(SilcStack stack, SilcStackFrame *frame)
314 if (stack->frame->sp >= SILC_STACK_ALIGN(stack->frame->sp,
315 SILC_STACK_DEFAULT_NUM)) {
316 SILC_LOG_DEBUG(("SilcStack %p running out of frames, cannot push",
318 return stack->frame->sp;
321 frame = &stack->frames[stack->frame->sp];
325 frame->prev = stack->frame;
326 frame->sp = stack->frame->sp + 1;
327 frame->si = stack->frame->si;
328 frame->bytes_used = stack->stack->data[frame->si]->bytes_left;
329 stack->frame = frame;
331 SILC_ST_DEBUG(("Push %p: sp %d -> %d, si %d", stack, frame->prev->sp,
332 frame->sp, frame->si));
334 return stack->frame->sp;
337 /* Pop to previous stack frame */
339 SilcUInt32 silc_stack_pop(SilcStack stack)
343 if (!stack || !stack->frame->prev)
347 si = stack->frame->si;
348 while (si > stack->frame->prev->si) {
349 if (stack->stack->data[si])
350 stack->stack->data[si]->bytes_left = SILC_STACK_BLOCK_SIZE(stack, si);
353 stack->stack->data[si]->bytes_left = stack->frame->bytes_used;
354 stack->frame = stack->frame->prev;
356 SILC_ST_DEBUG(("Pop %p: sp %d -> %d, si %d", stack, stack->frame->sp + 1,
357 stack->frame->sp, stack->frame->si));
359 return stack->frame->sp + 1;
362 /* Allocate memory. Returns pointer to the memory or NULL on error. */
364 void *silc_stack_malloc(SilcStack stack, SilcUInt32 size)
367 SilcUInt32 bsize, bsize2;
368 SilcUInt32 si = stack->frame->si;
370 SILC_STACK_STAT(stack, num_malloc, 1);
371 SILC_ST_DEBUG(("Allocating %d bytes from %p", size, stack));
373 if (silc_unlikely(!size)) {
374 SILC_LOG_ERROR(("Allocation by zero (0)"));
375 SILC_STACK_STAT(stack, num_errors, 1);
379 if (silc_unlikely(size > SILC_STACK_MAX_ALLOC)) {
380 SILC_LOG_ERROR(("Allocating too much"));
381 SILC_STACK_STAT(stack, num_errors, 1);
382 if (stack->oom_handler)
383 stack->oom_handler(stack, stack->oom_context);
388 size = SILC_STACK_ALIGN(size, stack->alignment);
390 /* Compute the size of current stack block */
391 bsize = SILC_STACK_BLOCK_SIZE(stack, si);
393 /* See if there is space in the current stack block */
394 if (stack->stack->data[si]->bytes_left >= size) {
395 /* Get pointer to the memory */
396 ptr = SILC_STACK_DATA(stack, si, bsize);
397 stack->stack->data[si]->bytes_left -= size;
398 SILC_STACK_STAT(stack, bytes_malloc, size);
402 /* There is not enough space in this block. Find the spot to stack
403 block that can handle this size memory. */
404 if (bsize < SILC_STACK_DEFAULT_SIZE)
405 bsize = SILC_STACK_DEFAULT_SIZE;
407 bsize2 = SILC_STACK_DEFAULT_SIZE;
409 while (bsize2 < bsize) {
413 if (silc_unlikely(si >= SILC_STACK_BLOCK_NUM)) {
414 SILC_LOG_ERROR(("Allocating too large block"));
415 SILC_STACK_STAT(stack, num_errors, 1);
416 if (stack->oom_handler)
417 stack->oom_handler(stack, stack->oom_context);
421 /* Allocate the block if it doesn't exist yet */
422 if (!stack->stack->data[si]) {
423 SILC_ST_DEBUG(("Allocating new stack block, %d bytes", bsize2));
424 stack->stack->data[si] =
426 SILC_STACK_ALIGN(sizeof(**stack->stack->data),
428 if (silc_unlikely(!stack->stack->data[si])) {
429 SILC_STACK_STAT(stack, num_errors, 1);
430 if (stack->oom_handler)
431 stack->oom_handler(stack, stack->oom_context);
434 stack->stack->data[si]->bytes_left = bsize2;
437 /* Now return memory from this new block. It is guaranteed that in this
438 block there is enough space for this memory. */
439 assert(stack->stack->data[si]->bytes_left >= size);
440 ptr = SILC_STACK_DATA(stack, si, bsize2);
441 stack->stack->data[si]->bytes_left -= size;
442 stack->frame->si = si;
443 SILC_STACK_STAT(stack, bytes_malloc, size);
448 /* Attempts to reallocate memory by changing the size of the `ptr' into
449 `size'. This routine works only if the previous allocation to `stack'
450 was `ptr'. If there is another memory allocation between allocating
451 `ptr' and this call this routine will return NULL. NULL is also returned
452 if the `size' does not fit into the current block. If NULL is returned
453 the old memory remains intact. */
455 void *silc_stack_realloc(SilcStack stack, SilcUInt32 old_size,
456 void *ptr, SilcUInt32 size)
458 SilcUInt32 si = stack->frame->si;
463 return silc_stack_malloc(stack, size);
465 SILC_STACK_STAT(stack, num_malloc, 1);
466 SILC_ST_DEBUG(("Reallocating %d bytes (%d) from %p", size, old_size, stack));
468 if (silc_unlikely(!size || !old_size)) {
469 SILC_LOG_ERROR(("Allocation by zero (0)"));
470 SILC_STACK_STAT(stack, num_errors, 1);
474 if (silc_unlikely(size > SILC_STACK_MAX_ALLOC)) {
475 SILC_LOG_ERROR(("Allocating too much"));
476 SILC_STACK_STAT(stack, num_errors, 1);
477 if (stack->oom_handler)
478 stack->oom_handler(stack, stack->oom_context);
483 old_size = SILC_STACK_ALIGN(old_size, stack->alignment);
485 /* Compute the size of current stack block */
486 bsize = SILC_STACK_BLOCK_SIZE(stack, si);
488 /* Check that `ptr' is last allocation */
489 sptr = (unsigned char *)stack->stack->data[si] +
490 SILC_STACK_ALIGN(sizeof(**stack->stack->data), stack->alignment);
491 if (stack->stack->data[si]->bytes_left + old_size +
492 ((unsigned char *)ptr - (unsigned char *)sptr) != bsize) {
493 SILC_LOG_DEBUG(("Cannot reallocate"));
494 SILC_STACK_STAT(stack, num_errors, 1);
498 /* Now check that the new size fits to this block */
499 if (stack->stack->data[si]->bytes_left >= size) {
500 /* It fits, so simply return the old pointer */
501 size = SILC_STACK_ALIGN(size, stack->alignment);
502 stack->stack->data[si]->bytes_left -= (size - old_size);
503 SILC_STACK_STAT(stack, bytes_malloc, (size - old_size));
507 SILC_LOG_DEBUG(("Cannot reallocate in this block"));
508 SILC_STACK_STAT(stack, num_errors, 1);
512 /* Set OOM handler */
514 void silc_stack_set_oom_handler(SilcStack stack,
515 SilcStackOomHandler oom_handler,
518 stack->oom_handler = oom_handler;
519 stack->oom_context = context;
522 /* Set default alignment */
524 void silc_stack_set_alignment(SilcStack stack, SilcUInt32 alignment)
526 SILC_LOG_DEBUG(("Set stack %p alignment to %d bytes", stack, alignment));
527 stack->alignment = alignment;
530 /* Get default alignment */
532 SilcUInt32 silc_stack_get_alignment(SilcStack stack)
534 return stack->alignment;
539 SilcBool silc_stack_purge(SilcStack stack)
541 SilcStackDataEntry e;
542 SilcBool ret = FALSE;
545 SILC_LOG_DEBUG(("Purge stack %p", stack));
547 /* Go through the default stack */
548 for (i = SILC_STACK_BLOCK_NUM - 1; i > 3; i--) {
549 if (stack->stack->data[i] &&
550 stack->stack->data[i]->bytes_left == SILC_STACK_BLOCK_SIZE(stack, i)) {
551 SILC_LOG_DEBUG(("Purge %d bytes",
552 SILC_STACK_BLOCK_SIZE(stack, i)));
553 silc_free(stack->stack->data[i]);
554 stack->stack->data[i] = NULL;
559 silc_mutex_lock(stack->lock);
561 /* Remove one child stack */
562 if (silc_list_count(stack->stacks) > 2) {
563 silc_list_start(stack->stacks);
564 e = silc_list_get(stack->stacks);
566 SILC_LOG_DEBUG(("Remove stack blocks %p", e->data));
567 silc_list_del(stack->stacks, e);
570 for (i = 0; i < SILC_STACK_BLOCK_NUM; i++)
571 silc_free(e->data[i]);
575 /* Go through the child stacks */
576 silc_list_start(stack->stacks);
577 while ((e = silc_list_get(stack->stacks))) {
578 for (i = SILC_STACK_BLOCK_NUM - 1; i > 3; i--) {
580 SILC_LOG_DEBUG(("Purge %d bytes",
581 SILC_STACK_BLOCK_SIZE(stack, i)));
582 silc_free(e->data[i]);
589 silc_mutex_unlock(stack->lock);
594 #ifdef SILC_DIST_INPLACE
595 /* Statistics dumping. */
597 void silc_stack_stats(SilcStack stack)
599 SilcStackDataEntry e;
600 SilcUInt32 stack_size = 0;
603 for (i = 0; i < SILC_STACK_BLOCK_NUM; i++) {
604 if (!stack->stack->data[i])
606 stack_size += SILC_STACK_BLOCK_SIZE(stack, i);
610 fprintf(stdout, "\nSilcStack %p statistics :\n\n", stack);
611 fprintf(stdout, " Size of stack : %u\n",
612 (unsigned int)stack_size);
613 fprintf(stdout, " Stack alignment : %d\n",
614 (int)stack->alignment);
615 fprintf(stdout, " Number of allocs : %u\n",
616 (unsigned int)stack->snum_malloc);
617 fprintf(stdout, " Bytes allocated : %u\n",
618 (unsigned int)stack->sbytes_malloc);
619 fprintf(stdout, " Average alloc size : %.2f\n",
620 (double)((double)stack->sbytes_malloc / (double)stack->snum_malloc));
621 fprintf(stdout, " Number of alloc errors : %u\n",
622 (unsigned int)stack->snum_errors);
623 fprintf(stdout, " Number of frames : %u\n",
624 (unsigned int)SILC_STACK_ALIGN(stack->frame->sp,
625 SILC_STACK_DEFAULT_NUM));
626 fprintf(stdout, " Number of blocks : %u\n", c);
627 fprintf(stdout, " Number of stacks : %d\n",
628 silc_list_count(stack->stacks));
630 silc_list_start(stack->stacks);
631 while ((e = silc_list_get(stack->stacks))) {
634 for (i = 0; i < SILC_STACK_BLOCK_NUM; i++) {
637 stack_size += e->data[i]->bytes_left;
640 fprintf(stdout, "\n Size of stack : %u\n",
641 (unsigned int)stack_size);
642 fprintf(stdout, " Number of blocks : %u\n", c);
645 #endif /* SILC_DIST_INPLACE */