📄 memmgr.h
字号:
#ifndef __MEMMGR_H__
#define __MEMMGR_H__
#include "sync.h"
enum {
GC_WHITE,
GC_BLACK
};
#define IS_BLACK(obj) (((long)((CtkAllocHeader*)(obj)-1)->next & GC_BLACK) != 0)
#define SET_BLACK(obj) (*(char**)&((CtkAllocHeader*)(obj)-1)->next += GC_BLACK)
#define SET_WHITE(obj) (*(char**)&((CtkAllocHeader*)(obj)-1)->next -= GC_BLACK)
#define IS_OBJECT(var) ((var).type >= CTK_STRING && (var).u.ptr != NULL)
class CtkMemoryManager {
mutex_t mutex;
event_t event;
volatile int gcActive;
size_t allocated;
public:
CtkMemoryManager();
~CtkMemoryManager();
static size_t GCwatermark; // total size of allocated objects after which GC is initiated
void markObject(CtkObject var);
void beginAccess(CtkThread* thr) {
if (thr->memoryAccess++ == 0) {
if (gcActive) {
thr->memoryAccess = 0;
event.signal();
critical_section csGC(mutex);
thr->memoryAccess = 1;
}
}
}
void endAccess(CtkThread* thr) {
assert(thr->memoryAccess > 0);
if (--thr->memoryAccess == 0) {
if (gcActive) {
event.signal();
} else if (allocated > GCwatermark) {
doGC();
}
}
}
void doGC();
void* allocate(size_t size, CtkFinalizer finalizer);
void notEnoughMemory();
char* reservedSpace;
static CtkMemoryManager instance;
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -