-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtupdescstk.c
159 lines (133 loc) · 2.92 KB
/
rtupdescstk.c
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "rtupdescstk.h"
#include "rtupdesc.h"
#include "pllua_xact_cleanup.h"
static void* current_func_cxt = NULL;
RTupDescStack rtds_set_current(void *s){
RTupDescStack prev = current_func_cxt;
current_func_cxt = s;
return prev;
}
RTupDescStack rtds_get_current(void){
return current_func_cxt;
}
static void clean(RTupDescStack S){
void *top = rtds_pop(S);
while (top) {
RTupDesc* rtupdesc = (RTupDesc*)top;
rtupdesc_freedesc(rtupdesc);
rtupdesc->weakNodeStk = NULL;
top = rtds_pop(S);
}
}
void rtds_tryclean(RTupDescStack S){
if (S == NULL) return;
S->ref_count -= 1;
if (S->ref_count != 0) return;
clean(S);
}
void *rtds_pop(RTupDescStack S) {
void *hold;
RTDNodePtr temp;
if (rtds_isempty(S)) {
return NULL;
}
hold = S -> top -> data;
temp = S -> top;
S -> top = S -> top -> next;
if (S->top != NULL)
S -> top->prev = NULL;
pfree(temp);
return hold;
}
static RTDNodePtr rtds_push(RTupDescStack S, void *d) {
RTDNodePtr np;
if (S == NULL) return NULL;
MTOLUA(S->L);
np = (RTDNodePtr) palloc(sizeof(RTDNode));
MTOPG;
np->tail = S;
np -> data = d;
np -> next = S -> top;
np -> prev = NULL;
if (S->top != NULL)
S -> top->prev = np;
S -> top = np;
return np;
}
int rtds_isempty(RTupDescStack S) {
return (S -> top == NULL);
}
static void force_free(void *d){
if (d == NULL) return;
clean(d);
pfree(d);
}
RTupDescStack rtds_initStack(lua_State *L) {
RTupDescStack sp;
L = pllua_getmaster(L);
MTOLUA(L);
sp = (RTupDescStack) palloc(sizeof(RTupDescStackType));
MTOPG;
sp->ref_count = 0;
sp->L = L;
sp->top = NULL;
sp->resptr = register_resource(sp, force_free);
return sp;
}
RTDNodePtr rtds_push_current(void *d)
{
return rtds_push(current_func_cxt,d);
}
RTupDescStack rtds_unref(RTupDescStack S)
{
if (S == NULL) return NULL;
rtds_notinuse(S);
return rtds_free_if_not_used(S);
}
void rtds_remove_node(RTDNodePtr np)
{
RTupDescStack S;
if (np == NULL) return;
S = np->tail;
if (S->top == np){
S->top = np->next;
if (S->top){
S->top->prev = NULL;
}
}else{
if (np->prev)
np->prev->next = np->next;
if (np->next)
np->next->prev = np->prev;
}
pfree(np);
}
int rtds_get_length(RTupDescStack S)
{
int count = 0;
RTDNodePtr node = S->top;
while(node){
count += 1;
node = node->next;
}
return count;
}
void rtds_inuse(RTupDescStack S)
{
S->ref_count += 1;
}
void rtds_notinuse(RTupDescStack S)
{
S->ref_count -= 1;
}
RTupDescStack rtds_free_if_not_used(RTupDescStack S)
{
if (S == NULL) return NULL;
if (S->ref_count == 0){
clean(S);
S->resptr = unregister_resource(S->resptr);
pfree(S);
return NULL;
}
return S;
}