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

Go to the source code of this file.

Classes

struct  rz_graph_node_t
 
struct  rz_graph_edge_t
 
struct  rz_graph_t
 
struct  rz_graph_visitor_t
 

Typedefs

typedef struct rz_graph_node_t RzGraphNode
 
typedef struct rz_graph_edge_t RzGraphEdge
 
typedef struct rz_graph_t RzGraph
 
typedef struct rz_graph_visitor_t RzGraphVisitor
 
typedef void(* RzGraphNodeCallback) (RzGraphNode *n, RzGraphVisitor *vis)
 
typedef void(* RzGraphEdgeCallback) (const RzGraphEdge *e, RzGraphVisitor *vis)
 

Functions

RZ_API RzGraphrz_graph_new (void)
 
RZ_API void rz_graph_free (RzGraph *g)
 
RZ_API RzGraphNoderz_graph_get_node (const RzGraph *g, unsigned int idx)
 
RZ_API RzListIterrz_graph_node_iter (const RzGraph *g, unsigned int idx)
 
RZ_API void rz_graph_reset (RzGraph *g)
 
RZ_API RzGraphNoderz_graph_add_node (RzGraph *g, void *data)
 
RZ_API RzGraphNoderz_graph_add_nodef (RzGraph *g, void *data, RzListFree user_free)
 
RZ_API void rz_graph_del_node (RzGraph *g, RzGraphNode *n)
 
RZ_API void rz_graph_add_edge (RzGraph *g, RzGraphNode *from, RzGraphNode *to)
 
RZ_API void rz_graph_add_edge_at (RzGraph *g, RzGraphNode *from, RzGraphNode *to, int nth)
 
RZ_API RzGraphNoderz_graph_node_split_forward (RzGraph *g, RzGraphNode *split_me, void *data)
 
RZ_API void rz_graph_del_edge (RzGraph *g, RzGraphNode *from, RzGraphNode *to)
 
RZ_API const RzListrz_graph_get_neighbours (const RzGraph *g, const RzGraphNode *n)
 
RZ_API RzGraphNoderz_graph_nth_neighbour (const RzGraph *g, const RzGraphNode *n, int nth)
 
RZ_API const RzListrz_graph_innodes (const RzGraph *g, const RzGraphNode *n)
 
RZ_API const RzListrz_graph_all_neighbours (const RzGraph *g, const RzGraphNode *n)
 
RZ_API const RzListrz_graph_get_nodes (const RzGraph *g)
 
RZ_API bool rz_graph_adjacent (const RzGraph *g, const RzGraphNode *from, const RzGraphNode *to)
 
RZ_API void rz_graph_dfs_node (RzGraph *g, RzGraphNode *n, RzGraphVisitor *vis)
 
RZ_API void rz_graph_dfs_node_reverse (RzGraph *g, RzGraphNode *n, RzGraphVisitor *vis)
 
RZ_API void rz_graph_dfs (RzGraph *g, RzGraphVisitor *vis)
 

Typedef Documentation

◆ RzGraph

typedef struct rz_graph_t RzGraph

◆ RzGraphEdge

typedef struct rz_graph_edge_t RzGraphEdge

◆ RzGraphEdgeCallback

typedef void(* RzGraphEdgeCallback) (const RzGraphEdge *e, RzGraphVisitor *vis)

Definition at line 41 of file rz_graph.h.

◆ RzGraphNode

typedef struct rz_graph_node_t RzGraphNode

◆ RzGraphNodeCallback

typedef void(* RzGraphNodeCallback) (RzGraphNode *n, RzGraphVisitor *vis)

Definition at line 40 of file rz_graph.h.

◆ RzGraphVisitor

Function Documentation

◆ rz_graph_add_edge()

RZ_API void rz_graph_add_edge ( RzGraph g,
RzGraphNode from,
RzGraphNode to 
)

Definition at line 199 of file graph.c.

199  {
200  rz_graph_add_edge_at(t, from, to, -1);
201 }
RZ_API void rz_graph_add_edge_at(RzGraph *t, RzGraphNode *from, RzGraphNode *to, int nth)
Definition: graph.c:203
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr from
Definition: sfsocketcall.h:123
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr socklen_t static fromlen const void const struct sockaddr to
Definition: sfsocketcall.h:125

References from, rz_graph_add_edge_at(), and to.

Referenced by add_single_addr_xrefs(), create_dummy_nodes(), dot_trace_discover_child(), rz_agraph_add_edge(), and rz_analysis_class_get_inheritance_graph().

◆ rz_graph_add_edge_at()

RZ_API void rz_graph_add_edge_at ( RzGraph g,
RzGraphNode from,
RzGraphNode to,
int  nth 
)

Definition at line 203 of file graph.c.

