2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-31 06:25:31 +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);