changeset 394:24d69bc634d8 HEAD

t_try_grow() works now for "last allocated memory in current stack frame", instead of just for last allocated memory. ie. x = t_malloc(), t_push() ... t_pop(), t_try_grow(x) works.
author Timo Sirainen <tss@iki.fi>
date Wed, 09 Oct 2002 17:28:04 +0300
parents f09e67287f7d
children 8d3bfd8bc032
files src/lib/temp-mempool.c
diffstat 1 files changed, 11 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/temp-mempool.c	Wed Oct 09 17:03:08 2002 +0300
+++ b/src/lib/temp-mempool.c	Wed Oct 09 17:28:04 2002 +0300
@@ -65,6 +65,7 @@
 
 	MemBlock *block[MEM_LIST_BLOCK_COUNT];
         int block_space_used[MEM_LIST_BLOCK_COUNT];
+	int last_alloc_size;
 };
 
 static int stack_pos; /* next free position in current_stack->block[] */
@@ -74,8 +75,6 @@
 static MemBlock *current_block; /* block currently used for allocation */
 static MemBlock *unused_block; /* largest unused block is kept here */
 
-static int last_alloc_size;
-
 static MemBlock *last_buffer_block;
 static size_t last_buffer_size;
 
@@ -104,6 +103,7 @@
 	/* mark our current position */
 	current_stack->block[stack_pos] = current_block;
 	current_stack->block_space_used[stack_pos] = current_block->left;
+        current_stack->last_alloc_size = 0;
 
         return stack_pos++;
 }
@@ -197,7 +197,7 @@
 	size = MEM_ALIGN(size);
 
 	/* used for t_try_grow() */
-	last_alloc_size = size;
+	current_stack->last_alloc_size = size;
 
 	if (current_block->left >= size) {
 		/* enough space in current block, use it */
@@ -243,16 +243,19 @@
 
 int t_try_grow(void *mem, size_t size)
 {
+	size_t grow_size;
+
 	/* see if we want to grow the memory we allocated last */
 	if (MEM_BLOCK_DATA(current_block) +
 	    (current_block->size - current_block->left -
-	     last_alloc_size) == mem) {
-		/* yeah, see if we can grow */
+	     current_stack->last_alloc_size) == mem) {
+		/* yeah, see if we have space to grow */
 		size = MEM_ALIGN(size);
-		if (current_block->left >= size-last_alloc_size) {
+		grow_size = size - current_stack->last_alloc_size;
+		if (current_block->left >= grow_size) {
 			/* just shrink the available size */
-			current_block->left -= size - last_alloc_size;
-			last_alloc_size = size;
+			current_block->left -= grow_size;
+			current_stack->last_alloc_size = size;
 			return TRUE;
 		}
 	}
@@ -309,8 +312,6 @@
 
 	t_push();
 
-        last_alloc_size = 0;
-
         last_buffer_block = NULL;
 	last_buffer_size = 0;
 }