203  {
204  if (from && to) {
205  rz_list_insert(from->out_nodes, nth, to);
206  rz_list_append(from->all_neighbours, to);
207  rz_list_append(to->in_nodes, from);
208  rz_list_append(to->all_neighbours, from);
209  t->n_edges++;
210  }
211 }
RZ_API RZ_BORROW RzListIter * rz_list_insert(RZ_NONNULL RzList *list, ut32 n, void *data)
Inserts a new element at the N-th position.
Definition: list.c:342
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288

References from, rz_graph_t::n_edges, rz_list_append(), rz_list_insert(), and to.

Referenced by rz_agraph_add_edge_at(), and rz_graph_add_edge().

◆ rz_graph_add_node()

RZ_API RzGraphNode* rz_graph_add_node ( RzGraph g,
void *  data 
)

Definition at line 153 of file graph.c.

153  {
154  if (!t) {
155  return NULL;
156  }
158  if (!n) {
159  return NULL;
160  }
161  n->idx = t->last_index++;
162  rz_list_append(t->nodes, n);
163  t->n_nodes++;
164  return n;
165 }
#define NULL
Definition: cris-opc.c:27
static RzGraphNode * rz_graph_node_new(void *data)
Definition: graph.c:13
int n
Definition: mipsasm.c:19

References rz_graph_t::last_index, n, rz_graph_t::n_nodes, rz_graph_t::nodes, NULL, rz_graph_node_new(), and rz_list_append().

Referenced by get_graphtrace_node(), rz_agraph_add_node_with_color(), rz_graph_add_nodef(), and rz_graph_node_split_forward().

◆ rz_graph_add_nodef()

RZ_API RzGraphNode* rz_graph_add_nodef ( RzGraph g,
void *  data,
RzListFree  user_free 
)

Definition at line 167 of file graph.c.

167  {
168  RzGraphNode *node = rz_graph_add_node(graph, data);
169  if (node) {
170  node->free = user_free;
171  }
172  return node;
173 }
RZ_API RzGraphNode * rz_graph_add_node(RzGraph *t, void *data)
Definition: graph.c:153
RzListFree free
Definition: rz_graph.h:16

References rz_graph_node_t::free, and rz_graph_add_node().

Referenced by rz_graph_add_node_info().

◆ rz_graph_adjacent()

RZ_API bool rz_graph_adjacent ( const RzGraph g,
const RzGraphNode from,
const RzGraphNode to 
)

Definition at line 270 of file graph.c.

270  {
271  if (!g || !from) {
272  return false;
273  }
274  return rz_list_contains(from->out_nodes, to);
275 }
struct @667 g
RZ_API RZ_BORROW RzListIter * rz_list_contains(RZ_NONNULL const RzList *list, RZ_NONNULL const void *ptr)
Returns the RzListIter of the given pointer, if found.
Definition: list.c:592

References from, g, rz_list_contains(), and to.

Referenced by dot_trace_discover_child(), and rz_graph_del_edge().

◆ rz_graph_all_neighbours()

RZ_API const RzList* rz_graph_all_neighbours ( const RzGraph g,
const RzGraphNode n 
)

Definition at line 261 of file graph.c.

261  {
262  return n ? n->all_neighbours : NULL;
263 }

References n, and NULL.

Referenced by adjust_class().

◆ rz_graph_del_edge()

RZ_API void rz_graph_del_edge ( RzGraph g,
RzGraphNode from,
RzGraphNode to 
)

Definition at line 232 of file graph.c.

232  {
233  if (!from || !to || !rz_graph_adjacent(t, from, to)) {
234  return;
235  }
236  rz_list_delete_data(from->out_nodes, to);
237  rz_list_delete_data(from->all_neighbours, to);
238  rz_list_delete_data(to->in_nodes, from);
239  rz_list_delete_data(to->all_neighbours, from);
240  t->n_edges--;
241 }
RZ_API bool rz_graph_adjacent(const RzGraph *g, const RzGraphNode *from, const RzGraphNode *to)
Definition: graph.c:270
RZ_API bool rz_list_delete_data(RZ_NONNULL RzList *list, void *ptr)
Deletes an entry in the list by searching for a pointer.
Definition: list.c:148

References from, rz_graph_t::n_edges, rz_graph_adjacent(), rz_list_delete_data(), and to.

Referenced by rz_agraph_del_edge().

◆ rz_graph_del_node()

RZ_API void rz_graph_del_node ( RzGraph g,
RzGraphNode n 
)

Definition at line 177 of file graph.c.

