Skip to content

Commit

Permalink
lib: Created data_stack_frame_t type for data_stack_frame
Browse files Browse the repository at this point in the history
  • Loading branch information
sirainen authored and GitLab committed Sep 2, 2016
1 parent 7bc9e4a commit e258887
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
12 changes: 6 additions & 6 deletions src/lib/data-stack.c
Expand Up @@ -67,7 +67,7 @@ struct stack_frame_block {
#endif
};

unsigned int data_stack_frame = 0;
data_stack_frame_t data_stack_frame = 0;

static bool data_stack_initialized = FALSE;
static int frame_pos = BLOCK_FRAME_COUNT-1; /* in current_frame_block */
Expand Down Expand Up @@ -127,7 +127,7 @@ static void data_stack_last_buffer_reset(bool preserve_data ATTR_UNUSED)
}
}

unsigned int t_push(const char *marker)
data_stack_frame_t t_push(const char *marker)
{
struct stack_frame_block *frame_block;

Expand Down Expand Up @@ -179,9 +179,9 @@ unsigned int t_push(const char *marker)
return data_stack_frame++;
}

unsigned int t_push_named(const char *format, ...)
data_stack_frame_t t_push_named(const char *format, ...)
{
unsigned int ret = t_push(NULL);
data_stack_frame_t ret = t_push(NULL);
#ifdef DEBUG
va_list args;
va_start(args, format);
Expand Down Expand Up @@ -260,7 +260,7 @@ static void t_pop_verify(void)
}
#endif

unsigned int t_pop(void)
data_stack_frame_t t_pop(void)
{
struct stack_frame_block *frame_block;

Expand Down Expand Up @@ -310,7 +310,7 @@ unsigned int t_pop(void)
return --data_stack_frame;
}

