Rizin
unix-like reverse engineering framework and cli tools
rz_idpool.h File Reference
#include <rz_vector.h>
#include <rz_types.h>
#include <rz_list.h>

Go to the source code of this file.

Classes

struct  rz_id_pool_t
 
struct  rz_id_storage_t
 
struct  rz_ordered_id_storage_t
 

Typedefs

typedef struct rz_id_pool_t RzIDPool
 
typedef struct rz_id_storage_t RzIDStorage
 
typedef bool(* RzIDStorageForeachCb) (void *user, void *data, ut32 id)
 
typedef bool(* ROIDStorageCompareCb) (void *in, void *incoming, void *user, int *cmp_res)
 
typedef struct rz_ordered_id_storage_t ROIDStorage
 

Functions

RZ_API RzIDPoolrz_id_pool_new (ut32 start_id, ut32 last_id)
 
RZ_API bool rz_id_pool_grab_id (RzIDPool *pool, ut32 *grabber)
 
RZ_API bool rz_id_pool_kick_id (RzIDPool *pool, ut32 kick)
 
RZ_API void rz_id_pool_free (RzIDPool *pool)
 
RZ_API RzIDStoragerz_id_storage_new (ut32 start_id, ut32 last_id)
 
RZ_API bool rz_id_storage_set (RzIDStorage *storage, void *data, ut32 id)
 
RZ_API bool rz_id_storage_add (RzIDStorage *storage, void *data, ut32 *id)
 
RZ_API void * rz_id_storage_get (RzIDStorage *storage, ut32 id)
 
RZ_API bool rz_id_storage_get_next (RzIDStorage *storage, ut32 *id)
 
RZ_API bool rz_id_storage_get_prev (RzIDStorage *storage, ut32 *id)
 
RZ_API void rz_id_storage_delete (RzIDStorage *storage, ut32 id)
 
RZ_API void * rz_id_storage_take (RzIDStorage *storage, ut32 id)
 
RZ_API bool rz_id_storage_foreach (RzIDStorage *storage, RzIDStorageForeachCb cb, void *user)
 
RZ_API void rz_id_storage_free (RzIDStorage *storage)
 
RZ_API RzListrz_id_storage_list (RzIDStorage *s)
 
RZ_API bool rz_id_storage_get_lowest (RzIDStorage *storage, ut32 *id)
 
RZ_API bool rz_id_storage_get_highest (RzIDStorage *storage, ut32 *id)
 
RZ_API ROIDStoragerz_oids_new (ut32 start_id, ut32 last_id)
 
RZ_API void * rz_oids_get (ROIDStorage *storage, ut32 id)
 
RZ_API void * rz_oids_oget (ROIDStorage *storage, ut32 od)
 
RZ_API bool rz_oids_get_id (ROIDStorage *storage, ut32 od, ut32 *id)
 
RZ_API bool rz_oids_get_od (ROIDStorage *storage, ut32 id, ut32 *od)
 
RZ_API bool rz_oids_to_front (ROIDStorage *storage, const ut32 id)
 
RZ_API bool rz_oids_to_rear (ROIDStorage *storage, const ut32 id)
 
RZ_API void rz_oids_delete (ROIDStorage *storage, ut32 id)
 
RZ_API void rz_oids_odelete (ROIDStorage *st, ut32 od)
 
RZ_API void rz_oids_free (ROIDStorage *storage)
 
RZ_API bool rz_oids_add (ROIDStorage *storage, void *data, ut32 *id, ut32 *od)
 
RZ_API void * rz_oids_take (ROIDStorage *storage, ut32 id)
 
RZ_API void * rz_oids_otake (ROIDStorage *st, ut32 od)
 
RZ_API bool rz_oids_foreach (ROIDStorage *storage, RzIDStorageForeachCb cb, void *user)
 
RZ_API bool rz_oids_foreach_prev (ROIDStorage *storage, RzIDStorageForeachCb cb, void *user)
 
RZ_API bool rz_oids_insert (ROIDStorage *storage, void *data, ut32 *id, ut32 *od, void *user)
 
RZ_API bool rz_oids_sort (ROIDStorage *storage, void *user)
 
RZ_API ut32 rz_oids_find (ROIDStorage *storage, void *incoming, void *user)
 
RZ_API void * rz_oids_last (ROIDStorage *storage)
 
RZ_API void * rz_oids_first (ROIDStorage *storage)
 

Typedef Documentation

◆ ROIDStorage

◆ ROIDStorageCompareCb

typedef bool(* ROIDStorageCompareCb) (void *in, void *incoming, void *user, int *cmp_res)

Definition at line 35 of file rz_idpool.h.

◆ RzIDPool

typedef struct rz_id_pool_t RzIDPool

◆ RzIDStorage

typedef struct rz_id_storage_t RzIDStorage

◆ RzIDStorageForeachCb

typedef bool(* RzIDStorageForeachCb) (void *user, void *data, ut32 id)

Definition at line 34 of file rz_idpool.h.

Function Documentation

◆ rz_id_pool_free()

RZ_API void rz_id_pool_free ( RzIDPool pool)

Definition at line 72 of file idpool.c.

72  {
73  if (!pool) {
74  return;
75  }
77  free(pool);
78 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API void rz_vector_free(RzVector *vec)
Definition: vector.c:75
RzVector * freed_ids
Definition: rz_idpool.h:19

References free(), rz_id_pool_t::freed_ids, and rz_vector_free().

Referenced by rz_id_storage_delete(), rz_id_storage_free(), rz_id_storage_new(), rz_io_map_fini(), and rz_io_map_init().

◆ rz_id_pool_grab_id()

RZ_API bool rz_id_pool_grab_id ( RzIDPool pool,
ut32 grabber 
)

Definition at line 34 of file idpool.c.

34  {
35  rz_return_val_if_fail(pool && grabber, false);
36 
37  *grabber = UT32_MAX;
38  if (pool->freed_ids) {
39  rz_vector_pop_front(pool->freed_ids, grabber);
40  if (rz_vector_empty(pool->freed_ids)) {
42  }
43  return true;
44  }
45  if (pool->next_id < pool->last_id) {
46  *grabber = pool->next_id;
47  pool->next_id++;
48  return true;
49  }
50  return false;
51 }
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define RZ_FREE_CUSTOM(x, y)
Definition: rz_types.h:375
#define UT32_MAX
Definition: rz_types_base.h:99
RZ_API void rz_vector_pop_front(RzVector *vec, void *into)
Definition: vector.c:192
static bool rz_vector_empty(const RzVector *vec)
Definition: rz_vector.h:74
ut32 next_id
Definition: rz_idpool.h:18
ut32 last_id
Definition: rz_idpool.h:17

References rz_id_pool_t::freed_ids, rz_id_pool_t::last_id, rz_id_pool_t::next_id, RZ_FREE_CUSTOM, rz_return_val_if_fail, rz_vector_empty(), rz_vector_free(), rz_vector_pop_front(), and UT32_MAX.

Referenced by io_map_new(), rz_bin_file_new(), rz_id_storage_add(), and rz_io_desc_new().

◆ rz_id_pool_kick_id()

RZ_API bool rz_id_pool_kick_id ( RzIDPool pool,
ut32  kick 
)

Definition at line 53 of file idpool.c.

53  {
54  if (!pool || (kick < pool->start_id) || (pool->start_id == pool->next_id)) {
55  return false;
56  }
57  if (kick == (pool->next_id - 1)) {
58  pool->next_id--;
59  return true;
60  }
61  if (!pool->freed_ids) {
62  pool->freed_ids = rz_vector_new(sizeof(ut32), NULL, NULL);
63  if (!pool->freed_ids) {
64  return false;
65  }
66  rz_vector_reserve(pool->freed_ids, 2);
67  }
68  rz_vector_push(pool->freed_ids, &kick);
69  return true;
70 }
#define NULL
Definition: cris-opc.c:27
uint32_t ut32
RZ_API void * rz_vector_reserve(RzVector *vec, size_t capacity)
Definition: vector.c:214
RZ_API void * rz_vector_push(RzVector *vec, void *x)
Definition: vector.c:197
RZ_API RzVector * rz_vector_new(size_t elem_size, RzVectorFree free, void *free_user)
Definition: vector.c:42
ut32 start_id
Definition: rz_idpool.h:16

References rz_id_pool_t::freed_ids, rz_id_pool_t::next_id, NULL, rz_vector_new(), rz_vector_push(), rz_vector_reserve(), and rz_id_pool_t::start_id.

Referenced by map_del(), rz_bin_file_free(), and rz_id_storage_delete().

◆ rz_id_pool_new()

RZ_API RzIDPool* rz_id_pool_new ( ut32  start_id,
ut32  last_id 
)

Definition at line 22 of file idpool.c.

22  {
23  RzIDPool *pool = NULL;
24  if (start_id < last_id) {
25  pool = RZ_NEW0(RzIDPool);
26  if (pool) {
27  pool->next_id = pool->start_id = start_id;
28  pool->last_id = last_id;
29  }
30  }
31  return pool;
32 }
#define RZ_NEW0(x)
Definition: rz_types.h:284

References rz_id_pool_t::last_id, rz_id_pool_t::next_id, NULL, RZ_NEW0, and rz_id_pool_t::start_id.

Referenced by rz_id_storage_delete(), rz_id_storage_new(), and rz_io_map_init().

◆ rz_id_storage_add()

RZ_API bool rz_id_storage_add ( RzIDStorage storage,
void *  data,
ut32 id 
)

Definition at line 155 of file idpool.c.

155  {
156  if (!storage || !rz_id_pool_grab_id(storage->pool, id)) {
157  return false;
158  }
159  return rz_id_storage_set(storage, data, *id);
160 }
RZ_API bool rz_id_storage_set(RzIDStorage *storage, void *data, ut32 id)
Definition: idpool.c:131
RZ_API bool rz_id_pool_grab_id(RzIDPool *pool, ut32 *grabber)
Definition: idpool.c:34
RzIDPool * pool
Definition: rz_idpool.h:28

References rz_id_storage_t::pool, rz_id_pool_grab_id(), and rz_id_storage_set().

Referenced by parse_namemap(), rz_analysis_esil_load_source(), rz_oids_add(), and rz_oids_insert().

◆ rz_id_storage_delete()

RZ_API void rz_id_storage_delete ( RzIDStorage storage,
ut32  id 
)

Definition at line 221 of file idpool.c.

221  {
222  if (!storage || !storage->data || (storage->size <= id)) {
223  return;
224  }
225  storage->data[id] = NULL;
226  if (id == storage->top_id) {
227  while (storage->top_id && !storage->data[storage->top_id]) {
228  storage->top_id--;
229  }
230  if (!storage->top_id) {
231  if (storage->data[storage->top_id]) {
232  id_storage_reallocate(storage, 2);
233  } else {
234  RzIDPool *pool = rz_id_pool_new(storage->pool->start_id, storage->pool->last_id);
235  RZ_FREE(storage->data);
236  storage->size = 0;
237  rz_id_pool_free(storage->pool);
238  storage->pool = pool;
239  return;
240  }
241  } else if ((storage->top_id + 1) < (storage->size / 4)) {
242  id_storage_reallocate(storage, storage->size / 2);
243  }
244  }
245  rz_id_pool_kick_id(storage->pool, id);
246 }
RZ_API RzIDPool * rz_id_pool_new(ut32 start_id, ut32 last_id)
Definition: idpool.c:22
static bool id_storage_reallocate(RzIDStorage *storage, ut32 size)
Definition: idpool.c:94
RZ_API void rz_id_pool_free(RzIDPool *pool)
Definition: idpool.c:72
RZ_API bool rz_id_pool_kick_id(RzIDPool *pool, ut32 kick)
Definition: idpool.c:53
int id
Definition: op.c:540
#define RZ_FREE(x)
Definition: rz_types.h:369
void ** data
Definition: rz_idpool.h:29

References rz_id_storage_t::data, id, id_storage_reallocate(), rz_id_pool_t::last_id, NULL, rz_id_storage_t::pool, RZ_FREE, rz_id_pool_free(), rz_id_pool_kick_id(), rz_id_pool_new(), rz_id_storage_t::size, rz_id_pool_t::start_id, and rz_id_storage_t::top_id.

Referenced by rz_analysis_esil_release_source(), rz_bin_file_close(), rz_id_storage_take(), rz_io_desc_free(), rz_oids_add(), rz_oids_delete(), and rz_oids_odelete().

◆ rz_id_storage_foreach()

RZ_API bool rz_id_storage_foreach ( RzIDStorage storage,
RzIDStorageForeachCb  cb,
void *  user 
)

Definition at line 254 of file idpool.c.

254  {
255  ut32 i;
256  if (!cb || !storage || !storage->data) {
257  return false;
258  }
259  for (i = 0; i < storage->top_id; i++) {
260  if (storage->data[i] && !cb(user, storage->data[i], i)) {
261  return false;
262  }
263  }
264  if (storage->data[i]) {
265  return cb(user, storage->data[i], i);
266  }
267  return true;
268 }
lzma_index ** i
Definition: index.h:629
static const char * cb[]
Definition: z80_tab.h:176

References cb, rz_id_storage_t::data, i, and rz_id_storage_t::top_id.

Referenced by file_is_loaded(), rz_analysis_esil_sources_fini(), rz_core_bin_load(), rz_core_file_close_all_but(), rz_core_file_reopen_in_malloc(), rz_id_storage_list(), rz_io_desc_cache_fini_all(), rz_io_desc_fini(), rz_open_list_ascii_handler(), rz_open_list_handler(), and rz_serialize_io_files_save().

◆ rz_id_storage_free()

RZ_API void rz_id_storage_free ( RzIDStorage storage)

Definition at line 270 of file idpool.c.

270  {
271  if (storage) {
272  rz_id_pool_free(storage->pool);
273  free(storage->data);
274  }
275  free(storage);
276 }

References rz_id_storage_t::data, free(), rz_id_storage_t::pool, and rz_id_pool_free().

Referenced by parse_custom_name_entry(), rz_analysis_esil_sources_fini(), rz_bin_free(), rz_bin_new(), rz_bin_wasm_destroy(), rz_io_desc_fini(), and rz_oids_free().

◆ rz_id_storage_get()

RZ_API void* rz_id_storage_get ( RzIDStorage storage,
ut32  id 
)

Definition at line 162 of file idpool.c.

162  {
163  if (!storage || !storage->data || (storage->size <= id)) {
164  return NULL;
165  }
166  return storage->data[id];
167 }

References rz_id_storage_t::data, id, NULL, and rz_id_storage_t::size.

Referenced by _get_source(), rz_bin_wasm_get_function_name(), rz_id_storage_take(), rz_io_desc_del(), rz_io_desc_get(), rz_io_desc_get_next(), rz_io_desc_get_prev(), rz_oids_get(), rz_oids_oget(), rz_oids_sort(), and rz_oids_take().

◆ rz_id_storage_get_highest()

RZ_API bool rz_id_storage_get_highest ( RzIDStorage storage,
ut32 id 
)

Definition at line 178 of file idpool.c.

178  {
179  rz_return_val_if_fail(storage, false);
180  size_t i = 0;
181  if (storage->size > 0) {
182  for (i = storage->size - 1; !storage->data[i] && i > 0; i--)
183  ;
184  *id = i;
185  return storage->data[i] != NULL;
186  }
187  // *id = i;
188  return false;
189 }

References rz_id_storage_t::data, i, NULL, rz_return_val_if_fail, and rz_id_storage_t::size.

Referenced by rz_io_fd_get_highest().

◆ rz_id_storage_get_lowest()

RZ_API bool rz_id_storage_get_lowest ( RzIDStorage storage,
ut32 id 
)

Definition at line 169 of file idpool.c.

169  {
170  rz_return_val_if_fail(storage, false);
171  ut32 i;
172  for (i = 0; i < storage->size && !storage->data[i]; i++)
173  ;
174  *id = i;
175  return i < storage->size;
176 }

References rz_id_storage_t::data, i, rz_return_val_if_fail, and rz_id_storage_t::size.

Referenced by rz_io_fd_get_lowest().

◆ rz_id_storage_get_next()

RZ_API bool rz_id_storage_get_next ( RzIDStorage storage,
ut32 id 
)

Definition at line 191 of file idpool.c.

191  {
192  rz_return_val_if_fail(idref && storage, false);
193  ut32 id = *idref;
194  if (storage->size < 1 || id >= storage->size || !storage->data) {
195  return false;
196  }
197  for (id = *idref + 1; id < storage->size && !storage->data[id]; id++)
198  ;
199  if (id < storage->size) {
200  *idref = id;
201  return true;
202  }
203  return false;
204 }
voidpf void uLong size
Definition: ioapi.h:138

References rz_id_storage_t::data, id, rz_return_val_if_fail, and rz_id_storage_t::size.

Referenced by rz_io_fd_get_next().

◆ rz_id_storage_get_prev()

RZ_API bool rz_id_storage_get_prev ( RzIDStorage storage,
ut32 id 
)

Definition at line 206 of file idpool.c.

206  {
207  rz_return_val_if_fail(idref && storage, false);
208  ut32 id = *idref;
209  if (id == 0 || id >= storage->size || storage->size < 1 || !storage->data) {
210  return false;
211  }
212  for (id = *idref - 1; id > 0 && !storage->data[id]; id--)
213  ;
214  if (storage->data[id]) {
215  *idref = id;
216  return true;
217  }
218  return false;
219 }

References rz_id_storage_t::data, id, rz_return_val_if_fail, and rz_id_storage_t::size.

Referenced by rz_io_fd_get_prev().

◆ rz_id_storage_list()

RZ_API RzList* rz_id_storage_list ( RzIDStorage s)

Definition at line 283 of file idpool.c.

283  { // remove this pls
286  return list;
287 }
static bool _list(void *user, void *data, ut32 id)
Definition: idpool.c:278
RZ_API bool rz_id_storage_foreach(RzIDStorage *storage, RzIDStorageForeachCb cb, void *user)
Definition: idpool.c:254
static void list(RzEgg *egg)
Definition: rz-gg.c:52
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
static RzSocket * s
Definition: rtr.c:28

References _list(), list(), NULL, rz_id_storage_foreach(), rz_list_newf(), and s.

Referenced by __file_history_down(), __file_history_up(), rz_core_vfile_bin_file_deleted(), and rz_open_binary_file_handler().

◆ rz_id_storage_new()

RZ_API RzIDStorage* rz_id_storage_new ( ut32  start_id,
ut32  last_id 
)

Definition at line 80 of file idpool.c.

80  {
81  RzIDStorage *storage = NULL;
82  RzIDPool *pool = rz_id_pool_new(start_id, last_id);
83  if (pool) {
84  storage = RZ_NEW0(RzIDStorage);
85  if (!storage) {
86  rz_id_pool_free(pool);
87  return NULL;
88  }
89  storage->pool = pool;
90  }
91  return storage;
92 }

References NULL, rz_id_storage_t::pool, rz_id_pool_free(), rz_id_pool_new(), and RZ_NEW0.

Referenced by parse_custom_name_entry(), rz_analysis_esil_sources_init(), rz_bin_new(), rz_io_desc_init(), and rz_oids_new().

◆ rz_id_storage_set()

RZ_API bool rz_id_storage_set ( RzIDStorage storage,
void *  data,
ut32  id 
)

Definition at line 131 of file idpool.c.

131  {
132  ut32 n;
133  if (!storage || !storage->pool || (id >= storage->pool->next_id)) {
134  return false;
135  }
136  n = get_msb(id + 1);
137  if (n > ((storage->size / 2) + (storage->size / 4))) {
138  if ((n * 2) < storage->pool->last_id) {
139  if (!id_storage_reallocate(storage, n * 2)) {
140  return false;
141  }
142  } else if (n != (storage->pool->last_id)) {
143  if (!id_storage_reallocate(storage, storage->pool->last_id)) {
144  return false;
145  }
146  }
147  }
148  storage->data[id] = data;
149  if (id > storage->top_id) {
150  storage->top_id = id;
151  }
152  return true;
153 }
static ut32 get_msb(ut32 v)
Definition: idpool.c:12
int n
Definition: mipsasm.c:19

References rz_id_storage_t::data, get_msb(), id, id_storage_reallocate(), rz_id_pool_t::last_id, n, rz_id_pool_t::next_id, rz_id_storage_t::pool, rz_id_storage_t::size, and rz_id_storage_t::top_id.

Referenced by rz_bin_open_buf(), rz_id_storage_add(), rz_io_desc_add(), and rz_io_desc_exchange().

◆ rz_id_storage_take()

RZ_API void* rz_id_storage_take ( RzIDStorage storage,
ut32  id 
)

Definition at line 248 of file idpool.c.

248  {
249  void *ret = rz_id_storage_get(storage, id);
250  rz_id_storage_delete(storage, id);
251  return ret;
252 }
RZ_API void * rz_id_storage_get(RzIDStorage *storage, ut32 id)
Definition: idpool.c:162
RZ_API void rz_id_storage_delete(RzIDStorage *storage, ut32 id)
Definition: idpool.c:221

References rz_id_storage_delete(), and rz_id_storage_get().

Referenced by rz_bin_file_close().

◆ rz_oids_add()

RZ_API bool rz_oids_add ( ROIDStorage storage,
void *  data,
ut32 id,
ut32 od 
)

Definition at line 336 of file idpool.c.

336  {
337  if (!storage || !id || !od) {
338  return false;
339  }
340  if (!rz_id_storage_add(storage->data, data, id)) {
341  return false;
342  }
343  if (!storage->permutation) {
344  oid_storage_preallocate(storage, 4);
345  } else if (storage->ptop > (storage->psize * 3 / 4)) {
346  oid_storage_preallocate(storage, storage->psize * 2);
347  }
348  if (storage->psize <= storage->ptop) {
349  rz_id_storage_delete(storage->data, *id);
350  return false;
351  }
352  if (!storage->permutation) {
353  return false;
354  }
355  *od = storage->ptop;
356  storage->permutation[*od] = *id;
357  storage->ptop++;
358  return true;
359 }
static bool oid_storage_preallocate(ROIDStorage *st, ut32 size)
Definition: idpool.c:110
RZ_API bool rz_id_storage_add(RzIDStorage *storage, void *data, ut32 *id)
Definition: idpool.c:155
RzIDStorage * data
Definition: rz_idpool.h:55

References rz_ordered_id_storage_t::data, id, oid_storage_preallocate(), rz_ordered_id_storage_t::permutation, rz_ordered_id_storage_t::psize, rz_ordered_id_storage_t::ptop, rz_id_storage_add(), and rz_id_storage_delete().

Referenced by rz_oids_insert().

◆ rz_oids_delete()

RZ_API void rz_oids_delete ( ROIDStorage storage,
ut32  id 
)

Definition at line 406 of file idpool.c.

406  {
407  if (!rz_oids_to_front(storage, id)) {
408  return;
409  }
410  rz_id_storage_delete(storage->data, id);
411  storage->ptop--;
412  if (!storage->ptop) {
413  RZ_FREE(storage->permutation);
414  storage->psize = 0;
415  } else if ((storage->ptop + 1) < (storage->psize / 4)) {
416  oid_storage_preallocate(storage, storage->psize / 2);
417  }
418 }
RZ_API bool rz_oids_to_front(ROIDStorage *storage, const ut32 id)
Definition: idpool.c:361

References rz_ordered_id_storage_t::data, oid_storage_preallocate(), rz_ordered_id_storage_t::permutation, rz_ordered_id_storage_t::psize, rz_ordered_id_storage_t::ptop, RZ_FREE, rz_id_storage_delete(), and rz_oids_to_front().

Referenced by rz_oids_take().

◆ rz_oids_find()

RZ_API ut32 rz_oids_find ( ROIDStorage storage,
void *  incoming,
void *  user 
)

Definition at line 614 of file idpool.c.

614  {
615  ut32 ret;
616  return oids_od_bfind(storage, &ret, incoming, user) ? ret : storage->ptop;
617 }
bool oids_od_bfind(ROIDStorage *st, ut32 *od, void *incoming, void *user)
Definition: idpool.c:505

References oids_od_bfind(), and rz_ordered_id_storage_t::ptop.

◆ rz_oids_first()

RZ_API void* rz_oids_first ( ROIDStorage storage)

Definition at line 467 of file idpool.c.

467  {
468  if (storage && storage->data && storage->data->data && storage->permutation) {
469  return storage->data->data[storage->permutation[storage->ptop - 1]];
470  }
471  return NULL;
472 }

References rz_id_storage_t::data, rz_ordered_id_storage_t::data, NULL, rz_ordered_id_storage_t::permutation, and rz_ordered_id_storage_t::ptop.

◆ rz_oids_foreach()

RZ_API bool rz_oids_foreach ( ROIDStorage storage,
RzIDStorageForeachCb  cb,
void *  user 
)

Definition at line 474 of file idpool.c.

474  {
475  ut32 i;
476  ut32 id;
477  if (!cb || !storage || !storage->data || !storage->data->data || !storage->permutation) {
478  return false;
479  }
480  for (i = storage->ptop - 1; i != 0; i--) {
481  id = storage->permutation[i];
482  if (!cb(user, storage->data->data[id], id)) {
483  return false;
484  }
485  }
486  id = storage->permutation[0];
487  return cb(user, storage->data->data[id], id);
488 }

References cb, rz_id_storage_t::data, rz_ordered_id_storage_t::data, i, id, rz_ordered_id_storage_t::permutation, and rz_ordered_id_storage_t::ptop.

◆ rz_oids_foreach_prev()

RZ_API bool rz_oids_foreach_prev ( ROIDStorage storage,
RzIDStorageForeachCb  cb,
void *  user 
)

Definition at line 490 of file idpool.c.

490  {
491  ut32 i;
492  ut32 id;
493  if (!cb || !storage || !storage->data || !storage->data->data || !storage->permutation) {
494  return false;
495  }
496  for (i = 0; i < storage->ptop; i++) {
497  id = storage->permutation[i];
498  if (!cb(user, storage->data->data[id], id)) {
499  return false;
500  }
501  }
502  return true;
503 }

References cb, rz_id_storage_t::data, rz_ordered_id_storage_t::data, i, id, rz_ordered_id_storage_t::permutation, and rz_ordered_id_storage_t::ptop.

◆ rz_oids_free()

RZ_API void rz_oids_free ( ROIDStorage storage)

Definition at line 450 of file idpool.c.

450  {
451  if (storage) {
452  free(storage->permutation);
453  rz_id_storage_free(storage->data);
454  }
455  free(storage);
456 }
RZ_API void rz_id_storage_free(RzIDStorage *storage)
Definition: idpool.c:270

References rz_ordered_id_storage_t::data, free(), rz_ordered_id_storage_t::permutation, and rz_id_storage_free().

◆ rz_oids_get()

RZ_API void* rz_oids_get ( ROIDStorage storage,
ut32  id 
)

Definition at line 301 of file idpool.c.

301  {
302  if (storage) {
303  return rz_id_storage_get(storage->data, id);
304  }
305  return NULL;
306 }

References rz_ordered_id_storage_t::data, NULL, and rz_id_storage_get().

◆ rz_oids_get_id()

RZ_API bool rz_oids_get_id ( ROIDStorage storage,
ut32  od,
ut32 id 
)

Definition at line 316 of file idpool.c.

316  {
317  if (storage && storage->permutation && (storage->ptop > od)) {
318  *id = storage->permutation[od];
319  return true;
320  }
321  return false;
322 }

References rz_ordered_id_storage_t::permutation, and rz_ordered_id_storage_t::ptop.

Referenced by rz_oids_oget().

◆ rz_oids_get_od()

RZ_API bool rz_oids_get_od ( ROIDStorage storage,
ut32  id,
ut32 od 
)

Definition at line 324 of file idpool.c.

324  {
325  if (storage && storage->permutation &&
326  storage->data && (id < storage->data->pool->next_id)) {
327  for (od[0] = 0; od[0] < storage->ptop; od[0]++) {
328  if (id == storage->permutation[od[0]]) {
329  return true;
330  }
331  }
332  }
333  return false;
334 }

References rz_ordered_id_storage_t::data, rz_ordered_id_storage_t::permutation, and rz_ordered_id_storage_t::ptop.

◆ rz_oids_insert()

RZ_API bool rz_oids_insert ( ROIDStorage storage,
void *  data,
ut32 id,
ut32 od,
void *  user 
)

Definition at line 563 of file idpool.c.

563  {
564  if (!storage || !storage->cmp || !id || !od) {
565  return false;
566  }
567  if (!storage->ptop) { // empty storage
568  return rz_oids_add(storage, data, id, od);
569  }
570  if (!rz_id_storage_add(storage->data, data, id)) {
571  return false;
572  }
573  if (storage->ptop > (storage->psize * 3 / 4)) {
574  oid_storage_preallocate(storage, storage->psize * 2);
575  }
576  return oids_od_binsert(storage, id[0], od, data, user);
577 }
bool oids_od_binsert(ROIDStorage *storage, ut32 id, ut32 *od, void *incoming, void *user)
Definition: idpool.c:551
RZ_API bool rz_oids_add(ROIDStorage *storage, void *data, ut32 *id, ut32 *od)
Definition: idpool.c:336
ROIDStorageCompareCb cmp
Definition: rz_idpool.h:56

References rz_ordered_id_storage_t::cmp, rz_ordered_id_storage_t::data, oid_storage_preallocate(), oids_od_binsert(), rz_ordered_id_storage_t::psize, rz_ordered_id_storage_t::ptop, rz_id_storage_add(), and rz_oids_add().

◆ rz_oids_last()

RZ_API void* rz_oids_last ( ROIDStorage storage)

Definition at line 459 of file idpool.c.

459  {
460  if (storage && storage->data && storage->data->data && storage->permutation) {
461  return storage->data->data[storage->permutation[0]];
462  }
463  return NULL;
464 }

References rz_id_storage_t::data, rz_ordered_id_storage_t::data, NULL, and rz_ordered_id_storage_t::permutation.

◆ rz_oids_new()

RZ_API ROIDStorage* rz_oids_new ( ut32  start_id,
ut32  last_id 
)

Definition at line 289 of file idpool.c.

289  {
290  ROIDStorage *storage = RZ_NEW0(ROIDStorage);
291  if (!storage) {
292  return NULL;
293  }
294  if (!(storage->data = rz_id_storage_new(start_id, last_id))) {
295  free(storage);
296  return NULL;
297  }
298  return storage;
299 }
RZ_API RzIDStorage * rz_id_storage_new(ut32 start_id, ut32 last_id)
Definition: idpool.c:80

References rz_ordered_id_storage_t::data, free(), NULL, rz_id_storage_new(), and RZ_NEW0.

◆ rz_oids_odelete()

RZ_API void rz_oids_odelete ( ROIDStorage st,
ut32  od 
)

Definition at line 420 of file idpool.c.

420  {
421  ut32 n;
422  if (!st || !st->permutation || od >= st->ptop) {
423  return;
424  }
425  n = st->ptop - od - 1;
426  rz_id_storage_delete(st->data, st->permutation[od]);
427  memmove(&st->permutation[od], &st->permutation[od + 1], n * sizeof(ut32));
428  st->ptop--;
429  if (!st->ptop) {
430  RZ_FREE(st->permutation);
431  st->psize = 0;
432  } else if ((st->ptop + 1) < (st->psize / 4)) {
433  oid_storage_preallocate(st, st->psize / 2);
434  }
435 }

References rz_ordered_id_storage_t::data, n, oid_storage_preallocate(), rz_ordered_id_storage_t::permutation, rz_ordered_id_storage_t::psize, rz_ordered_id_storage_t::ptop, RZ_FREE, and rz_id_storage_delete().

Referenced by rz_oids_otake().

◆ rz_oids_oget()

RZ_API void* rz_oids_oget ( ROIDStorage storage,
ut32  od 
)

Definition at line 308 of file idpool.c.

308  {
309  ut32 id;
310  if (rz_oids_get_id(storage, od, &id)) {
311  return rz_id_storage_get(storage->data, id);
312  }
313  return NULL;
314 }
RZ_API bool rz_oids_get_id(ROIDStorage *storage, ut32 od, ut32 *id)
Definition: idpool.c:316

References rz_ordered_id_storage_t::data, id, NULL, rz_id_storage_get(), and rz_oids_get_id().

Referenced by oids_od_bfind(), and rz_oids_otake().

◆ rz_oids_otake()

RZ_API void* rz_oids_otake ( ROIDStorage st,
ut32  od 
)

Definition at line 444 of file idpool.c.

444  {
445  void *ret = rz_oids_oget(st, od);
446  rz_oids_odelete(st, od);
447  return ret;
448 }
RZ_API void * rz_oids_oget(ROIDStorage *storage, ut32 od)
Definition: idpool.c:308
RZ_API void rz_oids_odelete(ROIDStorage *st, ut32 od)
Definition: idpool.c:420

References rz_oids_odelete(), and rz_oids_oget().

◆ rz_oids_sort()

RZ_API bool rz_oids_sort ( ROIDStorage storage,
void *  user 
)

Definition at line 579 of file idpool.c.

579  {
580  ut32 od, id, ptop, *permutation;
581 
582  if (!storage || !storage->ptop || !storage->cmp) {
583  return false;
584  }
585  if (storage->ptop == 1) {
586  return true;
587  }
588  permutation = storage->permutation;
589  storage->permutation = RZ_NEWS0(ut32, storage->psize);
590  if (!storage->permutation) {
591  storage->permutation = permutation;
592  return false;
593  }
594  storage->permutation[0] = permutation[0];
595  ptop = storage->ptop;
596  storage->ptop = 1;
597  while (storage->ptop != ptop) {
598  id = permutation[storage->ptop];
599  void *incoming = rz_id_storage_get(storage->data, id);
600  if (!oids_od_binsert(storage, id, &od, incoming, user)) {
601  goto beach;
602  }
603  }
604  free(permutation);
605  return true;
606 
607 beach:
608  free(storage->permutation);
609  storage->permutation = permutation;
610  storage->ptop = ptop;
611  return false;
612 }
#define RZ_NEWS0(x, y)
Definition: rz_types.h:282

References rz_ordered_id_storage_t::cmp, rz_ordered_id_storage_t::data, free(), id, oids_od_binsert(), rz_ordered_id_storage_t::permutation, rz_ordered_id_storage_t::psize, rz_ordered_id_storage_t::ptop, rz_id_storage_get(), and RZ_NEWS0.

◆ rz_oids_take()

RZ_API void* rz_oids_take ( ROIDStorage storage,
ut32  id 
)

Definition at line 437 of file idpool.c.

437  {
438  rz_return_val_if_fail(storage, NULL);
439  void *ret = rz_id_storage_get(storage->data, id);
440  rz_oids_delete(storage, id);
441  return ret;
442 }
RZ_API void rz_oids_delete(ROIDStorage *storage, ut32 id)
Definition: idpool.c:406

References rz_ordered_id_storage_t::data, NULL, rz_id_storage_get(), rz_oids_delete(), and rz_return_val_if_fail.

◆ rz_oids_to_front()

RZ_API bool rz_oids_to_front ( ROIDStorage storage,
const ut32  id 
)

Definition at line 361 of file idpool.c.

361  {
362  ut32 od;
363  if (!storage || !storage->permutation) {
364  return false;
365  }
366  for (od = 0; od < storage->ptop; od++) {
367  if (id == storage->permutation[od]) {
368  break;
369  }
370  }
371  if (od == storage->ptop) {
372  return false;
373  } else if (od == (storage->ptop - 1)) {
374  return true;
375  }
376  memmove(&storage->permutation[od], &storage->permutation[od + 1],
377  (storage->ptop - od - 1) * sizeof(ut32));
378  storage->permutation[storage->ptop - 1] = id;
379  return true;
380 }

References id, rz_ordered_id_storage_t::permutation, and rz_ordered_id_storage_t::ptop.

Referenced by rz_oids_delete().

◆ rz_oids_to_rear()

RZ_API bool rz_oids_to_rear ( ROIDStorage storage,
const ut32  id 
)

Definition at line 382 of file idpool.c.

382  {
383  ut32 od;
384  if (!storage || !storage->permutation ||
385  !storage->data || (id >= storage->data->pool->next_id)) {
386  return false;
387  }
388  bool found = false;
389  for (od = 0; od < storage->ptop; od++) {
390  if (id == storage->permutation[od]) {
391  found = true;
392  break;
393  }
394  }
395  if (od == storage->ptop) {
396  return false;
397  }
398  if (!found) {
399  return true;
400  }
401  memmove(&storage->permutation[1], &storage->permutation[0], od * sizeof(ut32));
402  storage->permutation[0] = id;
403  return true;
404 }
RZ_API const KEY_TYPE bool * found
Definition: ht_inc.h:130

References rz_ordered_id_storage_t::data, found, id, rz_id_pool_t::next_id, rz_ordered_id_storage_t::permutation, rz_id_storage_t::pool, and rz_ordered_id_storage_t::ptop.