177  {
178  RzGraphNode *gn;
179  RzListIter *it;
180  if (!n) {
181  return;
182  }
183  rz_list_foreach (n->in_nodes, it, gn) {
186  t->n_edges--;
187  }
188 
189  rz_list_foreach (n->out_nodes, it, gn) {
192  t->n_edges--;
193  }
194 
195  rz_list_delete_data(t->nodes, n);
196  t->n_nodes--;
197 }
RzList * out_nodes
Definition: rz_graph.h:13
RzList * all_neighbours
Definition: rz_graph.h:15
RzList * in_nodes
Definition: rz_graph.h:14

References rz_graph_node_t::all_neighbours, rz_graph_node_t::in_nodes, n, rz_graph_t::n_edges, rz_graph_t::n_nodes, rz_graph_t::nodes, rz_graph_node_t::out_nodes, and rz_list_delete_data().

Referenced by fix_back_edge_dummy_nodes(), and rz_agraph_del_node().

◆ rz_graph_dfs()

RZ_API void rz_graph_dfs ( RzGraph g,
RzGraphVisitor vis 
)

Definition at line 299 of file graph.c.

299  {
300  rz_return_if_fail(g && vis);
301  RzGraphNode *n;
302  RzListIter *it;
303 
304  int *color = RZ_NEWS0(int, g->last_index);
305  if (color) {
306  rz_list_foreach (g->nodes, it, n) {
307  if (color[n->idx] == WHITE_COLOR) {
308  dfs_node(g, n, vis, color, true);
309  }
310  }
311  free(color);
312  }
313 }
static void dfs_node(RzGraph *g, RzGraphNode *n, RzGraphVisitor *vis, int color[], const bool direction)
Definition: graph.c:43
@ WHITE_COLOR
Definition: graph.c:8
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
#define RZ_NEWS0(x, y)
Definition: rz_types.h:282
static int color
Definition: visual.c:20

References color, dfs_node(), free(), g, n, RZ_NEWS0, rz_return_if_fail, and WHITE_COLOR.

Referenced by assign_layers(), create_dummy_nodes(), and remove_cycles().

◆ rz_graph_dfs_node()

RZ_API void rz_graph_dfs_node ( RzGraph g,
RzGraphNode n,
RzGraphVisitor vis 
)

Definition at line 277 of file graph.c.

277  {
278  if (!g || !n || !vis) {
279  return;
280  }
281  int *color = RZ_NEWS0(int, g->last_index);
282  if (color) {
283  dfs_node(g, n, vis, color, true);
284  free(color);
285  }
286 }

References color, dfs_node(), free(), g, n, and RZ_NEWS0.

◆ rz_graph_dfs_node_reverse()

RZ_API void rz_graph_dfs_node_reverse ( RzGraph g,
RzGraphNode n,
RzGraphVisitor vis 
)

Definition at line 288 of file graph.c.

288  {
289  if (!g || !n || !vis) {
290  return;
291  }
292  int *color = RZ_NEWS0(int, g->last_index);
293  if (color) {
294  dfs_node(g, n, vis, color, false);
295  free(color);
296  }
297 }

References color, dfs_node(), free(), g, n, and RZ_NEWS0.

◆ rz_graph_free()

RZ_API void rz_graph_free ( RzGraph g)

Definition at line 124 of file graph.c.

124  {
125  rz_list_free(t->nodes);
126  free(t);
127 }
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137

References free(), rz_graph_t::nodes, and rz_list_free().

Referenced by cmd_analysis_graph(), dot_trace_traverse(), rz_agraph_free(), rz_analysis_class_get_inheritance_graph(), rz_analysis_class_graph_handler(), and rz_graph_new().

◆ rz_graph_get_neighbours()

RZ_API const RzList* rz_graph_get_neighbours ( const RzGraph g,
const RzGraphNode n 
)

◆ rz_graph_get_node()

RZ_API RzGraphNode* rz_graph_get_node ( const RzGraph g,
unsigned int  idx 
)

Definition at line 129 of file graph.c.

129  {
130  RzListIter *it = rz_list_find(t->nodes, (void *)(size_t)idx, (RzListComparator)node_cmp);
131  if (!it) {
132  return NULL;
133  }
134  return (RzGraphNode *)it->data;
135 }
static int node_cmp(unsigned int idx, RzGraphNode *b)
Definition: graph.c:38
RZ_API RZ_BORROW RzListIter * rz_list_find(RZ_NONNULL const RzList *list, const void *p, RZ_NONNULL RzListComparator cmp)
Returns RzListIter element which matches via the RzListComparator.
Definition: list.c:620
int idx
Definition: setup.py:197
int(* RzListComparator)(const void *value, const void *list_data)
Definition: rz_list.h:33
void * data
Definition: rz_list.h:14

References rz_list_iter_t::data, setup::idx, node_cmp(), rz_graph_t::nodes, NULL, and rz_list_find().

