81 lines
2.2 KiB
C
81 lines
2.2 KiB
C
#ifndef UTILITIEC_TRACINGHEAP_H
|
|
#define UTILITIEC_TRACINGHEAP_H
|
|
|
|
#include "../allocator-interface/allocator-interface.h"
|
|
|
|
#include <stdalign.h>
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
|
|
enum TracingColor {
|
|
TRACINGCOLOR_BLACK,
|
|
TRACINGCOLOR_GREY,
|
|
TRACINGCOLOR_WHITE,
|
|
};
|
|
|
|
union TracingObjectDataAlignment {
|
|
double d;
|
|
void* p;
|
|
};
|
|
|
|
typedef struct TracingObject {
|
|
// color linked list members
|
|
struct TracingObject* color_next;
|
|
struct TracingObject* color_prev;
|
|
|
|
// Selector for the callback function
|
|
// TODO: Think about something for 16-bit systems
|
|
#define TRACINGHEAPDATAKIND_MAX (((uint64_t) 1 << 62) - 1)
|
|
uint64_t data_kind: 62;
|
|
|
|
// color of this object
|
|
enum TracingColor color: 2;
|
|
|
|
// data member of the requested size
|
|
alignas(union TracingObjectDataAlignment) char data[];
|
|
} TracingObject;
|
|
|
|
|
|
typedef int (*TracingHeapVisit) (void* context, void* reference);
|
|
typedef int (*TracingHeapTraceData) (void* context, void* data, TracingHeapVisit trace_callback, void* callback_context);
|
|
typedef int (*TracingHeapDataDestructor) (void* context, void* data);
|
|
typedef size_t (*TracingHeapSizeQuery) (void* context, void* data);
|
|
|
|
typedef struct TracingHeapConfig_s {
|
|
TracingHeapTraceData* trace_data;
|
|
void** trace_context;
|
|
|
|
TracingHeapDataDestructor* destructor;
|
|
void** destructor_context;
|
|
|
|
TracingHeapSizeQuery* size_query;
|
|
void** size_query_context;
|
|
} TracingHeapConfig;
|
|
|
|
typedef struct TracingHeap_s {
|
|
TracingObject* white_objects;
|
|
TracingObject* grey_objects;
|
|
TracingObject* black_objects;
|
|
|
|
enum TracingColor reachable_color;
|
|
enum TracingColor unreachable_color;
|
|
|
|
TracingHeapConfig config;
|
|
allocator_t* allocator;
|
|
} TracingHeap;
|
|
|
|
int TracingHeap_Create(TracingHeap* self, allocator_t* allocator);
|
|
void TracingHeap_Destroy(TracingHeap* self);
|
|
|
|
void* TracingHeap_Allocate(TracingHeap* self, size_t bytes, uint64_t object_kind);
|
|
|
|
int TracingHeap_BeginTrace(TracingHeap* self);
|
|
bool TracingHeap_IsTraceFinished(TracingHeap* self);
|
|
int TracingHeap_EndTrace(TracingHeap* self);
|
|
|
|
int TracingHeap_TraceNext(TracingHeap* self);
|
|
size_t TracingHeap_TraceNextN(TracingHeap* self, size_t n, int* error_code);
|
|
int TracingHeap_AddTracingRoot(TracingHeap* self, void* data);
|
|
enum TracingColor TracingHeap_GetDataColor(TracingHeap* self, void* data);
|
|
|
|
#endif // header
|