-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc_custom._const_part.c
188 lines (164 loc) · 4.72 KB
/
gc_custom._const_part.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* @name gc_custom.c
* @author Mgr. Radomir Skrabal
* This file contains implemetations of Custom Cheneys copying garbage collector for gc_custom.h
*/
#include "gc_custom.h"
#include "gc_cheney_base.h"
#include "garbage_collector.h"
#include <unistd.h>
#include <string.h>
#include "gc_util.h"
#include <syslog.h>
#include "gc_cheney_base.h"
/**
* Initializes the Garbage Collector objects
* @return If everything went well 0, otherwise error code
*/
int gc_custom_init()
{
void *chunk;
chunk = get_memory_primitive(2*SEMISPACE_SIZE);
if(chunk == NULL)
{
return 1;
}
// gc_cheney_base_to_space = init_block_from_chunk(chunk, (2*SEMISPACE_SIZE) - sizeof(block_t));
gc_cheney_base_to_space = (block_t*)((uint64_t)chunk + SEMISPACE_SIZE);
if(gc_cheney_base_to_space == NULL)
{
return 2;
}
// gc_cheney_base_from_space = gc_cheney_base_get_mem((void*)&gc_cheney_base_to_space, SEMISPACE_SIZE - sizeof(block_t));
gc_cheney_base_from_space = chunk;
gc_cheney_base_semispace_middle = gc_cheney_base_to_space;
if(gc_cheney_base_from_space == NULL)
{
return 3;
}
gc_cheney_base_remaining_block = gc_cheney_base_from_space;
gc_cheney_base_roots_count = 0;
return 0;
}
/**
* Cleans up the garbage collectors objext
* @return If everything went well 0, otherwise error code
*/
int gc_custom_cleanup()
{
void *ptr = gc_cheney_base_from_space > gc_cheney_base_to_space ? gc_cheney_base_to_space : gc_cheney_base_from_space;
release_memory_primitive(ptr);
}
/**
* Allocates memory for single (non-array) value
* @par type type number
* @return pointer to allocated memory or NULL
*/
void *gc_custom_malloc(int type)
{
block_t *block;
if(type_table[type].size <= sizeof(uint64_t))
{
block = gc_cheney_base_alloc_block_of_size(0);
}
else
{
block = gc_cheney_base_alloc_block_of_size(type_table[type].size - sizeof(uint64_t));
}
if(block == NULL)
{
return block;
}
block_set_is_array(block, 0);
block_set_type(block, type);
block_set_array_size(block, 0);
return get_data_start(block);
}
/**
* Allocates memory for an array of values
* @par type type number
* @return pointer to allocated memory or NULL
*/
void *gc_custom_malloc_array(int type, size_t size)
{
block_t *block;
block = gc_cheney_base_alloc_block_of_size(type_table[type].size * size);
if(block == NULL)
{
return block;
}
block_set_is_array(block, 1);
block_set_type(block, type);
block_set_array_size(block, size);
return get_data_start(block);
}
/**
* Carries out the "sweep" part of the algorithm
* @return 0 if everything went well, error code otherwise
*/
int gc_custom_collect()
{
return gc_custom_collect_from_roots(gc_cheney_base_roots, gc_cheney_base_roots_count);
}
/**
* Carries out the "sweep" part of the algorithm
* @par roots Array of root elements for garbage collection
* @par size size of a roots arraay
* @return 0 if everything went well, error code otherwise
*/
int gc_custom_collect_from_roots(root_ptr roots[], size_t size)
{
block_t *todo_ptr;
int i;
gc_cheney_base_remaining_to_space = gc_cheney_base_to_space;
todo_ptr = gc_cheney_base_to_space;
for(i = 0; i < size; i++)
{
roots[i].ptr = gc_custom_scan_ptr(roots[i].ptr, TYPE_PTR, roots[i].is_array);
}
while(todo_ptr < gc_cheney_base_remaining_to_space)
{
gc_custom_walk_block(todo_ptr);
todo_ptr = next_block(todo_ptr);
}
gc_cheney_base_swich_semispaces();
return 0;
}
/**
* Scans the (copied) block of memory and copies the references it points to to the to_space
* @par block memory block to be scanned
* @return if everything went well 0 otherwise error code
*/
int gc_custom_walk_block(block_t *block)
{
if(block_is_array(block))
{
return gc_custom_walk_array(block);
}
switch(block_get_type(block))
{
case TYPE_UNDEFINED:
case TYPE_INT:
case TYPE_DOUBLE:
return 0;
default:
return gc_custom_walk_struct(block);
}
}
/**
* Scans the block of type MEM_TYPE_STRUCT
* @par block block of memory of type MEM_TYPE_STRUCT
* @return 0 if everything went well, error code otherwise
*/
int gc_custom_walk_struct(block_t *block)
{
gc_custom_scan_struct(get_data_start(block), block_get_type(block));
}
/**
* Returns the remaining space in bytes that collector has available
* @return space in bytes or -1 if collector is limmited only by system
*/
int64_t gc_custom_remaining_space()
{
return gc_cheney_base_remaining_space();
}