From d9048b3db186650a8422b1cf0576449b0394759e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Wed, 23 Aug 2023 11:05:14 +0200 Subject: [PATCH] Remove ISC_MEM_ZERO and isc_mem_*x() API Use the new isc_mem_c*() calloc-like API for allocations that are zeroed. In turn, this also fixes couple of incorrect usage of the ISC_MEM_ZERO for structures that need to be zeroed explicitly. There are few places where isc_mem_cput() is used on structures with a flexible member (or similar). --- bin/tests/system/dyndb/driver/db.c | 15 +++-- bin/tests/system/dyndb/driver/instance.c | 6 +- bin/tests/system/dyndb/driver/syncptr.c | 3 +- cocci/isc_mem_zero.spatch | 17 ------ lib/dns/dnstap.c | 4 +- lib/dns/ipkeylist.c | 77 +++++++++++------------- lib/dns/journal.c | 2 +- lib/dns/rbt-zonedb.c | 3 +- lib/dns/rbt.c | 23 ++----- lib/dns/rdataslab.c | 2 +- lib/dns/resolver.c | 7 +-- lib/dns/rrl.c | 10 +-- lib/dns/zoneverify.c | 12 ++-- lib/isc/histo.c | 4 +- lib/isc/include/isc/mem.h | 36 ++--------- lib/isc/mem.c | 8 ++- lib/isc/netmgr/http.c | 4 +- lib/isc/netmgr/netmgr.c | 4 +- tests/bench/qpmulti.c | 9 +-- tests/isc/mem_test.c | 26 ++++---- tests/isc/ratelimiter_test.c | 21 ++++--- 21 files changed, 124 insertions(+), 169 deletions(-) delete mode 100644 cocci/isc_mem_zero.spatch diff --git a/bin/tests/system/dyndb/driver/db.c b/bin/tests/system/dyndb/driver/db.c index 12293942f3..8a9f694f8b 100644 --- a/bin/tests/system/dyndb/driver/db.c +++ b/bin/tests/system/dyndb/driver/db.c @@ -606,18 +606,17 @@ create_db(isc_mem_t *mctx, const dns_name_t *origin, dns_dbtype_t type, a_addr.s_addr = 0x0100007fU; - sampledb = isc_mem_getx(mctx, sizeof(*sampledb), ISC_MEM_ZERO); + sampledb = isc_mem_get(mctx, sizeof(*sampledb)); + *sampledb = (sampledb_t){ + .common.magic = DNS_DB_MAGIC, + .common.impmagic = SAMPLEDB_MAGIC, + .common.methods = &sampledb_methods, + .common.rdclass = rdclass, + }; isc_mem_attach(mctx, &sampledb->common.mctx); dns_name_init(&sampledb->common.origin, NULL); - sampledb->common.magic = DNS_DB_MAGIC; - sampledb->common.impmagic = SAMPLEDB_MAGIC; - - sampledb->common.methods = &sampledb_methods; - sampledb->common.attributes = 0; - sampledb->common.rdclass = rdclass; - CHECK(dns_name_dupwithoffsets(origin, mctx, &sampledb->common.origin)); isc_refcount_init(&sampledb->common.references, 1); diff --git a/bin/tests/system/dyndb/driver/instance.c b/bin/tests/system/dyndb/driver/instance.c index 9fa597961c..22a6a00697 100644 --- a/bin/tests/system/dyndb/driver/instance.c +++ b/bin/tests/system/dyndb/driver/instance.c @@ -114,12 +114,12 @@ new_sample_instance(isc_mem_t *mctx, const char *db_name, int argc, char **argv, REQUIRE(sample_instp != NULL && *sample_instp == NULL); - sample_instance_t *inst = isc_mem_getx(mctx, sizeof(*inst), - ISC_MEM_ZERO); + sample_instance_t *inst = isc_mem_get(mctx, sizeof(*inst)); + *inst = (sample_instance_t){ 0 }; + isc_mem_attach(mctx, &inst->mctx); inst->db_name = isc_mem_strdup(mctx, db_name); - inst->zone1_name = dns_fixedname_initname(&inst->zone1_fn); inst->zone2_name = dns_fixedname_initname(&inst->zone2_fn); diff --git a/bin/tests/system/dyndb/driver/syncptr.c b/bin/tests/system/dyndb/driver/syncptr.c index 4bbca88ce9..364fa670ee 100644 --- a/bin/tests/system/dyndb/driver/syncptr.c +++ b/bin/tests/system/dyndb/driver/syncptr.c @@ -226,7 +226,8 @@ syncptr(sample_instance_t *inst, dns_name_t *name, dns_rdata_t *addr_rdata, DNS_RDATACOMMON_INIT(&ptr_struct, dns_rdatatype_ptr, dns_rdataclass_in); dns_name_init(&ptr_struct.ptr, NULL); - syncptr = isc_mem_getx(mctx, sizeof(*syncptr), ISC_MEM_ZERO); + syncptr = isc_mem_get(mctx, sizeof(*syncptr)); + *syncptr = (syncptr_t){ 0 }; isc_mem_attach(mctx, &syncptr->mctx); isc_buffer_init(&syncptr->b, syncptr->buf, sizeof(syncptr->buf)); dns_fixedname_init(&syncptr->ptr_target_name); diff --git a/cocci/isc_mem_zero.spatch b/cocci/isc_mem_zero.spatch deleted file mode 100644 index 319c26b6e3..0000000000 --- a/cocci/isc_mem_zero.spatch +++ /dev/null @@ -1,17 +0,0 @@ -@@ -expression ptr, ctx, size; -@@ - -- ptr = isc_mem_get(ctx, size); -+ ptr = isc_mem_getx(ctx, size, ISC_MEM_ZERO); -... -- memset(ptr, 0, size); - -@@ -expression ptr, ctx, size; -@@ - -- ptr = isc_mem_allocate(ctx, size); -+ ptr = isc_mem_allocatex(ctx, size, ISC_MEM_ZERO); -... -- memset(ptr, 0, size); diff --git a/lib/dns/dnstap.c b/lib/dns/dnstap.c index 75dbee18e9..0ceba0c07f 100644 --- a/lib/dns/dnstap.c +++ b/lib/dns/dnstap.c @@ -1033,7 +1033,9 @@ dns_dt_parse(isc_mem_t *mctx, isc_region_t *src, dns_dtdata_t **destp) { REQUIRE(src != NULL); REQUIRE(destp != NULL && *destp == NULL); - d = isc_mem_getx(mctx, sizeof(*d), ISC_MEM_ZERO); + d = isc_mem_get(mctx, sizeof(*d)); + *d = (dns_dtdata_t){ 0 }; + isc_mem_attach(mctx, &d->mctx); d->frame = dnstap__dnstap__unpack(NULL, src->length, src->base); diff --git a/lib/dns/ipkeylist.c b/lib/dns/ipkeylist.c index d0a2b015e6..d55b19ca09 100644 --- a/lib/dns/ipkeylist.c +++ b/lib/dns/ipkeylist.c @@ -34,8 +34,6 @@ dns_ipkeylist_init(dns_ipkeylist_t *ipkl) { void dns_ipkeylist_clear(isc_mem_t *mctx, dns_ipkeylist_t *ipkl) { - uint32_t i; - REQUIRE(ipkl != NULL); if (ipkl->allocated == 0) { @@ -44,59 +42,54 @@ dns_ipkeylist_clear(isc_mem_t *mctx, dns_ipkeylist_t *ipkl) { if (ipkl->addrs != NULL) { isc_mem_cput(mctx, ipkl->addrs, ipkl->allocated, - sizeof(isc_sockaddr_t)); + sizeof(ipkl->addrs[0])); } if (ipkl->sources != NULL) { isc_mem_cput(mctx, ipkl->sources, ipkl->allocated, - sizeof(isc_sockaddr_t)); - } - - if (ipkl->addrs != NULL) { - isc_mem_cput(mctx, ipkl->addrs, ipkl->allocated, - sizeof(isc_sockaddr_t)); + sizeof(ipkl->sources[0])); } if (ipkl->keys != NULL) { - for (i = 0; i < ipkl->allocated; i++) { - if (ipkl->keys[i] == NULL) { - continue; + for (size_t i = 0; i < ipkl->allocated; i++) { + if (ipkl->keys[i] != NULL) { + if (dns_name_dynamic(ipkl->keys[i])) { + dns_name_free(ipkl->keys[i], mctx); + } + isc_mem_put(mctx, ipkl->keys[i], + sizeof(*ipkl->keys[i])); } - if (dns_name_dynamic(ipkl->keys[i])) { - dns_name_free(ipkl->keys[i], mctx); - } - isc_mem_put(mctx, ipkl->keys[i], sizeof(dns_name_t)); } isc_mem_cput(mctx, ipkl->keys, ipkl->allocated, - sizeof(dns_name_t *)); + sizeof(ipkl->keys[0])); } if (ipkl->tlss != NULL) { - for (i = 0; i < ipkl->allocated; i++) { - if (ipkl->tlss[i] == NULL) { - continue; + for (size_t i = 0; i < ipkl->allocated; i++) { + if (ipkl->tlss[i] != NULL) { + if (dns_name_dynamic(ipkl->tlss[i])) { + dns_name_free(ipkl->tlss[i], mctx); + } + isc_mem_put(mctx, ipkl->tlss[i], + sizeof(*ipkl->tlss[i])); } - if (dns_name_dynamic(ipkl->tlss[i])) { - dns_name_free(ipkl->tlss[i], mctx); - } - isc_mem_put(mctx, ipkl->tlss[i], sizeof(dns_name_t)); } isc_mem_cput(mctx, ipkl->tlss, ipkl->allocated, - sizeof(dns_name_t *)); + sizeof(ipkl->tlss[0])); } if (ipkl->labels != NULL) { - for (i = 0; i < ipkl->allocated; i++) { - if (ipkl->labels[i] == NULL) { - continue; + for (size_t i = 0; i < ipkl->allocated; i++) { + if (ipkl->labels[i] != NULL) { + if (dns_name_dynamic(ipkl->labels[i])) { + dns_name_free(ipkl->labels[i], mctx); + } + isc_mem_put(mctx, ipkl->labels[i], + sizeof(*ipkl->labels[i])); } - if (dns_name_dynamic(ipkl->labels[i])) { - dns_name_free(ipkl->labels[i], mctx); - } - isc_mem_put(mctx, ipkl->labels[i], sizeof(dns_name_t)); } isc_mem_cput(mctx, ipkl->labels, ipkl->allocated, - sizeof(dns_name_t *)); + sizeof(ipkl->labels[0])); } dns_ipkeylist_init(ipkl); @@ -181,15 +174,15 @@ dns_ipkeylist_resize(isc_mem_t *mctx, dns_ipkeylist_t *ipkl, unsigned int n) { } ipkl->addrs = isc_mem_creget(mctx, ipkl->addrs, ipkl->allocated, n, - sizeof(isc_sockaddr_t)); - ipkl->sources = isc_mem_creget(mctx, ipkl->addrs, ipkl->allocated, n, - sizeof(isc_sockaddr_t)); - ipkl->keys = isc_mem_creget(mctx, ipkl->addrs, ipkl->allocated, n, - sizeof(dns_name_t *)); - ipkl->tlss = isc_mem_creget(mctx, ipkl->addrs, ipkl->allocated, n, - sizeof(dns_name_t *)); - ipkl->labels = isc_mem_creget(mctx, ipkl->addrs, ipkl->allocated, n, - sizeof(dns_name_t *)); + sizeof(ipkl->addrs[0])); + ipkl->sources = isc_mem_creget(mctx, ipkl->sources, ipkl->allocated, n, + sizeof(ipkl->sources[0])); + ipkl->keys = isc_mem_creget(mctx, ipkl->keys, ipkl->allocated, n, + sizeof(ipkl->keys[0])); + ipkl->tlss = isc_mem_creget(mctx, ipkl->tlss, ipkl->allocated, n, + sizeof(ipkl->tlss[0])); + ipkl->labels = isc_mem_creget(mctx, ipkl->labels, ipkl->allocated, n, + sizeof(ipkl->labels[0])); ipkl->allocated = n; return (ISC_R_SUCCESS); diff --git a/lib/dns/journal.c b/lib/dns/journal.c index 12e1713ae3..604766d56f 100644 --- a/lib/dns/journal.c +++ b/lib/dns/journal.c @@ -591,7 +591,7 @@ journal_file_create(isc_mem_t *mctx, bool downgrade, const char *filename) { size = sizeof(journal_rawheader_t) + ISC_CHECKED_MUL(index_size, sizeof(journal_rawpos_t)); - mem = isc_mem_getx(mctx, size, ISC_MEM_ZERO); + mem = isc_mem_cget(mctx, 1, size); memmove(mem, &rawheader, sizeof(rawheader)); result = isc_stdio_write(mem, 1, (size_t)size, fp, NULL); diff --git a/lib/dns/rbt-zonedb.c b/lib/dns/rbt-zonedb.c index 8c87191114..6f60aa1aa8 100644 --- a/lib/dns/rbt-zonedb.c +++ b/lib/dns/rbt-zonedb.c @@ -2116,7 +2116,8 @@ setgluecachestats(dns_db_t *db, isc_stats_t *stats) { static dns_glue_t * new_gluelist(isc_mem_t *mctx, dns_name_t *name) { - dns_glue_t *glue = isc_mem_getx(mctx, sizeof(*glue), ISC_MEM_ZERO); + dns_glue_t *glue = isc_mem_get(mctx, sizeof(*glue)); + *glue = (dns_glue_t){ 0 }; dns_name_t *gluename = dns_fixedname_initname(&glue->fixedname); isc_mem_attach(mctx, &glue->mctx); diff --git a/lib/dns/rbt.c b/lib/dns/rbt.c index 62385f939d..32e1c2cc3d 100644 --- a/lib/dns/rbt.c +++ b/lib/dns/rbt.c @@ -1508,28 +1508,15 @@ create_node(isc_mem_t *mctx, const dns_name_t *name, dns_rbtnode_t **nodep) { * Allocate space for the node structure, the name, and the offsets. */ nodelen = sizeof(dns_rbtnode_t) + region.length + labels + 1; - node = isc_mem_getx(mctx, nodelen, ISC_MEM_ZERO); - - node->is_root = 0; - node->parent = NULL; - node->right = NULL; - node->left = NULL; - node->down = NULL; - node->data = NULL; - - node->hashnext = NULL; - node->hashval = 0; + node = isc_mem_get(mctx, nodelen); + *node = (dns_rbtnode_t){ + .nsec = DNS_RBT_NSEC_NORMAL, + .color = BLACK, + }; ISC_LINK_INIT(node, deadlink); - node->locknum = 0; - node->wild = 0; - node->dirty = 0; isc_refcount_init(&node->references, 0); - node->find_callback = 0; - node->nsec = DNS_RBT_NSEC_NORMAL; - - node->color = BLACK; /* * The following is stored to make reconstructing a name from the diff --git a/lib/dns/rdataslab.c b/lib/dns/rdataslab.c index bc439359b2..294761f94e 100644 --- a/lib/dns/rdataslab.c +++ b/lib/dns/rdataslab.c @@ -329,7 +329,7 @@ dns_rdataslab_fromrdataset(dns_rdataset_t *rdataset, isc_mem_t *mctx, * Allocate the memory, set up a buffer, start copying in * data. */ - rawbuf = isc_mem_getx(mctx, buflen, ISC_MEM_ZERO); + rawbuf = isc_mem_cget(mctx, 1, buflen); #if DNS_RDATASET_FIXED /* Allocate temporary offset table. */ diff --git a/lib/dns/resolver.c b/lib/dns/resolver.c index 8955ca2c0b..b5a911ccc8 100644 --- a/lib/dns/resolver.c +++ b/lib/dns/resolver.c @@ -10863,8 +10863,6 @@ isc_result_t dns_resolver_disable_ds_digest(dns_resolver_t *resolver, const dns_name_t *name, unsigned int digest_type) { unsigned int len, mask; - unsigned char *tmp; - unsigned char *digests; isc_result_t result; dns_rbtnode_t *node = NULL; @@ -10892,7 +10890,7 @@ dns_resolver_disable_ds_digest(dns_resolver_t *resolver, const dns_name_t *name, result = dns_rbt_addnode(resolver->digests, name, &node); if (result == ISC_R_SUCCESS || result == ISC_R_EXISTS) { - digests = node->data; + unsigned char *digests = node->data; /* If digests is set, digests[0] contains its length. */ if (digests == NULL || len > *digests) { /* @@ -10901,7 +10899,8 @@ dns_resolver_disable_ds_digest(dns_resolver_t *resolver, const dns_name_t *name, * bitfield and copy the old (smaller) bitfield * into it if one exists. */ - tmp = isc_mem_getx(resolver->mctx, len, ISC_MEM_ZERO); + unsigned char *tmp = isc_mem_cget(resolver->mctx, 1, + len); if (digests != NULL) { memmove(tmp, digests, *digests); } diff --git a/lib/dns/rrl.c b/lib/dns/rrl.c index 328e9506d4..73c7b53ef4 100644 --- a/lib/dns/rrl.c +++ b/lib/dns/rrl.c @@ -261,7 +261,7 @@ expand_entries(dns_rrl_t *rrl, int newsize) { bsize = sizeof(dns_rrl_block_t) + ISC_CHECKED_MUL((newsize - 1), sizeof(dns_rrl_entry_t)); - b = isc_mem_getx(rrl->mctx, bsize, ISC_MEM_ZERO); + b = isc_mem_cget(rrl->mctx, 1, bsize); b->size = bsize; e = b->entries; @@ -327,7 +327,7 @@ expand_rrl_hash(dns_rrl_t *rrl, isc_stdtime_t now) { hsize = sizeof(dns_rrl_hash_t) + ISC_CHECKED_MUL((new_bins - 1), sizeof(hash->bins[0])); - hash = isc_mem_getx(rrl->mctx, hsize, ISC_MEM_ZERO); + hash = isc_mem_cget(rrl->mctx, 1, hsize); hash->length = new_bins; rrl->hash_gen ^= 1; hash->gen = rrl->hash_gen; @@ -1345,10 +1345,12 @@ dns_rrl_init(dns_rrl_t **rrlp, dns_view_t *view, int min_entries) { *rrlp = NULL; - rrl = isc_mem_getx(view->mctx, sizeof(*rrl), ISC_MEM_ZERO); + rrl = isc_mem_get(view->mctx, sizeof(*rrl)); + *rrl = (dns_rrl_t){ + .ts_bases[0] = isc_stdtime_now(), + }; isc_mem_attach(view->mctx, &rrl->mctx); isc_mutex_init(&rrl->lock); - rrl->ts_bases[0] = isc_stdtime_now(); view->rrl = rrl; diff --git a/lib/dns/zoneverify.c b/lib/dns/zoneverify.c index 3a9570769b..684ab9984c 100644 --- a/lib/dns/zoneverify.c +++ b/lib/dns/zoneverify.c @@ -404,11 +404,13 @@ record_nsec3(const vctx_t *vctx, const unsigned char *rawhash, len = sizeof(*element) + nsec3->next_length * 2 + nsec3->salt_length; - element = isc_mem_getx(vctx->mctx, len, ISC_MEM_ZERO); - element->hash = nsec3->hash; - element->salt_length = nsec3->salt_length; - element->next_length = nsec3->next_length; - element->iterations = nsec3->iterations; + element = isc_mem_get(vctx->mctx, len); + *element = (struct nsec3_chain_fixed){ + .hash = nsec3->hash, + .salt_length = nsec3->salt_length, + .next_length = nsec3->next_length, + .iterations = nsec3->iterations, + }; cp = (unsigned char *)(element + 1); memmove(cp, nsec3->salt, nsec3->salt_length); cp += nsec3->salt_length; diff --git a/lib/isc/histo.c b/lib/isc/histo.c index a16a4244e6..fa4ca5b06d 100644 --- a/lib/isc/histo.c +++ b/lib/isc/histo.c @@ -375,8 +375,8 @@ isc_histomulti_create(isc_mem_t *mctx, uint sigbits, isc_histomulti_t **hmp) { uint size = isc_tid_count(); INSIST(size > 0); - isc_histomulti_t *hm = isc_mem_getx( - mctx, STRUCT_FLEX_SIZE(hm, hg, size), ISC_MEM_ZERO); + isc_histomulti_t *hm = isc_mem_cget(mctx, 1, + STRUCT_FLEX_SIZE(hm, hg, size)); *hm = (isc_histomulti_t){ .magic = HISTOMULTI_MAGIC, .size = size, diff --git a/lib/isc/include/isc/mem.h b/lib/isc/include/isc/mem.h index 9532b2ae6d..73196afbe2 100644 --- a/lib/isc/include/isc/mem.h +++ b/lib/isc/include/isc/mem.h @@ -129,39 +129,30 @@ extern unsigned int isc_mem_defaultflags; */ /*% - * Flags that can be passed to isc_mem_*x() variants of the macros. - * * The definitions of the macros have been pulled directly from jemalloc.h * and checked for consistency in mem.c. * - *\li ISC_MEM_ZERO - fill the memory with zeroes before returning + *\li ISC__MEM_ZERO - fill the memory with zeroes before returning */ -#define ISC_MEM_ZERO ((int)0x40) +#define ISC__MEM_ZERO ((int)0x40) #define isc_mem_get(c, s) isc__mem_get((c), (s), 0 _ISC_MEM_FILELINE) #define isc_mem_cget(c, n, s) \ isc__mem_get((c), ISC_CHECKED_MUL((n), (s)), \ - ISC_MEM_ZERO _ISC_MEM_FILELINE) -#define isc_mem_getx(c, s, f) isc__mem_get((c), (s), (f)_ISC_MEM_FILELINE) + ISC__MEM_ZERO _ISC_MEM_FILELINE) #define isc_mem_reget(c, p, o, n) \ isc__mem_reget((c), (p), (o), (n), 0 _ISC_MEM_FILELINE) #define isc_mem_creget(c, p, o, n, s) \ isc__mem_reget((c), (p), ISC_CHECKED_MUL((o), (s)), \ ISC_CHECKED_MUL((n), (s)), \ - ISC_MEM_ZERO _ISC_MEM_FILELINE) -#define isc_mem_regetx(c, p, o, n, f) \ - isc__mem_reget((c), (p), (o), (n), (f)_ISC_MEM_FILELINE) + ISC__MEM_ZERO _ISC_MEM_FILELINE) #define isc_mem_allocate(c, s) isc__mem_allocate((c), (s), 0 _ISC_MEM_FILELINE) -#define isc_mem_allocatex(c, s, f) \ - isc__mem_allocate((c), (s), (f)_ISC_MEM_FILELINE) #define isc_mem_callocate(c, n, s) \ isc__mem_allocate((c), ISC_CHECKED_MUL((n), (s)), \ - ISC_MEM_ZERO _ISC_MEM_FILELINE) + ISC__MEM_ZERO _ISC_MEM_FILELINE) #define isc_mem_reallocate(c, p, s) \ isc__mem_reallocate((c), (p), (s), 0 _ISC_MEM_FILELINE) -#define isc_mem_reallocatex(c, p, s, f) \ - isc__mem_reallocate((c), (p), (s), (f)_ISC_MEM_FILELINE) #define isc_mem_strdup(c, p) isc__mem_strdup((c), (p)_ISC_MEM_FILELINE) #define isc_mem_strndup(c, p, l) \ isc__mem_strndup((c), (p), (l)_ISC_MEM_FILELINE) @@ -175,34 +166,19 @@ extern unsigned int isc_mem_defaultflags; #define isc_mem_cput(c, p, n, s) \ do { \ isc__mem_put((c), (p), ISC_CHECKED_MUL((n), (s)), \ - ISC_MEM_ZERO _ISC_MEM_FILELINE); \ + ISC__MEM_ZERO _ISC_MEM_FILELINE); \ (p) = NULL; \ } while (0) -#define isc_mem_putx(c, p, s, f) \ - do { \ - isc__mem_put((c), (p), (s), (f)_ISC_MEM_FILELINE); \ - (p) = NULL; \ - } while (0) #define isc_mem_putanddetach(c, p, s) \ do { \ isc__mem_putanddetach((c), (p), (s), 0 _ISC_MEM_FILELINE); \ (p) = NULL; \ } while (0) -#define isc_mem_putanddetachx(c, p, s, f) \ - do { \ - isc__mem_putanddetach((c), (p), (s), (f)_ISC_MEM_FILELINE); \ - (p) = NULL; \ - } while (0) #define isc_mem_free(c, p) \ do { \ isc__mem_free((c), (p), 0 _ISC_MEM_FILELINE); \ (p) = NULL; \ } while (0) -#define isc_mem_freex(c, p, f) \ - do { \ - isc__mem_free((c), (p), (f)_ISC_MEM_FILELINE); \ - (p) = NULL; \ - } while (0) #define isc_mempool_put(c, p) \ do { \ isc__mempool_put((c), (p)_ISC_MEM_FILELINE); \ diff --git a/lib/isc/mem.c b/lib/isc/mem.c index e3242f92e0..9d382c98cf 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -306,7 +306,8 @@ mem_get(isc_mem_t *ctx, size_t size, int flags) { ret = mallocx(size, flags); INSIST(ret != NULL); - if ((flags & ISC_MEM_ZERO) == 0 && (ctx->flags & ISC_MEMFLAG_FILL) != 0) + if ((flags & ISC__MEM_ZERO) == 0 && + (ctx->flags & ISC_MEMFLAG_FILL) != 0) { memset(ret, 0xbe, size); /* Mnemonic for "beef". */ } @@ -338,7 +339,8 @@ mem_realloc(isc_mem_t *ctx, void *old_ptr, size_t old_size, size_t new_size, new_ptr = rallocx(old_ptr, new_size, flags); INSIST(new_ptr != NULL); - if ((flags & ISC_MEM_ZERO) == 0 && (ctx->flags & ISC_MEMFLAG_FILL) != 0) + if ((flags & ISC__MEM_ZERO) == 0 && + (ctx->flags & ISC_MEMFLAG_FILL) != 0) { ssize_t diff_size = new_size - old_size; void *diff_ptr = (uint8_t *)new_ptr + old_size; @@ -383,7 +385,7 @@ mem_initialize(void) { * Check if the values copied from jemalloc still match */ #ifdef JEMALLOC_API_SUPPORTED - RUNTIME_CHECK(ISC_MEM_ZERO == MALLOCX_ZERO); + RUNTIME_CHECK(ISC__MEM_ZERO == MALLOCX_ZERO); #endif /* JEMALLOC_API_SUPPORTED */ isc_mutex_init(&contextslock); diff --git a/lib/isc/netmgr/http.c b/lib/isc/netmgr/http.c index 0b5848a5be..a3b5bd6cb5 100644 --- a/lib/isc/netmgr/http.c +++ b/lib/isc/netmgr/http.c @@ -2956,8 +2956,8 @@ isc_nm_http_set_endpoints(isc_nmsocket_t *listener, for (size_t i = 0; i < isc_loopmgr_nloops(loopmgr); i++) { isc__networker_t *worker = &listener->worker->netmgr->workers[i]; - http_endpoints_data_t *data = isc_mem_getx( - worker->loop->mctx, sizeof(*data), ISC_MEM_ZERO); + http_endpoints_data_t *data = isc_mem_cget(worker->loop->mctx, + 1, sizeof(*data)); isc__nmsocket_attach(listener, &data->listener); isc_nm_http_endpoints_attach(eps, &data->endpoints); diff --git a/lib/isc/netmgr/netmgr.c b/lib/isc/netmgr/netmgr.c index a4c0588d91..e9a682df45 100644 --- a/lib/isc/netmgr/netmgr.c +++ b/lib/isc/netmgr/netmgr.c @@ -2223,8 +2223,8 @@ set_tlsctx_workers(isc_nmsocket_t *listener, isc_tlsctx_t *tlsctx) { for (size_t i = 0; i < nworkers; i++) { isc__networker_t *worker = &listener->worker->netmgr->workers[i]; - settlsctx_data_t *data = isc_mem_getx( - worker->loop->mctx, sizeof(*data), ISC_MEM_ZERO); + settlsctx_data_t *data = isc_mem_cget(worker->loop->mctx, 1, + sizeof(*data)); isc__nmsocket_attach(listener, &data->listener); isc_tlsctx_attach(tlsctx, &data->tlsctx); diff --git a/tests/bench/qpmulti.c b/tests/bench/qpmulti.c index 39b56e3e13..feec46f088 100644 --- a/tests/bench/qpmulti.c +++ b/tests/bench/qpmulti.c @@ -820,7 +820,7 @@ startup(void *arg) { uint32_t nloops = isc_loopmgr_nloops(loopmgr); size_t bytes = sizeof(struct bench_state) + sizeof(struct thread_args) * nloops; - struct bench_state *bctx = isc_mem_getx(mctx, bytes, ISC_MEM_ZERO); + struct bench_state *bctx = isc_mem_cget(mctx, 1, bytes); *bctx = (struct bench_state){ .loopmgr = loopmgr, @@ -871,10 +871,11 @@ setup_tickers(isc_mem_t *mctx, isc_loopmgr_t *loopmgr) { uint32_t nloops = isc_loopmgr_nloops(loopmgr); for (uint32_t i = 0; i < nloops; i++) { isc_loop_t *loop = isc_loop_get(loopmgr, i); - struct ticker *ticker = isc_mem_getx(mctx, sizeof(*ticker), - ISC_MEM_ZERO); + struct ticker *ticker = isc_mem_get(mctx, sizeof(*ticker)); + *ticker = (struct ticker){ + .loopmgr = loopmgr, + }; isc_mem_attach(mctx, &ticker->mctx); - ticker->loopmgr = loopmgr; isc_loop_setup(loop, start_ticker, ticker); isc_loop_teardown(loop, stop_ticker, ticker); } diff --git a/tests/isc/mem_test.c b/tests/isc/mem_test.c index 6d781ddba0..4a05efb1d9 100644 --- a/tests/isc/mem_test.c +++ b/tests/isc/mem_test.c @@ -127,7 +127,7 @@ ISC_RUN_TEST_IMPL(isc_mem_get) { } /* zeroed memory system tests */ -ISC_RUN_TEST_IMPL(isc_mem_get_zero) { +ISC_RUN_TEST_IMPL(isc_mem_cget_zero) { uint8_t *ptr; bool zeroed; uint8_t expected[4096] = { 0 }; @@ -147,12 +147,12 @@ ISC_RUN_TEST_IMPL(isc_mem_get_zero) { return; } - ptr = isc_mem_getx(mctx, sizeof(expected), ISC_MEM_ZERO); + ptr = isc_mem_cget(mctx, 1, sizeof(expected)); assert_memory_equal(ptr, expected, sizeof(expected)); isc_mem_put(mctx, ptr, sizeof(expected)); } -ISC_RUN_TEST_IMPL(isc_mem_allocate_zero) { +ISC_RUN_TEST_IMPL(isc_mem_callocate_zero) { uint8_t *ptr; bool zeroed; uint8_t expected[4096] = { 0 }; @@ -172,7 +172,7 @@ ISC_RUN_TEST_IMPL(isc_mem_allocate_zero) { return; } - ptr = isc_mem_allocatex(mctx, sizeof(expected), ISC_MEM_ZERO); + ptr = isc_mem_callocate(mctx, 1, sizeof(expected)); assert_memory_equal(ptr, expected, sizeof(expected)); isc_mem_free(mctx, ptr); } @@ -251,26 +251,26 @@ ISC_RUN_TEST_IMPL(isc_mem_reget) { isc_mem_put(mctx, data, REGET_SHRINK_SIZE); } -ISC_RUN_TEST_IMPL(isc_mem_reallocatex) { +ISC_RUN_TEST_IMPL(isc_mem_reallocate) { uint8_t *data = NULL; /* test that we can reallocate NULL */ - data = isc_mem_reallocatex(mctx, NULL, REGET_INIT_SIZE, 0); + data = isc_mem_reallocate(mctx, NULL, REGET_INIT_SIZE); assert_non_null(data); isc_mem_free(mctx, data); /* test that we can re-get a zero-length allocation */ - data = isc_mem_allocatex(mctx, 0, 0); + data = isc_mem_allocate(mctx, 0); assert_non_null(data); - data = isc_mem_reallocatex(mctx, data, REGET_INIT_SIZE, 0); + data = isc_mem_reallocate(mctx, data, REGET_INIT_SIZE); assert_non_null(data); for (size_t i = 0; i < REGET_INIT_SIZE; i++) { data[i] = i % UINT8_MAX; } - data = isc_mem_reallocatex(mctx, data, REGET_GROW_SIZE, 0); + data = isc_mem_reallocate(mctx, data, REGET_GROW_SIZE); assert_non_null(data); for (size_t i = 0; i < REGET_INIT_SIZE; i++) { @@ -281,7 +281,7 @@ ISC_RUN_TEST_IMPL(isc_mem_reallocatex) { data[i - 1] = i % UINT8_MAX; } - data = isc_mem_reallocatex(mctx, data, REGET_SHRINK_SIZE, 0); + data = isc_mem_reallocate(mctx, data, REGET_SHRINK_SIZE); assert_non_null(data); for (size_t i = REGET_SHRINK_SIZE; i > 0; i--) { @@ -480,12 +480,12 @@ ISC_RUN_TEST_IMPL(isc_mem_benchmark) { ISC_TEST_LIST_START ISC_TEST_ENTRY(isc_mem_get) -ISC_TEST_ENTRY(isc_mem_get_zero) -ISC_TEST_ENTRY(isc_mem_allocate_zero) +ISC_TEST_ENTRY(isc_mem_cget_zero) +ISC_TEST_ENTRY(isc_mem_callocate_zero) ISC_TEST_ENTRY(isc_mem_inuse) ISC_TEST_ENTRY(isc_mem_zeroget) ISC_TEST_ENTRY(isc_mem_reget) -ISC_TEST_ENTRY(isc_mem_reallocatex) +ISC_TEST_ENTRY(isc_mem_reallocate) #if ISC_MEM_TRACKLINES ISC_TEST_ENTRY(isc_mem_noflags) diff --git a/tests/isc/ratelimiter_test.c b/tests/isc/ratelimiter_test.c index 5e591605e2..4bc987970f 100644 --- a/tests/isc/ratelimiter_test.c +++ b/tests/isc/ratelimiter_test.c @@ -105,7 +105,8 @@ ISC_LOOP_TEARDOWN_IMPL(ratelimiter_enqueue) { assert_int_equal(ticks, 1); } ISC_LOOP_TEST_SETUP_TEARDOWN_IMPL(ratelimiter_enqueue) { isc_result_t result; - rlstat_t *rlstat = isc_mem_getx(mctx, sizeof(*rlstat), ISC_MEM_ZERO); + rlstat_t *rlstat = isc_mem_get(mctx, sizeof(*rlstat)); + *rlstat = (rlstat_t){ 0 }; result = isc_ratelimiter_enqueue(rl, mainloop, tick, rlstat, &rlstat->event); @@ -123,8 +124,9 @@ ISC_LOOP_TEARDOWN_IMPL(ratelimiter_enqueue_shutdown) { } ISC_LOOP_TEST_SETUP_TEARDOWN_IMPL(ratelimiter_enqueue_shutdown) { - rlstat_t *rlstat = isc_mem_getx(mctx, sizeof(*rlstat), ISC_MEM_ZERO); isc_rlevent_t *event = NULL; + rlstat_t *rlstat = isc_mem_get(mctx, sizeof(*rlstat)); + *rlstat = (rlstat_t){ 0 }; expect_assert_failure( isc_ratelimiter_enqueue(NULL, mainloop, tick, NULL, &event)); @@ -156,8 +158,9 @@ ISC_LOOP_TEARDOWN_IMPL(ratelimiter_dequeue) { /* */ } ISC_LOOP_TEST_SETUP_TEARDOWN_IMPL(ratelimiter_dequeue) { - rlstat_t *rlstat = isc_mem_getx(mctx, sizeof(*rlstat), ISC_MEM_ZERO); isc_rlevent_t *fake = isc_mem_get(mctx, sizeof(*fake)); + rlstat_t *rlstat = isc_mem_get(mctx, sizeof(*rlstat)); + *rlstat = (rlstat_t){ 0 }; assert_int_equal(isc_ratelimiter_enqueue(rl, mainloop, tick, rlstat, &rlstat->event), @@ -225,12 +228,14 @@ ISC_LOOP_TEST_SETUP_TEARDOWN_IMPL(ratelimiter_pertick_interval) { isc_ratelimiter_setpertic(rl, 1); isc_ratelimiter_setpushpop(rl, false); - rlstat = isc_mem_getx(mctx, sizeof(*rlstat), ISC_MEM_ZERO); + rlstat = isc_mem_get(mctx, sizeof(*rlstat)); + *rlstat = (rlstat_t){ 0 }; assert_int_equal(isc_ratelimiter_enqueue(rl, mainloop, tock, rlstat, &rlstat->event), ISC_R_SUCCESS); - rlstat = isc_mem_getx(mctx, sizeof(*rlstat), ISC_MEM_ZERO); + rlstat = isc_mem_get(mctx, sizeof(*rlstat)); + *rlstat = (rlstat_t){ 0 }; assert_int_equal(isc_ratelimiter_enqueue(rl, mainloop, tick, rlstat, &rlstat->event), ISC_R_SUCCESS); @@ -258,12 +263,14 @@ ISC_LOOP_TEST_SETUP_TEARDOWN_IMPL(ratelimiter_pushpop) { isc_ratelimiter_setpertic(rl, 2); isc_ratelimiter_setpushpop(rl, true); - rlstat = isc_mem_getx(mctx, sizeof(*rlstat), ISC_MEM_ZERO); + rlstat = isc_mem_get(mctx, sizeof(*rlstat)); + *rlstat = (rlstat_t){ 0 }; assert_int_equal(isc_ratelimiter_enqueue(rl, mainloop, tock, rlstat, &rlstat->event), ISC_R_SUCCESS); - rlstat = isc_mem_getx(mctx, sizeof(*rlstat), ISC_MEM_ZERO); + rlstat = isc_mem_get(mctx, sizeof(*rlstat)); + *rlstat = (rlstat_t){ 0 }; assert_int_equal(isc_ratelimiter_enqueue(rl, mainloop, tick, rlstat, &rlstat->event), ISC_R_SUCCESS);