From 14caa074a83fa2e659b3e490bf9bd31ff0b6b4b1 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Thu, 13 Jul 2017 18:17:02 -0700 Subject: Make idhash non-inlined (so we can add a mutex.) --- src/core/idhash.c | 44 +++++++++++++++++++++++++++++++++----------- src/core/idhash.h | 20 +++----------------- src/core/pipe.c | 1 + 3 files changed, 37 insertions(+), 28 deletions(-) (limited to 'src/core') diff --git a/src/core/idhash.c b/src/core/idhash.c index 9f3b4b03..b3974983 100644 --- a/src/core/idhash.c +++ b/src/core/idhash.c @@ -1,5 +1,6 @@ // -// Copyright 2016 Garrett D'Amore +// Copyright 2017 Garrett D'Amore +// Copyright 2017 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -17,9 +18,26 @@ struct nni_idhash_entry { void * ihe_val; }; +struct nni_idhash { + size_t ih_cap; + size_t ih_count; + size_t ih_load; + size_t ih_minload; // considers placeholders + size_t ih_maxload; + uint32_t ih_walkers; + uint32_t ih_minval; + uint32_t ih_maxval; + uint32_t ih_dynval; + nni_idhash_entry *ih_entries; +}; + int -nni_idhash_init(nni_idhash *h) +nni_idhash_init(nni_idhash **hp) { + nni_idhash *h; + if ((h = NNI_ALLOC_STRUCT(h)) == NULL) { + return (NNG_ENOMEM); + } h->ih_entries = NULL; h->ih_count = 0; h->ih_load = 0; @@ -30,18 +48,22 @@ nni_idhash_init(nni_idhash *h) h->ih_minval = 0; h->ih_maxval = 0xffffffff; h->ih_dynval = 0; + *hp = h; return (0); } void nni_idhash_fini(nni_idhash *h) { - NNI_ASSERT(h->ih_walkers == 0); - if (h->ih_entries != NULL) { - nni_free(h->ih_entries, h->ih_cap * sizeof(nni_idhash_entry)); - h->ih_entries = NULL; - h->ih_cap = h->ih_count = 0; - h->ih_load = h->ih_minload = h->ih_maxload = 0; + if (h != NULL) { + NNI_ASSERT(h->ih_walkers == 0); + if (h->ih_entries != NULL) { + NNI_FREE_STRUCTS(h->ih_entries, h->ih_cap); + h->ih_entries = NULL; + h->ih_cap = h->ih_count = 0; + h->ih_load = h->ih_minload = h->ih_maxload = 0; + } + NNI_FREE_STRUCT(h); } } @@ -50,7 +72,7 @@ nni_idhash_reclaim(nni_idhash *h) { // Reclaim the buffer if we want, but preserve the limits. if ((h->ih_count == 0) && (h->ih_cap != 0) && (h->ih_walkers == 0)) { - nni_free(h->ih_entries, h->ih_cap * sizeof(nni_idhash_entry)); + NNI_FREE_STRUCTS(h->ih_entries, h->ih_cap); h->ih_cap = 0; h->ih_entries = NULL; h->ih_minload = 0; @@ -124,7 +146,7 @@ nni_hash_resize(nni_idhash *h) } oldents = h->ih_entries; - newents = nni_alloc(sizeof(nni_idhash_entry) * newsize); + newents = NNI_ALLOC_STRUCTS(newents, newsize); if (newents == NULL) { return (NNG_ENOMEM); } @@ -157,7 +179,7 @@ nni_hash_resize(nni_idhash *h) } } if (oldsize != 0) { - nni_free(oldents, sizeof(nni_idhash_entry) * oldsize); + NNI_FREE_STRUCTS(oldents, oldsize); } return (0); } diff --git a/src/core/idhash.h b/src/core/idhash.h index bb3b1581..018420bc 100644 --- a/src/core/idhash.h +++ b/src/core/idhash.h @@ -1,5 +1,6 @@ // -// Copyright 2016 Garrett D'Amore +// Copyright 2017 Garrett D'Amore +// Copyright 2017 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -25,21 +26,6 @@ typedef struct nni_idhash nni_idhash; typedef struct nni_idhash_entry nni_idhash_entry; -// The details of the nni_idhash are "private". But they let us inline -// this into structures. -struct nni_idhash { - size_t ih_cap; - size_t ih_count; - size_t ih_load; - size_t ih_minload; // considers placeholders - size_t ih_maxload; - uint32_t ih_walkers; - uint32_t ih_minval; - uint32_t ih_maxval; - uint32_t ih_dynval; - nni_idhash_entry *ih_entries; -}; - // nni_idhash_walkfn is called when walking a hash table. If the // return value is non-zero, then nni_idhash_walk will terminate further // process and return that return value. The function takes the generic @@ -48,7 +34,7 @@ struct nni_idhash { // Note that the walkfn must not attempt to change the hash table. // The user must provide any locking needed. typedef int (*nni_idhash_walkfn)(void *, uint32_t, void *); -extern int nni_idhash_init(nni_idhash *); +extern int nni_idhash_init(nni_idhash **); extern void nni_idhash_fini(nni_idhash *); extern void nni_idhash_reclaim(nni_idhash *); extern void nni_idhash_set_limits(nni_idhash *, uint32_t, uint32_t, uint32_t); diff --git a/src/core/pipe.c b/src/core/pipe.c index 702e8eb1..91b78192 100644 --- a/src/core/pipe.c +++ b/src/core/pipe.c @@ -1,5 +1,6 @@ // // Copyright 2017 Garrett D'Amore +// Copyright 2017 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this -- cgit v1.2.3-70-g09d2