⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memmgr.h

📁 C-Talk is interpreted scripting language with C-like syntax and dynamic type checking. Variables in
💻 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 + -