unorthidox use of GAllocator and GSList?
- From: Ben Johnson <ben blarg net>
- To: gtk-list <gtk-list gnome org>
- Subject: unorthidox use of GAllocator and GSList?
- Date: Wed, 16 Feb 2005 15:09:30 -0800
Hi
I'm wondering if I should cease and desist. I'm using the GSList and
the GAllocator in way that they may not be designed for. The reason I
am doing it this way is I want to allocate space for my list nodes and
my data in the same big chunk. In other words, I'm creating list nodes
that contain more information than just a next pointer and a data
pointer, and then using the usual g_slist*() interface to manage the
memory and and the (extended) list nodes. code blurbs included below...
I haven't tested this very thoroughly yet, but it does appear to work.
What I want to know is, should I avoid doing this? I worry there's a
possibility glib may change in a way that will cause my app to crash or
fail to compile, etc..
Thanks,
- Ben
typedef struct menu_page menu_page_t;
struct menu_page {
GSList listnode;
char page_name[PAGE_LABEL_LEN+1];
int page_number;
unsigned int row_count;
unsigned int col_count;
menu_option_group_t * grouplist;
};
void
func1( GAllocator ** a )
{
sqlbuf = queries[i];
count = db_run_count_query(sqlbuf, strlen(sqlbuf));
/* gslist.c showed me how to do this. */
*a = g_allocator_new("my allocator", count);
(*a)->type = G_ALLOCATOR_SLIST;
(*a)->free_lists = NULL;
(*a)->mem_chunk = g_mem_chunk_new((*a)->name,
sizeof(menu_page_t),
sizeof(menu_page_t) * (*a)->n_preallocs,
G_ALLOC_ONLY);
}
static menu_page_t * menu_pages = NULL;
void
func2( GAllocator * a )
{
g_slist_push_allocator( a );
/* g_slist_append() returns the list node you pass to it,
* except when you pass NULL, then it a new node. so, */
if( ! menu_pages ) {
/* make first node */
menu_pages = (menu_page_t *)g_slist_append(NULL, NULL);
new_menu_page = menu_pages;
} else {
/* returns node passed. */
g_slist_append( (GSList*)new_menu_page, NULL );
/* make sure new_menu_page is always the
* latest added and the last in the list. */
new_menu_page = (menu_page_t *)((GSList*)new_menu_page)->next;
}
g_slist_pop_allocator();
initialize_new_node( new_menu_page );
/* and so on */
}
static GAllocator * menu_page_allocator = NULL;
void func0(void)
{
/* create my allocator */
func1( &menu_page_allocator );
/* add a node to the list using the allocator */
func2( menu_page_allocator );
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]