13
13
*******************************************************************************/
14
14
15
15
#include "digraphs.h"
16
- #include "digraphs-config.h"
17
16
18
17
#include <stdbool.h> // for false, true, bool
19
18
#include <stdint.h> // for uint64_t
20
19
#include <stdlib.h> // for NULL, free
20
+ #include <string.h> // for memcpy
21
21
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
27
29
28
30
#undef PACKAGE
29
31
#undef PACKAGE_BUGREPORT
@@ -184,7 +186,7 @@ static Obj FuncGABOW_SCC(Obj self, Obj digraph) {
184
186
185
187
comps = NEW_PLIST_IMM (T_PLIST_TAB , n );
186
188
187
- stack2 = malloc ((4 * n + 2 ) * sizeof (UInt ));
189
+ stack2 = safe_malloc ((4 * n + 2 ) * sizeof (UInt ));
188
190
frames = stack2 + n + 1 ;
189
191
end2 = 0 ;
190
192
@@ -292,7 +294,7 @@ static Obj FuncDIGRAPH_CONNECTED_COMPONENTS(Obj self, Obj digraph) {
292
294
gid = NEW_PLIST_IMM (T_PLIST_EMPTY , 0 );
293
295
gcomps = NEW_PLIST_IMM (T_PLIST_EMPTY , 0 );
294
296
} else {
295
- id = malloc (n * sizeof (UInt ));
297
+ id = safe_malloc (n * sizeof (UInt ));
296
298
for (i = 0 ; i < n ; i ++ ) {
297
299
id [i ] = i ;
298
300
}
@@ -308,7 +310,7 @@ static Obj FuncDIGRAPH_CONNECTED_COMPONENTS(Obj self, Obj digraph) {
308
310
}
309
311
310
312
// "Normalise" id, giving it sensible labels
311
- nid = malloc (n * sizeof (UInt ));
313
+ nid = safe_malloc (n * sizeof (UInt ));
312
314
nrcomps = 0 ;
313
315
for (i = 0 ; i < n ; i ++ ) {
314
316
f = UF_FIND (id , i );
@@ -346,8 +348,8 @@ static Obj FuncIS_ACYCLIC_DIGRAPH(Obj self, Obj adj) {
346
348
nr = LEN_PLIST (adj );
347
349
348
350
// 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 ));
351
353
352
354
for (i = 1 ; i <= nr ; i ++ ) {
353
355
nbs = ELM_PLIST (adj , i );
@@ -416,9 +418,9 @@ static Obj FuncDIGRAPH_LONGEST_DIST_VERTEX(Obj self, Obj adj, Obj start) {
416
418
return INTOBJ_INT (0 );
417
419
}
418
420
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 ));
422
424
423
425
level = 1 ;
424
426
stack [0 ] = i ;
@@ -503,9 +505,9 @@ static Obj FuncDIGRAPH_TRANS_REDUCTION(Obj self, Obj D) {
503
505
Obj const in_list = FuncDIGRAPH_IN_OUT_NBS (self , FuncOutNeighbours (self , D ));
504
506
505
507
// 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 ));
509
511
510
512
// Start a depth-first search from each source of the digraph
511
513
for (UInt i = 1 ; i <= n ; i ++ ) {
@@ -604,8 +606,8 @@ static Obj FuncDIGRAPH_PATH(Obj self, Obj adj, Obj u, Obj v) {
604
606
nr = LEN_PLIST (adj );
605
607
606
608
// 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 ));
609
611
610
612
level = 1 ;
611
613
stack [0 ] = i ;
@@ -673,8 +675,8 @@ Obj FuncIS_ANTISYMMETRIC_DIGRAPH(Obj self, Obj adj) {
673
675
}
674
676
675
677
// 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 ));
678
680
679
681
for (i = 1 ; i <= nr ; i ++ ) {
680
682
nbs = ELM_PLIST (adj , i );
@@ -751,11 +753,11 @@ static Obj FuncIS_STRONGLY_CONNECTED_DIGRAPH(Obj self, Obj digraph) {
751
753
}
752
754
753
755
nextid = 1 ;
754
- bag = malloc (n * 4 * sizeof (UInt ));
756
+ bag = safe_malloc (n * 4 * sizeof (UInt ));
755
757
ptr1 = bag ;
756
758
ptr2 = bag + n ;
757
759
fptr = bag + n * 2 ;
758
- id = calloc (n + 1 , sizeof (UInt ));
760
+ id = safe_calloc (n + 1 , sizeof (UInt ));
759
761
760
762
// first vertex v=1
761
763
PLAIN_LIST (ELM_PLIST (digraph , 1 ));
@@ -817,8 +819,8 @@ static Obj FuncDIGRAPH_TOPO_SORT(Obj self, Obj adj) {
817
819
}
818
820
819
821
// 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 ));
822
824
count = 0 ;
823
825
824
826
for (i = 1 ; i <= nr ; i ++ ) {
@@ -912,8 +914,8 @@ static Obj FuncDIGRAPH_SYMMETRIC_SPANNING_FOREST(Obj self, Obj adj) {
912
914
}
913
915
914
916
// 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 ));
917
919
918
920
for (i = 1 ; i <= nr ; i ++ ) {
919
921
// perform DFS only on still-undiscovered non-trivial connected components
@@ -1082,7 +1084,7 @@ static Obj FuncIS_MULTI_DIGRAPH(Obj self, Obj digraph) {
1082
1084
1083
1085
adj = FuncOutNeighbours (self , digraph );
1084
1086
n = DigraphNrVertices (digraph );
1085
- seen = calloc (n + 1 , sizeof (UInt ));
1087
+ seen = safe_calloc (n + 1 , sizeof (UInt ));
1086
1088
1087
1089
for (i = 1 ; i <= n ; i ++ ) {
1088
1090
adji = ELM_PLIST (adj , i );
@@ -1155,7 +1157,7 @@ static Obj FLOYD_WARSHALL(Obj digraph,
1155
1157
}
1156
1158
1157
1159
// 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 ));
1159
1161
for (i = 0 ; i < n * n ; i ++ ) {
1160
1162
dist [i ] = val1 ;
1161
1163
}
@@ -1178,7 +1180,7 @@ static Obj FLOYD_WARSHALL(Obj digraph,
1178
1180
1179
1181
if (copy ) {
1180
1182
// This is the special case for IS_TRANSITIVE_DIGRAPH
1181
- adj = malloc (n * n * sizeof (Int ));
1183
+ adj = safe_malloc (n * n * sizeof (Int ));
1182
1184
for (i = 0 ; i < n * n ; i ++ ) {
1183
1185
adj [i ] = dist [i ];
1184
1186
}
@@ -1406,7 +1408,7 @@ static Obj FuncDIGRAPH_EQUALS(Obj self, Obj digraph1, Obj digraph2) {
1406
1408
out1 = FuncOutNeighbours (self , digraph1 );
1407
1409
out2 = FuncOutNeighbours (self , digraph2 );
1408
1410
1409
- buf = calloc (n1 , sizeof (Int ));
1411
+ buf = safe_calloc (n1 , sizeof (Int ));
1410
1412
1411
1413
// Compare OutNeighbours of each vertex in turn
1412
1414
for (i = 1 ; i <= n1 ; i ++ ) {
@@ -1513,7 +1515,7 @@ static Obj FuncDIGRAPH_LT(Obj self, Obj digraph1, Obj digraph2) {
1513
1515
out1 = FuncOutNeighbours (self , digraph1 );
1514
1516
out2 = FuncOutNeighbours (self , digraph2 );
1515
1517
1516
- buf = calloc (n1 , sizeof (Int ));
1518
+ buf = safe_calloc (n1 , sizeof (Int ));
1517
1519
1518
1520
// Compare Sorted(out1[i]) and Sorted(out2[i]) for each vertex i
1519
1521
for (i = 1 ; i <= n1 ; i ++ ) {
0 commit comments