diff --git a/include/QF/set.h b/include/QF/set.h index 56dc4f6a2..a30840b18 100644 --- a/include/QF/set.h +++ b/include/QF/set.h @@ -96,11 +96,19 @@ typedef struct set_iter_s { unsigned element; } 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. \param set_iter The set iterator to be deleted. */ 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. @@ -109,6 +117,7 @@ void set_del_iter (set_iter_t *set_iter); \return The newly created, empty set. */ 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. @@ -122,12 +131,14 @@ set_t *set_new (void); \return The newly created, empty set. */ 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. \param set The set to be deleted. */ void set_delete (set_t *set); +void set_delete_r (set_pool_t *set_pool, set_t *set); /** Add an element to a set. @@ -325,6 +336,7 @@ unsigned set_size (const set_t *set); of everything. */ 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. @@ -342,6 +354,7 @@ set_iter_t *set_first (const set_t *set); is reached. */ 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. diff --git a/libs/util/set.c b/libs/util/set.c index ee538c36d..4b3b3a033 100644 --- a/libs/util/set.c +++ b/libs/util/set.c @@ -45,46 +45,70 @@ #include "QF/mathlib.h" #include "QF/set.h" -set_t *sets_freelist; -set_iter_t *set_iters_freelist; +static set_pool_t static_set_pool = {0, 0}; static set_iter_t * -new_setiter (void) +new_setiter (set_pool_t *set_pool) { 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; } 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 set_del_iter (set_iter_t *set_iter) { - delete_setiter (set_iter); + delete_setiter (&static_set_pool, set_iter); } -set_t * -set_new (void) +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; - ALLOC (16, set_t, sets, set); + ALLOC (16, set_t, set_pool->set, set); set->size = sizeof (set->defmap) * 8; set->map = set->defmap; return set; } +set_t * +set_new (void) +{ + return set_new_r (&static_set_pool); +} + void -set_delete (set_t *set) +set_delete_r (set_pool_t *set_pool, set_t *set) { if (set->map != set->defmap) 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 @@ -105,17 +129,23 @@ set_expand (set_t *set, unsigned x) free (map); } -set_t * -set_new_size (int size) +inline set_t * +set_new_size_r (set_pool_t *set_pool, int size) { set_t *set; - set = set_new (); + set = set_new_r (set_pool); set_expand (set, size); return set; } +set_t * +set_new_size (int size) +{ + return set_new_size_r (&static_set_pool, size); +} + static inline void _set_add (set_t *set, unsigned x) { @@ -534,14 +564,14 @@ set_size (const set_t *set) } set_iter_t * -set_first (const set_t *set) +set_first_r (set_pool_t *set_pool, const set_t *set) { unsigned x; set_iter_t *set_iter; for (x = 0; x < set->size; x++) { if (_set_is_member (set, x)) { - set_iter = new_setiter (); + set_iter = new_setiter (set_pool); set_iter->set = set; set_iter->element = x; return set_iter; @@ -551,7 +581,13 @@ set_first (const set_t *set) } 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; @@ -561,10 +597,16 @@ set_next (set_iter_t *set_iter) return set_iter; } } - delete_setiter (set_iter); + delete_setiter (set_pool, set_iter); return 0; } +set_iter_t * +set_next (set_iter_t *set_iter) +{ + return set_next_r (&static_set_pool, set_iter); +} + const char * set_as_string (const set_t *set) {