◆ rz_graph_get_nodes()

◆ rz_graph_innodes()

RZ_API const RzList* rz_graph_innodes ( const RzGraph g,
const RzGraphNode n 
)

Definition at line 256 of file graph.c.

256  {
257  return n ? n->in_nodes : NULL;
258 }

References n, and NULL.

Referenced by adjust_directions(), assign_layers(), backedge_info(), collect_changes(), get_edge_number(), place_single(), rz_agraph_del_node(), and update_graph_sizes().

◆ rz_graph_new()

RZ_API RzGraph* rz_graph_new ( void  )

Definition at line 108 of file graph.c.

108  {
109  RzGraph *t = RZ_NEW0(RzGraph);
110  if (!t) {
111  return NULL;
112  }
113  t->nodes = rz_list_new();
114  if (!t->nodes) {
115  rz_graph_free(t);
116  return NULL;
117  }
119  t->n_nodes = 0;
120  t->last_index = 0;
121  return t;
122 }
RZ_API void rz_graph_free(RzGraph *t)
Definition: graph.c:124
static void rz_graph_node_free(RzGraphNode *n)
Definition: graph.c:25
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
#define RZ_NEW0(x)
Definition: rz_types.h:284
RzList * nodes
Definition: rz_graph.h:29
int last_index
Definition: rz_graph.h:28
unsigned int n_nodes
Definition: rz_graph.h:26
RzListFree free
Definition: rz_list.h:21

References rz_list_t::free, rz_graph_t::last_index, rz_graph_t::n_nodes, rz_graph_t::nodes, NULL, rz_graph_free(), rz_graph_node_free(), rz_list_new(), and RZ_NEW0.

Referenced by agraph_init(), dot_trace_traverse(), rz_analysis_class_get_inheritance_graph(), rz_core_analysis_codexrefs(), and rz_core_analysis_importxrefs().

◆ rz_graph_node_iter()

RZ_API RzListIter* rz_graph_node_iter ( const RzGraph g,
unsigned int  idx 
)

Definition at line 137 of file graph.c.

137  {
138  return rz_list_find(t->nodes, (void *)(size_t)idx, (RzListComparator)node_cmp);
139 }

References setup::idx, node_cmp(), rz_graph_t::nodes, and rz_list_find().

◆ rz_graph_node_split_forward()

RZ_API RzGraphNode* rz_graph_node_split_forward ( RzGraph g,
RzGraphNode split_me,
void *  data 
)

Definition at line 214 of file graph.c.

214  {
215  RzGraphNode *front = rz_graph_add_node(g, data);
216  RzList *tmp = front->out_nodes;
217  front->out_nodes = split_me->out_nodes;
218  split_me->out_nodes = tmp;
219  RzListIter *iter;
220  RzGraphNode *n;
221  rz_list_foreach (front->out_nodes, iter, n) {
222  rz_list_delete_data(n->in_nodes, split_me); // optimize me
223  rz_list_delete_data(n->all_neighbours, split_me); // boy this all_neighbours is so retarding perf here
225  rz_list_append(n->all_neighbours, front);
226  rz_list_append(n->in_nodes, front);
228  }
229  return front;
230 }

References rz_graph_node_t::all_neighbours, g, n, rz_graph_node_t::out_nodes, rz_graph_add_node(), rz_list_append(), rz_list_delete_data(), and autogen_x86imm::tmp.

◆ rz_graph_nth_neighbour()

RZ_API RzGraphNode* rz_graph_nth_neighbour ( const RzGraph g,
const RzGraphNode n,
int  nth 
)

Definition at line 251 of file graph.c.

251  {
252  return n ? (RzGraphNode *)rz_list_get_n(n->out_nodes, nth) : NULL;
253 }
RZ_API RZ_BORROW void * rz_list_get_n(RZ_NONNULL const RzList *list, ut32 n)
Returns the N-th element of the list.
Definition: list.c:574

References n, NULL, and rz_list_get_n().

Referenced by adjust_directions(), compute_vertical_nodes(), and follow_nth().

◆ rz_graph_reset()

RZ_API void rz_graph_reset ( RzGraph g)

Definition at line 141 of file graph.c.

141  {
142  rz_list_free(t->nodes);
143  t->nodes = rz_list_new();
144  if (!t->nodes) {
145  return;
146  }
147  t->nodes->free = (RzListFree)rz_graph_node_free;
148  t->n_nodes = 0;
149  t->n_edges = 0;
150  t->last_index = 0;
151 }

References rz_list_t::free, rz_graph_t::last_index, rz_graph_t::n_edges, rz_graph_t::n_nodes, rz_graph_t::nodes, rz_graph_node_free(), rz_list_free(), and rz_list_new().

Referenced by rz_agraph_reset().