diff --git a/libr/include/r_util/r_idpool.h b/libr/include/r_util/r_idpool.h index dcb75cff5a667..0ca1ab936c00b 100644 --- a/libr/include/r_util/r_idpool.h +++ b/libr/include/r_util/r_idpool.h @@ -33,6 +33,7 @@ typedef struct r_id_storage_t { typedef bool (*RIDStorageForeachCb)(void *user, void *data, ut32 id); typedef bool (*ROIDStorageCompareCb)(void *in, void *incoming, void *user, int *cmp_res); +R_API bool r_id_storage_init(RIDStorage *storage, ut32 start_id, ut32 last_id); R_API RIDStorage *r_id_storage_new(ut32 start_id, ut32 last_id); R_API bool r_id_storage_set(RIDStorage *storage, void *data, ut32 id); R_API bool r_id_storage_add(RIDStorage *storage, void *data, ut32 *id); @@ -42,6 +43,7 @@ R_API bool r_id_storage_get_prev(RIDStorage *storage, ut32 *id); R_API void r_id_storage_delete(RIDStorage *storage, ut32 id); R_API void *r_id_storage_take(RIDStorage *storage, ut32 id); R_API bool r_id_storage_foreach(RIDStorage *storage, RIDStorageForeachCb cb, void *user); +R_API void r_id_storage_fini(RIDStorage *storage); R_API void r_id_storage_free(RIDStorage *storage); R_API RList *r_id_storage_list(RIDStorage *s); R_API bool r_id_storage_get_lowest(RIDStorage *storage, ut32 *id); diff --git a/libr/util/idpool.c b/libr/util/idpool.c index 72d1d10d4ade3..38009014159dc 100644 --- a/libr/util/idpool.c +++ b/libr/util/idpool.c @@ -64,28 +64,29 @@ R_API bool r_id_pool_kick_id(RIDPool* pool, ut32 kick) { return true; } -R_API void r_id_pool_free(RIDPool* pool) { +R_API void r_id_pool_free(RIDPool *pool) { if (pool) { r_queue_free (pool->freed_ids); free (pool); } } -R_API RIDStorage* r_id_storage_new(ut32 start_id, ut32 last_id) { - RIDStorage* storage = NULL; - RIDPool *pool = r_id_pool_new (start_id, last_id); - if (pool) { - storage = R_NEW0 (RIDStorage); - if (!storage) { - r_id_pool_free (pool); - return NULL; - } - storage->pool = pool; +R_API bool r_id_storage_init(RIDStorage *storage, ut32 start_id, ut32 last_id) { + R_RETURN_VAL_IF_FAIL (storage, false); + storage->pool = r_id_pool_new (start_id, last_id); + return !!storage->pool; +} + +R_API RIDStorage *r_id_storage_new(ut32 start_id, ut32 last_id) { + RIDStorage *storage = R_NEW0 (RIDStorage); + if (!storage || r_id_storage_init (storage, start_id, last_id)) { + return storage; } - return storage; + free (storage); + return NULL; } -static bool id_storage_reallocate(RIDStorage* storage, ut32 size) { +static bool id_storage_reallocate(RIDStorage *storage, ut32 size) { if (!storage) { return false; } @@ -258,15 +259,18 @@ R_API bool r_id_storage_foreach(RIDStorage* storage, RIDStorageForeachCb cb, voi return true; } -R_API void r_id_storage_free(RIDStorage* storage) { - if (storage) { - r_id_pool_free (storage->pool); - free (storage->data); - free (storage); - } +R_API void r_id_storage_fini(RIDStorage *storage){ + R_RETURN_IF_FAIL (storage); + r_id_pool_free (storage->pool); + free (storage->data); +} + +R_API void r_id_storage_free(RIDStorage *storage) { + r_id_storage_fini (storage); + free (storage); } -static bool _list(void* user, void* data, ut32 id) { +static bool _list(void *user, void *data, ut32 id) { r_list_append (user, data); return true; }