source: trunk/util/xc_stack.c @ 1385

Last change on this file since 1385 was 1385, checked in by moo, 12 months ago

readonly protection for copied array

  • 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{
[1385]30    assert(stack != NULL);
31    assert(stack->size > 0);
[1]32    return stack->data[--stack->cnt];
33}
34
35void* xc_stack_top(S stack)
36{
[1385]37    assert(stack != NULL);
38    assert(stack->cnt > 0);
[1]39    return stack->data[stack->cnt-1];
40}
41
42void* xc_stack_get(S stack, int n)
43{
[1385]44    assert(stack != NULL);
45    assert(stack->cnt > 0);
[1]46    return stack->data[n];
47}
48
[307]49int xc_stack_count(S stack)
[1]50{
51    assert(stack != NULL);
52    return stack->cnt;
53}
54
55void xc_stack_reverse(S stack)
56{
[11]57    int i, j;
[1]58    void *tmp;
59
60    assert(stack != NULL);
61    for (i = 0, j = stack->cnt - 1; i < j; i ++, j --) {
62        tmp = stack->data[i];
63        stack->data[i] = stack->data[j];
64        stack->data[j] = tmp;
65    }
66}
Note: See TracBrowser for help on using the repository browser.