grid: refactor: remove union range_data_for_insertion

This union is identical to row_range_data, except the URI char pointer
is const. Let's ignore that, and re-use row_range_data, casting the
URI pointer when necessary.

Also remove uri_range_insert() and curly_range_insert(), and use the
generic version of range_insert() everywhere.
This commit is contained in:
Daniel Eklöf 2024-06-25 10:26:07 +02:00
parent 6a0110446c
commit 45f4eb48fb
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
2 changed files with 33 additions and 51 deletions

74
grid.c
View file

@ -176,17 +176,13 @@ range_ensure_size(struct row_ranges *ranges, int count_to_add)
xassert(ranges->count + count_to_add <= ranges->size); xassert(ranges->count + count_to_add <= ranges->size);
} }
union range_data_for_insertion { /*
struct { * Be careful! This function may xrealloc() the URI range vector, thus
uint64_t id; * invalidating pointers into it.
const char *uri; */
} uri;
struct curly_range_data curly;
};
static void static void
range_insert(struct row_ranges *ranges, size_t idx, int start, int end, range_insert(struct row_ranges *ranges, size_t idx, int start, int end,
enum row_range_type type, const union range_data_for_insertion *data) enum row_range_type type, const union row_range_data *data)
{ {
range_ensure_size(ranges, 1); range_ensure_size(ranges, 1);
@ -215,26 +211,6 @@ range_insert(struct row_ranges *ranges, size_t idx, int start, int end,
} }
} }
/*
* Be careful! This function may xrealloc() the URI range vector, thus
* invalidating pointers into it.
*/
static void
uri_range_insert(struct row_ranges *ranges, size_t idx, int start, int end,
uint64_t id, const char *uri)
{
range_insert(ranges, idx, start, end, ROW_RANGE_URI,
&(union range_data_for_insertion){.uri = {.id = id, .uri = uri}});
}
static void
curly_range_insert(struct row_ranges *ranges, size_t idx, int start, int end,
struct curly_range_data data)
{
range_insert(ranges, idx, start, end, ROW_RANGE_CURLY,
&(union range_data_for_insertion){.curly = data});
}
static void static void
uri_range_append_no_strdup(struct row_data *extra, int start, int end, uri_range_append_no_strdup(struct row_data *extra, int start, int end,
uint64_t id, char *uri) uint64_t id, char *uri)
@ -1324,8 +1300,7 @@ ranges_match(const struct row_range *r1, const struct row_range *r2,
} }
static bool static bool
range_match_data(const struct row_range *r, range_match_data(const struct row_range *r, const union row_range_data *data,
const union range_data_for_insertion *data,
enum row_range_type type) enum row_range_type type)
{ {
switch (type) { switch (type) {
@ -1344,8 +1319,7 @@ range_match_data(const struct row_range *r,
static void static void
grid_row_range_put(struct row_ranges *ranges, int col, grid_row_range_put(struct row_ranges *ranges, int col,
const union range_data_for_insertion *data, const union row_range_data *data, enum row_range_type type)
enum row_range_type type)
{ {
size_t insert_idx = 0; size_t insert_idx = 0;
bool replace = false; bool replace = false;
@ -1393,9 +1367,13 @@ grid_row_range_put(struct row_ranges *ranges, int col,
xassert(r->start < col); xassert(r->start < col);
xassert(r->end > col); xassert(r->end > col);
range_insert( union row_range_data insert_data;
ranges, i + 1, col + 1, r->end, type, switch (type) {
&(union range_data_for_insertion){.uri = {.id = r->uri.id, .uri = r->uri.uri}}); case ROW_RANGE_URI: insert_data.uri = r->uri; break;
case ROW_RANGE_CURLY: insert_data.curly = r->curly; break;
}
range_insert(ranges, i + 1, col + 1, r->end, type, &insert_data);
/* The insertion may xrealloc() the vector, making our /* The insertion may xrealloc() the vector, making our
* 'old' pointer invalid */ * 'old' pointer invalid */
@ -1453,7 +1431,7 @@ grid_row_uri_range_put(struct row *row, int col, const char *uri, uint64_t id)
grid_row_range_put( grid_row_range_put(
&row->extra->uri_ranges, col, &row->extra->uri_ranges, col,
&(union range_data_for_insertion){.uri = {.id = id, .uri = uri}}, &(union row_range_data){.uri = {.id = id, .uri = (char *)uri}},
ROW_RANGE_URI); ROW_RANGE_URI);
verify_no_overlapping_ranges(row->extra); verify_no_overlapping_ranges(row->extra);
@ -1467,7 +1445,7 @@ grid_row_curly_range_put(struct row *row, int col, struct curly_range_data data)
grid_row_range_put( grid_row_range_put(
&row->extra->curly_ranges, col, &row->extra->curly_ranges, col,
&(union range_data_for_insertion){.curly = data}, &(union row_range_data){.curly = data},
ROW_RANGE_CURLY); ROW_RANGE_CURLY);
verify_no_overlapping_ranges(row->extra); verify_no_overlapping_ranges(row->extra);
@ -1561,17 +1539,15 @@ grid_row_range_erase(struct row_ranges *ranges, enum row_range_type type,
} }
else if (start > old->start && end < old->end) { else if (start > old->start && end < old->end) {
/* Erase range erases a part in the middle of the URI */ /*
switch (type) { * Erase range erases a part in the middle of the URI
case ROW_RANGE_URI: *
uri_range_insert( * Must copy, since range_insert() may xrealloc() (thus
ranges, i + 1, end + 1, old->end, old->uri.id, old->uri.uri); * causing 'old' to be invalid) before it dereferences
break; * old->data
*/
case ROW_RANGE_CURLY: union row_range_data data = old->data;
curly_range_insert(ranges, i + 1, end + 1, old->end, old->curly); range_insert(ranges, i + 1, end + 1, old->end, type, &data);
break;
}
/* The insertion may xrealloc() the vector, making our /* The insertion may xrealloc() the vector, making our
* 'old' pointer invalid */ * 'old' pointer invalid */

View file

@ -129,8 +129,14 @@ struct row_range {
int end; int end;
union { union {
struct uri_range_data uri; /* This is just an expanded union row_range_data, but
struct curly_range_data curly; * anonymous, so that we don't have to write range->u.uri.id,
* but can instead do range->uri.id */
union {
struct uri_range_data uri;
struct curly_range_data curly;
};
union row_range_data data;
}; };
}; };