Rizin
unix-like reverse engineering framework and cli tools
stack.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2007-2020 ret2libc <sirmy15@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_util.h>
5 
8  if (!s) {
9  return NULL;
10  }
11  s->elems = RZ_NEWS0(void *, n);
12  if (!s->elems) {
13  free(s);
14  return NULL;
15  }
16  s->n_elems = n;
17  s->top = -1;
18  return s;
19 }
20 
22  RzStack *s = rz_stack_new(n);
23  if (s) {
24  s->free = f;
25  }
26  return s;
27 }
28 
30  if (s) {
31  if (s->free) {
32  int i;
33  for (i = 0; i <= s->top; i++) {
34  s->free(s->elems[i]);
35  }
36  }
37  free(s->elems);
38  free(s);
39  }
40 }
41 
42 RZ_API bool rz_stack_push(RzStack *s, void *el) {
43  if (s->top == s->n_elems - 1) {
44  /* reallocate the stack */
45  s->n_elems *= 2;
46  void **elems = realloc(s->elems, s->n_elems * sizeof(void *));
47  if (!elems) {
48  return false;
49  }
50  s->elems = elems;
51  }
52 
53  s->top++;
54  s->elems[s->top] = el;
55  return true;
56 }
57 
58 // the caller should be take care of the object returned
60  if (s->top == -1) {
61  return NULL;
62  }
63  void *res = s->elems[s->top];
64  s->top--;
65  return res;
66 }
67 
69  return s->top == -1;
70 }
71 
73  return (size_t)(s->top + 1);
74 }
75 
77  return rz_stack_is_empty(s) ? NULL : s->elems[s->top];
78 }
lzma_index ** i
Definition: index.h:629
#define RZ_API
#define NULL
Definition: cris-opc.c:27
uint32_t ut32
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API bool rz_stack_is_empty(RzStack *s)
Definition: stack.c:68
RZ_API RzStack * rz_stack_newf(ut32 n, RzStackFree f)
Definition: stack.c:21
RZ_API void * rz_stack_pop(RzStack *s)
Definition: stack.c:59
RZ_API void * rz_stack_peek(RzStack *s)
Definition: stack.c:76
RZ_API bool rz_stack_push(RzStack *s, void *el)
Definition: stack.c:42
RZ_API size_t rz_stack_size(RzStack *s)
Definition: stack.c:72
RZ_API RzStack * rz_stack_new(ut32 n)
Definition: stack.c:6
RZ_API void rz_stack_free(RzStack *s)
Definition: stack.c:29
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
int n
Definition: mipsasm.c:19
static RzSocket * s
Definition: rtr.c:28
void(* RzStackFree)(void *ptr)
Definition: rz_stack.h:10
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_NEWS0(x, y)
Definition: rz_types.h:282
#define f(i)
Definition: sha256.c:46