Added.
[silc.git] / lib / silcutil / silcstack.c
index d22fba3f5b1d544f1129cfb8ba1ee4d52ee786bf..02e2e0c203b145a72ad1f3e3153afdefd4af335e 100644 (file)
@@ -95,25 +95,22 @@ static SilcStackDataEntry silc_stack_ref_stack(SilcStack stack,
     return e;
   }
 
+  silc_mutex_unlock(stack->lock);
+
   /* If we are child, get block from parent */
-  if (stack->parent) {
-    silc_mutex_unlock(stack->lock);
+  if (stack->parent)
     return silc_stack_ref_stack(stack->parent, size, ret_si, ret_bsize);
-  }
 
   SILC_ST_DEBUG(("Allocate new stack blocks"));
 
   /* Allocate new stack blocks */
   e = silc_calloc(1, sizeof(*e));
-  if (!e) {
-    silc_mutex_unlock(stack->lock);
+  if (!e)
     return NULL;
-  }
   e->data[si] = silc_malloc(bsize + SILC_STACK_ALIGN(sizeof(*e->data[0]),
                                                     stack->alignment));
   if (!e->data[si]) {
     silc_free(e);
-    silc_mutex_unlock(stack->lock);
     return NULL;
   }
   e->data[si]->bytes_left = bsize;
@@ -122,7 +119,6 @@ static SilcStackDataEntry silc_stack_ref_stack(SilcStack stack,
 
   SILC_ST_DEBUG(("Got stack blocks %p from stack %p", e->data, stack));
 
-  silc_mutex_unlock(stack->lock);
   return e;
 }
 
@@ -183,12 +179,13 @@ SilcStack silc_stack_alloc(SilcUInt32 stack_size, SilcStack parent)
 
   if (parent) {
     /* Get stack from parent.  The stack itself is allocated from the
-       parent. */
+       parent (but does not consume parent's own stack). */
     e = silc_stack_ref_stack(parent, stack_size, &si, &bsize);
     if (!e)
       return NULL;
 
-    /* Allocate stack from the parent */
+    /* Allocate stack from the returned stack.  We allocate ourselves from
+       our own stack. */
     stack = silc_stack_alloc_block(parent, e, 1, sizeof(*stack));
     if (!stack) {
       silc_stack_unref_stack(parent, e);
@@ -203,8 +200,8 @@ SilcStack silc_stack_alloc(SilcUInt32 stack_size, SilcStack parent)
     stack->lock = parent->lock;
     silc_list_init(stack->stacks, struct SilcStackDataEntryStruct, next);
 
-    /* Allocate stack frames from the parent */
-    stack->frames = silc_stack_alloc_block(parent, e, SILC_STACK_BLOCK_NUM,
+    /* Allocate stack frames from the stack itself */
+    stack->frames = silc_stack_alloc_block(stack, e, SILC_STACK_BLOCK_NUM,
                                           sizeof(*stack->frames));
     if (!stack->frames) {
       silc_stack_unref_stack(parent, e);