-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspine_stack.c
117 lines (96 loc) · 3 KB
/
spine_stack.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
/*
Copyright (C) 2010-2011, Bruce Ediger
This file is part of acl.
acl is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
acl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with acl; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* $Id: spine_stack.c,v 1.7 2011/06/12 18:22:01 bediger Exp $ */
#include <stdlib.h>
#include <stdio.h>
#include <spine_stack.h>
#include <node.h>
/* Only a single spine stack instance gets created
* during any run of the interpreter. That single
* instance gets reused to evaluate the second and
* later expressions evaluated. */
struct spine_stack *old_spine_stack = NULL;
static int spine_stack_resizes = 0;
extern int interpreter_interrupted;
struct spine_stack *
new_spine_stack(int sz)
{
struct spine_stack *r;
if (old_spine_stack)
{
r = old_spine_stack;
r->top = 0;
/* Don't NULL out old_spine_stack: if someone control-c's
* the interpreter during a reduction, it might miss putting
* the spine stack back in place. */
} else {
r = malloc(sizeof(*r));
r->stack = malloc(sz * sizeof(struct spine_stack_element));
r->size = sz;
r->maxdepth = 0;
r->top = 0;
}
return r;
}
void
delete_spine_stack(struct spine_stack *ss)
{
old_spine_stack = ss;
}
/*
* Push a struct node pointer onto the spine stack,
* re-sizing the stack if necessary, since the "stack"
* part of struct spine_stack is an array.
*
* The "re-sizing" part actually doubles the usuable
* stack each time it gets executed. May cause trouble
* for reduction of pathological CL expressions.
*
* The "mark" argument allows us to push the RHS of
* an application on the stack, and use the corresponding
* depth element of struct spine_stack_element as the
* "depth" of the RHS of the graph.
*/
void
pushnode(struct spine_stack *ss, struct node *n, int mark)
{
ss->stack[ss->top].node = n;
ss->stack[ss->top].depth = (mark? mark: ss->stack[ss->top-1].depth + 1);
++ss->top;
if (ss->top > ss->maxdepth) ++ss->maxdepth;
if (ss->top >= ss->size)
{
/* resize the allocation pointed to by stack */
struct spine_stack_element *old_stack = ss->stack;
size_t new_size = ss->size * 2; /* XXX !!! */
++spine_stack_resizes;
ss->stack = realloc(old_stack, sizeof(struct spine_stack_element)*new_size);
ss->size = new_size;
}
}
void
free_all_spine_stacks(void)
{
if (old_spine_stack)
{
free(old_spine_stack->stack);
old_spine_stack->stack = NULL;
old_spine_stack->top = 0;
old_spine_stack->size = 0;
free(old_spine_stack);
old_spine_stack = NULL;
}
}