#ifndef _GC_H_ #define _GC_H_ #include #include "defs.h" #include "types/types.h" using namespace std; #define ALLOCSIZE 20 /* Name: FANG Garbage Collector Author: Jacob Torrey Date: Oct. 18, 2008 Desc: Keeps FANG from eating memory and never returning it */ namespace fang { class GCRef { public: int place, refs; }; class GC { public: GC(); void addRef(FANGFunction * ref); void minRef(FANGFunction * ref); FANGFunction * newFunction(); private: void *heapstart, *heapend; map func_count; vector func_map; int size; }; GC::GC() { size = ALLOCSIZE; FANGFunction * alloc = new FANGFunction[ALLOCSIZE]; for(int i = 0; i < ALLOCSIZE; i++) { func_map.push_back(0); } heapstart = (void*) alloc; heapend = (void*) (alloc + ALLOCSIZE); } FANGFunction * GC::newFunction() { FANGFunction *func = NULL; for(int i = 0; i < size; i++) { if(func_map[i] == 0) { func_map[i] = 1; GCRef ref; ref.place = i; ref.refs = 1; func = ((FANGFunction *)heapstart) + i; func_count.insert(make_pair(func, ref)); break; } } if(func == NULL) { // Need to increase size } return func; } void GC::addRef(FANGFunction * ref) { func_count[ref].refs++; } void GC::minRef(FANGFunction * ref) { func_count[ref].refs--; if(func_count[ref].refs == 0) { // Find a way to clean this out } } } #endif