Finger info for theoddone33@icculus.org...


I had a brilliant idea for what to write in here, but I forgot what it was.  In
celebration of this fact, I'm going to post my solution to Ryan's recent
programming challenge.  Note that I didn't come up with the method myself, I
just think that this implementation is pretty concise and altogether nice.  If
you find bugs in it don't tell me because it will hurt my feelings.

------

#include <cstdlib>

using namespace std;

typedef void* pointer;

typedef struct node_s
{
	struct node_s* parent;
	struct node_s* sibling;
	struct node_s* child;
	size_t depth;
	pointer ptr;
} *callstackid;

static callstackid alloc_node ()
{
	callstackid ret = (callstackid) malloc (sizeof (struct node_s));

	if (ret == NULL)
	{
		// Tee hee
		__asm__ __volatile__ ("int $3");
	}
	else
	{
		ret->parent = NULL;
		ret->child = NULL;
		ret->sibling = NULL;
		ret->depth = 0;
		ret->ptr = NULL;
	}
	return ret;
}

static callstackid root_node = NULL;

void callstack_init ()
{
	root_node = alloc_node ();
}


callstackid callstack_add (pointer* ptrs, size_t count, callstackid parent = root_node, size_t depth = 0)
{
	if (count == 0) return parent;

	callstackid node = parent->child;

	while (node)
	{
		if (node->child && node->child->ptr == ptrs[0])
		{
			break;
		}
		node = node->sibling;
	}

	if (!node)
	{
		node = alloc_node();
		node->parent = parent;
		node->child = NULL;
		node->sibling = NULL;
		node->depth = depth;
		node->ptr = ptrs[0];
	}

	return callstack_add (&ptrs[1], count - 1, node, depth + 1);
}

void callstack_doneadding (void)
{
	// Lame.
}

size_t callstack_framecount (callstackid id)
{
	return id->depth;
}

void callstack_get(callstackid id, pointer* ptrs)
{
	size_t depth = id->depth;
	size_t count = 0;
	callstackid node = id;

	while (id && id->parent)
	{
		ptrs[depth-count-1] = id->ptr;
		id = id->parent;
		count++;
	}
}
    

When this .plan was written: 2004-08-06 04:41:49
.plan archives for this user are here (RSS here).
Powered by IcculusFinger v2.1.24
Stick it in the camel and go.