Rizin
unix-like reverse engineering framework and cli tools
ls.c File Reference
#include <string.h>
#include "ls.h"

Go to the source code of this file.

Functions

RZ_API SdbListls_newf (SdbListFree freefn)
 
RZ_API SdbListls_new (void)
 
static void ls_insertion_sort_iter (SdbListIter *iter, SdbListComparator cmp)
 
static void ls_insertion_sort (SdbList *list, SdbListComparator cmp)
 
static SdbListIter_merge (SdbListIter *first, SdbListIter *second, SdbListComparator cmp)
 
static SdbListIter_sdb_list_split (SdbListIter *head)
 
static SdbListIter_merge_sort (SdbListIter *head, SdbListComparator cmp)
 
RZ_API bool ls_merge_sort (SdbList *list, SdbListComparator cmp)
 
RZ_API bool ls_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_split_iter (SdbList *list, SdbListIter *iter)
 
RZ_API void ls_destroy (SdbList *list)
 
RZ_API void ls_free (SdbList *list)
 
RZ_API SdbListIterls_append (SdbList *list, void *data)
 
RZ_API SdbListIterls_prepend (SdbList *list, void *data)
 
RZ_API void * ls_pop (SdbList *list)
 
RZ_API SdbListls_clone (SdbList *list)
 
RZ_API int ls_join (SdbList *list1, SdbList *list2)
 
RZ_API SdbListIterls_insert (SdbList *list, int n, void *data)
 
RZ_API void * ls_pop_head (SdbList *list)
 
RZ_API int ls_del_n (SdbList *list, int n)
 

Function Documentation

◆ _merge()

static SdbListIter* _merge ( SdbListIter first,
SdbListIter second,
SdbListComparator  cmp 
)
static

Definition at line 41 of file ls.c.

41  {
42  SdbListIter *next = NULL, *result = NULL, *head = NULL;
43  while (first || second) {
44  if (!second) {
45  next = first;
46  first = first->n;
47  } else if (!first) {
48  next = second;
49  second = second->n;
50  } else if (cmp(first->data, second->data) <= 0) {
51  next = first;
52  first = first->n;
53  } else {
54  next = second;
55  second = second->n;
56  }
57  if (!head) {
58  result = next;
59  head = result;
60  head->p = NULL;
61  } else {
62  result->n = next;
63  next->p = result;
64  result = result->n;
65  }
66  }
67  head->p = NULL;
68  next->n = NULL;
69  return head;
70 }
static RzILOpEffect * cmp(cs_insn *insn, bool is_thumb)
Definition: arm_il32.c:942
#define NULL
Definition: cris-opc.c:27
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 cmp(), ls_iter_t::data, test-lz4-versions::head, ls_iter_t::n, NULL, and ls_iter_t::p.

Referenced by _merge_sort().

◆ _merge_sort()

static SdbListIter* _merge_sort ( SdbListIter head,
SdbListComparator  cmp 
)
static

Definition at line 90 of file ls.c.

90  {
91  SdbListIter *second;
92  if (!head || !head->n) {
93  return head;
94  }
95  second = _sdb_list_split(head);
97  second = _merge_sort(second, cmp);
98  return _merge(head, second, cmp);
99 }
static SdbListIter * _sdb_list_split(SdbListIter *head)
Definition: ls.c:72
static SdbListIter * _merge_sort(SdbListIter *head, SdbListComparator cmp)
Definition: ls.c:90
static SdbListIter * _merge(SdbListIter *first, SdbListIter *second, SdbListComparator cmp)
Definition: ls.c:41

References _merge(), _sdb_list_split(), cmp(), and test-lz4-versions::head.

Referenced by ls_merge_sort().

◆ _sdb_list_split()

static SdbListIter* _sdb_list_split ( SdbListIter head)
static

Definition at line 72 of file ls.c.

72  {
76  if (!head || !head->n) {
77  return head;
78  }
79  slow = head;
80  fast = head;
81  while (fast && fast->n && fast->n->n) {
82  fast = fast->n->n;
83  slow = slow->n;
84  }
85  tmp = slow->n;
86  slow->n = NULL;
87  return tmp;
88 }
static char * slow(struct match *, char *, char *, sopno, sopno)
Definition: engine.c:806
static char * fast(struct match *, char *, char *, sopno, sopno)
Definition: engine.c:717

References fast(), test-lz4-versions::head, NULL, slow(), and autogen_x86imm::tmp.

Referenced by _merge_sort().

◆ 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 }
static void list(RzEgg *egg)
Definition: rz-gg.c:52
#define RZ_NEW(x)
Definition: rz_types.h:285

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_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_insertion_sort()

static void ls_insertion_sort ( SdbList list,
SdbListComparator  cmp 
)
static

Definition at line 37 of file ls.c.

37  {
39 }
static void ls_insertion_sort_iter(SdbListIter *iter, SdbListComparator cmp)
Definition: ls.c:24

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

Referenced by ls_sort().

◆ ls_insertion_sort_iter()

static void ls_insertion_sort_iter ( SdbListIter iter,
SdbListComparator  cmp 
)
static

Definition at line 24 of file ls.c.

24  {
25  SdbListIter *it, *it2;
26  for (it = iter; it && it->data; it = it->n) {
27  for (it2 = it->n; it2 && it2->data; it2 = it2->n) {
28  if (cmp(it->data, it2->data) > 0) {
29  void *t = it->data;
30  it->data = it2->data;
31  it2->data = t;
32  }
33  }
34  }
35 }

References cmp(), ls_iter_t::data, and ls_iter_t::n.

Referenced by ls_insertion_sort().

◆ ls_join()

RZ_API int ls_join ( SdbList list1,
SdbList list2 
)

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 }
SdbListIter * head
Definition: ls.h:24
size_t length
Definition: ls.h:23
SdbListIter * tail
Definition: ls.h:25
bool sorted
Definition: ls.h:28

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 }

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_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_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().