void t_pop_check(unsigned int *id)
void t_pop_check(data_stack_frame_t *id)
{
if (unlikely(t_pop() != *id))
i_panic("Leaked t_pop() call");
Expand Down
18 changes: 10 additions & 8 deletions src/lib/data-stack.h
Expand Up @@ -31,7 +31,9 @@
overflows.
*/

extern unsigned int data_stack_frame;
typedef unsigned int data_stack_frame_t;

extern data_stack_frame_t data_stack_frame;

/* All t_..() allocations between t_push*() and t_pop() are freed after t_pop()
is called. Returns the current stack frame number, which can be used
Expand All @@ -43,24 +45,24 @@ extern unsigned int data_stack_frame;
but is safe to call in a loop as it performs the allocation within its own
frame. However, you should always prefer to use T_BEGIN { ... } T_END below.
*/
unsigned int t_push(const char *marker) ATTR_HOT;
unsigned int t_push_named(const char *format, ...) ATTR_HOT ATTR_FORMAT(1, 2);
unsigned int t_pop(void) ATTR_HOT;
data_stack_frame_t t_push(const char *marker) ATTR_HOT;
data_stack_frame_t t_push_named(const char *format, ...) ATTR_HOT ATTR_FORMAT(1, 2);
data_stack_frame_t t_pop(void) ATTR_HOT;
/* Simplifies the if (t_pop() != x) check by comparing it internally and
panicking if it doesn't match. */
void t_pop_check(unsigned int *id) ATTR_HOT;
void t_pop_check(data_stack_frame_t *id) ATTR_HOT;

/* Usage: T_BEGIN { code } T_END */
#ifndef DEBUG
#define T_BEGIN \
STMT_START { unsigned int _data_stack_cur_id = t_push(NULL);
STMT_START { data_stack_frame_t _data_stack_cur_id = t_push(NULL);
#elif defined (__GNUC__) && !defined (__STRICT_ANSI__)
#define T_BEGIN \
STMT_START { unsigned int _data_stack_cur_id = t_push(__FUNCTION__);
STMT_START { data_stack_frame_t _data_stack_cur_id = t_push(__FUNCTION__);
#else
#define T_CAT2(a,b) (a ":" #b)
#define T_BEGIN \
STMT_START { unsigned int _data_stack_cur_id = t_push(T_CAT2(__FILE__,__LINE__));
STMT_START { data_stack_frame_t _data_stack_cur_id = t_push(T_CAT2(__FILE__,__LINE__));
#endif
#define T_END \
t_pop_check(&_data_stack_cur_id); } STMT_END
Expand Down
4 changes: 2 additions & 2 deletions src/lib/ioloop.c
Expand Up @@ -478,7 +478,7 @@ static void io_loop_handle_timeouts_real(struct ioloop *ioloop)
{
struct priorityq_item *item;
struct timeval tv, tv_call, prev_ioloop_timeval = ioloop_timeval;
unsigned int t_id;
data_stack_frame_t t_id;

if (gettimeofday(&ioloop_timeval, NULL) < 0)
i_fatal("gettimeofday(): %m");
Expand Down Expand Up @@ -549,7 +549,7 @@ void io_loop_handle_timeouts(struct ioloop *ioloop)
void io_loop_call_io(struct io *io)
{
struct ioloop *ioloop = io->ioloop;
unsigned int t_id;
data_stack_frame_t t_id;

if (io->pending) {
i_assert(ioloop->io_pending_count > 0);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/mempool-datastack.c
Expand Up @@ -40,7 +40,7 @@ struct datastack_pool {
struct pool pool;
int refcount;

unsigned int data_stack_frame;
data_stack_frame_t data_stack_frame;
};

pool_t pool_datastack_create(void)
Expand Down
13 changes: 7 additions & 6 deletions src/lib/test-data-stack.c
Expand Up @@ -93,7 +93,7 @@ static void test_ds_recurse(int depth, int number, size_t size)
char **ps;
char tag[2] = { depth+1, '\0' };
int try_fails = 0;
unsigned int t_id = t_push_named("test_ds_recurse[%i]", depth);
data_stack_frame_t t_id = t_push_named("test_ds_recurse[%i]", depth);
ps = t_buffer_get_type(char *, number);
i_assert(ps != NULL);
t_buffer_alloc_type(char *, number);
Expand Down Expand Up @@ -148,9 +148,10 @@ void test_data_stack(void)
enum fatal_test_state fatal_data_stack(int stage)
{
#ifdef DEBUG
#define NONEXISTENT_STACK_FRAME_ID (data_stack_frame_t)999999999
/* If we abort, then we'll be left with a dangling t_push()
keep a record of our temporary stack id, so we can clean up. */
static unsigned int t_id = 999999999;
static data_stack_frame_t t_id = NONEXISTENT_STACK_FRAME_ID;
static unsigned char *undo_ptr = NULL;
static unsigned char undo_data;
static bool things_are_messed_up = FALSE;
Expand All @@ -164,10 +165,10 @@ enum fatal_test_state fatal_data_stack(int stage)
undo_ptr = NULL;
/* t_pop musn't abort, that would cause recursion */
things_are_messed_up = TRUE;
if (t_id != 999999999 && t_pop() != t_id)
if (t_id != NONEXISTENT_STACK_FRAME_ID && t_pop() != t_id)
return FATAL_TEST_ABORT; /* abort, things are messed up with us */
things_are_messed_up = FALSE;
t_id = 999999999;
t_id = NONEXISTENT_STACK_FRAME_ID;
test_end();
}

Expand Down Expand Up @@ -205,7 +206,7 @@ enum fatal_test_state fatal_data_stack(int stage)
*undo_ptr = '*';
/* t_pop will now fail */
(void)t_pop();
t_id = 999999999; /* We're FUBAR, mustn't pop next entry */
t_id = NONEXISTENT_STACK_FRAME_ID; /* We're FUBAR, mustn't pop next entry */
return FATAL_TEST_FAILURE;
}

Expand All @@ -218,7 +219,7 @@ enum fatal_test_state fatal_data_stack(int stage)
*undo_ptr = '*';
/* t_pop will now fail */
(void)t_pop();
t_id = 999999999; /* We're FUBAR, mustn't pop next entry */
t_id = NONEXISTENT_STACK_FRAME_ID; /* We're FUBAR, mustn't pop next entry */
return FATAL_TEST_FAILURE;
}

Expand Down

0 comments on commit e258887

Please sign in to comment.