1313*******************************************************************************/
1414
1515#include "digraphs.h"
16- #include "digraphs-config.h"
1716
1817#include <stdbool.h> // for false, true, bool
1918#include <stdint.h> // for uint64_t
2019#include <stdlib.h> // for NULL, free
20+ #include <string.h> // for memcpy
2121
22- #include "bliss-includes.h" // for bliss stuff
23- #include "cliques.h"
24- #include "digraphs-debug.h" // for DIGRAPHS_ASSERT
25- #include "homos.h" // for FuncHomomorphismDigraphsFinder
26- #include "planar.h" // for FUNC_IS_PLANAR, . . .
22+ #include "bliss-includes.h" // for bliss stuff
23+ #include "cliques.h" // for FuncDigraphsCliquesFinder
24+ #include "digraphs-config.h" // for DIGRAPHS_WITH_INCLUDED_BLISS
25+ #include "digraphs-debug.h" // for DIGRAPHS_ASSERT
26+ #include "homos.h" // for FuncHomomorphismDigraphsFinder
27+ #include "planar.h" // for FUNC_IS_PLANAR, . . .
28+ #include "safemalloc.h" // for safe_malloc
2729
2830#undef PACKAGE
2931#undef PACKAGE_BUGREPORT
@@ -184,7 +186,7 @@ static Obj FuncGABOW_SCC(Obj self, Obj digraph) {
184186
185187 comps = NEW_PLIST_IMM (T_PLIST_TAB , n );
186188
187- stack2 = malloc ((4 * n + 2 ) * sizeof (UInt ));
189+ stack2 = safe_malloc ((4 * n + 2 ) * sizeof (UInt ));
188190 frames = stack2 + n + 1 ;
189191 end2 = 0 ;
190192
@@ -292,7 +294,7 @@ static Obj FuncDIGRAPH_CONNECTED_COMPONENTS(Obj self, Obj digraph) {
292294 gid = NEW_PLIST_IMM (T_PLIST_EMPTY , 0 );
293295 gcomps = NEW_PLIST_IMM (T_PLIST_EMPTY , 0 );
294296 } else {
295- id = malloc (n * sizeof (UInt ));
297+ id = safe_malloc (n * sizeof (UInt ));
296298 for (i = 0 ; i < n ; i ++ ) {
297299 id [i ] = i ;
298300 }
@@ -308,7 +310,7 @@ static Obj FuncDIGRAPH_CONNECTED_COMPONENTS(Obj self, Obj digraph) {
308310 }
309311
310312 // "Normalise" id, giving it sensible labels
311- nid = malloc (n * sizeof (UInt ));
313+ nid = safe_malloc (n * sizeof (UInt ));
312314 nrcomps = 0 ;
313315 for (i = 0 ; i < n ; i ++ ) {
314316 f = UF_FIND (id , i );
@@ -346,8 +348,8 @@ static Obj FuncIS_ACYCLIC_DIGRAPH(Obj self, Obj adj) {
346348 nr = LEN_PLIST (adj );
347349
348350 // init the buf
349- ptr = calloc (nr + 1 , sizeof (UInt ));
350- stack = malloc ((2 * nr + 2 ) * sizeof (UInt ));
351+ ptr = safe_calloc (nr + 1 , sizeof (UInt ));
352+ stack = safe_malloc ((2 * nr + 2 ) * sizeof (UInt ));
351353
352354 for (i = 1 ; i <= nr ; i ++ ) {
353355 nbs = ELM_PLIST (adj , i );
@@ -416,9 +418,9 @@ static Obj FuncDIGRAPH_LONGEST_DIST_VERTEX(Obj self, Obj adj, Obj start) {
416418 return INTOBJ_INT (0 );
417419 }
418420
419- ptr = calloc (nr + 1 , sizeof (UInt ));
420- depth = calloc (nr + 1 , sizeof (UInt ));
421- stack = malloc ((2 * nr + 2 ) * sizeof (UInt ));
421+ ptr = safe_calloc (nr + 1 , sizeof (UInt ));
422+ depth = safe_calloc (nr + 1 , sizeof (UInt ));
423+ stack = safe_malloc ((2 * nr + 2 ) * sizeof (UInt ));
422424
423425 level = 1 ;
424426 stack [0 ] = i ;
@@ -503,9 +505,9 @@ static Obj FuncDIGRAPH_TRANS_REDUCTION(Obj self, Obj D) {
503505 Obj const in_list = FuncDIGRAPH_IN_OUT_NBS (self , FuncOutNeighbours (self , D ));
504506
505507 // Create data structures needed for computation
506- UInt * ptr = calloc (n + 1 , sizeof (UInt ));
507- bool * mat = calloc (n * n , sizeof (bool ));
508- UInt * stack = malloc ((2 * n + 2 ) * sizeof (UInt ));
508+ UInt * ptr = safe_calloc (n + 1 , sizeof (UInt ));
509+ bool * mat = safe_calloc (n * n , sizeof (bool ));
510+ UInt * stack = safe_malloc ((2 * n + 2 ) * sizeof (UInt ));
509511
510512 // Start a depth-first search from each source of the digraph
511513 for (UInt i = 1 ; i <= n ; i ++ ) {
@@ -604,8 +606,8 @@ static Obj FuncDIGRAPH_PATH(Obj self, Obj adj, Obj u, Obj v) {
604606 nr = LEN_PLIST (adj );
605607
606608 // init the buf
607- ptr = calloc (nr + 1 , sizeof (UInt ));
608- stack = malloc ((2 * nr + 2 ) * sizeof (UInt ));
609+ ptr = safe_calloc (nr + 1 , sizeof (UInt ));
610+ stack = safe_malloc ((2 * nr + 2 ) * sizeof (UInt ));
609611
610612 level = 1 ;
611613 stack [0 ] = i ;
@@ -673,8 +675,8 @@ Obj FuncIS_ANTISYMMETRIC_DIGRAPH(Obj self, Obj adj) {
673675 }
674676
675677 // init the buf (is this correct length?)
676- ptr = calloc (nr + 1 , sizeof (UInt ));
677- stack = malloc ((4 * nr + 4 ) * sizeof (UInt ));
678+ ptr = safe_calloc (nr + 1 , sizeof (UInt ));
679+ stack = safe_malloc ((4 * nr + 4 ) * sizeof (UInt ));
678680
679681 for (i = 1 ; i <= nr ; i ++ ) {
680682 nbs = ELM_PLIST (adj , i );
@@ -751,11 +753,11 @@ static Obj FuncIS_STRONGLY_CONNECTED_DIGRAPH(Obj self, Obj digraph) {
751753 }
752754
753755 nextid = 1 ;
754- bag = malloc (n * 4 * sizeof (UInt ));
756+ bag = safe_malloc (n * 4 * sizeof (UInt ));
755757 ptr1 = bag ;
756758 ptr2 = bag + n ;
757759 fptr = bag + n * 2 ;
758- id = calloc (n + 1 , sizeof (UInt ));
760+ id = safe_calloc (n + 1 , sizeof (UInt ));
759761
760762 // first vertex v=1
761763 PLAIN_LIST (ELM_PLIST (digraph , 1 ));
@@ -817,8 +819,8 @@ static Obj FuncDIGRAPH_TOPO_SORT(Obj self, Obj adj) {
817819 }
818820
819821 // init the buf
820- ptr = calloc (nr + 1 , sizeof (UInt ));
821- stack = malloc ((2 * nr + 2 ) * sizeof (UInt ));
822+ ptr = safe_calloc (nr + 1 , sizeof (UInt ));
823+ stack = safe_malloc ((2 * nr + 2 ) * sizeof (UInt ));
822824 count = 0 ;
823825
824826 for (i = 1 ; i <= nr ; i ++ ) {
@@ -912,8 +914,8 @@ static Obj FuncDIGRAPH_SYMMETRIC_SPANNING_FOREST(Obj self, Obj adj) {
912914 }
913915
914916 // init the buffer
915- ptr = calloc (nr + 1 , sizeof (UInt ));
916- stack = malloc ((2 * nr + 2 ) * sizeof (UInt ));
917+ ptr = safe_calloc (nr + 1 , sizeof (UInt ));
918+ stack = safe_malloc ((2 * nr + 2 ) * sizeof (UInt ));
917919
918920 for (i = 1 ; i <= nr ; i ++ ) {
919921 // perform DFS only on still-undiscovered non-trivial connected components
@@ -1082,7 +1084,7 @@ static Obj FuncIS_MULTI_DIGRAPH(Obj self, Obj digraph) {
10821084
10831085 adj = FuncOutNeighbours (self , digraph );
10841086 n = DigraphNrVertices (digraph );
1085- seen = calloc (n + 1 , sizeof (UInt ));
1087+ seen = safe_calloc (n + 1 , sizeof (UInt ));
10861088
10871089 for (i = 1 ; i <= n ; i ++ ) {
10881090 adji = ELM_PLIST (adj , i );
@@ -1155,7 +1157,7 @@ static Obj FLOYD_WARSHALL(Obj digraph,
11551157 }
11561158
11571159 // Initialise the n x n matrix with val1 and val2
1158- dist = malloc (n * n * sizeof (Int ));
1160+ dist = safe_malloc (n * n * sizeof (Int ));
11591161 for (i = 0 ; i < n * n ; i ++ ) {
11601162 dist [i ] = val1 ;
11611163 }
@@ -1178,7 +1180,7 @@ static Obj FLOYD_WARSHALL(Obj digraph,
11781180
11791181 if (copy ) {
11801182 // This is the special case for IS_TRANSITIVE_DIGRAPH
1181- adj = malloc (n * n * sizeof (Int ));
1183+ adj = safe_malloc (n * n * sizeof (Int ));
11821184 for (i = 0 ; i < n * n ; i ++ ) {
11831185 adj [i ] = dist [i ];
11841186 }
@@ -1406,7 +1408,7 @@ static Obj FuncDIGRAPH_EQUALS(Obj self, Obj digraph1, Obj digraph2) {
14061408 out1 = FuncOutNeighbours (self , digraph1 );
14071409 out2 = FuncOutNeighbours (self , digraph2 );
14081410
1409- buf = calloc (n1 , sizeof (Int ));
1411+ buf = safe_calloc (n1 , sizeof (Int ));
14101412
14111413 // Compare OutNeighbours of each vertex in turn
14121414 for (i = 1 ; i <= n1 ; i ++ ) {
@@ -1513,7 +1515,7 @@ static Obj FuncDIGRAPH_LT(Obj self, Obj digraph1, Obj digraph2) {
15131515 out1 = FuncOutNeighbours (self , digraph1 );
15141516 out2 = FuncOutNeighbours (self , digraph2 );
15151517
1516- buf = calloc (n1 , sizeof (Int ));
1518+ buf = safe_calloc (n1 , sizeof (Int ));
15171519
15181520 // Compare Sorted(out1[i]) and Sorted(out2[i]) for each vertex i
15191521 for (i = 1 ; i <= n1 ; i ++ ) {
0 commit comments