Patch from Darin Adler to remove GReal* structures in favor of simple

Wed Sep 19 14:05:27 2001  Owen Taylor  <otaylor@redhat.com>

	* glib/{gcache.c,gmem.c,grel.c,gstring.c,gtimer.c,gtree}:
	Patch from Darin Adler to remove GReal* structures in
	favor of simple opaque typedefs in cases where there
	were no non-private members. (#59693)
This commit is contained in:
Owen Taylor 2001-09-19 18:08:19 +00:00 committed by Owen Taylor
parent 031a4b0f46
commit 4d892e879e
14 changed files with 345 additions and 429 deletions

View File

@ -1,3 +1,10 @@
Wed Sep 19 14:05:27 2001 Owen Taylor <otaylor@redhat.com>
* glib/{gcache.c,gmem.c,grel.c,gstring.c,gtimer.c,gtree}:
Patch from Darin Adler to remove GReal* structures in
favor of simple opaque typedefs in cases where there
were no non-private members. (#59693)
Wed Sep 19 13:03:38 2001 Owen Taylor <otaylor@redhat.com>
* glib/giochannel.c (g_io_channel_read/write_chars): Handle NUL

View File

@ -1,3 +1,10 @@
Wed Sep 19 14:05:27 2001 Owen Taylor <otaylor@redhat.com>
* glib/{gcache.c,gmem.c,grel.c,gstring.c,gtimer.c,gtree}:
Patch from Darin Adler to remove GReal* structures in
favor of simple opaque typedefs in cases where there
were no non-private members. (#59693)
Wed Sep 19 13:03:38 2001 Owen Taylor <otaylor@redhat.com>
* glib/giochannel.c (g_io_channel_read/write_chars): Handle NUL

View File

@ -1,3 +1,10 @@
Wed Sep 19 14:05:27 2001 Owen Taylor <otaylor@redhat.com>
* glib/{gcache.c,gmem.c,grel.c,gstring.c,gtimer.c,gtree}:
Patch from Darin Adler to remove GReal* structures in
favor of simple opaque typedefs in cases where there
were no non-private members. (#59693)
Wed Sep 19 13:03:38 2001 Owen Taylor <otaylor@redhat.com>
* glib/giochannel.c (g_io_channel_read/write_chars): Handle NUL

View File

@ -1,3 +1,10 @@
Wed Sep 19 14:05:27 2001 Owen Taylor <otaylor@redhat.com>
* glib/{gcache.c,gmem.c,grel.c,gstring.c,gtimer.c,gtree}:
Patch from Darin Adler to remove GReal* structures in
favor of simple opaque typedefs in cases where there
were no non-private members. (#59693)
Wed Sep 19 13:03:38 2001 Owen Taylor <otaylor@redhat.com>
* glib/giochannel.c (g_io_channel_read/write_chars): Handle NUL

View File

@ -1,3 +1,10 @@
Wed Sep 19 14:05:27 2001 Owen Taylor <otaylor@redhat.com>
* glib/{gcache.c,gmem.c,grel.c,gstring.c,gtimer.c,gtree}:
Patch from Darin Adler to remove GReal* structures in
favor of simple opaque typedefs in cases where there
were no non-private members. (#59693)
Wed Sep 19 13:03:38 2001 Owen Taylor <otaylor@redhat.com>
* glib/giochannel.c (g_io_channel_read/write_chars): Handle NUL

View File

@ -1,3 +1,10 @@
Wed Sep 19 14:05:27 2001 Owen Taylor <otaylor@redhat.com>
* glib/{gcache.c,gmem.c,grel.c,gstring.c,gtimer.c,gtree}:
Patch from Darin Adler to remove GReal* structures in
favor of simple opaque typedefs in cases where there
were no non-private members. (#59693)
Wed Sep 19 13:03:38 2001 Owen Taylor <otaylor@redhat.com>
* glib/giochannel.c (g_io_channel_read/write_chars): Handle NUL

View File

@ -1,3 +1,10 @@
Wed Sep 19 14:05:27 2001 Owen Taylor <otaylor@redhat.com>
* glib/{gcache.c,gmem.c,grel.c,gstring.c,gtimer.c,gtree}:
Patch from Darin Adler to remove GReal* structures in
favor of simple opaque typedefs in cases where there
were no non-private members. (#59693)
Wed Sep 19 13:03:38 2001 Owen Taylor <otaylor@redhat.com>
* glib/giochannel.c (g_io_channel_read/write_chars): Handle NUL

View File

@ -1,3 +1,10 @@
Wed Sep 19 14:05:27 2001 Owen Taylor <otaylor@redhat.com>
* glib/{gcache.c,gmem.c,grel.c,gstring.c,gtimer.c,gtree}:
Patch from Darin Adler to remove GReal* structures in
favor of simple opaque typedefs in cases where there
were no non-private members. (#59693)
Wed Sep 19 13:03:38 2001 Owen Taylor <otaylor@redhat.com>
* glib/giochannel.c (g_io_channel_read/write_chars): Handle NUL

View File

@ -32,7 +32,6 @@
typedef struct _GCacheNode GCacheNode;
typedef struct _GRealCache GRealCache;
struct _GCacheNode
{
@ -41,7 +40,7 @@ struct _GCacheNode
gint ref_count;
};
struct _GRealCache
struct _GCache
{
/* Called to create a value from a key */
GCacheNewFunc value_new_func;
@ -79,7 +78,7 @@ g_cache_new (GCacheNewFunc value_new_func,
GHashFunc hash_value_func,
GEqualFunc key_equal_func)
{
GRealCache *cache;
GCache *cache;
g_return_val_if_fail (value_new_func != NULL, NULL);
g_return_val_if_fail (value_destroy_func != NULL, NULL);
@ -89,7 +88,7 @@ g_cache_new (GCacheNewFunc value_new_func,
g_return_val_if_fail (hash_value_func != NULL, NULL);
g_return_val_if_fail (key_equal_func != NULL, NULL);
cache = g_new (GRealCache, 1);
cache = g_new (GCache, 1);
cache->value_new_func = value_new_func;
cache->value_destroy_func = value_destroy_func;
cache->key_dup_func = key_dup_func;
@ -97,47 +96,41 @@ g_cache_new (GCacheNewFunc value_new_func,
cache->key_table = g_hash_table_new (hash_key_func, key_equal_func);
cache->value_table = g_hash_table_new (hash_value_func, NULL);
return (GCache*) cache;
return cache;
}
void
g_cache_destroy (GCache *cache)
{
GRealCache *rcache;
g_return_if_fail (cache != NULL);
rcache = (GRealCache*) cache;
g_hash_table_destroy (rcache->key_table);
g_hash_table_destroy (rcache->value_table);
g_free (rcache);
g_hash_table_destroy (cache->key_table);
g_hash_table_destroy (cache->value_table);
g_free (cache);
}
gpointer
g_cache_insert (GCache *cache,
gpointer key)
{
GRealCache *rcache;
GCacheNode *node;
gpointer value;
g_return_val_if_fail (cache != NULL, NULL);
rcache = (GRealCache*) cache;
node = g_hash_table_lookup (rcache->key_table, key);
node = g_hash_table_lookup (cache->key_table, key);
if (node)
{
node->ref_count += 1;
return node->value;
}
key = (* rcache->key_dup_func) (key);
value = (* rcache->value_new_func) (key);
key = (* cache->key_dup_func) (key);
value = (* cache->value_new_func) (key);
node = g_cache_node_new (value);
g_hash_table_insert (rcache->key_table, key, node);
g_hash_table_insert (rcache->value_table, value, key);
g_hash_table_insert (cache->key_table, key, node);
g_hash_table_insert (cache->value_table, value, key);
return node->value;
}
@ -146,27 +139,24 @@ void
g_cache_remove (GCache *cache,
gconstpointer value)
{
GRealCache *rcache;
GCacheNode *node;
gpointer key;
g_return_if_fail (cache != NULL);
rcache = (GRealCache*) cache;
key = g_hash_table_lookup (rcache->value_table, value);
node = g_hash_table_lookup (rcache->key_table, key);
key = g_hash_table_lookup (cache->value_table, value);
node = g_hash_table_lookup (cache->key_table, key);
g_return_if_fail (node != NULL);
node->ref_count -= 1;
if (node->ref_count == 0)
{
g_hash_table_remove (rcache->value_table, value);
g_hash_table_remove (rcache->key_table, key);
g_hash_table_remove (cache->value_table, value);
g_hash_table_remove (cache->key_table, key);
(* rcache->key_destroy_func) (key);
(* rcache->value_destroy_func) (node->value);
(* cache->key_destroy_func) (key);
(* cache->value_destroy_func) (node->value);
g_cache_node_destroy (node);
}
}
@ -176,14 +166,10 @@ g_cache_key_foreach (GCache *cache,
GHFunc func,
gpointer user_data)
{
GRealCache *rcache;
g_return_if_fail (cache != NULL);
g_return_if_fail (func != NULL);
rcache = (GRealCache*) cache;
g_hash_table_foreach (rcache->value_table, func, user_data);
g_hash_table_foreach (cache->value_table, func, user_data);
}
void
@ -191,14 +177,10 @@ g_cache_value_foreach (GCache *cache,
GHFunc func,
gpointer user_data)
{
GRealCache *rcache;
g_return_if_fail (cache != NULL);
g_return_if_fail (func != NULL);
rcache = (GRealCache*) cache;
g_hash_table_foreach (rcache->key_table, func, user_data);
g_hash_table_foreach (cache->key_table, func, user_data);
}

View File

@ -573,7 +573,6 @@ GMemVTable *glib_mem_profiler_table = &profiler_table;
/* --- MemChunks --- */
typedef struct _GFreeAtom GFreeAtom;
typedef struct _GMemArea GMemArea;
typedef struct _GRealMemChunk GRealMemChunk;
struct _GFreeAtom
{
@ -597,7 +596,7 @@ struct _GMemArea
*/
};
struct _GRealMemChunk
struct _GMemChunk
{
const gchar *name; /* name of this MemChunk...used for debugging output */
gint type; /* the type of MemChunk: ALLOC_ONLY or ALLOC_AND_FREE */
@ -610,8 +609,8 @@ struct _GRealMemChunk
GMemArea *free_mem_area; /* the free area...which is about to be destroyed */
GFreeAtom *free_atoms; /* the free atoms list */
GTree *mem_tree; /* tree of mem areas sorted by memory address */
GRealMemChunk *next; /* pointer to the next chunk */
GRealMemChunk *prev; /* pointer to the previous chunk */
GMemChunk *next; /* pointer to the next chunk */
GMemChunk *prev; /* pointer to the previous chunk */
};
@ -627,7 +626,7 @@ static gint g_mem_chunk_area_search (GMemArea *a,
* g_malloc, the same holds true for StaticPrivate
*/
static GMutex *mem_chunks_lock = NULL;
static GRealMemChunk *mem_chunks = NULL;
static GMemChunk *mem_chunks = NULL;
GMemChunk*
g_mem_chunk_new (const gchar *name,
@ -635,7 +634,7 @@ g_mem_chunk_new (const gchar *name,
gulong area_size,
gint type)
{
GRealMemChunk *mem_chunk;
GMemChunk *mem_chunk;
gulong rarea_size;
g_return_val_if_fail (atom_size > 0, NULL);
@ -646,7 +645,7 @@ g_mem_chunk_new (const gchar *name,
area_size = (area_size + atom_size - 1) / atom_size;
area_size *= atom_size;
mem_chunk = g_new (struct _GRealMemChunk, 1);
mem_chunk = g_new (GMemChunk, 1);
mem_chunk->name = name;
mem_chunk->type = type;
mem_chunk->num_mem_areas = 0;
@ -678,13 +677,12 @@ g_mem_chunk_new (const gchar *name,
LEAVE_MEM_CHUNK_ROUTINE ();
return ((GMemChunk*) mem_chunk);
return mem_chunk;
}
void
g_mem_chunk_destroy (GMemChunk *mem_chunk)
{
GRealMemChunk *rmem_chunk;
GMemArea *mem_areas;
GMemArea *temp_area;
@ -692,9 +690,7 @@ g_mem_chunk_destroy (GMemChunk *mem_chunk)
ENTER_MEM_CHUNK_ROUTINE ();
rmem_chunk = (GRealMemChunk*) mem_chunk;
mem_areas = rmem_chunk->mem_areas;
mem_areas = mem_chunk->mem_areas;
while (mem_areas)
{
temp_area = mem_areas;
@ -702,20 +698,20 @@ g_mem_chunk_destroy (GMemChunk *mem_chunk)
g_free (temp_area);
}
if (rmem_chunk->next)
rmem_chunk->next->prev = rmem_chunk->prev;
if (rmem_chunk->prev)
rmem_chunk->prev->next = rmem_chunk->next;
if (mem_chunk->next)
mem_chunk->next->prev = mem_chunk->prev;
if (mem_chunk->prev)
mem_chunk->prev->next = mem_chunk->next;
g_mutex_lock (mem_chunks_lock);
if (rmem_chunk == mem_chunks)
if (mem_chunk == mem_chunks)
mem_chunks = mem_chunks->next;
g_mutex_unlock (mem_chunks_lock);
if (rmem_chunk->type == G_ALLOC_AND_FREE)
g_tree_destroy (rmem_chunk->mem_tree);
if (mem_chunk->type == G_ALLOC_AND_FREE)
g_tree_destroy (mem_chunk->mem_tree);
g_free (rmem_chunk);
g_free (mem_chunk);
LEAVE_MEM_CHUNK_ROUTINE ();
}
@ -723,7 +719,6 @@ g_mem_chunk_destroy (GMemChunk *mem_chunk)
gpointer
g_mem_chunk_alloc (GMemChunk *mem_chunk)
{
GRealMemChunk *rmem_chunk;
GMemArea *temp_area;
gpointer mem;
@ -731,20 +726,18 @@ g_mem_chunk_alloc (GMemChunk *mem_chunk)
g_return_val_if_fail (mem_chunk != NULL, NULL);
rmem_chunk = (GRealMemChunk*) mem_chunk;
while (rmem_chunk->free_atoms)
while (mem_chunk->free_atoms)
{
/* Get the first piece of memory on the "free_atoms" list.
* We can go ahead and destroy the list node we used to keep
* track of it with and to update the "free_atoms" list to
* point to its next element.
*/
mem = rmem_chunk->free_atoms;
rmem_chunk->free_atoms = rmem_chunk->free_atoms->next;
mem = mem_chunk->free_atoms;
mem_chunk->free_atoms = mem_chunk->free_atoms->next;
/* Determine which area this piece of memory is allocated from */
temp_area = g_tree_search (rmem_chunk->mem_tree,
temp_area = g_tree_search (mem_chunk->mem_tree,
(GCompareFunc) g_mem_chunk_area_search,
mem);
@ -763,32 +756,32 @@ g_mem_chunk_alloc (GMemChunk *mem_chunk)
if (temp_area->mark)
{
/* Update the "free" memory available in that area */
temp_area->free += rmem_chunk->atom_size;
temp_area->free += mem_chunk->atom_size;
if (temp_area->free == rmem_chunk->area_size)
if (temp_area->free == mem_chunk->area_size)
{
if (temp_area == rmem_chunk->mem_area)
rmem_chunk->mem_area = NULL;
if (temp_area == mem_chunk->mem_area)
mem_chunk->mem_area = NULL;
if (rmem_chunk->free_mem_area)
if (mem_chunk->free_mem_area)
{
rmem_chunk->num_mem_areas -= 1;
mem_chunk->num_mem_areas -= 1;
if (temp_area->next)
temp_area->next->prev = temp_area->prev;
if (temp_area->prev)
temp_area->prev->next = temp_area->next;
if (temp_area == rmem_chunk->mem_areas)
rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
if (temp_area == mem_chunk->mem_areas)
mem_chunk->mem_areas = mem_chunk->mem_areas->next;
if (rmem_chunk->type == G_ALLOC_AND_FREE)
g_tree_remove (rmem_chunk->mem_tree, temp_area);
if (mem_chunk->type == G_ALLOC_AND_FREE)
g_tree_remove (mem_chunk->mem_tree, temp_area);
g_free (temp_area);
}
else
rmem_chunk->free_mem_area = temp_area;
mem_chunk->free_mem_area = temp_area;
rmem_chunk->num_marked_areas -= 1;
mem_chunk->num_marked_areas -= 1;
}
}
else
@ -807,50 +800,50 @@ g_mem_chunk_alloc (GMemChunk *mem_chunk)
* then allocate a new mem area. We'll first check and see if we can use
* the "free_mem_area". Otherwise we'll just malloc the mem area.
*/
if ((!rmem_chunk->mem_area) ||
((rmem_chunk->mem_area->index + rmem_chunk->atom_size) > rmem_chunk->area_size))
if ((!mem_chunk->mem_area) ||
((mem_chunk->mem_area->index + mem_chunk->atom_size) > mem_chunk->area_size))
{
if (rmem_chunk->free_mem_area)
if (mem_chunk->free_mem_area)
{
rmem_chunk->mem_area = rmem_chunk->free_mem_area;
rmem_chunk->free_mem_area = NULL;
mem_chunk->mem_area = mem_chunk->free_mem_area;
mem_chunk->free_mem_area = NULL;
}
else
{
#ifdef ENABLE_GC_FRIENDLY
rmem_chunk->mem_area = (GMemArea*) g_malloc0 (sizeof (GMemArea) -
MEM_AREA_SIZE +
rmem_chunk->area_size);
#else /* !ENABLE_GC_FRIENDLY */
rmem_chunk->mem_area = (GMemArea*) g_malloc (sizeof (GMemArea) -
mem_chunk->mem_area = (GMemArea*) g_malloc0 (sizeof (GMemArea) -
MEM_AREA_SIZE +
rmem_chunk->area_size);
mem_chunk->area_size);
#else /* !ENABLE_GC_FRIENDLY */
mem_chunk->mem_area = (GMemArea*) g_malloc (sizeof (GMemArea) -
MEM_AREA_SIZE +
mem_chunk->area_size);
#endif /* ENABLE_GC_FRIENDLY */
rmem_chunk->num_mem_areas += 1;
rmem_chunk->mem_area->next = rmem_chunk->mem_areas;
rmem_chunk->mem_area->prev = NULL;
mem_chunk->num_mem_areas += 1;
mem_chunk->mem_area->next = mem_chunk->mem_areas;
mem_chunk->mem_area->prev = NULL;
if (rmem_chunk->mem_areas)
rmem_chunk->mem_areas->prev = rmem_chunk->mem_area;
rmem_chunk->mem_areas = rmem_chunk->mem_area;
if (mem_chunk->mem_areas)
mem_chunk->mem_areas->prev = mem_chunk->mem_area;
mem_chunk->mem_areas = mem_chunk->mem_area;
if (rmem_chunk->type == G_ALLOC_AND_FREE)
g_tree_insert (rmem_chunk->mem_tree, rmem_chunk->mem_area, rmem_chunk->mem_area);
if (mem_chunk->type == G_ALLOC_AND_FREE)
g_tree_insert (mem_chunk->mem_tree, mem_chunk->mem_area, mem_chunk->mem_area);
}
rmem_chunk->mem_area->index = 0;
rmem_chunk->mem_area->free = rmem_chunk->area_size;
rmem_chunk->mem_area->allocated = 0;
rmem_chunk->mem_area->mark = 0;
mem_chunk->mem_area->index = 0;
mem_chunk->mem_area->free = mem_chunk->area_size;
mem_chunk->mem_area->allocated = 0;
mem_chunk->mem_area->mark = 0;
}
/* Get the memory and modify the state variables appropriately.
*/
mem = (gpointer) &rmem_chunk->mem_area->mem[rmem_chunk->mem_area->index];
rmem_chunk->mem_area->index += rmem_chunk->atom_size;
rmem_chunk->mem_area->free -= rmem_chunk->atom_size;
rmem_chunk->mem_area->allocated += 1;
mem = (gpointer) &mem_chunk->mem_area->mem[mem_chunk->mem_area->index];
mem_chunk->mem_area->index += mem_chunk->atom_size;
mem_chunk->mem_area->free -= mem_chunk->atom_size;
mem_chunk->mem_area->allocated += 1;
outa_here:
@ -867,9 +860,7 @@ g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
mem = g_mem_chunk_alloc (mem_chunk);
if (mem)
{
GRealMemChunk *rmem_chunk = (GRealMemChunk*) mem_chunk;
memset (mem, 0, rmem_chunk->atom_size);
memset (mem, 0, mem_chunk->atom_size);
}
return mem;
@ -879,7 +870,6 @@ void
g_mem_chunk_free (GMemChunk *mem_chunk,
gpointer mem)
{
GRealMemChunk *rmem_chunk;
GMemArea *temp_area;
GFreeAtom *free_atom;
@ -888,23 +878,21 @@ g_mem_chunk_free (GMemChunk *mem_chunk,
ENTER_MEM_CHUNK_ROUTINE ();
rmem_chunk = (GRealMemChunk*) mem_chunk;
#ifdef ENABLE_GC_FRIENDLY
memset (mem, 0, rmem_chunk->atom_size);
memset (mem, 0, mem_chunk->atom_size);
#endif /* ENABLE_GC_FRIENDLY */
/* Don't do anything if this is an ALLOC_ONLY chunk
*/
if (rmem_chunk->type == G_ALLOC_AND_FREE)
if (mem_chunk->type == G_ALLOC_AND_FREE)
{
/* Place the memory on the "free_atoms" list
*/
free_atom = (GFreeAtom*) mem;
free_atom->next = rmem_chunk->free_atoms;
rmem_chunk->free_atoms = free_atom;
free_atom->next = mem_chunk->free_atoms;
mem_chunk->free_atoms = free_atom;
temp_area = g_tree_search (rmem_chunk->mem_tree,
temp_area = g_tree_search (mem_chunk->mem_tree,
(GCompareFunc) g_mem_chunk_area_search,
mem);
@ -913,7 +901,7 @@ g_mem_chunk_free (GMemChunk *mem_chunk,
if (temp_area->allocated == 0)
{
temp_area->mark = 1;
rmem_chunk->num_marked_areas += 1;
mem_chunk->num_marked_areas += 1;
}
}
@ -924,7 +912,6 @@ g_mem_chunk_free (GMemChunk *mem_chunk,
void
g_mem_chunk_clean (GMemChunk *mem_chunk)
{
GRealMemChunk *rmem_chunk;
GMemArea *mem_area;
GFreeAtom *prev_free_atom;
GFreeAtom *temp_free_atom;
@ -934,18 +921,16 @@ g_mem_chunk_clean (GMemChunk *mem_chunk)
ENTER_MEM_CHUNK_ROUTINE ();
rmem_chunk = (GRealMemChunk*) mem_chunk;
if (rmem_chunk->type == G_ALLOC_AND_FREE)
if (mem_chunk->type == G_ALLOC_AND_FREE)
{
prev_free_atom = NULL;
temp_free_atom = rmem_chunk->free_atoms;
temp_free_atom = mem_chunk->free_atoms;
while (temp_free_atom)
{
mem = (gpointer) temp_free_atom;
mem_area = g_tree_search (rmem_chunk->mem_tree,
mem_area = g_tree_search (mem_chunk->mem_tree,
(GCompareFunc) g_mem_chunk_area_search,
mem);
@ -957,26 +942,26 @@ g_mem_chunk_clean (GMemChunk *mem_chunk)
if (prev_free_atom)
prev_free_atom->next = temp_free_atom->next;
else
rmem_chunk->free_atoms = temp_free_atom->next;
mem_chunk->free_atoms = temp_free_atom->next;
temp_free_atom = temp_free_atom->next;
mem_area->free += rmem_chunk->atom_size;
if (mem_area->free == rmem_chunk->area_size)
mem_area->free += mem_chunk->atom_size;
if (mem_area->free == mem_chunk->area_size)
{
rmem_chunk->num_mem_areas -= 1;
rmem_chunk->num_marked_areas -= 1;
mem_chunk->num_mem_areas -= 1;
mem_chunk->num_marked_areas -= 1;
if (mem_area->next)
mem_area->next->prev = mem_area->prev;
if (mem_area->prev)
mem_area->prev->next = mem_area->next;
if (mem_area == rmem_chunk->mem_areas)
rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
if (mem_area == rmem_chunk->mem_area)
rmem_chunk->mem_area = NULL;
if (mem_area == mem_chunk->mem_areas)
mem_chunk->mem_areas = mem_chunk->mem_areas->next;
if (mem_area == mem_chunk->mem_area)
mem_chunk->mem_area = NULL;
if (rmem_chunk->type == G_ALLOC_AND_FREE)
g_tree_remove (rmem_chunk->mem_tree, mem_area);
if (mem_chunk->type == G_ALLOC_AND_FREE)
g_tree_remove (mem_chunk->mem_tree, mem_area);
g_free (mem_area);
}
}
@ -993,7 +978,6 @@ g_mem_chunk_clean (GMemChunk *mem_chunk)
void
g_mem_chunk_reset (GMemChunk *mem_chunk)
{
GRealMemChunk *rmem_chunk;
GMemArea *mem_areas;
GMemArea *temp_area;
@ -1001,12 +985,10 @@ g_mem_chunk_reset (GMemChunk *mem_chunk)
ENTER_MEM_CHUNK_ROUTINE ();
rmem_chunk = (GRealMemChunk*) mem_chunk;
mem_areas = rmem_chunk->mem_areas;
rmem_chunk->num_mem_areas = 0;
rmem_chunk->mem_areas = NULL;
rmem_chunk->mem_area = NULL;
mem_areas = mem_chunk->mem_areas;
mem_chunk->num_mem_areas = 0;
mem_chunk->mem_areas = NULL;
mem_chunk->mem_area = NULL;
while (mem_areas)
{
@ -1015,11 +997,11 @@ g_mem_chunk_reset (GMemChunk *mem_chunk)
g_free (temp_area);
}
rmem_chunk->free_atoms = NULL;
mem_chunk->free_atoms = NULL;
if (rmem_chunk->mem_tree)
g_tree_destroy (rmem_chunk->mem_tree);
rmem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
if (mem_chunk->mem_tree)
g_tree_destroy (mem_chunk->mem_tree);
mem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
LEAVE_MEM_CHUNK_ROUTINE ();
}
@ -1027,31 +1009,29 @@ g_mem_chunk_reset (GMemChunk *mem_chunk)
void
g_mem_chunk_print (GMemChunk *mem_chunk)
{
GRealMemChunk *rmem_chunk;
GMemArea *mem_areas;
gulong mem;
g_return_if_fail (mem_chunk != NULL);
rmem_chunk = (GRealMemChunk*) mem_chunk;
mem_areas = rmem_chunk->mem_areas;
mem_areas = mem_chunk->mem_areas;
mem = 0;
while (mem_areas)
{
mem += rmem_chunk->area_size - mem_areas->free;
mem += mem_chunk->area_size - mem_areas->free;
mem_areas = mem_areas->next;
}
g_log (g_log_domain_glib, G_LOG_LEVEL_INFO,
"%s: %ld bytes using %d mem areas",
rmem_chunk->name, mem, rmem_chunk->num_mem_areas);
mem_chunk->name, mem, mem_chunk->num_mem_areas);
}
void
g_mem_chunk_info (void)
{
GRealMemChunk *mem_chunk;
GMemChunk *mem_chunk;
gint count;
count = 0;
@ -1080,7 +1060,7 @@ g_mem_chunk_info (void)
void
g_blow_chunks (void)
{
GRealMemChunk *mem_chunk;
GMemChunk *mem_chunk;
g_mutex_lock (mem_chunks_lock);
mem_chunk = mem_chunks;

View File

@ -31,10 +31,9 @@
#include <stdarg.h>
#include <string.h>
typedef struct _GRealRelation GRealRelation;
typedef struct _GRealTuples GRealTuples;
struct _GRealRelation
struct _GRelation
{
gint fields;
gint current_field;
@ -102,7 +101,7 @@ tuple_equal (gint fields)
GRelation*
g_relation_new (gint fields)
{
GRealRelation* rel = g_new0 (GRealRelation, 1);
GRelation* rel = g_new0 (GRelation, 1);
rel->fields = fields;
rel->tuple_chunk = g_mem_chunk_new ("Relation Chunk",
@ -112,7 +111,7 @@ g_relation_new (gint fields)
rel->all_tuples = g_hash_table_new (tuple_hash (fields), tuple_equal (fields));
rel->hashed_tuple_tables = g_new0 (GHashTable*, fields);
return (GRelation*) rel;
return rel;
}
static void
@ -124,25 +123,24 @@ g_relation_free_array (gpointer key, gpointer value, gpointer user_data)
void
g_relation_destroy (GRelation *relation)
{
GRealRelation *rel = (GRealRelation *) relation;
gint i;
if (rel)
if (relation)
{
g_hash_table_destroy (rel->all_tuples);
g_mem_chunk_destroy (rel->tuple_chunk);
g_hash_table_destroy (relation->all_tuples);
g_mem_chunk_destroy (relation->tuple_chunk);
for (i = 0; i < rel->fields; i += 1)
for (i = 0; i < relation->fields; i += 1)
{
if (rel->hashed_tuple_tables[i])
if (relation->hashed_tuple_tables[i])
{
g_hash_table_foreach (rel->hashed_tuple_tables[i], g_relation_free_array, NULL);
g_hash_table_destroy (rel->hashed_tuple_tables[i]);
g_hash_table_foreach (relation->hashed_tuple_tables[i], g_relation_free_array, NULL);
g_hash_table_destroy (relation->hashed_tuple_tables[i]);
}
}
g_free (rel->hashed_tuple_tables);
g_free (rel);
g_free (relation->hashed_tuple_tables);
g_free (relation);
}
}
@ -152,42 +150,39 @@ g_relation_index (GRelation *relation,
GHashFunc hash_func,
GEqualFunc key_equal_func)
{
GRealRelation *rel = (GRealRelation *) relation;
g_return_if_fail (relation != NULL);
g_return_if_fail (rel->count == 0 && rel->hashed_tuple_tables[field] == NULL);
g_return_if_fail (relation->count == 0 && relation->hashed_tuple_tables[field] == NULL);
rel->hashed_tuple_tables[field] = g_hash_table_new (hash_func, key_equal_func);
relation->hashed_tuple_tables[field] = g_hash_table_new (hash_func, key_equal_func);
}
void
g_relation_insert (GRelation *relation,
...)
{
GRealRelation *rel = (GRealRelation *) relation;
gpointer* tuple = g_chunk_new (gpointer, rel->tuple_chunk);
gpointer* tuple = g_chunk_new (gpointer, relation->tuple_chunk);
va_list args;
gint i;
va_start(args, relation);
for (i = 0; i < rel->fields; i += 1)
for (i = 0; i < relation->fields; i += 1)
tuple[i] = va_arg(args, gpointer);
va_end(args);
g_hash_table_insert (rel->all_tuples, tuple, tuple);
g_hash_table_insert (relation->all_tuples, tuple, tuple);
rel->count += 1;
relation->count += 1;
for (i = 0; i < rel->fields; i += 1)
for (i = 0; i < relation->fields; i += 1)
{
GHashTable *table;
gpointer key;
GHashTable *per_key_table;
table = rel->hashed_tuple_tables[i];
table = relation->hashed_tuple_tables[i];
if (table == NULL)
continue;
@ -197,7 +192,7 @@ g_relation_insert (GRelation *relation,
if (per_key_table == NULL)
{
per_key_table = g_hash_table_new (tuple_hash (rel->fields), tuple_equal (rel->fields));
per_key_table = g_hash_table_new (tuple_hash (relation->fields), tuple_equal (relation->fields));
g_hash_table_insert (table, key, per_key_table);
}
@ -211,7 +206,7 @@ g_relation_delete_tuple (gpointer tuple_key,
gpointer user_data)
{
gpointer *tuple = (gpointer*) tuple_value;
GRealRelation *rel = (GRealRelation *) user_data;
GRelation *rel = (GRelation *) user_data;
gint j;
g_assert (tuple_key == tuple_value);
@ -246,10 +241,9 @@ g_relation_delete (GRelation *relation,
gconstpointer key,
gint field)
{
GRealRelation *rel = (GRealRelation *) relation;
GHashTable *table = rel->hashed_tuple_tables[field];
GHashTable *table = relation->hashed_tuple_tables[field];
GHashTable *key_table;
gint count = rel->count;
gint count = relation->count;
g_return_val_if_fail (relation != NULL, 0);
g_return_val_if_fail (table != NULL, 0);
@ -259,9 +253,9 @@ g_relation_delete (GRelation *relation,
if (!key_table)
return 0;
rel->current_field = field;
relation->current_field = field;
g_hash_table_foreach (key_table, g_relation_delete_tuple, rel);
g_hash_table_foreach (key_table, g_relation_delete_tuple, relation);
g_hash_table_remove (table, key);
@ -269,7 +263,7 @@ g_relation_delete (GRelation *relation,
/* @@@ FIXME: Remove empty hash tables. */
return count - rel->count;
return count - relation->count;
}
static void
@ -295,8 +289,7 @@ g_relation_select (GRelation *relation,
gconstpointer key,
gint field)
{
GRealRelation *rel = (GRealRelation *) relation;
GHashTable *table = rel->hashed_tuple_tables[field];
GHashTable *table = relation->hashed_tuple_tables[field];
GHashTable *key_table;
GRealTuples *tuples = g_new0 (GRealTuples, 1);
gint count;
@ -311,8 +304,8 @@ g_relation_select (GRelation *relation,
count = g_relation_count (relation, key, field);
tuples->data = g_malloc (sizeof (gpointer) * rel->fields * count);
tuples->width = rel->fields;
tuples->data = g_malloc (sizeof (gpointer) * relation->fields * count);
tuples->width = relation->fields;
g_hash_table_foreach (key_table, g_relation_select_tuple, tuples);
@ -326,8 +319,7 @@ g_relation_count (GRelation *relation,
gconstpointer key,
gint field)
{
GRealRelation *rel = (GRealRelation *) relation;
GHashTable *table = rel->hashed_tuple_tables[field];
GHashTable *table = relation->hashed_tuple_tables[field];
GHashTable *key_table;
g_return_val_if_fail (relation != NULL, 0);
@ -344,22 +336,21 @@ g_relation_count (GRelation *relation,
gboolean
g_relation_exists (GRelation *relation, ...)
{
GRealRelation *rel = (GRealRelation *) relation;
gpointer* tuple = g_chunk_new (gpointer, rel->tuple_chunk);
gpointer* tuple = g_chunk_new (gpointer, relation->tuple_chunk);
va_list args;
gint i;
gboolean result;
va_start(args, relation);
for (i = 0; i < rel->fields; i += 1)
for (i = 0; i < relation->fields; i += 1)
tuple[i] = va_arg(args, gpointer);
va_end(args);
result = g_hash_table_lookup (rel->all_tuples, tuple) != NULL;
result = g_hash_table_lookup (relation->all_tuples, tuple) != NULL;
g_mem_chunk_free (rel->tuple_chunk, tuple);
g_mem_chunk_free (relation->tuple_chunk, tuple);
return result;
}
@ -399,7 +390,7 @@ g_relation_print_one (gpointer tuple_key,
{
gint i;
GString *gstring;
GRealRelation* rel = (GRealRelation*) user_data;
GRelation* rel = (GRelation*) user_data;
gpointer* tuples = (gpointer*) tuple_value;
gstring = g_string_new ("[");
@ -422,7 +413,7 @@ g_relation_print_index (gpointer tuple_key,
gpointer tuple_value,
gpointer user_data)
{
GRealRelation* rel = (GRealRelation*) user_data;
GRelation* rel = (GRelation*) user_data;
GHashTable* table = (GHashTable*) tuple_value;
g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "*** key %p", tuple_key);
@ -436,24 +427,23 @@ void
g_relation_print (GRelation *relation)
{
gint i;
GRealRelation* rel = (GRealRelation*) relation;
g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "*** all tuples (%d)", rel->count);
g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "*** all tuples (%d)", relation->count);
g_hash_table_foreach (rel->all_tuples,
g_hash_table_foreach (relation->all_tuples,
g_relation_print_one,
rel);
relation);
for (i = 0; i < rel->fields; i += 1)
for (i = 0; i < relation->fields; i += 1)
{
if (rel->hashed_tuple_tables[i] == NULL)
if (relation->hashed_tuple_tables[i] == NULL)
continue;
g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "*** index %d", i);
g_hash_table_foreach (rel->hashed_tuple_tables[i],
g_hash_table_foreach (relation->hashed_tuple_tables[i],
g_relation_print_index,
rel);
relation);
}
}

View File

@ -42,10 +42,7 @@
#include <ctype.h>
#include "glib.h"
typedef struct _GRealStringChunk GRealStringChunk;
typedef struct _GRealString GRealString;
struct _GRealStringChunk
struct _GStringChunk
{
GHashTable *const_table;
GSList *storage_list;
@ -54,13 +51,6 @@ struct _GRealStringChunk
gsize default_size;
};
struct _GRealString
{
gchar *str;
gsize len;
gsize allocated_len;
};
G_LOCK_DEFINE_STATIC (string_mem_chunk);
static GMemChunk *string_mem_chunk = NULL;
@ -117,7 +107,7 @@ nearest_power (gsize base, gsize num)
GStringChunk*
g_string_chunk_new (gsize default_size)
{
GRealStringChunk *new_chunk = g_new (GRealStringChunk, 1);
GStringChunk *new_chunk = g_new (GStringChunk, 1);
gsize size = 1;
size = nearest_power (1, default_size);
@ -128,13 +118,12 @@ g_string_chunk_new (gsize default_size)
new_chunk->default_size = size;
new_chunk->this_size = size;
return (GStringChunk*) new_chunk;
return new_chunk;
}
void
g_string_chunk_free (GStringChunk *fchunk)
g_string_chunk_free (GStringChunk *chunk)
{
GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
GSList *tmp_list;
g_return_if_fail (chunk != NULL);
@ -154,10 +143,9 @@ g_string_chunk_free (GStringChunk *fchunk)
}
gchar*
g_string_chunk_insert (GStringChunk *fchunk,
g_string_chunk_insert (GStringChunk *chunk,
const gchar *string)
{
GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
gsize len = strlen (string);
char* pos;
@ -184,10 +172,9 @@ g_string_chunk_insert (GStringChunk *fchunk,
}
gchar*
g_string_chunk_insert_const (GStringChunk *fchunk,
g_string_chunk_insert_const (GStringChunk *chunk,
const gchar *string)
{
GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
char* lookup;
g_return_val_if_fail (chunk != NULL, NULL);
@ -199,7 +186,7 @@ g_string_chunk_insert_const (GStringChunk *fchunk,
if (!lookup)
{
lookup = g_string_chunk_insert (fchunk, string);
lookup = g_string_chunk_insert (chunk, string);
g_hash_table_insert (chunk->const_table, lookup, lookup);
}
@ -209,8 +196,8 @@ g_string_chunk_insert_const (GStringChunk *fchunk,
/* Strings.
*/
static void
g_string_maybe_expand (GRealString* string,
gsize len)
g_string_maybe_expand (GString* string,
gsize len)
{
if (string->len + len >= string->allocated_len)
{
@ -222,15 +209,15 @@ g_string_maybe_expand (GRealString* string,
GString*
g_string_sized_new (gsize dfl_size)
{
GRealString *string;
GString *string;
G_LOCK (string_mem_chunk);
if (!string_mem_chunk)
string_mem_chunk = g_mem_chunk_new ("string mem chunk",
sizeof (GRealString),
sizeof (GString),
1024, G_ALLOC_AND_FREE);
string = g_chunk_new (GRealString, string_mem_chunk);
string = g_chunk_new (GString, string_mem_chunk);
G_UNLOCK (string_mem_chunk);
string->allocated_len = 0;
@ -240,7 +227,7 @@ g_string_sized_new (gsize dfl_size)
g_string_maybe_expand (string, MAX (dfl_size, 2));
string->str[0] = 0;
return (GString*) string;
return string;
}
GString*
@ -303,8 +290,8 @@ g_string_equal (const GString *v,
const GString *v2)
{
gchar *p, *q;
GRealString *string1 = (GRealString *) v;
GRealString *string2 = (GRealString *) v2;
GString *string1 = (GString *) v;
GString *string2 = (GString *) v2;
gsize i = string1->len;
if (i != string2->len)
@ -354,18 +341,15 @@ g_string_assign (GString *string,
}
GString*
g_string_truncate (GString *fstring,
g_string_truncate (GString *string,
gsize len)
{
GRealString *string = (GRealString *) fstring;
g_return_val_if_fail (string != NULL, NULL);
string->len = MIN (len, string->len);
string->str[string->len] = 0;
return fstring;
return string;
}
/**
@ -379,35 +363,31 @@ g_string_truncate (GString *fstring,
* of the newly added area are undefined. (However, as
* always, string->str[string->len] will be a nul byte.)
*
* Return value: @fstring
* Return value: @string
**/
GString*
g_string_set_size (GString *string,
gsize len)
{
GRealString *rstring = (GRealString *) string;
g_return_val_if_fail (string != NULL, NULL);
if (len >= rstring->allocated_len)
g_string_maybe_expand (rstring, len - string->len);
if (len >= string->allocated_len)
g_string_maybe_expand (string, len - string->len);
rstring->len = len;
rstring->str[len] = 0;
string->len = len;
string->str[len] = 0;
return string;
}
GString*
g_string_insert_len (GString *fstring,
g_string_insert_len (GString *string,
gssize pos,
const gchar *val,
gssize len)
{
GRealString *string = (GRealString *) fstring;
g_return_val_if_fail (string != NULL, NULL);
g_return_val_if_fail (val != NULL, fstring);
g_return_val_if_fail (val != NULL, string);
if (len < 0)
len = strlen (val);
@ -415,7 +395,7 @@ g_string_insert_len (GString *fstring,
if (pos < 0)
pos = string->len;
else
g_return_val_if_fail (pos <= string->len, fstring);
g_return_val_if_fail (pos <= string->len, string);
g_string_maybe_expand (string, len);
@ -432,17 +412,17 @@ g_string_insert_len (GString *fstring,
string->str[string->len] = 0;
return fstring;
return string;
}
GString*
g_string_append (GString *fstring,
g_string_append (GString *string,
const gchar *val)
{
g_return_val_if_fail (fstring != NULL, NULL);
g_return_val_if_fail (val != NULL, fstring);
g_return_val_if_fail (string != NULL, NULL);
g_return_val_if_fail (val != NULL, string);
return g_string_insert_len (fstring, -1, val, -1);
return g_string_insert_len (string, -1, val, -1);
}
GString*
@ -457,12 +437,12 @@ g_string_append_len (GString *string,
}
GString*
g_string_append_c (GString *fstring,
g_string_append_c (GString *string,
gchar c)
{
g_return_val_if_fail (fstring != NULL, NULL);
g_return_val_if_fail (string != NULL, NULL);
return g_string_insert_c (fstring, -1, c);
return g_string_insert_c (string, -1, c);
}
/**
@ -485,13 +465,13 @@ g_string_append_unichar (GString *string,
}
GString*
g_string_prepend (GString *fstring,
g_string_prepend (GString *string,
const gchar *val)
{
g_return_val_if_fail (fstring != NULL, NULL);
g_return_val_if_fail (val != NULL, fstring);
g_return_val_if_fail (string != NULL, NULL);
g_return_val_if_fail (val != NULL, string);
return g_string_insert_len (fstring, 0, val, -1);
return g_string_insert_len (string, 0, val, -1);
}
GString*
@ -506,12 +486,12 @@ g_string_prepend_len (GString *string,
}
GString*
g_string_prepend_c (GString *fstring,
g_string_prepend_c (GString *string,
gchar c)
{
g_return_val_if_fail (fstring != NULL, NULL);
g_return_val_if_fail (string != NULL, NULL);
return g_string_insert_c (fstring, 0, c);
return g_string_insert_c (string, 0, c);
}
/**
@ -534,25 +514,23 @@ g_string_prepend_unichar (GString *string,
}
GString*
g_string_insert (GString *fstring,
g_string_insert (GString *string,
gssize pos,
const gchar *val)
{
g_return_val_if_fail (fstring != NULL, NULL);
g_return_val_if_fail (val != NULL, fstring);
g_return_val_if_fail (string != NULL, NULL);
g_return_val_if_fail (val != NULL, string);
if (pos >= 0)
g_return_val_if_fail (pos <= fstring->len, fstring);
g_return_val_if_fail (pos <= string->len, string);
return g_string_insert_len (fstring, pos, val, -1);
return g_string_insert_len (string, pos, val, -1);
}
GString*
g_string_insert_c (GString *fstring,
g_string_insert_c (GString *string,
gssize pos,
gchar c)
{
GRealString *string = (GRealString *) fstring;
g_return_val_if_fail (string != NULL, NULL);
g_string_maybe_expand (string, 1);
@ -560,7 +538,7 @@ g_string_insert_c (GString *fstring,
if (pos < 0)
pos = string->len;
else
g_return_val_if_fail (pos <= string->len, fstring);
g_return_val_if_fail (pos <= string->len, string);
/* If not just an append, move the old stuff */
if (pos < string->len)
@ -572,7 +550,7 @@ g_string_insert_c (GString *fstring,
string->str[string->len] = 0;
return fstring;
return string;
}
/**
@ -606,21 +584,19 @@ g_string_insert_unichar (GString *string,
}
GString*
g_string_erase (GString *fstring,
g_string_erase (GString *string,
gsize pos,
gsize len)
{
GRealString *string = (GRealString*)fstring;
g_return_val_if_fail (string != NULL, NULL);
g_return_val_if_fail (pos >= 0, fstring);
g_return_val_if_fail (pos <= string->len, fstring);
g_return_val_if_fail (pos >= 0, string);
g_return_val_if_fail (pos <= string->len, string);
if (len < 0)
len = string->len - pos;
else
{
g_return_val_if_fail (pos + len <= string->len, fstring);
g_return_val_if_fail (pos + len <= string->len, string);
if (pos + len < string->len)
g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
@ -630,7 +606,7 @@ g_string_erase (GString *fstring,
string->str[string->len] = 0;
return fstring;
return string;
}
/**
@ -694,9 +670,8 @@ g_string_ascii_up (GString *string)
}
GString*
g_string_down (GString *fstring)
g_string_down (GString *string)
{
GRealString *string = (GRealString *) fstring;
guchar *s;
glong n = string->len;
@ -712,13 +687,12 @@ g_string_down (GString *fstring)
n--;
}
return fstring;
return string;
}
GString*
g_string_up (GString *fstring)
g_string_up (GString *string)
{
GRealString *string = (GRealString *) fstring;
guchar *s;
glong n = string->len;
@ -734,7 +708,7 @@ g_string_up (GString *fstring)
n--;
}
return fstring;
return string;
}
static void

View File

@ -46,9 +46,7 @@
#include <windows.h>
#endif /* G_OS_WIN32 */
typedef struct _GRealTimer GRealTimer;
struct _GRealTimer
struct _GTimer
{
#ifdef G_OS_WIN32
DWORD start;
@ -72,14 +70,14 @@ struct _GRealTimer
GTimer*
g_timer_new (void)
{
GRealTimer *timer;
GTimer *timer;
timer = g_new (GRealTimer, 1);
timer = g_new (GTimer, 1);
timer->active = TRUE;
GETTIME (timer->start);
return ((GTimer*) timer);
return timer;
}
void
@ -93,46 +91,35 @@ g_timer_destroy (GTimer *timer)
void
g_timer_start (GTimer *timer)
{
GRealTimer *rtimer;
g_return_if_fail (timer != NULL);
rtimer = (GRealTimer*) timer;
rtimer->active = TRUE;
timer->active = TRUE;
GETTIME (rtimer->start);
GETTIME (timer->start);
}
void
g_timer_stop (GTimer *timer)
{
GRealTimer *rtimer;
g_return_if_fail (timer != NULL);
rtimer = (GRealTimer*) timer;
rtimer->active = FALSE;
timer->active = FALSE;
GETTIME(rtimer->end);
GETTIME(timer->end);
}
void
g_timer_reset (GTimer *timer)
{
GRealTimer *rtimer;
g_return_if_fail (timer != NULL);
rtimer = (GRealTimer*) timer;
GETTIME (rtimer->start);
GETTIME (timer->start);
}
gdouble
g_timer_elapsed (GTimer *timer,
gulong *microseconds)
{
GRealTimer *rtimer;
gdouble total;
#ifndef G_OS_WIN32
struct timeval elapsed;
@ -140,39 +127,37 @@ g_timer_elapsed (GTimer *timer,
g_return_val_if_fail (timer != NULL, 0);
rtimer = (GRealTimer*) timer;
#ifdef G_OS_WIN32
if (rtimer->active)
rtimer->end = GetTickCount ();
if (timer->active)
timer->end = GetTickCount ();
/* Check for wraparound, which happens every 49.7 days. */
if (rtimer->end < rtimer->start)
total = (UINT_MAX - (rtimer->start - rtimer->end)) / 1000.0;
if (timer->end < timer->start)
total = (UINT_MAX - (timer->start - timer->end)) / 1000.0;
else
total = (rtimer->end - rtimer->start) / 1000.0;
total = (timer->end - timer->start) / 1000.0;
if (microseconds)
{
if (rtimer->end < rtimer->start)
if (timer->end < timer->start)
*microseconds =
((UINT_MAX - (rtimer->start - rtimer->end)) % 1000) * 1000;
((UINT_MAX - (timer->start - timer->end)) % 1000) * 1000;
else
*microseconds =
((rtimer->end - rtimer->start) % 1000) * 1000;
((timer->end - timer->start) % 1000) * 1000;
}
#else /* !G_OS_WIN32 */
if (rtimer->active)
gettimeofday (&rtimer->end, NULL);
if (timer->active)
gettimeofday (&timer->end, NULL);
if (rtimer->start.tv_usec > rtimer->end.tv_usec)
if (timer->start.tv_usec > timer->end.tv_usec)
{
rtimer->end.tv_usec += G_USEC_PER_SEC;
rtimer->end.tv_sec--;
timer->end.tv_usec += G_USEC_PER_SEC;
timer->end.tv_sec--;
}
elapsed.tv_usec = rtimer->end.tv_usec - rtimer->start.tv_usec;
elapsed.tv_sec = rtimer->end.tv_sec - rtimer->start.tv_sec;
elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
if (total < 0)

View File

@ -35,10 +35,9 @@
#include "glib.h"
typedef struct _GRealTree GRealTree;
typedef struct _GTreeNode GTreeNode;
struct _GRealTree
struct _GTree
{
GTreeNode *root;
GCompareDataFunc key_compare;
@ -235,18 +234,18 @@ g_tree_new_full (GCompareDataFunc key_compare_func,
GDestroyNotify key_destroy_func,
GDestroyNotify value_destroy_func)
{
GRealTree *rtree;
GTree *tree;
g_return_val_if_fail (key_compare_func != NULL, NULL);
rtree = g_new (GRealTree, 1);
rtree->root = NULL;
rtree->key_compare = key_compare_func;
rtree->key_destroy_func = key_destroy_func;
rtree->value_destroy_func = value_destroy_func;
rtree->key_compare_data = key_compare_data;
tree = g_new (GTree, 1);
tree->root = NULL;
tree->key_compare = key_compare_func;
tree->key_destroy_func = key_destroy_func;
tree->value_destroy_func = value_destroy_func;
tree->key_compare_data = key_compare_data;
return (GTree*) rtree;
return tree;
}
/**
@ -261,17 +260,13 @@ g_tree_new_full (GCompareDataFunc key_compare_func,
void
g_tree_destroy (GTree *tree)
{
GRealTree *rtree;
g_return_if_fail (tree != NULL);
rtree = (GRealTree*) tree;
g_tree_node_destroy (tree->root,
tree->key_destroy_func,
tree->value_destroy_func);
g_tree_node_destroy (rtree->root,
rtree->key_destroy_func,
rtree->value_destroy_func);
g_free (rtree);
g_free (tree);
}
/**
@ -294,18 +289,15 @@ g_tree_insert (GTree *tree,
gpointer key,
gpointer value)
{
GRealTree *rtree;
gboolean inserted;
g_return_if_fail (tree != NULL);
rtree = (GRealTree*) tree;
inserted = FALSE;
rtree->root = g_tree_node_insert (tree,
rtree->root,
key, value,
FALSE, &inserted);
tree->root = g_tree_node_insert (tree,
tree->root,
key, value,
FALSE, &inserted);
}
/**
@ -329,18 +321,15 @@ g_tree_replace (GTree *tree,
gpointer key,
gpointer value)
{
GRealTree *rtree;
gboolean inserted;
g_return_if_fail (tree != NULL);
rtree = (GRealTree*) tree;
inserted = FALSE;
rtree->root = g_tree_node_insert (tree,
rtree->root,
key, value,
TRUE, &inserted);
tree->root = g_tree_node_insert (tree,
tree->root,
key, value,
TRUE, &inserted);
}
/**
@ -358,13 +347,9 @@ void
g_tree_remove (GTree *tree,
gconstpointer key)
{
GRealTree *rtree;
g_return_if_fail (tree != NULL);
rtree = (GRealTree*) tree;
rtree->root = g_tree_node_remove (tree, rtree->root, key, TRUE);
tree->root = g_tree_node_remove (tree, tree->root, key, TRUE);
}
/**
@ -379,13 +364,9 @@ void
g_tree_steal (GTree *tree,
gconstpointer key)
{
GRealTree *rtree;
g_return_if_fail (tree != NULL);
rtree = (GRealTree*) tree;
rtree->root = g_tree_node_remove (tree, rtree->root, key, FALSE);
tree->root = g_tree_node_remove (tree, tree->root, key, FALSE);
}
/**
@ -403,15 +384,12 @@ gpointer
g_tree_lookup (GTree *tree,
gconstpointer key)
{
GRealTree *rtree;
GTreeNode *node;
g_return_val_if_fail (tree != NULL, NULL);
rtree = (GRealTree*) tree;
node = g_tree_node_lookup (rtree->root,
rtree->key_compare, rtree->key_compare_data, key);
node = g_tree_node_lookup (tree->root,
tree->key_compare, tree->key_compare_data, key);
return node ? node->value : NULL;
}
@ -436,15 +414,12 @@ g_tree_lookup_extended (GTree *tree,
gpointer *orig_key,
gpointer *value)
{
GRealTree *rtree;
GTreeNode *node;
g_return_val_if_fail (tree != NULL, FALSE);
rtree = (GRealTree*) tree;
node = g_tree_node_lookup (rtree->root,
rtree->key_compare, rtree->key_compare_data, lookup_key);
node = g_tree_node_lookup (tree->root,
tree->key_compare, tree->key_compare_data, lookup_key);
if (node)
{
@ -474,16 +449,12 @@ g_tree_foreach (GTree *tree,
GTraverseFunc func,
gpointer user_data)
{
GRealTree *rtree;
g_return_if_fail (tree != NULL);
rtree = (GRealTree*) tree;
if (!rtree->root)
if (!tree->root)
return;
g_tree_node_in_order (rtree->root, func, user_data);
g_tree_node_in_order (tree->root, func, user_data);
}
/**
@ -504,27 +475,23 @@ g_tree_traverse (GTree *tree,
GTraverseType traverse_type,
gpointer user_data)
{
GRealTree *rtree;
g_return_if_fail (tree != NULL);
rtree = (GRealTree*) tree;
if (!rtree->root)
if (!tree->root)
return;
switch (traverse_type)
{
case G_PRE_ORDER:
g_tree_node_pre_order (rtree->root, traverse_func, user_data);
g_tree_node_pre_order (tree->root, traverse_func, user_data);
break;
case G_IN_ORDER:
g_tree_node_in_order (rtree->root, traverse_func, user_data);
g_tree_node_in_order (tree->root, traverse_func, user_data);
break;
case G_POST_ORDER:
g_tree_node_post_order (rtree->root, traverse_func, user_data);
g_tree_node_post_order (tree->root, traverse_func, user_data);
break;
case G_LEVEL_ORDER:
@ -560,14 +527,10 @@ g_tree_search (GTree *tree,
GCompareFunc search_func,
gconstpointer user_data)
{
GRealTree *rtree;
g_return_val_if_fail (tree != NULL, NULL);
rtree = (GRealTree*) tree;
if (rtree->root)
return g_tree_node_search (rtree->root, search_func, user_data);
if (tree->root)
return g_tree_node_search (tree->root, search_func, user_data);
else
return NULL;
}
@ -587,14 +550,10 @@ g_tree_search (GTree *tree,
gint
g_tree_height (GTree *tree)
{
GRealTree *rtree;
g_return_val_if_fail (tree != NULL, 0);
rtree = (GRealTree*) tree;
if (rtree->root)
return g_tree_node_height (rtree->root);
if (tree->root)
return g_tree_node_height (tree->root);
else
return 0;
}
@ -610,14 +569,10 @@ g_tree_height (GTree *tree)
gint
g_tree_nnodes (GTree *tree)
{
GRealTree *rtree;
g_return_val_if_fail (tree != NULL, 0);
rtree = (GRealTree*) tree;
if (rtree->root)
return g_tree_node_count (rtree->root);
if (tree->root)
return g_tree_node_count (tree->root);
else
return 0;
}
@ -630,40 +585,37 @@ g_tree_node_insert (GTree *tree,
gboolean replace,
gboolean *inserted)
{
GRealTree *rtree;
gint old_balance;
gint cmp;
rtree = (GRealTree*) tree;
if (!node)
{
*inserted = TRUE;
return g_tree_node_new (key, value);
}
cmp = rtree->key_compare (key, node->key, rtree->key_compare_data);
cmp = tree->key_compare (key, node->key, tree->key_compare_data);
if (cmp == 0)
{
*inserted = FALSE;
if (rtree->value_destroy_func)
rtree->value_destroy_func (node->value);
if (tree->value_destroy_func)
tree->value_destroy_func (node->value);
node->value = value;
if (replace)
{
if (rtree->key_destroy_func)
rtree->key_destroy_func (node->key);
if (tree->key_destroy_func)
tree->key_destroy_func (node->key);
node->key = key;
}
else
{
/* free the passed key */
if (rtree->key_destroy_func)
rtree->key_destroy_func (key);
if (tree->key_destroy_func)
tree->key_destroy_func (key);
}
return node;
@ -725,7 +677,6 @@ g_tree_node_remove (GTree *tree,
gconstpointer key,
gboolean notify)
{
GRealTree *rtree;
GTreeNode *new_root;
gint old_balance;
gint cmp;
@ -733,9 +684,7 @@ g_tree_node_remove (GTree *tree,
if (!node)
return NULL;
rtree = (GRealTree *) tree;
cmp = rtree->key_compare (key, node->key, rtree->key_compare_data);
cmp = tree->key_compare (key, node->key, tree->key_compare_data);
if (cmp == 0)
{
GTreeNode *garbage;
@ -758,10 +707,10 @@ g_tree_node_remove (GTree *tree,
if (notify)
{
if (rtree->key_destroy_func)
rtree->key_destroy_func (garbage->key);
if (rtree->value_destroy_func)
rtree->value_destroy_func (garbage->value);
if (tree->key_destroy_func)
tree->key_destroy_func (garbage->key);
if (tree->value_destroy_func)
tree->value_destroy_func (garbage->value);
}
#ifdef ENABLE_GC_FRIENDLY