source: trunk/util/xc_stack.c @ 990

Last change on this file since 990 was 990, checked in by moo, 2 years ago

split trace

  • Property svn:eol-style set to native
File size: 1.1 KB
RevLine 
[1]1#include <stdlib.h>
[990]2#include "xc_trace.h"
[982]3#include "xc_stack.h"
[990]4
[1]5typedef xc_stack_t* S;
6
[307]7void xc_stack_init_ex(S stack, int initsize)
[1]8{
9    stack->cnt = 0;
[307]10    stack->size = initsize;
[1]11    stack->data = malloc(sizeof(void *) * stack->size);
12}
13
14void xc_stack_destroy(S stack)
15{
16    free(stack->data);
17}
18
19void xc_stack_push(S stack, void *item)
20{
21    if (stack->cnt == stack->size) {
22        stack->size <<= 1;
23        stack->data = realloc(stack->data, sizeof(void *) * stack->size);
24    }
25    stack->data[stack->cnt++] = item;
26}
27
28void* xc_stack_pop(S stack)
29{
30    assert(stack != NULL && stack->size > 0);
31    return stack->data[--stack->cnt];
32}
33
34void* xc_stack_top(S stack)
35{
36    assert(stack != NULL && stack->cnt > 0);
37    return stack->data[stack->cnt-1];
38}
39
40void* xc_stack_get(S stack, int n)
41{
42    assert(stack != NULL && stack->cnt > 0);
43    return stack->data[n];
44}
45
[307]46int xc_stack_count(S stack)
[1]47{
48    assert(stack != NULL);
49    return stack->cnt;
50}
51
52void xc_stack_reverse(S stack)
53{
[11]54    int i, j;
[1]55    void *tmp;
56
57    assert(stack != NULL);
58    for (i = 0, j = stack->cnt - 1; i < j; i ++, j --) {
59        tmp = stack->data[i];
60        stack->data[i] = stack->data[j];
61        stack->data[j] = tmp;
62    }
63}
Note: See TracBrowser for help on using the repository browser.