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);
#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
*/
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,
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);
#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.