Skip to content

Commit

Permalink
Add r_id_storage_{init/fini} ##util
Browse files Browse the repository at this point in the history
  • Loading branch information
condret committed Nov 26, 2024
1 parent a2e5c2c commit e8158ca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
2 changes: 2 additions & 0 deletions libr/include/r_util/r_idpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
44 changes: 24 additions & 20 deletions libr/util/idpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit e8158ca

Please sign in to comment.