Mercurial > dovecot > original-hg > dovecot-1.2
changeset 536:37893ed97492 HEAD
changed t_push() and t_pop() to return unsigned int. added global
data_stack_frame which is used by TempString to verify it's accessed only
in the same frame.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Mon, 28 Oct 2002 06:50:14 +0200 |
parents | 5a193db8fe56 |
children | fe8a014a479e |
files | src/lib/data-stack.c src/lib/data-stack.h src/lib/ioloop-poll.c src/lib/ioloop.c src/lib/temp-string.c |
diffstat | 5 files changed, 31 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/src/lib/data-stack.c Mon Oct 28 06:36:43 2002 +0200 +++ b/src/lib/data-stack.c Mon Oct 28 06:50:14 2002 +0200 @@ -70,6 +70,8 @@ size_t last_alloc_size[BLOCK_FRAME_COUNT]; }; +unsigned int data_stack_frame; + static int frame_pos; /* current frame position current_frame_block */ static StackFrameBlock *current_frame_block; /* current stack frame block */ static StackFrameBlock *unused_frame_blocks; /* unused stack frames */ @@ -80,7 +82,7 @@ static StackBlock *last_buffer_block; static size_t last_buffer_size; -int t_push(void) +unsigned int t_push(void) { StackFrameBlock *frame_block; @@ -108,7 +110,7 @@ current_frame_block->block_space_used[frame_pos] = current_block->left; current_frame_block->last_alloc_size[frame_pos] = 0; - return frame_pos; + return data_stack_frame++; } static void free_blocks(StackBlock *block) @@ -127,7 +129,7 @@ } } -int t_pop(void) +unsigned int t_pop(void) { StackFrameBlock *frame_block; int popped_frame_pos; @@ -159,7 +161,7 @@ unused_frame_blocks = frame_block; } - return popped_frame_pos; + return --data_stack_frame; } static StackBlock *mem_block_alloc(size_t min_size) @@ -309,6 +311,8 @@ void data_stack_init(void) { + data_stack_frame = 0; + current_block = mem_block_alloc(INITIAL_STACK_SIZE); current_block->left = current_block->size; current_block->next = NULL; @@ -356,11 +360,10 @@ void *mem; }; -static int stack_counter; static StackFrame *current_frame; static void *buffer_mem; -int t_push(void) +unsigned int t_push(void) { StackFrame *frame; @@ -371,10 +374,10 @@ frame->next = current_frame; current_frame = frame; - return stack_counter++; + return data_stack_frame++; } -int t_pop(void) +unsigned int t_pop(void) { StackFrame *frame; FrameAlloc *alloc; @@ -391,7 +394,7 @@ } free(frame); - return --stack_counter; + return --data_stack_frame; } static void add_alloc(void *mem) @@ -479,7 +482,7 @@ void data_stack_init(void) { - stack_counter = 0; + data_stack_frame = 0; current_frame = NULL; buffer_mem = NULL; @@ -490,7 +493,7 @@ { t_pop(); - if (stack_counter != 0) + if (data_stack_frame != 0) i_panic("Missing t_pop() call"); }
--- a/src/lib/data-stack.h Mon Oct 28 06:36:43 2002 +0200 +++ b/src/lib/data-stack.h Mon Oct 28 06:50:14 2002 +0200 @@ -32,16 +32,16 @@ slower. */ +extern unsigned int data_stack_frame; + /* All t_..() allocations between t_push() and t_pop() are free'd after t_pop() is called. Returns stack frame number which can be used to detect missing t_pop() calls: x = t_push(); .. if (t_pop() != x) abort(); - - Note that the frame number wraps at some point (but t_pop() wraps it back). */ -int t_push(void); -int t_pop(void); +unsigned int t_push(void); +unsigned int t_pop(void); /* WARNING: Be careful when using this functions, it's too easy to accidentally save the returned value somewhere permanently.
--- a/src/lib/ioloop-poll.c Mon Oct 28 06:36:43 2002 +0200 +++ b/src/lib/ioloop-poll.c Mon Oct 28 06:50:14 2002 +0200 @@ -139,7 +139,8 @@ struct pollfd *pollfd; struct timeval tv; IO io, next; - int msecs, ret, t_id; + unsigned int t_id; + int msecs, ret; data = ioloop->handler_data;
--- a/src/lib/ioloop.c Mon Oct 28 06:36:43 2002 +0200 +++ b/src/lib/ioloop.c Mon Oct 28 06:50:14 2002 +0200 @@ -252,7 +252,7 @@ { Timeout t, next; struct timeval tv; - int t_id; + unsigned int t_id; gettimeofday(&ioloop_timeval, &ioloop_timezone); ioloop_time = ioloop_timeval.tv_sec;
--- a/src/lib/temp-string.c Mon Oct 28 06:36:43 2002 +0200 +++ b/src/lib/temp-string.c Mon Oct 28 06:50:14 2002 +0200 @@ -33,6 +33,7 @@ size_t len; size_t alloc_size; + unsigned int data_stack_frame; } RealTempString; TempString *t_string_new(size_t initial_size) @@ -43,6 +44,7 @@ initial_size = 64; rstr = t_new(RealTempString, 1); + rstr->data_stack_frame = data_stack_frame; rstr->alloc_size = initial_size; rstr->str = t_malloc(rstr->alloc_size); rstr->str[0] = '\0'; @@ -54,6 +56,8 @@ RealTempString *rstr = (RealTempString *) tstr; char *str; + i_assert(data_stack_frame == rstr->data_stack_frame); + size += rstr->len + 1; if (size <= rstr->len || size > SSIZE_T_MAX) { /* overflow */ @@ -111,6 +115,9 @@ void t_string_erase(TempString *tstr, size_t pos, size_t len) { + RealTempString *rstr = (RealTempString *) tstr; + + i_assert(data_stack_frame == rstr->data_stack_frame); i_assert(pos < tstr->len && tstr->len - pos >= len); memmove(tstr->str + pos + len, tstr->str + pos, @@ -119,6 +126,9 @@ void t_string_truncate(TempString *tstr, size_t len) { + RealTempString *rstr = (RealTempString *) tstr; + + i_assert(data_stack_frame == rstr->data_stack_frame); i_assert(len <= tstr->len); tstr->len = len;