Create reentrant versions of the set functions that need it.

Only the functions that allocate or delete sets or iterators.
This commit is contained in:
Bill Currie 2013-03-18 13:20:39 +09:00
parent a28ec8aa82
commit 247f3be0c0
2 changed files with 74 additions and 19 deletions

View file

@ -96,11 +96,19 @@ typedef struct set_iter_s {
unsigned element; unsigned element;
} set_iter_t; } set_iter_t;
typedef struct set_pool_s {
set_t *set_freelist;
set_iter_t *set_iter_freelist;
} set_pool_t;
void set_pool_init (set_pool_t *set_pool);
/** Delete a set iterator that is no longer needed. /** Delete a set iterator that is no longer needed.
\param set_iter The set iterator to be deleted. \param set_iter The set iterator to be deleted.
*/ */
void set_del_iter (set_iter_t *set_iter); void set_del_iter (set_iter_t *set_iter);
void set_del_iter_r (set_pool_t *set_pool, set_iter_t *set_iter);
/** Create a new set. /** Create a new set.
@ -109,6 +117,7 @@ void set_del_iter (set_iter_t *set_iter);
\return The newly created, empty set. \return The newly created, empty set.
*/ */
set_t *set_new (void); set_t *set_new (void);
set_t *set_new_r (set_pool_t *set_pool);
/** Create a new set with space pre-allocated for the specified set size. /** Create a new set with space pre-allocated for the specified set size.
@ -122,12 +131,14 @@ set_t *set_new (void);
\return The newly created, empty set. \return The newly created, empty set.
*/ */
set_t *set_new_size (int size); set_t *set_new_size (int size);
set_t *set_new_size_r (set_pool_t *set_pool, int size);
/** Delete a set that is no longer needed. /** Delete a set that is no longer needed.
\param set The set to be deleted. \param set The set to be deleted.
*/ */
void set_delete (set_t *set); void set_delete (set_t *set);
void set_delete_r (set_pool_t *set_pool, set_t *set);
/** Add an element to a set. /** Add an element to a set.
@ -325,6 +336,7 @@ unsigned set_size (const set_t *set);
of everything. of everything.
*/ */
set_iter_t *set_first (const set_t *set); set_iter_t *set_first (const set_t *set);
set_iter_t *set_first_r (set_pool_t *set_pool, const set_t *set);
/** Find the next "member" of the set. /** Find the next "member" of the set.
@ -342,6 +354,7 @@ set_iter_t *set_first (const set_t *set);
is reached. is reached.
*/ */
set_iter_t *set_next (set_iter_t *set_iter); set_iter_t *set_next (set_iter_t *set_iter);
set_iter_t *set_next_r (set_pool_t *set_pool, set_iter_t *set_iter);
/** Return a human-readable string representing the set. /** Return a human-readable string representing the set.

View file

@ -45,46 +45,70 @@
#include "QF/mathlib.h" #include "QF/mathlib.h"
#include "QF/set.h" #include "QF/set.h"
set_t *sets_freelist; static set_pool_t static_set_pool = {0, 0};
set_iter_t *set_iters_freelist;
static set_iter_t * static set_iter_t *
new_setiter (void) new_setiter (set_pool_t *set_pool)
{ {
set_iter_t *set_iter; set_iter_t *set_iter;
ALLOC (16, set_iter_t, set_iters, set_iter); ALLOC (16, set_iter_t, set_pool->set_iter, set_iter);
return set_iter; return set_iter;
} }
static void static void
delete_setiter (set_iter_t *set_iter) delete_setiter (set_pool_t *set_pool, set_iter_t *set_iter)
{ {
FREE (set_iters, set_iter); FREE (set_pool->set_iter, set_iter);
} }
void void
set_del_iter (set_iter_t *set_iter) set_del_iter (set_iter_t *set_iter)
{ {
delete_setiter (set_iter); delete_setiter (&static_set_pool, set_iter);
} }
set_t * void
set_new (void) set_del_iter_r (set_pool_t *set_pool, set_iter_t *set_iter)
{
delete_setiter (set_pool, set_iter);
}
void
set_pool_init (set_pool_t *set_pool)
{
set_pool->set_freelist = 0;
set_pool->set_iter_freelist = 0;
}
inline set_t *
set_new_r (set_pool_t *set_pool)
{ {
set_t *set; set_t *set;
ALLOC (16, set_t, sets, set); ALLOC (16, set_t, set_pool->set, set);
set->size = sizeof (set->defmap) * 8; set->size = sizeof (set->defmap) * 8;
set->map = set->defmap; set->map = set->defmap;
return set; return set;
} }
set_t *
set_new (void)
{
return set_new_r (&static_set_pool);
}
void void
set_delete (set_t *set) set_delete_r (set_pool_t *set_pool, set_t *set)
{ {
if (set->map != set->defmap) if (set->map != set->defmap)
free (set->map); free (set->map);
FREE (sets, set); FREE (set_pool->set, set);
}
void
set_delete (set_t *set)
{
set_delete_r (&static_set_pool, set);
} }
static void static void
@ -105,17 +129,23 @@ set_expand (set_t *set, unsigned x)
free (map); free (map);
} }
set_t * inline set_t *
set_new_size (int size) set_new_size_r (set_pool_t *set_pool, int size)
{ {
set_t *set; set_t *set;
set = set_new (); set = set_new_r (set_pool);
set_expand (set, size); set_expand (set, size);
return set; return set;
} }
set_t *
set_new_size (int size)
{
return set_new_size_r (&static_set_pool, size);
}
static inline void static inline void
_set_add (set_t *set, unsigned x) _set_add (set_t *set, unsigned x)
{ {
@ -534,14 +564,14 @@ set_size (const set_t *set)
} }
set_iter_t * set_iter_t *
set_first (const set_t *set) set_first_r (set_pool_t *set_pool, const set_t *set)
{ {
unsigned x; unsigned x;
set_iter_t *set_iter; set_iter_t *set_iter;
for (x = 0; x < set->size; x++) { for (x = 0; x < set->size; x++) {
if (_set_is_member (set, x)) { if (_set_is_member (set, x)) {
set_iter = new_setiter (); set_iter = new_setiter (set_pool);
set_iter->set = set; set_iter->set = set;
set_iter->element = x; set_iter->element = x;
return set_iter; return set_iter;
@ -551,7 +581,13 @@ set_first (const set_t *set)
} }
set_iter_t * set_iter_t *
set_next (set_iter_t *set_iter) set_first (const set_t *set)
{
return set_first_r (&static_set_pool, set);
}
set_iter_t *
set_next_r (set_pool_t *set_pool, set_iter_t *set_iter)
{ {
unsigned x; unsigned x;
@ -561,10 +597,16 @@ set_next (set_iter_t *set_iter)
return set_iter; return set_iter;
} }
} }
delete_setiter (set_iter); delete_setiter (set_pool, set_iter);
return 0; return 0;
} }
set_iter_t *
set_next (set_iter_t *set_iter)
{
return set_next_r (&static_set_pool, set_iter);
}
const char * const char *
set_as_string (const set_t *set) set_as_string (const set_t *set)
{ {