2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-09-01 15:05:23 +00:00

A macro for the size of a struct with a flexible array member

It can be fairly long-winded to allocate space for a struct with a
flexible array member: in general we need the size of the struct, the
size of the member, and the number of elements. Wrap them all up in a
STRUCT_FLEX_SIZE() macro, and use the new macro for the flexible
arrays in isc_ht and dns_qp.
This commit is contained in:
Tony Finch
2023-03-09 10:43:53 +00:00
parent 5673c9912e
commit 4f97a679f0
4 changed files with 13 additions and 12 deletions

View File

@@ -426,13 +426,13 @@ static void
realloc_chunk_arrays(dns_qp_t *qp, qp_chunk_t newmax) {
size_t oldptrs = sizeof(qp->base->ptr[0]) * qp->chunk_max;
size_t newptrs = sizeof(qp->base->ptr[0]) * newmax;
size_t allbytes = sizeof(dns_qpbase_t) + newptrs;
size_t size = STRUCT_FLEX_SIZE(qp->base, ptr, newmax);
if (qp->base == NULL || qpbase_unref(qp)) {
qp->base = isc_mem_reallocate(qp->mctx, qp->base, allbytes);
qp->base = isc_mem_reallocate(qp->mctx, qp->base, size);
} else {
dns_qpbase_t *oldbase = qp->base;
qp->base = isc_mem_allocate(qp->mctx, allbytes);
qp->base = isc_mem_allocate(qp->mctx, size);
memmove(&qp->base->ptr[0], &oldbase->ptr[0], oldptrs);
}
memset(&qp->base->ptr[qp->chunk_max], 0, newptrs - oldptrs);

View File

@@ -27,13 +27,6 @@
#include <isc/mem.h>
#include <isc/tid.h>
/*
* XXXFANF to be added to <isc/util.h> by a commmit in a qp-trie
* feature branch
*/
#define STRUCT_FLEX_SIZE(pointer, member, count) \
(sizeof(*(pointer)) + sizeof(*(pointer)->member) * (count))
/*
* XXXFANF this should probably be in <isc/util.h> too
*/

View File

@@ -287,7 +287,7 @@ isc__ht_add(isc_ht_t *ht, const unsigned char *key, const uint32_t keysize,
hash = hash_32(hashval, ht->hashbits[idx]);
node = isc_mem_get(ht->mctx, sizeof(*node) + keysize);
node = isc_mem_get(ht->mctx, STRUCT_FLEX_SIZE(node, key, keysize));
*node = (isc_ht_node_t){
.keysize = keysize,
.hashval = hashval,
@@ -396,7 +396,7 @@ isc__ht_delete(isc_ht_t *ht, const unsigned char *key, const uint32_t keysize,
prev->next = node->next;
}
isc_mem_put(ht->mctx, node,
sizeof(*node) + node->keysize);
STRUCT_FLEX_SIZE(node, key, node->keysize));
ht->count--;
return (ISC_R_SUCCESS);

View File

@@ -96,6 +96,14 @@
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
/*%
* Get the allocation size for a struct with a flexible array member
* containing `count` elements. The struct is identified by a pointer,
* typically the one that points to (or will point to) the allocation.
*/
#define STRUCT_FLEX_SIZE(pointer, member, count) \
(sizeof(*(pointer)) + sizeof(*(pointer)->member) * (count))
/*%
* Use this in translation units that would otherwise be empty, to
* suppress compiler warnings.