From: Matthew Fernandez Date: Sun, 28 Aug 2022 15:48:00 +0000 (-0700) Subject: mm2gv SparseMatrix_import_matrix_market: use cgraph wrappers for allocation X-Git-Tag: 6.0.1~17^2~9 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4f7c3700dea4588225ca3bd7b6f1a80ed6203aa0;p=graphviz mm2gv SparseMatrix_import_matrix_market: use cgraph wrappers for allocation 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. This change also fixes a number of unchecked allocation failures where `malloc` was being called directly and the result was not checked. --- diff --git a/cmd/tools/matrix_market.c b/cmd/tools/matrix_market.c index de35d7c9e..7e1c19ef4 100644 --- a/cmd/tools/matrix_market.c +++ b/cmd/tools/matrix_market.c @@ -11,11 +11,9 @@ #include #include "mmio.h" #include "matrix_market.h" +#include #include -#include #include -#define MALLOC gmalloc -#define REALLOC grealloc static int mm_get_type(MM_typecode typecode) { @@ -69,8 +67,8 @@ SparseMatrix SparseMatrix_import_matrix_market(FILE * f) } /* reseve memory for matrices */ - I = MALLOC(nz * sizeof(int)); - J = MALLOC(nz * sizeof(int)); + I = gv_calloc(nz, sizeof(int)); + J = gv_calloc(nz, sizeof(int)); /* NOTE: when reading in doubles, ANSI C requires the use of the "l" */ /* specifier as in "%lg", "%lf", "%le", otherwise errors will occur */ @@ -78,7 +76,7 @@ SparseMatrix SparseMatrix_import_matrix_market(FILE * f) type = mm_get_type(matcode); switch (type) { case MATRIX_TYPE_REAL: - val = malloc(nz * sizeof(double)); + val = gv_calloc(nz, sizeof(double)); for (i = 0; i < nz; i++) { int num = fscanf(f, "%d %d %lg\n", &I[i], &J[i], &val[i]); (void)num; @@ -87,9 +85,9 @@ SparseMatrix SparseMatrix_import_matrix_market(FILE * f) J[i]--; } if (mm_is_symmetric(matcode)) { - I = REALLOC(I, 2 * sizeof(int) * nz); - J = REALLOC(J, 2 * sizeof(int) * nz); - val = REALLOC(val, 2 * sizeof(double) * nz); + I = gv_recalloc(I, nz, 2 * nz, sizeof(int)); + J = gv_recalloc(J, nz, 2 * nz, sizeof(int)); + val = gv_recalloc(val, nz, 2 * nz, sizeof(double)); nzold = nz; for (i = 0; i < nzold; i++) { if (I[i] != J[i]) { @@ -99,9 +97,9 @@ SparseMatrix SparseMatrix_import_matrix_market(FILE * f) } } } else if (mm_is_skew(matcode)) { - I = REALLOC(I, 2 * sizeof(int) * nz); - J = REALLOC(J, 2 * sizeof(int) * nz); - val = REALLOC(val, 2 * sizeof(double) * nz); + I = gv_recalloc(I, nz, 2 * nz, sizeof(int)); + J = gv_recalloc(J, nz, 2 * nz, sizeof(int)); + val = gv_recalloc(val, nz, 2 * nz, sizeof(double)); nzold = nz; for (i = 0; i < nzold; i++) { assert(I[i] != J[i]); /* skew symm has no diag */ @@ -115,7 +113,7 @@ SparseMatrix SparseMatrix_import_matrix_market(FILE * f) vp = val; break; case MATRIX_TYPE_INTEGER: - vali = malloc(nz * sizeof(int)); + vali = gv_calloc(nz, sizeof(int)); for (i = 0; i < nz; i++) { int num = fscanf(f, "%d %d %d\n", &I[i], &J[i], &vali[i]); (void)num; @@ -124,9 +122,9 @@ SparseMatrix SparseMatrix_import_matrix_market(FILE * f) J[i]--; } if (mm_is_symmetric(matcode)) { - I = REALLOC(I, 2 * sizeof(int) * nz); - J = REALLOC(J, 2 * sizeof(int) * nz); - vali = REALLOC(vali, 2 * sizeof(int) * nz); + I = gv_recalloc(I, nz, 2 * nz, sizeof(int)); + J = gv_recalloc(J, nz, 2 * nz, sizeof(int)); + vali = gv_recalloc(vali, nz, 2 * nz, sizeof(int)); nzold = nz; for (i = 0; i < nzold; i++) { if (I[i] != J[i]) { @@ -136,9 +134,9 @@ SparseMatrix SparseMatrix_import_matrix_market(FILE * f) } } } else if (mm_is_skew(matcode)) { - I = REALLOC(I, 2 * sizeof(int) * nz); - J = REALLOC(J, 2 * sizeof(int) * nz); - vali = REALLOC(vali, 2 * sizeof(int) * nz); + I = gv_recalloc(I, nz, 2 * nz, sizeof(int)); + J = gv_recalloc(J, nz, 2 * nz, sizeof(int)); + vali = gv_recalloc(vali, nz, 2 * nz, sizeof(int)); nzold = nz; for (i = 0; i < nzold; i++) { assert(I[i] != J[i]); /* skew symm has no diag */ @@ -160,8 +158,8 @@ SparseMatrix SparseMatrix_import_matrix_market(FILE * f) J[i]--; } if (mm_is_symmetric(matcode) || mm_is_skew(matcode)) { - I = REALLOC(I, 2 * sizeof(int) * nz); - J = REALLOC(J, 2 * sizeof(int) * nz); + I = gv_recalloc(I, nz, 2 * nz, sizeof(int)); + J = gv_recalloc(J, nz, 2 * nz, sizeof(int)); nzold = nz; for (i = 0; i < nzold; i++) { if (I[i] != J[i]) { @@ -174,7 +172,7 @@ SparseMatrix SparseMatrix_import_matrix_market(FILE * f) } break; case MATRIX_TYPE_COMPLEX: - val = malloc(2 * nz * sizeof(double)); + val = gv_calloc(2 * nz, sizeof(double)); v = val; for (i = 0; i < nz; i++) { int num = fscanf(f, "%d %d %lg %lg\n", &I[i], &J[i], &v[0], &v[1]); @@ -185,9 +183,9 @@ SparseMatrix SparseMatrix_import_matrix_market(FILE * f) J[i]--; } if (mm_is_symmetric(matcode)) { - I = REALLOC(I, 2 * sizeof(int) * nz); - J = REALLOC(J, 2 * sizeof(int) * nz); - val = REALLOC(val, 4 * sizeof(double) * nz); + I = gv_recalloc(I, nz, 2 * nz, sizeof(int)); + J = gv_recalloc(J, nz, 2 * nz, sizeof(int)); + val = gv_recalloc(val, 2 * nz, 4 * nz, sizeof(double)); nzold = nz; for (i = 0; i < nzold; i++) { if (I[i] != J[i]) { @@ -199,9 +197,9 @@ SparseMatrix SparseMatrix_import_matrix_market(FILE * f) } } } else if (mm_is_skew(matcode)) { - I = REALLOC(I, 2 * sizeof(int) * nz); - J = REALLOC(J, 2 * sizeof(int) * nz); - val = REALLOC(val, 4 * sizeof(double) * nz); + I = gv_recalloc(I, nz, 2 * nz, sizeof(int)); + J = gv_recalloc(J, nz, 2 * nz, sizeof(int)); + val = gv_recalloc(val, 2 * nz, 4 * nz, sizeof(double)); nzold = nz; for (i = 0; i < nzold; i++) { assert(I[i] != J[i]); /* skew symm has no diag */ @@ -213,9 +211,9 @@ SparseMatrix SparseMatrix_import_matrix_market(FILE * f) } } else if (mm_is_hermitian(matcode)) { - I = REALLOC(I, 2 * sizeof(int) * nz); - J = REALLOC(J, 2 * sizeof(int) * nz); - val = REALLOC(val, 4 * sizeof(double) * nz); + I = gv_recalloc(I, nz, 2 * nz, sizeof(int)); + J = gv_recalloc(J, nz, 2 * nz, sizeof(int)); + val = gv_recalloc(val, 2 * nz, 4 * nz, sizeof(double)); nzold = nz; for (i = 0; i < nzold; i++) { if (I[i] != J[i]) {