From: Matthew Fernandez Date: Mon, 5 Dec 2022 02:28:29 +0000 (-0800) Subject: cgraph: inline 'bitarray_new' into 'bitarray_new_or_exit' and rename X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fd852f96c49c834f953694df143eb7ca531b7188;p=graphviz cgraph: inline 'bitarray_new' into 'bitarray_new_or_exit' and rename All clients of this functionality were calling `bitarray_new_or_exit`. That is, none of them could cope with failure. In the intervening time since this API was added, several other exit-on-failure functions have sprung up. For example, `gv_alloc`. It seems reasonable to now abbreviate this, leading to lesser code to maintain, with the “or exit” now implicit. --- diff --git a/lib/cgraph/bitarray.h b/lib/cgraph/bitarray.h index 54c3ef697..a8e698ccc 100644 --- a/lib/cgraph/bitarray.h +++ b/lib/cgraph/bitarray.h @@ -22,12 +22,10 @@ #pragma once #include -#include -#include +#include #include #include #include -#include #include #include @@ -45,39 +43,18 @@ typedef struct { } bitarray_t; /// create an array of the given element length -static inline int bitarray_new(bitarray_t *self, size_t size_bits) { - assert(self != NULL); - assert(self->size_bits == 0); +static inline bitarray_t bitarray_new(size_t size_bits) { + + bitarray_t ba = {.size_bits = size_bits}; // if the array is small enough, we can use inline storage - if (size_bits <= sizeof(self->block) * 8) { - memset(self->block, 0, sizeof(self->block)); + if (size_bits <= sizeof(ba.block) * 8) { + // nothing to be done // otherwise we need to heap-allocate } else { size_t capacity = size_bits / 8 + (size_bits % 8 == 0 ? 0 : 1); - uint8_t *base = calloc(capacity, sizeof(self->base[0])); - if (UNLIKELY(base == NULL)) - return ENOMEM; - - self->base = base; - } - - self->size_bits = size_bits; - - return 0; -} - -/// `bitarray_new` for callers who cannot handle failure -static inline bitarray_t bitarray_new_or_exit(size_t size_bits) { - - bitarray_t ba; - memset(&ba, 0, sizeof(ba)); - - int error = bitarray_new(&ba, size_bits); - if (UNLIKELY(error != 0)) { - fprintf(stderr, "out of memory\n"); - exit(EXIT_FAILURE); + ba.base = gv_calloc(capacity, sizeof(uint8_t)); } return ba; diff --git a/lib/cgraph/test_bitarray.c b/lib/cgraph/test_bitarray.c index 093b94913..ce87f9a30 100644 --- a/lib/cgraph/test_bitarray.c +++ b/lib/cgraph/test_bitarray.c @@ -10,14 +10,10 @@ #include #include #include -#include // helper for basic construction and destruction, with nothing in-between static void create_reset(size_t size) { - bitarray_t b; - memset(&b, 0, sizeof(b)); - int r = bitarray_new(&b, size); - assert(r == 0); + bitarray_t b = bitarray_new(size); bitarray_reset(&b); } @@ -30,10 +26,7 @@ static void test_create_reset_large(void) { create_reset(1023); } // setting and unsetting of all bits static void set_unset(size_t size) { - bitarray_t b; - memset(&b, 0, sizeof(b)); - int r = bitarray_new(&b, size); - assert(r == 0); + bitarray_t b = bitarray_new(size); // set and unset each bit for (size_t i = 0; i < size; ++i) { @@ -100,10 +93,7 @@ static void test_reuse(void) { size_t size = 10; - bitarray_t b; - memset(&b, 0, sizeof(b)); - int r = bitarray_new(&b, size); - assert(r == 0); + bitarray_t b = bitarray_new(size); // set and unset each bit for (size_t i = 0; i < size; ++i) { @@ -121,8 +111,7 @@ static void test_reuse(void) { // reuse it with a different size size = 1023; - r = bitarray_new(&b, size); - assert(r == 0); + b = bitarray_new(size); // set and unset each bit for (size_t i = 0; i < size; ++i) { @@ -142,10 +131,7 @@ static void test_reuse(void) { // redundant write to a bit static void double_set(size_t size, bool value) { - bitarray_t b; - memset(&b, 0, sizeof(b)); - int r = bitarray_new(&b, size); - assert(r == 0); + bitarray_t b = bitarray_new(size); static const size_t index = 7; assert(!bitarray_get(b, index)); diff --git a/lib/dotgen/aspect.c b/lib/dotgen/aspect.c index 7457a7e2b..7afb4eaf9 100644 --- a/lib/dotgen/aspect.c +++ b/lib/dotgen/aspect.c @@ -201,7 +201,7 @@ static void computeLayerWidths(graph_t * g) layerWidthInfo[i].nodeGroupsInLayer = gv_calloc(nNodeGroups, sizeof(nodeGroup_t*)); assert(nNodeGroups >= 0); - layerWidthInfo[i].removed = bitarray_new_or_exit((size_t)nNodeGroups); + layerWidthInfo[i].removed = bitarray_new((size_t)nNodeGroups); layerWidthInfo[i].layerNumber = i; layerWidthInfo[i].nNodeGroupsInLayer = 0; diff --git a/lib/neatogen/dijkstra.c b/lib/neatogen/dijkstra.c index 99e81811f..10d3f1bde 100644 --- a/lib/neatogen/dijkstra.c +++ b/lib/neatogen/dijkstra.c @@ -199,7 +199,7 @@ dijkstra_bounded(int vertex, vtx_data * graph, int n, DistType * dist, } num_visited_nodes = bfs_bounded(vertex, graph, dist, &Q, bound, visited_nodes); - bitarray_t node_in_neighborhood = bitarray_new_or_exit(n); + bitarray_t node_in_neighborhood = bitarray_new(n); for (i = 0; i < num_visited_nodes; i++) { bitarray_set(&node_in_neighborhood, visited_nodes[i], true); } diff --git a/lib/neatogen/neatoinit.c b/lib/neatogen/neatoinit.c index 470c940e7..6ef199695 100644 --- a/lib/neatogen/neatoinit.c +++ b/lib/neatogen/neatoinit.c @@ -187,7 +187,7 @@ static cluster_data* cluster_map(graph_t *mastergraph, graph_t *g) /* array of arrays of node indices in each cluster */ int **cs,*cn; int i,j,nclusters=0; - bitarray_t assigned = bitarray_new_or_exit(agnnodes(g)); + bitarray_t assigned = bitarray_new(agnnodes(g)); cluster_data *cdata = GNEW(cluster_data); cdata->ntoplevel = agnnodes(g); diff --git a/lib/neatogen/sgd.c b/lib/neatogen/sgd.c index 8ee6d26a6..304160e2d 100644 --- a/lib/neatogen/sgd.c +++ b/lib/neatogen/sgd.c @@ -52,7 +52,7 @@ static graph_sgd * extract_adjacency(graph_t *G, int model) { } graph_sgd *graph = N_NEW(1, graph_sgd); graph->sources = N_NEW(n_nodes + 1, size_t); - graph->pinneds = bitarray_new_or_exit(n_nodes); + graph->pinneds = bitarray_new(n_nodes); graph->targets = N_NEW(n_edges, size_t); graph->weights = N_NEW(n_edges, float); @@ -87,8 +87,8 @@ static graph_sgd * extract_adjacency(graph_t *G, int model) { } else if (model == MODEL_SUBSET) { // i,j,k refer to actual node indices, while x,y refer to edge indices in graph->targets // initialise to no neighbours - bitarray_t neighbours_i = bitarray_new_or_exit(graph->n); - bitarray_t neighbours_j = bitarray_new_or_exit(graph->n); + bitarray_t neighbours_i = bitarray_new(graph->n); + bitarray_t neighbours_j = bitarray_new(graph->n); for (size_t i = 0; i < graph->n; i++) { int deg_i = 0; for (size_t x = graph->sources[i]; x < graph->sources[i + 1]; x++) { diff --git a/lib/ortho/partition.c b/lib/ortho/partition.c index 2c7cc00ff..3e9cb0e97 100644 --- a/lib/ortho/partition.c +++ b/lib/ortho/partition.c @@ -596,7 +596,7 @@ monotonate_trapezoids(int nsegs, segment_t *seg, traps_t *tr, int flip, boxes_t *decomp) { int i; int tr_start; - bitarray_t visited = bitarray_new_or_exit(tr->length); + bitarray_t visited = bitarray_new(tr->length); mchain = gv_calloc(tr->length, sizeof(monchain_t)); vert = gv_calloc(nsegs + 1, sizeof(vertexchain_t)); diff --git a/lib/sfdpgen/spring_electrical.c b/lib/sfdpgen/spring_electrical.c index 321ac0b6a..3c5f8b587 100644 --- a/lib/sfdpgen/spring_electrical.c +++ b/lib/sfdpgen/spring_electrical.c @@ -372,7 +372,7 @@ static void beautify_leaves(int dim, SparseMatrix A, double *x){ assert(!SparseMatrix_has_diagonal(A)); - bitarray_t checked = bitarray_new_or_exit(m); + bitarray_t checked = bitarray_new(m); angles = gv_calloc(nangles_max, sizeof(double)); leaves = gv_calloc(nleaves_max, sizeof(int));