rizin/libr/util/stack.c
Riccardo Schirone b279f282ba Refactoring ascii art graph
util/stack: extend stack api with r_stack_size
core/graph: use RStack instead of a custom implementation
util/graph: change implementation to use lists and extend api
core/cmd_debug: avoid free r_graph_get_nodes
core/graph: rename some functions and use typedefs for graph struct
core/graph: use RGraph for the ascii art graph
util/list: add const whenever possible
util/graph: add const on r_graph_get_nodes/neighbours
core/graph,core/cmd_debug: use const
core/graph: clean the code (add comments, use const, remove magic nums)

* use r_graph_node_iter for the current node
* reset graph when reloading nodes
* on callgraph edges printing, nth should be 0
* force seek of current node when reloading nodes
* use graph_foreach_node
* core/graph: remove get_current_node because useless
2015-06-14 00:46:11 +02:00

51 lines
901 B
C

/* radare - LGPL - Copyright 2007-2015 - ret2libc */
#include <r_util.h>
R_API RStack *r_stack_new (unsigned int n) {
RStack *s = R_NEW0 (RStack);
s->elems = R_NEWS0 (void *, n);
if (!s->elems)
return NULL;
s->n_elems = n;
s->top = -1;
return s;
}
R_API void r_stack_free (RStack *s) {
free (s->elems);
free (s);
}
R_API int r_stack_push (RStack *s, void *el) {
if (s->top == s->n_elems - 1) {
/* reallocate the stack */
s->n_elems *= 2;
s->elems = realloc (s->elems, s->n_elems * sizeof(void *));
if (!s->elems)
return R_FALSE;
}
s->top++;
s->elems[s->top] = el;
return R_TRUE;
}
R_API void *r_stack_pop (RStack *s) {
void *res;
if (s->top == -1)
return NULL;
res = s->elems[s->top];
s->top--;
return res;
}
R_API int r_stack_is_empty (RStack *s) {
return s->top == -1;
}
R_API unsigned int r_stack_size (RStack *s) {
return (unsigned int)(s->top + 1);
}