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
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -1390,6 +1392,28 @@ bool BLI_gset_add(GSet *gs, void *key)
|
||||
return ghash_insert_safe_keyonly((GHash *)gs, key, false, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user