-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm.c
121 lines (104 loc) · 2.25 KB
/
m.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
118
119
120
121
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct block_t {
size_t size;
unsigned free : 1;
unsigned last : 1;
unsigned marked : 1;
};
static struct block_t* root_ = 0;
struct block_t* init_gc(size_t size) {
struct block_t* root = malloc(size);
root->size = size - sizeof(struct block_t);
root->free = 1;
root->last = 1;
root->marked = 0;
return root;
}
void init_memory_runtime() {
root_ = init_gc(16384);
}
void destroy_mem_runtime() {
free(root_);
}
struct block_t* next_block(struct block_t* b) {
return (void*)b + b->size + sizeof(struct block_t);
}
struct block_t* block_from_ptr(void* p) {
return p - sizeof(struct block_t);
}
struct block_t* divide_block(struct block_t* b, size_t bnew) {
if (b->size <= bnew + sizeof(struct block_t)) return 0;
struct block_t* n = (void*)b + bnew + sizeof(struct block_t);
n->free = 1;
n->last = b->last;
n->marked = 0;
n->size = b->size - bnew - sizeof(struct block_t);
b->size = bnew;
b->last = 0;
return n;
}
struct block_t* find_create_block(struct block_t* root, size_t sz) {
struct block_t* c = root;
do {
if (c->free == 1 && c->size > sz) {
divide_block(c, sz);
return c;
} else {
if (c->last) return 0;
c = next_block(c);
}
} while(1);
}
void free_block(struct block_t* b) {
b->free = 1;
if (b->last) return;
struct block_t* n = next_block(b);
if (n->free) {
b->size += n->size + sizeof(struct block_t);
b->last = n->last;
}
}
void* bmalloc(size_t sz) {
struct block_t* nfb = find_create_block(root_, sz);
if (0 == nfb)
return 0;
nfb->free = 0;
return (void*)nfb + sizeof(struct block_t);
}
void bfree(void* p) {
if (0 == p) return;
struct block_t* b = block_from_ptr(p);
free_block(b);
}
void print_mem(struct block_t* b) {
if (b == 0) return;
printf("addr %p size %u free %u\n", b, b->size, b->free);
if (b->last) return;
print_mem(next_block(b));
}
void dbg_printm() {
print_mem(root_);
}
void mark(void* p) {
struct block_t* b = block_from_ptr(p);
if (0 == b) return;
b->marked = 1;
}
void sweep() {
if (root_ == 0) {
printf("Init memory subsystem please;\n");
return;
}
struct block_t* bl = root_;
do {
if (!bl->marked)
free_block(bl);
if (bl->last)
return;
else
bl = next_block(bl);
} while (1);
}