Compare commits
3 commits
bc49570d12
...
840b19adf2
Author | SHA1 | Date | |
---|---|---|---|
840b19adf2 | |||
d3722aac83 | |||
c6ff6ad31d |
2 changed files with 28 additions and 22 deletions
|
@ -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;
|
||||||
|
@ -29,8 +28,8 @@ int TracingHeap_Create(TracingHeap* self, allocator_t* allocator)
|
||||||
|
|
||||||
static void _TracingHeap_ForceDestroyObject(TracingHeap* self, TracingObject* object)
|
static void _TracingHeap_ForceDestroyObject(TracingHeap* self, TracingObject* object)
|
||||||
{
|
{
|
||||||
self->config.destructor(self->config.destructor_context, object->data);
|
|
||||||
size_t allocated_size = self->config.size_query(self->config.size_query_context, object->data);
|
size_t allocated_size = self->config.size_query(self->config.size_query_context, object->data);
|
||||||
|
self->config.destructor(self->config.destructor_context, object->data);
|
||||||
Allocator_Free(self->allocator, object, allocated_size);
|
Allocator_Free(self->allocator, object, allocated_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,10 +158,12 @@ int TracingHeap_TraceNext(TracingHeap* self)
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t TracingHeap_TraceNextN(TracingHeap* self, size_t n)
|
size_t TracingHeap_TraceNextN(TracingHeap* self, size_t n, int* error_code)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < n; i++) {
|
for (size_t i = 0; i < n; i++) {
|
||||||
if (TracingHeap_TraceNext(self)) {
|
int trace_code = TracingHeap_TraceNext(self);
|
||||||
|
if (trace_code != EXIT_SUCCESS) {
|
||||||
|
if (error_code != NULL) *error_code = trace_code;
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -166,13 +173,13 @@ size_t TracingHeap_TraceNextN(TracingHeap* self, size_t n)
|
||||||
|
|
||||||
static int _TracingHeap_DestroyObject(TracingHeap* self, TracingObject* unreachable_object)
|
static int _TracingHeap_DestroyObject(TracingHeap* self, TracingObject* unreachable_object)
|
||||||
{
|
{
|
||||||
|
size_t allocated_size = self->config.size_query(self->config.size_query_context, unreachable_object->data);
|
||||||
|
|
||||||
int destructor_code = self->config.destructor(self->config.destructor_context, unreachable_object->data);
|
int destructor_code = self->config.destructor(self->config.destructor_context, unreachable_object->data);
|
||||||
if (destructor_code) {
|
if (destructor_code) {
|
||||||
return destructor_code;
|
return destructor_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t allocated_size = self->config.size_query(self->config.size_query_context, unreachable_object->data);
|
|
||||||
|
|
||||||
Allocator_Free(self->allocator, unreachable_object, allocated_size);
|
Allocator_Free(self->allocator, unreachable_object, allocated_size);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
@ -199,6 +206,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 +224,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 +258,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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
@ -51,15 +49,17 @@ typedef struct TracingHeap_s {
|
||||||
allocator_t* allocator;
|
allocator_t* allocator;
|
||||||
} TracingHeap;
|
} TracingHeap;
|
||||||
|
|
||||||
int TracingHeap_Create(TracingHeap* self, allocator_t* allocator);
|
int TracingHeap_Create(TracingHeap* self, allocator_t* allocator);
|
||||||
void TracingHeap_Destroy(TracingHeap* self);
|
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, int* error_code);
|
||||||
int TracingHeap_AddTracingRoot(TracingHeap* self, void* data);
|
int TracingHeap_AddTracingRoot(TracingHeap* self, void* data);
|
||||||
|
|
||||||
#endif // header
|
#endif // header
|
||||||
|
|
Loading…
Reference in a new issue