////////////////////////////////////////////////////////////
// gc_imp.h Copyright 1995 Gregory Colvin.
// Free distribution OK with this notice.
//
// Smart pointers to garbage collected objects.
#ifndef GC_IMP_H
#define GC_IMP_H
// Base class for smart pointers.
class gc_link {
protected:
void* ptr;
gc_link() : ptr(0) {}
gc_link(void* p) : ptr(0) { set_ptr(p); }
gc_link(const gc_link& r) { set_ptr(r.ptr); }
~gc_link() { decr_root(), ptr = 0; }
void set_ptr(void*);
void incr_root();
void decr_root();
void mark_or_copy(const gc_link& r);
};
// Base class for all gc_value objects.
struct gc_data {
gc_data();
struct { union { double d; void* p; } align; } data;
static size_t min() { return offsetof(gc_data,data); }
private:
friend struct gc_statics;
friend struct gc_marking;
friend struct gc_link;
friend void gc_all();
friend void gc_min();
friend void gc_handler() throw(bad_alloc);
static void Mark();
static int Sweep();
virtual void mark_all()=0:
virtual void mark(void*)=0;
virtual void destroy_all()=0;
virtual void destroy(void*)=0;
protected:
void mark_loop(size_t);
void destroy_loop(size_t);
static int IsMarking;
};
// Collected objects are allocated in gc_value objects.
template<class T> struct gc_value : gc_data {
gc_value() : gc_data() {}
private:
friend gc_link;
virtual void mark_all() { mark_loop(sizeof(T)); }
virtual void mark(void* p) { T* q= (T*)p; *q = *q; }
virtual void destroy_all() { destroy_loop(sizeof(T)); }
virtual void destroy(void* p) { ((T*)p)->~T(); }
};
#endif
/* End of File */