Implemented IsTraceFinished, removed global linked list

This commit is contained in:
VegOwOtenks 2024-10-16 17:23:37 +02:00
parent c6ff6ad31d
commit d3722aac83
2 changed files with 16 additions and 13 deletions

View file

@ -16,7 +16,6 @@ int TracingHeap_Create(TracingHeap* self, allocator_t* allocator)
self->allocator = allocator; self->allocator = allocator;
self->objects = NULL;
self->black_objects = NULL; self->black_objects = NULL;
self->white_objects = NULL; self->white_objects = NULL;
self->grey_objects = NULL; self->grey_objects = NULL;
@ -40,7 +39,13 @@ void TracingHeap_Destroy(TracingHeap* self)
return; return;
} }
for (TracingObject* object = self->objects; object != NULL; object = object->global_next) { for (TracingObject* object = self->white_objects; object != NULL; object = object->color_next) {
_TracingHeap_ForceDestroyObject(self, object);
}
for (TracingObject* object = self->grey_objects; object != NULL; object = object->color_next) {
_TracingHeap_ForceDestroyObject(self, object);
}
for (TracingObject* object = self->black_objects; object != NULL; object = object->color_next) {
_TracingHeap_ForceDestroyObject(self, object); _TracingHeap_ForceDestroyObject(self, object);
} }
@ -199,6 +204,11 @@ int TracingHeap_BeginTrace(TracingHeap* self)
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
bool TracingHeap_IsTraceFinished(TracingHeap* self)
{
return self->grey_objects == NULL;
}
int TracingHeap_EndTrace(TracingHeap* self) int TracingHeap_EndTrace(TracingHeap* self)
{ {
if (self == NULL) { if (self == NULL) {
@ -212,11 +222,11 @@ int TracingHeap_EndTrace(TracingHeap* self)
TracingObject* unreachable_object = *unreachable_list; TracingObject* unreachable_object = *unreachable_list;
while (unreachable_object != NULL) { while (unreachable_object != NULL) {
TracingObject* next_unreachable = unreachable_object->global_next; TracingObject* next_unreachable = unreachable_object->color_next;
if (_TracingHeap_DestroyObject(self, unreachable_object)) { if (_TracingHeap_DestroyObject(self, unreachable_object)) {
*unreachable_list = unreachable_object; *unreachable_list = unreachable_object;
unreachable_object->global_prev = NULL; unreachable_object->color_prev = NULL;
return ECANCELED; return ECANCELED;
} }
@ -246,11 +256,5 @@ void* TracingHeap_Allocate(TracingHeap* self, size_t bytes)
*reachable_list_start = object; *reachable_list_start = object;
object->color_prev = NULL; object->color_prev = NULL;
object->global_next = self->objects; // link before global object list
object->global_prev = NULL; // no previous object
self->objects->global_prev = object; // link first object via previous link
self->objects = object; // insert into linked list
return object->data; return object->data;
} }

View file

@ -4,6 +4,7 @@
#include "../allocator-interface/allocator-interface.h" #include "../allocator-interface/allocator-interface.h"
#include <stdalign.h> #include <stdalign.h>
#include <stdbool.h>
enum TracingColor { enum TracingColor {
TRACINGCOLOR_BLACK, TRACINGCOLOR_BLACK,
@ -12,8 +13,6 @@ enum TracingColor {
}; };
typedef struct TracingObject { typedef struct TracingObject {
struct TracingObject* global_next;
struct TracingObject* global_prev;
struct TracingObject* color_next; struct TracingObject* color_next;
struct TracingObject* color_prev; struct TracingObject* color_prev;
enum TracingColor color; enum TracingColor color;
@ -39,7 +38,6 @@ typedef struct TracingHeapConfig_s {
} TracingHeapConfig; } TracingHeapConfig;
typedef struct TracingHeap_s { typedef struct TracingHeap_s {
TracingObject* objects;
TracingObject* white_objects; TracingObject* white_objects;
TracingObject* grey_objects; TracingObject* grey_objects;
TracingObject* black_objects; TracingObject* black_objects;
@ -57,6 +55,7 @@ void TracingHeap_Destroy(TracingHeap* self);
void* TracingHeap_Allocate(TracingHeap* self, size_t bytes); void* TracingHeap_Allocate(TracingHeap* self, size_t bytes);
int TracingHeap_BeginTrace(TracingHeap* self); int TracingHeap_BeginTrace(TracingHeap* self);
bool TracingHeap_IsTraceFinished(TracingHeap* self);
int TracingHeap_EndTrace(TracingHeap* self); int TracingHeap_EndTrace(TracingHeap* self);
int TracingHeap_TraceNext(TracingHeap* self); int TracingHeap_TraceNext(TracingHeap* self);
size_t TracingHeap_TraceNextN(TracingHeap* self, size_t n); size_t TracingHeap_TraceNextN(TracingHeap* self, size_t n);