Compare commits
No commits in common. "840b19adf2cb7a2f3a99c34e82842d966629ffad" and "bc49570d12eea41fe7e9720a46cc6690673ccdf7" have entirely different histories.
840b19adf2
...
bc49570d12
2 changed files with 22 additions and 28 deletions
|
@ -16,6 +16,7 @@ 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;
|
||||||
|
@ -28,8 +29,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)
|
||||||
{
|
{
|
||||||
size_t allocated_size = self->config.size_query(self->config.size_query_context, object->data);
|
|
||||||
self->config.destructor(self->config.destructor_context, object->data);
|
self->config.destructor(self->config.destructor_context, object->data);
|
||||||
|
size_t allocated_size = self->config.size_query(self->config.size_query_context, object->data);
|
||||||
Allocator_Free(self->allocator, object, allocated_size);
|
Allocator_Free(self->allocator, object, allocated_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,13 +40,7 @@ void TracingHeap_Destroy(TracingHeap* self)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (TracingObject* object = self->white_objects; object != NULL; object = object->color_next) {
|
for (TracingObject* object = self->objects; object != NULL; object = object->global_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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,12 +153,10 @@ int TracingHeap_TraceNext(TracingHeap* self)
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t TracingHeap_TraceNextN(TracingHeap* self, size_t n, int* error_code)
|
size_t TracingHeap_TraceNextN(TracingHeap* self, size_t n)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < n; i++) {
|
for (size_t i = 0; i < n; i++) {
|
||||||
int trace_code = TracingHeap_TraceNext(self);
|
if (TracingHeap_TraceNext(self)) {
|
||||||
if (trace_code != EXIT_SUCCESS) {
|
|
||||||
if (error_code != NULL) *error_code = trace_code;
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -173,13 +166,13 @@ size_t TracingHeap_TraceNextN(TracingHeap* self, size_t n, int* error_code)
|
||||||
|
|
||||||
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;
|
||||||
|
@ -206,11 +199,6 @@ 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) {
|
||||||
|
@ -224,11 +212,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->color_next;
|
TracingObject* next_unreachable = unreachable_object->global_next;
|
||||||
|
|
||||||
if (_TracingHeap_DestroyObject(self, unreachable_object)) {
|
if (_TracingHeap_DestroyObject(self, unreachable_object)) {
|
||||||
*unreachable_list = unreachable_object;
|
*unreachable_list = unreachable_object;
|
||||||
unreachable_object->color_prev = NULL;
|
unreachable_object->global_prev = NULL;
|
||||||
return ECANCELED;
|
return ECANCELED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -258,5 +246,11 @@ 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,7 +4,6 @@
|
||||||
#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,
|
||||||
|
@ -13,6 +12,8 @@ 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;
|
||||||
|
@ -38,6 +39,7 @@ 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;
|
||||||
|
@ -55,11 +57,9 @@ 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, int* error_code);
|
size_t TracingHeap_TraceNextN(TracingHeap* self, size_t n);
|
||||||
int TracingHeap_AddTracingRoot(TracingHeap* self, void* data);
|
int TracingHeap_AddTracingRoot(TracingHeap* self, void* data);
|
||||||
|
|
||||||
#endif // header
|
#endif // header
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue