forked from ArweaveTeam/jiffy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtermstack.c
87 lines (70 loc) · 2.03 KB
/
termstack.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
// This file is part of Jiffy released under the MIT license.
// See the LICENSE file for more information.
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "jiffy.h"
#include "termstack.h"
ERL_NIF_TERM
termstack_save(ErlNifEnv* env, TermStack* stack)
{
return enif_make_tuple_from_array(env, stack->elements, stack->top);
}
int
termstack_restore(ErlNifEnv* env, ERL_NIF_TERM from, TermStack* stack)
{
const ERL_NIF_TERM* elements;
int arity;
if(enif_get_tuple(env, from, &arity, &elements)) {
stack->top = arity;
if(arity <= SMALL_TERMSTACK_SIZE) {
stack->elements = &stack->__default_elements[0];
stack->size = SMALL_TERMSTACK_SIZE;
} else {
stack->size = arity * 2;
stack->elements = enif_alloc(stack->size * sizeof(ERL_NIF_TERM));
if(!stack->elements) {
return 0;
}
}
memcpy(stack->elements, elements, arity * sizeof(ERL_NIF_TERM));
return 1;
}
return 0;
}
void
termstack_destroy(TermStack* stack)
{
if(stack->elements != &stack->__default_elements[0]) {
enif_free(stack->elements);
}
}
inline void
termstack_push(TermStack* stack, ERL_NIF_TERM term)
{
if(stack->top == stack->size) {
size_t new_size = stack->size * 2;
size_t num_bytes = new_size * sizeof(ERL_NIF_TERM);
if (stack->elements == &stack->__default_elements[0]) {
ERL_NIF_TERM* elems = enif_alloc(num_bytes);
memcpy(elems, stack->elements, num_bytes);
stack->elements = elems;
} else {
stack->elements = enif_realloc(stack->elements, num_bytes);
}
stack->size = new_size;
}
assert(stack->top < stack->size);
stack->elements[stack->top++] = term;
}
inline ERL_NIF_TERM
termstack_pop(TermStack* stack)
{
assert(stack->top > 0 && stack->top <= stack->size);
return stack->elements[--stack->top];
}
inline int
termstack_is_empty(TermStack* stack)
{
return stack->top == 0;
}