From 87f871aae392279b9475ea74ffe3bb9149e02e7c Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 19 Jun 2012 12:43:08 +0930 Subject: ntdb: add NTDB_ATTRIBUTE_HASHSIZE Since we've given up on expansion, let them frob the hashsize again. We have attributes, so we should use them for optional stuff like this. Signed-off-by: Rusty Russell --- lib/ntdb/open.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'lib/ntdb/open.c') diff --git a/lib/ntdb/open.c b/lib/ntdb/open.c index 4b8c45298b..9de9e9b48c 100644 --- a/lib/ntdb/open.c +++ b/lib/ntdb/open.c @@ -273,6 +273,7 @@ _PUBLIC_ enum NTDB_ERROR ntdb_set_attribute(struct ntdb_context *ntdb, case NTDB_ATTRIBUTE_HASH: case NTDB_ATTRIBUTE_SEED: case NTDB_ATTRIBUTE_OPENHOOK: + case NTDB_ATTRIBUTE_HASHSIZE: return ntdb_logerr(ntdb, NTDB_ERR_EINVAL, NTDB_LOG_USE_ERROR, "ntdb_set_attribute:" @@ -281,7 +282,9 @@ _PUBLIC_ enum NTDB_ERROR ntdb_set_attribute(struct ntdb_context *ntdb, ? "NTDB_ATTRIBUTE_HASH" : attr->base.attr == NTDB_ATTRIBUTE_SEED ? "NTDB_ATTRIBUTE_SEED" - : "NTDB_ATTRIBUTE_OPENHOOK"); + : attr->base.attr == NTDB_ATTRIBUTE_OPENHOOK + ? "NTDB_ATTRIBUTE_OPENHOOK" + : "NTDB_ATTRIBUTE_HASHSIZE"); case NTDB_ATTRIBUTE_STATS: return ntdb_logerr(ntdb, NTDB_ERR_EINVAL, NTDB_LOG_USE_ERROR, @@ -349,6 +352,9 @@ _PUBLIC_ enum NTDB_ERROR ntdb_get_attribute(struct ntdb_context *ntdb, attr->alloc.free = ntdb->free_fn; attr->alloc.priv_data = ntdb->alloc_data; break; + case NTDB_ATTRIBUTE_HASHSIZE: + attr->hashsize.size = 1 << ntdb->hash_bits; + break; default: return ntdb_logerr(ntdb, NTDB_ERR_EINVAL, NTDB_LOG_USE_ERROR, @@ -475,6 +481,15 @@ static struct ntdb_context *alloc_ntdb(const union ntdb_attribute *attr, return default_alloc(NULL, len, NULL); } +static unsigned int next_pow2(uint64_t size) +{ + unsigned int bits = 1; + + while ((1ULL << bits) < size) + bits++; + return bits; +} + _PUBLIC_ struct ntdb_context *ntdb_open(const char *name, int ntdb_flags, int open_flags, mode_t mode, union ntdb_attribute *attr) @@ -528,6 +543,17 @@ _PUBLIC_ struct ntdb_context *ntdb_open(const char *name, int ntdb_flags, ntdb->openhook = attr->openhook.fn; ntdb->openhook_data = attr->openhook.data; break; + case NTDB_ATTRIBUTE_HASHSIZE: + ntdb->hash_bits = next_pow2(attr->hashsize.size); + if (ntdb->hash_bits > 31) { + ecode = ntdb_logerr(ntdb, NTDB_ERR_EINVAL, + NTDB_LOG_USE_ERROR, + "ntdb_open: hash_size %u" + " too large", + attr->hashsize.size); + goto fail; + } + break; default: /* These are set as normal. */ ecode = ntdb_set_attribute(ntdb, attr); -- cgit