From 89cba92d13fbc5e059336fd054be30e50d8a2621 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Fri, 6 Jul 2018 12:53:06 -0700 Subject: fixes #581 nni_idhash could use 32-bit variant --- src/core/idhash.c | 17 +++++++++++++++++ src/core/idhash.h | 3 +++ 2 files changed, 20 insertions(+) (limited to 'src/core') diff --git a/src/core/idhash.c b/src/core/idhash.c index 0de83eb1..0ba9056c 100644 --- a/src/core/idhash.c +++ b/src/core/idhash.c @@ -306,3 +306,20 @@ nni_idhash_alloc(nni_idhash *h, uint64_t *idp, void *val) return (rv); } + +int +nni_idhash_alloc32(nni_idhash *h, uint32_t *idp, void *val) +{ + uint64_t id64; + int rv; + + if ((rv = nni_idhash_alloc(h, &id64, val)) == 0) { + if (id64 > 0xffffffffull) { + nni_idhash_remove(h, id64); + rv = NNG_EINVAL; + } else { + *idp = (uint32_t) id64; + } + } + return (rv); +} diff --git a/src/core/idhash.h b/src/core/idhash.h index 36797a76..5b9ed593 100644 --- a/src/core/idhash.h +++ b/src/core/idhash.h @@ -34,6 +34,9 @@ extern int nni_idhash_remove(nni_idhash *, uint64_t); extern int nni_idhash_insert(nni_idhash *, uint64_t, void *); extern int nni_idhash_alloc(nni_idhash *, uint64_t *, void *); +// 32-bit version of idhash -- limits must have been set accordingly. +extern int nni_idhash_alloc32(nni_idhash *, uint32_t *, void *); + extern size_t nni_idhash_count(nni_idhash *); #endif // CORE_IDHASH_H -- cgit v1.2.3-70-g09d2