Tweaked sweeping, test with detached pointers
This commit is contained in:
parent
6cd6edb594
commit
3860d0ec4f
3 changed files with 42 additions and 33 deletions
|
|
@ -192,49 +192,51 @@ namespace GC {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not optimal for now, it doesn't have to loop over all objects
|
/**
|
||||||
// but mark needs some refinements before this can be optimised
|
* Sweeps the heap, unmarks the marked chunks for the next cycle,
|
||||||
|
* adds the unmarked nodes to the vector of freed chunks; to be freed.
|
||||||
|
*
|
||||||
|
* @param *heap The heap to oporate on.
|
||||||
|
*/
|
||||||
void Heap::sweep(Heap *heap) {
|
void Heap::sweep(Heap *heap) {
|
||||||
for (auto it = heap->m_allocated_chunks.begin(); it != heap->m_allocated_chunks.end();) {
|
for (auto it = heap->m_allocated_chunks.begin(); it != heap->m_allocated_chunks.end();) {
|
||||||
auto chunk = *it;
|
auto chunk = *it;
|
||||||
if (!chunk->marked) {
|
|
||||||
|
// Unmark the marked chunks for the next iteration.
|
||||||
|
if (chunk->marked) {
|
||||||
|
chunk->marked = false;
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Add the unmarked chunks to freed chunks and remove from
|
||||||
|
// the list of allocated chunks
|
||||||
heap->m_freed_chunks.push_back(chunk);
|
heap->m_freed_chunks.push_back(chunk);
|
||||||
it = heap->m_allocated_chunks.erase(it);
|
it = heap->m_allocated_chunks.erase(it);
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: return the worklist filtered on mark = true
|
|
||||||
// This assumes that there are no chains of pointers, will be fixed later on
|
// This assumes that there are no chains of pointers, will be fixed later on
|
||||||
void Heap::mark(uintptr_t *start, const uintptr_t *end, vector<Chunk*> worklist) {
|
void Heap::mark(uintptr_t *start, const uintptr_t *end, vector<Chunk*> worklist) {
|
||||||
for (; start > end; start--) { // to find adresses thats in the worklist
|
for (; start > end; start--) { // to find adresses thats in the worklist
|
||||||
if (*start % 8 == 0) { // all pointers must be aligned as double words
|
if (*start % 8 == 0) { // all pointers must be aligned as double words
|
||||||
for (auto it = worklist.begin(); it != worklist.end();) {
|
for (auto it = worklist.begin(); it != worklist.end();) {
|
||||||
//for (size_t i = 0; i < worklist.size(); i++) { // fix this
|
|
||||||
//auto chunk = worklist.at(i);
|
|
||||||
auto chunk = *it;
|
auto chunk = *it;
|
||||||
uintptr_t c_start = reinterpret_cast<uintptr_t>(chunk->start);
|
uintptr_t c_start = reinterpret_cast<uintptr_t>(chunk->start);
|
||||||
uintptr_t c_end = reinterpret_cast<uintptr_t>(chunk->start + chunk->size);
|
uintptr_t c_end = reinterpret_cast<uintptr_t>(chunk->start + chunk->size);
|
||||||
if (c_start <= *start && *start < c_end) {
|
// Check if the stack pointer aligns with the chunk
|
||||||
uintptr_t c_start = reinterpret_cast<uintptr_t>(chunk->start);
|
if ((c_start <= *start && *start < c_end) && chunk != nullptr) {
|
||||||
if (!chunk->marked) {
|
cout << "Chunk start:\t" << c_start << endl;
|
||||||
|
cout << "Chunk end:\t" << c_end << endl;
|
||||||
|
if (!chunk->marked) {
|
||||||
chunk->marked = true;
|
chunk->marked = true;
|
||||||
//worklist.erase(worklist.begin() + i);
|
|
||||||
it = worklist.erase(it);
|
it = worklist.erase(it);
|
||||||
//auto new_stack_start = reinterpret_cast<uintptr_t *>(start);
|
|
||||||
//mark(new_stack_start, end, worklist); //
|
|
||||||
//return;
|
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
++it;
|
++it;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
++it;
|
++it;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,36 +9,42 @@ GC::Heap *gc = GC::Heap::the();
|
||||||
*dst = local;
|
*dst = local;
|
||||||
} */
|
} */
|
||||||
|
|
||||||
|
void create_array(size_t size) {
|
||||||
|
int *arr = static_cast<int *>(gc->alloc(sizeof(int) * size));
|
||||||
|
}
|
||||||
|
|
||||||
|
void detach_pointer(long **ptr) {
|
||||||
|
long dummy = 10; // dummy value
|
||||||
|
long *dummy_ptr = &dummy;
|
||||||
|
*ptr = dummy_ptr;
|
||||||
|
std::cout << "Dummy pointer: \t" << dummy_ptr << std::endl;
|
||||||
|
std::cout << "Detach pointer result:\t" << ptr << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
void init() {
|
void init() {
|
||||||
|
|
||||||
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
|
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
|
||||||
//auto stack_end = stack_start - 40;
|
|
||||||
std::cout << "Stack start from init:\t" << stack_start << std::endl;
|
std::cout << "Stack start from init:\t" << stack_start << std::endl;
|
||||||
//std::cout << "Imaginary stack end:\t" << stack_end << std::endl;
|
|
||||||
int *arr = static_cast<int *>(gc->alloc(sizeof(int) * 100));
|
int *arr = static_cast<int *>(gc->alloc(sizeof(int) * 100));
|
||||||
|
create_array(100);
|
||||||
|
//arr = create_array(100);
|
||||||
//std::cout << "Arr_ptr" << std::hex << arr << "\n\n\n" << std::endl;
|
//std::cout << "Arr_ptr" << std::hex << arr << "\n\n\n" << std::endl;
|
||||||
for (int i = 0; i < (sizeof(int) * 100); i++) {
|
for (int i = 0; i < (sizeof(int) * 100); i++) {
|
||||||
arr[i] = i;
|
arr[i] = i;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < (sizeof(int) * 20); i++) {
|
|
||||||
gc->alloc(sizeof(int));
|
|
||||||
}
|
|
||||||
std::cout << "Pointer for arr:\t" << &arr << std::endl;
|
std::cout << "Pointer for arr:\t" << &arr << std::endl;
|
||||||
long a = 20;
|
|
||||||
long *l = static_cast<long *>(gc->alloc(sizeof(long)));
|
long *l = static_cast<long *>(gc->alloc(sizeof(long)));
|
||||||
std::cout << "Pointer for l:\t\t" << &l << std::endl;
|
std::cout << "l points to:\t\t" << l << std::endl;
|
||||||
int *i = static_cast<int *>(gc->alloc(sizeof(int)));
|
detach_pointer(&l);
|
||||||
std::cout << "Pointer for i:\t\t" << &i << std::endl;
|
std::cout << "l points to:\t\t" << l << std::endl;
|
||||||
//l = &a;
|
// l still gets marked, which is not supposed to happen
|
||||||
//*l = 20;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
|
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
|
||||||
std::cout << "Stack start from main:\t" << stack_start << std::endl;
|
std::cout << "Stack start from main:\t" << stack_start << std::endl;
|
||||||
init();
|
init();
|
||||||
gc->collect(MARK | SWEEP); // some bug in free (vector out of range)
|
gc->collect(MARK); // some bug in free (vector out of range)
|
||||||
gc->print_contents();
|
gc->print_contents();
|
||||||
//delete gc;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -8,6 +8,7 @@ Goal for next week (24/2):
|
||||||
## GC TODO:
|
## GC TODO:
|
||||||
- Merge to main branch
|
- Merge to main branch
|
||||||
- Fix singleton references
|
- Fix singleton references
|
||||||
|
- Think about how we want to determine if some object is a pointer or not, probably will have to discuss that with Samuel. Since it is not ideal to determine in the GC if an object is a pointer or not. It should preferably be done in a previous stage.
|
||||||
|
|
||||||
## Tests TODO
|
## Tests TODO
|
||||||
### Library linking
|
### Library linking
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue