Rizin
unix-like reverse engineering framework and cli tools
ls.h File Reference
#include <stdio.h>
#include <rz_types.h>

Go to the source code of this file.

Classes

struct  ls_iter_t
 
struct  ls_t
 

Macros

#define ls_foreach(list, it, pos)
 
#define ls_foreach_safe(list, it, tmp, pos)
 
#define ls_foreach_prev(list, it, pos)
 
#define ls_iterator(x)   (x) ? (x)->head : NULL
 
#define ls_empty(x)   (!x || !x->length)
 
#define ls_head(x)   x->head
 
#define ls_tail(x)   x->tail
 
#define ls_unref(x)   x
 
#define ls_iter_get(x)
 
#define ls_iter_next(x)   (x ? 1 : 0)
 
#define ls_iter_cur(x)   x->p
 
#define ls_iter_unref(x)   x
 
#define ls_length(x)   x->length
 
#define ls_push(x, y)   ls_append(x, y)
 

Typedefs

typedef void(* SdbListFree) (void *ptr)
 
typedef int(* SdbListComparator) (const void *a, const void *b)
 
typedef struct ls_iter_t SdbListIter
 
typedef struct ls_t SdbList
 

Functions

RZ_API SdbListls_new (void)
 
RZ_API SdbListls_newf (SdbListFree freefn)
 
RZ_API SdbListIterls_append (SdbList *list, void *data)
 
RZ_API SdbListIterls_prepend (SdbList *list, void *data)
 
RZ_API bool ls_sort (SdbList *list, SdbListComparator cmp)
 
RZ_API bool ls_merge_sort (SdbList *list, SdbListComparator cmp)
 
RZ_API void ls_delete (SdbList *list, SdbListIter *iter)
 
RZ_API bool ls_delete_data (SdbList *list, void *ptr)
 
RZ_API void ls_iter_init (SdbListIter *iter, SdbList *list)
 
RZ_API void ls_destroy (SdbList *list)
 
RZ_API void ls_free (SdbList *list)
 
RZ_API SdbListIterls_item_new (void *data)
 
RZ_API void ls_unlink (SdbList *list, void *ptr)
 
RZ_API void ls_split (SdbList *list, void *ptr)
 
RZ_API void ls_split_iter (SdbList *list, SdbListIter *iter)
 
RZ_API void * ls_get_n (SdbList *list, int n)
 
RZ_API void * ls_get_top (SdbList *list)
 
RZ_API void * ls_pop (SdbList *list)
 
RZ_API void ls_reverse (SdbList *list)
 
RZ_API SdbListls_clone (SdbList *list)
 
RZ_API int ls_join (SdbList *first, SdbList *second)
 
RZ_API int ls_del_n (SdbList *list, int n)
 
RZ_API SdbListIterls_insert (SdbList *list, int n, void *data)
 
RZ_API void * ls_pop_head (SdbList *list)
 

Macro Definition Documentation

◆ ls_empty

#define ls_empty (   x)    (!x || !x->length)

Definition at line 44 of file ls.h.

◆ ls_foreach

#define ls_foreach (   list,
  it,
  pos 
)
Value:
if ((list)) \
for (it = (list)->head; it && (pos = it->data); it = it->n)
static void list(RzEgg *egg)
Definition: rz-gg.c:52
int pos
Definition: main.c:11

Definition at line 31 of file ls.h.

◆ ls_foreach_prev

#define ls_foreach_prev (   list,
  it,
  pos 
)
Value:
if ((list)) \
for (it = list->tail; it && (pos = it->data); it = it->p)

Definition at line 38 of file ls.h.

◆ ls_foreach_safe

#define ls_foreach_safe (   list,
  it,
  tmp,
  pos 
)
Value:
if ((list)) \
for (it = list->head; \
it && (pos = it->data) && ((tmp = it->n) || 1); it = tmp)

Definition at line 34 of file ls.h.

◆ ls_head

#define ls_head (   x)    x->head

Definition at line 45 of file ls.h.

◆ ls_iter_cur

#define ls_iter_cur (   x)    x->p

Definition at line 52 of file ls.h.

◆ ls_iter_get

#define ls_iter_get (   x)
Value:
x->data; \
x = x->n
int x
Definition: mipsasm.c:20

Definition at line 48 of file ls.h.

◆ ls_iter_next

#define ls_iter_next (   x)    (x ? 1 : 0)

Definition at line 51 of file ls.h.

◆ ls_iter_unref

#define ls_iter_unref (   x)    x

Definition at line 53 of file ls.h.

◆ ls_iterator

#define ls_iterator (   x)    (x) ? (x)->head : NULL

Definition at line 42 of file ls.h.

◆ ls_length

#define ls_length (   x)    x->length

Definition at line 54 of file ls.h.

◆ ls_push

#define ls_push (   x,
 
)    ls_append(x, y)

Definition at line 75 of file ls.h.

◆ ls_tail

#define ls_tail (   x)    x->tail

Definition at line 46 of file ls.h.

◆ ls_unref

#define ls_unref (   x)    x

Definition at line 47 of file ls.h.

Typedef Documentation

◆ SdbList

typedef struct ls_t SdbList

◆ SdbListComparator

typedef int(* SdbListComparator) (const void *a, const void *b)

Definition at line 15 of file ls.h.

◆ SdbListFree

typedef void(* SdbListFree) (void *ptr)

Definition at line 14 of file ls.h.

◆ SdbListIter

typedef struct ls_iter_t SdbListIter

Function Documentation

◆ ls_append()

RZ_API SdbListIter* ls_append ( SdbList list,
void *  data 
)

Definition at line 200 of file ls.c.

200  {
201  SdbListIter *it;
202  if (!list) {
203  return NULL;
204  }
205  it = RZ_NEW(SdbListIter);
206  if (!it) {
207  return NULL;
208  }
209  if (list->tail) {
210  list->tail->n = it;
211  }
212  it->data = data;
213  it->p = list->tail;
214  it->n = NULL;
215  list->tail = it;
216  if (!list->head) {
217  list->head = it;
218  }
219  list->length++;
220  list->sorted = false;
221  return it;
222 }
#define NULL
Definition: cris-opc.c:27
#define RZ_NEW(x)
Definition: rz_types.h:285
Definition: ls.h:17
struct ls_iter_t * n
Definition: ls.h:19
void * data
Definition: ls.h:18
struct ls_iter_t * p
Definition: ls.h:19

References ls_iter_t::data, list(), ls_iter_t::n, NULL, ls_iter_t::p, and RZ_NEW.

Referenced by ls_clone(), ls_insert(), ns_free_exc_list(), ns_sync(), sdb_foreach_list_cb(), sdb_foreach_list_filter_cb(), sdb_foreach_match_cb(), sdb_hook(), sdb_ns(), and sdb_ns_set().

◆ ls_clone()

RZ_API SdbList* ls_clone ( SdbList list)

Definition at line 265 of file ls.c.

265  {
266  if (!list) {
267  return NULL;
268  }
269  SdbList *r = ls_new(); // ownership of elements stays in original list
270  if (!r) {
271  return NULL;
272  }
273  void *v;
274  SdbListIter *iter;
275  ls_foreach (list, iter, v) {
276  ls_append(r, v);
277  }
278  return r;
279 }
#define r
Definition: crypto_rc6.c:12
const char * v
Definition: dsignal.c:12
RZ_API SdbList * ls_new(void)
Definition: ls.c:16
RZ_API SdbListIter * ls_append(SdbList *list, void *data)
Definition: ls.c:200
#define ls_foreach(list, it, pos)
Definition: ls.h:31
Definition: ls.h:22

References list(), ls_append(), ls_foreach, ls_new(), NULL, r, and v.

Referenced by text_save().

◆ ls_del_n()

RZ_API int ls_del_n ( SdbList list,
int  n 
)

Definition at line 353 of file ls.c.

353  {
354  SdbListIter *it;
355  int i;
356  if (!list) {
357  return false;
358  }
359  for (it = list->head, i = 0; it && it->data; it = it->n, i++)
360  if (i == n) {
361  if (!it->p && !it->n) {
362  list->head = list->tail = NULL;
363  } else if (!it->p) {
364  it->n->p = NULL;
365  list->head = it->n;
366  } else if (!it->n) {
367  it->p->n = NULL;
368  list->tail = it->p;
369  } else {
370  it->p->n = it->n;
371  it->n->p = it->p;
372  }
373  free(it);
374  list->length--;
375  return true;
376  }
377  return false;
378 }
lzma_index ** i
Definition: index.h:629
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
int n
Definition: mipsasm.c:19

References ls_iter_t::data, free(), i, list(), n, ls_iter_t::n, NULL, and ls_iter_t::p.

◆ ls_delete()

RZ_API void ls_delete ( SdbList list,
SdbListIter iter 
)

Definition at line 133 of file ls.c.

133  {
134  if (!list || !iter) {
135  return;
136  }
138  if (list->free && iter->data) {
139  list->free(iter->data);
140  iter->data = NULL;
141  }
142  free(iter);
143 }
RZ_API void ls_split_iter(SdbList *list, SdbListIter *iter)
Definition: ls.c:157

