Original Post
Hi, I'm making my own programming language and I need to implement a function that will run at run time. It will never be invoked by the user, only by the prologue of functions. I decided to write it in C instead of asm to keep it out of the core asm code. I am more of a C++ programmer so my C isn't that good. Basically everytime a function is constructed (functions are first class values) I need to grab a node off of the free list and move it into a tree. Usually when a function's destructor is called the node is pushed back onto the free list. However if a lexically nested function is still in existance I need to retain the node. This is all background information so it doesn't really matter. Anyway when the free list runs out I need to be able to get some more nodes in it quickly. Each node on the free list needs to have three 4-byte members. One should be zero (reference count), one can be filled with garbage, and the third needs to point to the next free node. This does not need to be portable at all. Ok so here is what I am thinking: Is there something faster? Thanks (edited out a bug and used node count) [Edited by - Glak on March 27, 2007 5:19:55 PM]
#define node_count 100
#define byte_count node_count*12
int* new_nodes(void)
{
int* p = malloc(byte_count);
int* iter=p;
int* end=p+byte_count;
while(iter!=end)
{
iter[0]=0; //or does malloced memory come zeroed out?
iter[2]=(int)(iter+3);
++iter;
}
return p;
}