-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgc.c
116 lines (105 loc) · 2.6 KB
/
gc.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
/*
module : gc.c
version : 1.3
date : 02/10/25
*/
#include <string.h>
#include "malloc.h"
/*
* Set to allow playing tutorial.joy with minimal library support.
*/
#ifndef MAXSTRTAB
#define MAXSTRTAB 500
#endif
#ifdef NPROTO
void point();
#else
void point(int diag, char *mes);
#endif
void mark_string();
static int strindex;
static char *strtable[MAXSTRTAB + 1];
/*
* Mark all strings that have been allocated. Scan the table and free strings
* that have not been marked. Also compact the table. Unmark strings that have
* been marked, unless they have been marked as permanent. Register the new
* size of the table.
* The table can also be used to report about strings that are still allocated.
* As these are strings, they can easily be printed.
*/
static void my_gc()
{
int j;
char *str;
strindex = 0;
mark_string();
while ((str = strtable[strindex]) != 0) {
if (*str == 0) {
my_free(str);
for (j = strindex; strtable[j]; j++)
strtable[j] = strtable[j + 1];
} else {
if (*str == 1)
*str = 0;
strindex++;
}
}
}
/*
* In case my_malloc returns 0 or in case the string table is full, garbage
* collect strings.
*
* All roots must be searched (programme, s, dump) and whenever a string is
* found, it must be marked. Strings in the symbol table are marked as
* persistent. This is needed only once.
*
* Strings that are not marked are freed.
*
* Strings can be marked by allocating an extra byte in front and use that for
* marking; if strings are also stored in a table, then the table can be used
* during scanning.
*
* So, instead of producing a fatal error, garbage collect strings.
*/
void *GC_malloc_atomic(size)
size_t size;
{
char *ptr;
/*
* Allocate an extra byte, used for marking.
*/
if ((ptr = my_malloc(size + 1)) == 0) {
my_gc();
if ((ptr = my_malloc(size + 1)) == 0)
point('F', "too many characters in strings");
}
/*
* Tabulate the return value from my_malloc.
*/
if (strindex >= MAXSTRTAB) {
my_gc();
if (strindex >= MAXSTRTAB)
point('F', "too many strings");
}
strtable[strindex++] = ptr; /* remember return from my_malloc */
*ptr++ = 1; /* allocate from index 0 and mark */
return (void *)ptr; /* return user area */
}
char *GC_strdup(str)
char *str;
{
char *ptr;
size_t size;
size = strlen(str) + 1;
ptr = GC_malloc_atomic(size);
return strcpy(ptr, str);
}
/*
* Explicit free is not possible, because the pointer is still present in the
* string table.
*/
void GC_free(ptr)
void *ptr;
{
((char *)ptr)[-1] = 0; /* set to unmarked */
}