File: callstack.c

package info (click to toggle)
python-yappi 1.6.10-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,612 kB
  • sloc: python: 4,081; ansic: 2,500; makefile: 27
file content (102 lines) | stat: -rw-r--r-- 1,541 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "callstack.h"
#include "debug.h"
#include "mem.h"
#include "timing.h"
#include "config.h"

int
slen(_cstack *cs)
{
    return 1 + cs->head;
}

_cstack *
screate(int size)
{
    int i;
    _cstack *cs;

    cs = (_cstack *)ymalloc(sizeof(_cstack));
    if (!cs)
        return NULL;
    cs->_items = ymalloc(size * sizeof(_cstackitem));
    if (cs->_items == NULL) {
        yfree(cs);
        return NULL;
    }

    for(i=0; i<size; i++) {
        cs->_items[i].ckey = 0;
        cs->_items[i].t0 = 0;
    }

    cs->size = size;
    cs->head = -1;
    return cs;
}

static int
_sgrow(_cstack * cs)
{
    int i;
    _cstack *dummy;

    dummy = screate(cs->size*2);
    if(!dummy)
        return 0;

    for(i=0; i<cs->size; i++) {
        dummy->_items[i].ckey = cs->_items[i].ckey;
        dummy->_items[i].t0 = cs->_items[i].t0;
    }
    yfree(cs->_items);
    cs->_items = dummy->_items;
    cs->size = dummy->size;
    yfree(dummy);
    return 1;
}

void
sdestroy(_cstack * cs)
{
    yfree(cs->_items);
    yfree(cs);
}


_cstackitem *
spush(_cstack *cs, void *ckey)
{
    _cstackitem *ci;

    if (cs->head >= cs->size-1) {
        if (!_sgrow(cs))
            return NULL;
    }

    ci = &cs->_items[++cs->head];
    ci->ckey = ckey;

    return ci;
}

_cstackitem *
spop(_cstack * cs)
{
    _cstackitem *ci;

    if (cs->head < 0)
        return NULL;

    ci = &cs->_items[cs->head--];
    return ci;
}

_cstackitem *
shead(_cstack * cs)
{
    if (cs->head < 0)
        return NULL;

    return &cs->_items[cs->head];
}