-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.c
72 lines (54 loc) · 1.51 KB
/
global.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
#include <stdarg.h>
#include <string.h>
#include <malloc.h>
#include "global.h"
#include "parse.h"
void **ptr_array = NULL;
int allocated_objs = 0, max_objs = 0;
sigjmp_buf error_jmp;
// Allocator used for the parse code, which is not managed by the student.
void *parse_alloc(size_t sz) {
if (ptr_array == NULL) {
ptr_array = malloc(sizeof(void *) * INITIAL_SIZE);
max_objs = INITIAL_SIZE;
} else if (allocated_objs == max_objs) {
max_objs *= 2;
ptr_array = realloc(ptr_array, sizeof(void *) * max_objs);
}
void *mem = malloc(sz);
if (mem == NULL || ptr_array == NULL) {
error(-1, "%s", "Allocation failed!");
}
ptr_array[allocated_objs++] = mem;
return mem;
}
void error(int pos, const char *fmt, ...) {
printf("%s", curr_string());
if (pos != -1) {
for (int i = 0; i < pos; i++)
printf("-");
printf("^\n");
printf("Parse error: ");
} else {
printf("Error: ");
}
va_list argptr;
va_start(argptr, fmt);
vfprintf(stdout, fmt, argptr);
va_end(argptr);
printf("\n");
longjmp(error_jmp, 1);
}
void parse_free_all() {
for (int i = 0; i < allocated_objs; i++)
free(ptr_array[i]);
free(ptr_array);
ptr_array = NULL;
allocated_objs = max_objs = 0;
}
char *parse_string_dup(const char *str) {
char *str2 = parse_alloc(sizeof(char) * (strlen(str) + 1));
for (size_t i = 0; i < strlen(str) + 1; i++)
str2[i] = str[i];
return str2;
}