]> granicus.if.org Git - graphviz/commitdiff
sparse Dijkstra_internal: use cgraph wrappers for allocation
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Thu, 15 Sep 2022 01:01:23 +0000 (18:01 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Tue, 20 Sep 2022 14:36:59 +0000 (07:36 -0700)
The lib/cgraph/alloc.h wrappers are similar to the older lib/common/memory.h
wrappers except (1) they are header-only and (2) they live in a directory
(cgraph) that is at the root of the dependency tree. The long term plan is to
replace all use of lib/common/memory.h with lib/cgraph/alloc.h.

lib/sparse/SparseMatrix.c

index cd984f1c739ad94305ff14b6c32e4245399d8440..159ec4e96106546c761774ea4284c269ca5f1088 100644 (file)
@@ -1915,7 +1915,7 @@ static int Dijkstra_internal(SparseMatrix A, int root, double *dist, int *nlist,
   switch (A->type){
   case MATRIX_TYPE_COMPLEX:
     aa = (double*) A->a;
-    a = MALLOC(sizeof(double)*((size_t)(A->nz)));
+    a = gv_calloc((size_t)A->nz, sizeof(double));
     for (i = 0; i < A->nz; i++) a[i] = aa[i*2];
     break;
   case MATRIX_TYPE_REAL:
@@ -1923,18 +1923,18 @@ static int Dijkstra_internal(SparseMatrix A, int root, double *dist, int *nlist,
     break;
   case MATRIX_TYPE_INTEGER:
     ai = (int*) A->a;
-    a = MALLOC(sizeof(double)*((size_t)(A->nz)));
+    a = gv_calloc((size_t)A->nz, sizeof(double));
     for (i = 0; i < A->nz; i++) a[i] = (double) ai[i];
     break;
   case MATRIX_TYPE_PATTERN:
-    a = MALLOC(sizeof(double)*((size_t)A->nz));
+    a = gv_calloc((size_t)A->nz, sizeof(double));
     for (i = 0; i < A->nz; i++) a[i] = 1.;
     break;
   default:
     assert(0);/* no such matrix type */
   }
 
-  heap_ids = MALLOC(sizeof(int)*((size_t)m));
+  heap_ids = gv_calloc((size_t)m, sizeof(int));
   for (i = 0; i < m; i++) {
     dist[i] = -1;
     heap_ids[i] = UNVISITED;
@@ -1944,7 +1944,7 @@ static int Dijkstra_internal(SparseMatrix A, int root, double *dist, int *nlist,
   assert(h);
 
   /* add root as the first item in the heap */
-  ndata = MALLOC(sizeof(struct nodedata_struct));
+  ndata = gv_alloc(sizeof(struct nodedata_struct));
   ndata->dist = 0;
   ndata->id = root;
   heap_ids[root] = BinaryHeap_insert(h, ndata);
@@ -1964,7 +1964,7 @@ static int Dijkstra_internal(SparseMatrix A, int root, double *dist, int *nlist,
       if (jj == i || heap_id == FINISHED || (mask && mask[jj] < 0)) continue;
 
       if (heap_id == UNVISITED){
-       ndata = MALLOC(sizeof(struct nodedata_struct));
+       ndata = gv_alloc(sizeof(struct nodedata_struct));
        ndata->dist = fabs(a[j]) + ndata_min->dist;
        ndata->id = jj;
        heap_ids[jj] = BinaryHeap_insert(h, ndata);