Browse Source

Turmit stack enhancement & bugfixes

Now stack reallocate blocs of stack. If no new block needed just set new
stack_sz
Yann Weber 6 years ago
parent
commit
a43466c4ef
2 changed files with 24 additions and 10 deletions
  1. 22
    9
      libs/turmit.c
  2. 2
    1
      libs/turmit.h

+ 22
- 9
libs/turmit.c View File

@@ -3,18 +3,26 @@
3 3
 TURMIT_OP(mem_sz)
4 4
 {
5 5
 	turmit_int *new;
6
-	turmit_int new_sz, old_sz;
6
+	turmit_int new_sz, old_sz, new_blc, old_blc;
7 7
 	
8 8
 	old_sz = turmit->stack_sz;
9
+	old_blc = (old_sz / TURMIT_STACK_GROW) + 1;
10
+
9 11
 	new_sz = SPOP(turmit);
10 12
 	new_sz = new_sz<2?2:new_sz;
13
+	new_sz %= TURMIT_STACK_MAX;
14
+	new_blc = (new_sz / TURMIT_STACK_GROW) + 1;
11 15
 
12
-	new = realloc(turmit->stack, sizeof(turmit_int) * new_sz);
13
-	if(!new) {
14
-		perror("Unable to reallocate memory for stack");
15
-		return; 
16
+	if(new_blc != old_blc)
17
+	{
18
+		new = realloc(turmit->stack,
19
+			sizeof(turmit_int) * (new_blc * TURMIT_STACK_GROW));
20
+		if(!new) {
21
+			perror("Unable to reallocate memory for stack");
22
+			return; 
23
+		}
24
+		turmit->stack = new;
16 25
 	}
17
-	turmit->stack = new;
18 26
 	turmit->stack_sz = new_sz;
19 27
 	turmit->stack_cur %= new_sz;
20 28
 	if(old_sz < new_sz)
@@ -170,7 +178,8 @@ turmit_t *turmit_init(turmit_t *turmit, ssize_t stack_sz, ssize_t int_max,
170 178
 	turmit->stack_cur = turmit->stack_sz - 1;
171 179
 	turmit->err = 0;
172 180
 	turmit->err_str = NULL;
173
-	turmit->stack = malloc(sizeof(turmit_int) * turmit->stack_sz);
181
+	turmit->stack = malloc(sizeof(turmit_int) * 
182
+		((turmit->stack_sz / TURMIT_STACK_GROW)+1) * TURMIT_STACK_GROW);
174 183
 
175 184
 	if(!turmit->stack)
176 185
 	{
@@ -189,6 +198,8 @@ turmit_t *turmit_init(turmit_t *turmit, ssize_t stack_sz, ssize_t int_max,
189 198
 turmit_t *turmit_copy(turmit_t *turmit)
190 199
 {
191 200
 	turmit_t *res;
201
+	turmit_int stack_blck;
202
+
192 203
 	res = malloc(sizeof(turmit_t));
193 204
 	if(!res)
194 205
 	{
@@ -202,9 +213,11 @@ turmit_t *turmit_copy(turmit_t *turmit)
202 213
 	}
203 214
 	if(turmit->stack)
204 215
 	{
205
-		res->stack = malloc(sizeof(turmit_int) * res->stack_sz);
216
+		stack_blck = (res->stack_sz / TURMIT_STACK_GROW) + 1;
217
+		res->stack = malloc(
218
+			sizeof(turmit_int) * stack_blck * TURMIT_STACK_GROW);
206 219
 		memcpy(res->stack, turmit->stack,
207
-			sizeof(turmit_int) * res->stack_sz);
220
+			sizeof(turmit_int) * stack_blck * TURMIT_STACK_GROW);
208 221
 	}
209 222
 	if(turmit->op_expr)
210 223
 	{

+ 2
- 1
libs/turmit.h View File

@@ -26,6 +26,8 @@ typedef void (*turmit_op_f)(turmit_t*);
26 26
 	(t->op_end=1):\
27 27
 	(t->op_cur += (v>0?v-1:0) ))
28 28
 
29
+#define TURMIT_STACK_GROW	0xFF
30
+#define TURMIT_STACK_MAX	0xFFF
29 31
 #define TURMIT_EXPR_MAX_SZ	0xFFFFFFFF
30 32
 
31 33
 #define TURMIT_LAZY		0
@@ -45,7 +47,6 @@ typedef void (*turmit_op_f)(turmit_t*);
45 47
 #define TURMIT_OPSYM(NAME) {#NAME, NULL, turmit_op_##NAME}
46 48
 #define TURMIT_OPSYMALIAS(NAME, alias) {#NAME, alias, turmit_op_##NAME}
47 49
 
48
-
49 50
 // OPSYMALIAS has to be first !
50 51
 #define TURMIT_OP_LST {\
51 52
 	TURMIT_OPSYMALIAS(add, "+"),\

Loading…
Cancel
Save