From ec2f079b5bdcee15213d05bd1a81e90f9175f215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Tue, 4 Feb 2014 00:14:52 +0100 Subject: bfs: extract allocation out of eina_graph_bfs_new() --- src/lib/eina_graph_bfs.c | 51 +++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/src/lib/eina_graph_bfs.c b/src/lib/eina_graph_bfs.c index 2632c4a..12fa0ad 100644 --- a/src/lib/eina_graph_bfs.c +++ b/src/lib/eina_graph_bfs.c @@ -33,8 +33,33 @@ #include #include "eina_graph_private.h" +static Eina_Graph_BFS * +_eina_graph_bfs_allocate(_Eina_Graph *_g, unsigned int s) +{ + _Eina_Graph_BFS *_bfs; + + _bfs = calloc(1, sizeof(_Eina_Graph_BFS)); + if (!_bfs) + goto error_bfs; + + _bfs->data = calloc(_g->vertices, sizeof(_Eina_Graph_BFS_Data)); + if (!_bfs->data) + goto error_data; + + _bfs->s = s; + _bfs->vertices = _g->vertices; + + return (Eina_Graph_BFS *) _bfs; + +error_data: + free(_bfs); +error_bfs: + ERR("calloc failed : %s", strerror(errno)); + return NULL; +} + static void -_eina_graph_bfs_fwalk(_Eina_Graph *_g, _Eina_Graph_BFS *_bfs, unsigned int v) +_eina_graph_bfs_walk(_Eina_Graph *_g, _Eina_Graph_BFS *_bfs, unsigned int v) { Eina_List *fifo = NULL; Eina_Array *adjs; @@ -78,29 +103,15 @@ _eina_graph_bfs_fwalk(_Eina_Graph *_g, _Eina_Graph_BFS *_bfs, unsigned int v) EAPI Eina_Graph_BFS * eina_graph_bfs_new(Eina_Graph *g, unsigned int s) { - _Eina_Graph_BFS *_bfs; + Eina_Graph_BFS *bfs; _Eina_Graph * _g = (_Eina_Graph *) g; - _bfs = calloc(1, sizeof(_Eina_Graph_BFS)); - if (!_bfs) - goto error_bfs; - - _bfs->data = calloc(_g->vertices, sizeof(_Eina_Graph_BFS_Data)); - if (!_bfs->data) - goto error_data; - - _bfs->s = s; - _bfs->vertices = _g->vertices; + bfs = _eina_graph_bfs_allocate(_g, s); + if (!bfs) return NULL; - _eina_graph_bfs_fwalk(_g, _bfs, s); - - return (Eina_Graph_BFS *) _bfs; + _eina_graph_bfs_walk(_g, (_Eina_Graph_BFS *)bfs, s); -error_data: - free(_bfs); -error_bfs: - ERR("calloc failed : %s", strerror(errno)); - return NULL; + return bfs; } EAPI void -- cgit v1.1-2-g2b99