References free(), list(), ls_split_iter(), and NULL.

Referenced by ls_delete_data(), ls_destroy(), ns_free_exc_list(), sdb_ns_unset(), and sdb_unhook().

◆ ls_delete_data()

RZ_API bool ls_delete_data ( SdbList list,
void *  ptr 
)

Definition at line 145 of file ls.c.

145  {
146  void *kvp;
147  SdbListIter *iter;
148  ls_foreach (list, iter, kvp) {
149  if (ptr == kvp) {
150  ls_delete(list, iter);
151  return true;
152  }
153  }
154  return false;
155 }
RZ_API void ls_delete(SdbList *list, SdbListIter *iter)
Definition: ls.c:133

References list(), ls_delete(), and ls_foreach.

◆ ls_destroy()

RZ_API void ls_destroy ( SdbList list)

Definition at line 176 of file ls.c.

176  {
177  SdbListIter *it;
178  if (!list) {
179  return;
180  }
181  it = list->head;
182  while (it) {
183  SdbListIter *next = it->n;
184  ls_delete(list, it);
185  it = next;
186  }
187  list->head = list->tail = NULL;
188  list->length = 0;
189 }

References list(), ls_delete(), ls_iter_t::n, and NULL.

Referenced by load_process_line(), and ls_free().

◆ ls_free()

◆ ls_get_n()

RZ_API void* ls_get_n ( SdbList list,
int  n 
)

◆ ls_get_top()

RZ_API void* ls_get_top ( SdbList list)

◆ ls_insert()

RZ_API SdbListIter* ls_insert ( SdbList list,
int  n,
void *  data 
)

Definition at line 303 of file ls.c.

303  {
304  SdbListIter *it, *item;
305  int i;
306  if (list) {
307  if (!list->head || !n) {
308  return ls_prepend(list, data);
309  }
310  for (it = list->head, i = 0; it && it->data; it = it->n, i++) {
311  if (i == n) {
312  item = RZ_NEW0(SdbListIter);
313  if (!item) {
314  return NULL;
315  }
316  item->data = data;
317  item->n = it;
318  item->p = it->p;
319  if (it->p) {
320  it->p->n = item;
321  }
322  it->p = item;
323  list->length++;
324  list->sorted = false;
325  return item;
326  }
327  }
328  }
329  return ls_append(list, data);
330 }
RZ_API SdbListIter * ls_prepend(SdbList *list, void *data)
Definition: ls.c:224
#define RZ_NEW0(x)
Definition: rz_types.h:284

References ls_iter_t::data, i, list(), ls_append(), ls_prepend(), n, ls_iter_t::n, NULL, ls_iter_t::p, and RZ_NEW0.

◆ ls_item_new()

RZ_API SdbListIter* ls_item_new ( void *  data)

◆ ls_iter_init()

RZ_API void ls_iter_init ( SdbListIter iter,
SdbList list 
)

◆ ls_join()

RZ_API int ls_join ( SdbList first,
SdbList second 
)

Definition at line 281 of file ls.c.

281  {
282  if (!list1 || !list2) {
283  return 0;
284  }
285  if (!(list2->length)) {
286  return 0;
287  }
288  if (!(list1->length)) {
289  list1->head = list2->head;
290  list1->tail = list2->tail;
291  } else {
292  list1->tail->n = list2->head;
293  list2->head->p = list1->tail;
294  list1->tail = list2->tail;
295  list1->tail->n = NULL;
296  }
297  list1->length += list2->length;
298  list2->head = list2->tail = NULL;
299  list1->sorted = false;
300  return 1;
301 }

References ls_t::head, ls_t::length, ls_iter_t::n, NULL, ls_iter_t::p, ls_t::sorted, and ls_t::tail.

◆ ls_merge_sort()

RZ_API bool ls_merge_sort ( SdbList list,
SdbListComparator  cmp 
)

Definition at line 101 of file ls.c.

101  {
102  if (!cmp) {
103  return false;
104  }
105  if (list && list->head && cmp) {
106  SdbListIter *iter;
107  list->head = _merge_sort(list->head, cmp);
108  // update tail reference
109  iter = list->head;
110  while (iter && iter->n) {
111  iter = iter->n;
112  }
113  list->tail = iter;
114  list->sorted = true;
115  }
116  return true;
117 }
static RzILOpEffect * cmp(cs_insn *insn, bool is_thumb)
Definition: arm_il32.c:942
static SdbListIter * _merge_sort(SdbListIter *head, SdbListComparator cmp)
Definition: ls.c:90

References _merge_sort(), cmp(), and list().

Referenced by ls_sort().

