[Bf-blender-cvs] [0658659] master: GHash: BLI_ghash_ensure_p_ex now takes a pointer-to-key arg

Campbell Barton noreply at git.blender.org
Wed Mar 2 01:27:50 CET 2016


Commit: 0658659f74ad134bddbc982f7e1de0ff9b8bda66
Author: Campbell Barton
Date:   Wed Mar 2 11:12:48 2016 +1100
Branches: master
https://developer.blender.org/rB0658659f74ad134bddbc982f7e1de0ff9b8bda66

GHash: BLI_ghash_ensure_p_ex now takes a pointer-to-key arg

This is an alternative to passing a copy callback which is some times inconvenient.
Instead the caller can write to the key - needed when the key is duplicated memory.

Allows for minor optimization in ghash/gset use.

Also add BLI_gset_ensure_p_ex

===================================================================

M	source/blender/blenkernel/intern/icons.c
M	source/blender/blenlib/BLI_ghash.h
M	source/blender/blenlib/intern/BLI_ghash.c
M	source/blender/bmesh/operators/bmo_subdivide_edgering.c
M	source/blender/imbuf/intern/indexer.c

===================================================================

diff --git a/source/blender/blenkernel/intern/icons.c b/source/blender/blenkernel/intern/icons.c
index 45ebada..f3e86b4 100644
--- a/source/blender/blenkernel/intern/icons.c
+++ b/source/blender/blenkernel/intern/icons.c
@@ -273,9 +273,10 @@ PreviewImage *BKE_previewimg_cached_get(const char *name)
 PreviewImage *BKE_previewimg_cached_ensure(const char *name)
 {
 	PreviewImage *prv = NULL;
-	void **prv_p;
+	void **key_p, **prv_p;
 
-	if (!BLI_ghash_ensure_p_ex(gCachedPreviews, name, &prv_p, (GHashKeyCopyFP)BLI_strdup)) {
+	if (!BLI_ghash_ensure_p_ex(gCachedPreviews, name, &key_p, &prv_p)) {
+		*key_p = BLI_strdup(name);
 		*prv_p = BKE_previewimg_create();
 	}
 	prv = *prv_p;
diff --git a/source/blender/blenlib/BLI_ghash.h b/source/blender/blenlib/BLI_ghash.h
index f13e8cb..7e3a009 100644
--- a/source/blender/blenlib/BLI_ghash.h
+++ b/source/blender/blenlib/BLI_ghash.h
@@ -94,7 +94,7 @@ void  *BLI_ghash_lookup(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT;
 void  *BLI_ghash_lookup_default(GHash *gh, const void *key, void *val_default) ATTR_WARN_UNUSED_RESULT;
 void **BLI_ghash_lookup_p(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT;
 bool   BLI_ghash_ensure_p(GHash *gh, void *key, void ***r_val) ATTR_WARN_UNUSED_RESULT;
-bool   BLI_ghash_ensure_p_ex(GHash *gh, const void *key, void ***r_val, GHashKeyCopyFP keycopyfp) ATTR_WARN_UNUSED_RESULT;
+bool   BLI_ghash_ensure_p_ex(GHash *gh, const void *key, void ***r_key, void ***r_val) ATTR_WARN_UNUSED_RESULT;
 bool   BLI_ghash_remove(GHash *gh, const void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp);
 void   BLI_ghash_clear(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp);
 void   BLI_ghash_clear_ex(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp,
@@ -244,6 +244,7 @@ void   BLI_gset_flag_clear(GSet *gs, unsigned int flag);
 void   BLI_gset_free(GSet *gs, GSetKeyFreeFP keyfreefp);
 void   BLI_gset_insert(GSet *gh, void *key);
 bool   BLI_gset_add(GSet *gs, void *key);
+bool   BLI_gset_ensure_p_ex(GSet *gs, const void *key, void ***r_key);
 bool   BLI_gset_reinsert(GSet *gh, void *key, GSetKeyFreeFP keyfreefp);
 bool   BLI_gset_haskey(GSet *gs, const void *key) ATTR_WARN_UNUSED_RESULT;
 bool   BLI_gset_pop(GSet *gs, GSetIterState *state, void **r_key) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
diff --git a/source/blender/blenlib/intern/BLI_ghash.c b/source/blender/blenlib/intern/BLI_ghash.c
index 21b0a58..0cb3020 100644
--- a/source/blender/blenlib/intern/BLI_ghash.c
+++ b/source/blender/blenlib/intern/BLI_ghash.c
@@ -836,11 +836,13 @@ bool BLI_ghash_ensure_p(GHash *gh, void *key, void ***r_val)
 }
 
 /**
- * A version of #BLI_ghash_ensure_p copies the key on insertion.
+ * A version of #BLI_ghash_ensure_p that allows caller to re-assign the key.
+ * Typically used when the key is to be duplicated.
+ *
+ * \warning Caller _must_ write to \a r_key when returning false.
  */
 bool BLI_ghash_ensure_p_ex(
-        GHash *gh, const void *key, void ***r_val,
-        GHashKeyCopyFP keycopyfp)
+        GHash *gh, const void *key, void ***r_key, void ***r_val)
 {
 	const unsigned int hash = ghash_keyhash(gh, key);
 	const unsigned int bucket_index = ghash_bucket_index(gh, hash);
@@ -848,11 +850,11 @@ bool BLI_ghash_ensure_p_ex(
 	const bool haskey = (e != NULL);
 
 	if (!haskey) {
-		/* keycopyfp(key) is the only difference to BLI_ghash_ensure_p */
 		e = BLI_mempool_alloc(gh->entrypool);
-		ghash_insert_ex_keyonly_entry(gh, keycopyfp(key), bucket_index, (Entry *)e);
+		ghash_insert_ex_keyonly_entry(gh, NULL, bucket_index, (Entry *)e);
 	}
 
+	*r_key = &e->e.key;
 	*r_val = &e->val;
 	return haskey;
 }
@@ -1391,6 +1393,28 @@ bool BLI_gset_add(GSet *gs, void *key)
 }
 
 /**
+ * Set counterpart to #BLI_ghash_ensure_p_ex.
+ * similar to BLI_gset_add, except it returns the key pointer.
+ *
+ * \warning Caller _must_ write to \a r_key when returning false.
+ */
+bool BLI_gset_ensure_p_ex(GSet *gs, const void *key, void ***r_key)
+{
+	const unsigned int hash = ghash_keyhash((GHash *)gs, key);
+	const unsigned int bucket_index = ghash_bucket_index((GHash *)gs, hash);
+	GSetEntry *e = (GSetEntry *)ghash_lookup_entry_ex((GHash *)gs, key, bucket_index);
+	const bool haskey = (e != NULL);
+
+	if (!haskey) {
+		e = BLI_mempool_alloc(((GHash *)gs)->entrypool);
+		ghash_insert_ex_keyonly_entry((GHash *)gs, NULL, bucket_index, (Entry *)e);
+	}
+
+	*r_key = &e->key;
+	return haskey;
+}
+
+/**
  * Adds the key to the set (duplicates are managed).
  * Matching #BLI_ghash_reinsert
  *
diff --git a/source/blender/bmesh/operators/bmo_subdivide_edgering.c b/source/blender/bmesh/operators/bmo_subdivide_edgering.c
index 0e619b4..b5a95ad 100644
--- a/source/blender/bmesh/operators/bmo_subdivide_edgering.c
+++ b/source/blender/bmesh/operators/bmo_subdivide_edgering.c
@@ -258,11 +258,10 @@ static GSet *bm_edgering_pair_calc(BMesh *bm, ListBase *eloops_rim)
 					if (pair_test.first > pair_test.second)
 						SWAP(const void *, pair_test.first, pair_test.second);
 
-					if (!BLI_gset_haskey(eloop_pair_gs, &pair_test)) {
-						GHashPair *pair = BLI_ghashutil_pairalloc(pair_test.first, pair_test.second);
-						BLI_gset_insert(eloop_pair_gs, pair);
+					void **pair_key_p;
+					if (!BLI_gset_ensure_p_ex(eloop_pair_gs, &pair_test, &pair_key_p)) {
+						*pair_key_p = BLI_ghashutil_pairalloc(pair_test.first, pair_test.second);
 					}
-
 				}
 			}
 		}
diff --git a/source/blender/imbuf/intern/indexer.c b/source/blender/imbuf/intern/indexer.c
index a6012b1..e1b3abc 100644
--- a/source/blender/imbuf/intern/indexer.c
+++ b/source/blender/imbuf/intern/indexer.c
@@ -1167,12 +1167,13 @@ IndexBuildContext *IMB_anim_index_rebuild_context(struct anim *anim, IMB_Timecod
 				char filename[FILE_MAX];
 				get_proxy_filename(anim, proxy_size, filename, false);
 
-				if (BLI_gset_haskey(file_list, filename)) {
-					proxy_sizes_to_build &= ~proxy_size;
-					printf("Proxy: %s already registered for generation, skipping\n", filename);
+				void **filename_key_p;
+				if (!BLI_gset_ensure_p_ex(file_list, filename, &filename_key_p)) {
+					*filename_key_p = BLI_strdup(filename);
 				}
 				else {
-					BLI_gset_insert(file_list, BLI_strdup(filename));
+					proxy_sizes_to_build &= ~proxy_size;
+					printf("Proxy: %s already registered for generation, skipping\n", filename);
 				}
 			}
 		}




More information about the Bf-blender-cvs mailing list