Re: SGCL - Garbage Collector for C++
How could I use your smart-pointer interface to do something like this:
_____________________________________________________________
/* assume this is all garbage collected */
struct llnode {
llnode* next;
void* state;
};
static llnode* volatile g_list = 0;
int list_push(void* const state) {
llnode* const _this = gc_malloc(sizeof(*_this));
if (_this) {
_this->state = state;
do {
cmp = g_list;
_this->next = cmp;
} while (CASPTR_RELEASE(&g_list, cmp, _this));
return 0;
}
return ENOMEM;
}
llnode* list_pop() {
void* state;
llnode* cmp;
do {
if (! (cmp = g_list)) {
return 0;
}
} while (CASPTR_ACQUIRE(&g_list, cmp, cmp->next));
state = cmp->state;
cmp = 0;
return state;
}
_____________________________________________________________
Since I am using GC, there is no ABA problem, and everything gets cleaned
up. What I am most interested in is how this would look with your smart
pointer interface?
//----------------------------------------------------------
struct node
{
node(const gc<node>& n, void* s) : next(n), state(s)
{
}
gc<node> next;
void* state;
};
static gc<node> g_list;
static Sgcl::Mutex g_lmutex;
void list_push(void* state) throw(std::bad_alloc)
{
Sgcl::AutoLock l(g_lmutex);
g_list = gcnew node(g_list, state);
}
void* list_pop()
{
Sgcl::AutoLock l(g_lmutex);
if (g_list)
{
void* state = g_list->state;
g_list = g_list->next;
return state;
}
else return 0;
}
//----------------------------------------------------------
rgds
Sebastian Nibisz
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]