◆ ls_new()

RZ_API SdbList* ls_new ( void  )

Definition at line 16 of file ls.c.

16  {
18  if (!list) {
19  return NULL;
20  }
21  return list;
22 }

References list(), NULL, and RZ_NEW0.

Referenced by load_ctx_init(), ls_clone(), ls_newf(), sdb_diff(), sdb_hook(), sdb_new(), sdb_ns_free_all(), sdb_ns_sync(), and sdb_text_save_fd().

◆ ls_newf()

RZ_API SdbList* ls_newf ( SdbListFree  freefn)

Definition at line 8 of file ls.c.

8  {
9  SdbList *list = ls_new();
10  if (list) {
11  list->free = freefn;
12  }
13  return list;
14 }
static void freefn(HtName_(Ht) *ht, HT_(Kv) *kv)
Definition: ht_inc.c:46

References freefn(), list(), and ls_new().

Referenced by sdb_foreach_list(), sdb_foreach_list_filter_user(), and sdb_foreach_match().

◆ ls_pop()

RZ_API void* ls_pop ( SdbList list)

Definition at line 244 of file ls.c.

244  {
245  void *data = NULL;
246  SdbListIter *iter;
247  if (list) {
248  if (list->tail) {
249  iter = list->tail;
250  if (list->head == list->tail) {
251  list->head = list->tail = NULL;
252  } else {
253  list->tail = iter->p;
254  list->tail->n = NULL;
255  }
256  data = iter->data;
257  free(iter);
258  list->length--;
259  }
260  return data;
261  }
262  return NULL;
263 }

References free(), list(), and NULL.

Referenced by sdb_diff_ctx(), sdb_diff_report(), and text_save().

◆ ls_pop_head()

RZ_API void* ls_pop_head ( SdbList list)

Definition at line 332 of file ls.c.

332  {
333  void *data = NULL;
334  SdbListIter *iter;
335  if (list) {
336  if (list->head) {
337  iter = list->head;
338  if (list->head == list->tail) {
339  list->head = list->tail = NULL;
340  } else {
341  list->head = iter->n;
342  list->head->p = NULL;
343  }
344  data = iter->data;
345  free(iter);
346  }
347  list->length--;
348  return data;
349  }
350  return NULL;
351 }

References free(), list(), and NULL.

◆ ls_prepend()

RZ_API SdbListIter* ls_prepend ( SdbList list,
void *  data 
)

Definition at line 224 of file ls.c.

224  {
226  if (!it) {
227  return NULL;
228  }
229  if (list->head) {
230  list->head->p = it;
231  }
232  it->data = data;
233  it->n = list->head;
234  it->p = NULL;
235  list->head = it;
236  if (!list->tail) {
237  list->tail = it;
238  }
239  list->length++;
240  list->sorted = false;
241  return it;
242 }

References ls_iter_t::data, list(), ls_iter_t::n, NULL, ls_iter_t::p, and RZ_NEW.

Referenced by ls_insert().

◆ ls_reverse()

RZ_API void ls_reverse ( SdbList list)

◆ ls_sort()

RZ_API bool ls_sort ( SdbList list,
SdbListComparator  cmp 
)

Definition at line 119 of file ls.c.

119  {
120  if (!cmp || list->cmp == cmp) {
121  return false;
122  }
123  if (list->length > 43) {
125  } else {
127  }
128  list->cmp = cmp;
129  list->sorted = true;
130  return true;
131 }
static void ls_insertion_sort(SdbList *list, SdbListComparator cmp)
Definition: ls.c:37
RZ_API bool ls_merge_sort(SdbList *list, SdbListComparator cmp)
Definition: ls.c:101

References cmp(), list(), ls_insertion_sort(), and ls_merge_sort().

Referenced by sdb_foreach_list(), sdb_foreach_list_filter_user(), and text_save().

◆ ls_split()

RZ_API void ls_split ( SdbList list,
void *  ptr 
)

◆ ls_split_iter()

RZ_API void ls_split_iter ( SdbList list,
SdbListIter iter 
)

Definition at line 157 of file ls.c.

157  {
158  if (!list || !iter) {
159  return;
160  }
161  if (list->head == iter) {
162  list->head = iter->n;
163  }
164  if (list->tail == iter) {
165  list->tail = iter->p;
166  }
167  if (iter->p) {
168  iter->p->n = iter->n;
169  }
170  if (iter->n) {
171  iter->n->p = iter->p;
172  }
173  list->length--;
174 }

References list().

Referenced by ls_delete().

◆ ls_unlink()

RZ_API void ls_unlink ( SdbList list,
void *  ptr 
)