Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2014-02-02 10:08:26 +0400
committerCampbell Barton <ideasman42@gmail.com>2014-02-02 10:08:26 +0400
commitc4345a808c727e4a7052872bce91e35b74c9eea3 (patch)
treed864ab027e91d3881c633461a043a7bc9f26b515 /source/blender/blenlib/intern/smallhash.c
parentdcd90d67c8a6e6beae37fc02515b8c75942332ca (diff)
Smallhash: add reserve option to avoid resizing when size is known
Diffstat (limited to 'source/blender/blenlib/intern/smallhash.c')
-rw-r--r--source/blender/blenlib/intern/smallhash.c26
1 files changed, 25 insertions, 1 deletions
diff --git a/source/blender/blenlib/intern/smallhash.c b/source/blender/blenlib/intern/smallhash.c
index be5f70c7f49..38ebdc53f53 100644
--- a/source/blender/blenlib/intern/smallhash.c
+++ b/source/blender/blenlib/intern/smallhash.c
@@ -105,6 +105,16 @@ BLI_INLINE void smallhash_init_empty(SmallHash *sh)
}
}
+/**
+ * Increase initial bucket size to match a reserved amount.
+ */
+BLI_INLINE void smallhash_buckets_reserve(SmallHash *sh, const unsigned int nentries_reserve)
+{
+ while (smallhash_test_expand_buckets(nentries_reserve, sh->nbuckets)) {
+ sh->nbuckets = hashsizes[++sh->cursize];
+ }
+}
+
BLI_INLINE SmallHashEntry *smallhash_lookup(SmallHash *sh, const uintptr_t key)
{
SmallHashEntry *e;
@@ -181,7 +191,8 @@ BLI_INLINE void smallhash_resize_buckets(SmallHash *sh, const unsigned int nbuck
}
}
-void BLI_smallhash_init(SmallHash *sh)
+void BLI_smallhash_init_ex(SmallHash *sh,
+ const unsigned int nentries_reserve)
{
/* assume 'sh' is uninitialized */
@@ -191,9 +202,22 @@ void BLI_smallhash_init(SmallHash *sh)
sh->buckets = sh->buckets_stack;
+ if (nentries_reserve) {
+ smallhash_buckets_reserve(sh, nentries_reserve);
+
+ if (sh->nbuckets > SMSTACKSIZE) {
+ sh->buckets = MEM_mallocN(sizeof(*sh->buckets) * sh->nbuckets, __func__);
+ }
+ }
+
smallhash_init_empty(sh);
}
+void BLI_smallhash_init(SmallHash *sh)
+{
+ BLI_smallhash_init_ex(sh, 0);
+}
+
/*NOTE: does *not* free *sh itself! only the direct data!*/
void BLI_smallhash_release(SmallHash *sh)
{