-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.c
60 lines (53 loc) · 1.52 KB
/
runtime.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
#include "ast.h"
#include <stdlib.h>
/*
* It might be interesting to try implement green threads, as per this article
* and code sample:
*
* https://c9x.me/articles/gthreads/code0.html
* https://github.com/mpu/gthreads/tree/code0
*/
#define WORD_SIZE (sizeof(void*))
static struct SizeAlloc {
size_t size;
int count;
} sizes_allocated[1024];
void end_of_program()
{
int len = 1;
for (; len < 1024 && sizes_allocated[len].size > 0; len++);
fprintf(stdout, "Allocation Distribution:\n");
fprintf(stdout, "+--------+---------+\n");
fprintf(stdout, "| size | count |\n");
fprintf(stdout, "+--------+---------+\n");
for (int i = 0; i < len; i++) {
fprintf(stdout, "| %6lu | %7u |\n", sizes_allocated[i].size, sizes_allocated[i].count);
}
fprintf(stdout, "+--------+---------+\n");
}
// for the time being, just call malloc
void* ml_gc_alloc(size_t size)
{
static int first_time = 1;
if (first_time) {
first_time = 0;
//atexit(end_of_program);
}
if (size == 0) {
sizes_allocated[0].count++;
} else {
for (int i = 1; i < 1024; i++) {
if (sizes_allocated[i].size == 0) {
// Need to go back and add us in
sizes_allocated[i].size = size;
sizes_allocated[i].count = 1;
break;
}
if (sizes_allocated[i].size == size) {
sizes_allocated[i].count++;
break;
}
}
}
return malloc(size);
}