diff options
author | Campbell Barton <ideasman42@gmail.com> | 2013-08-26 13:37:15 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2013-08-26 13:37:15 +0400 |
commit | 762e7da97606190bfd533e126f510ac623703523 (patch) | |
tree | 1ccbb5722b3f62ec6cef973db1bf1d687ed2ce06 /source/blender | |
parent | f7388c1f2e7a0a23b40a4a2f8b1e2bfbca17e1b9 (diff) |
add some safety checks in debug mode to ensure sets/hashes aren't confused.
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenlib/intern/BLI_ghash.c | 24 | ||||
-rw-r--r-- | source/blender/blenlib/intern/edgehash.c | 25 |
2 files changed, 42 insertions, 7 deletions
diff --git a/source/blender/blenlib/intern/BLI_ghash.c b/source/blender/blenlib/intern/BLI_ghash.c index 2adc24b657c..5462a61d967 100644 --- a/source/blender/blenlib/intern/BLI_ghash.c +++ b/source/blender/blenlib/intern/BLI_ghash.c @@ -61,6 +61,16 @@ const unsigned int hashsizes[] = { 268435459 }; +/* internal flag to ensure sets values aren't used */ +#ifndef NDEBUG +# define GHASH_FLAG_IS_SET (1 << 8) +# define IS_GHASH_ASSERT(gh) BLI_assert((gh->flag & GHASH_FLAG_IS_SET) == 0) +// # define IS_GSET_ASSERT(gs) BLI_assert((gs->flag & GHASH_FLAG_IS_SET) != 0) +#else +# define IS_GHASH_ASSERT(gh) +// # define IS_GSET_ASSERT(eh) +#endif + /***/ typedef struct Entry { @@ -346,6 +356,7 @@ bool BLI_ghash_reinsert(GHash *gh, void *key, void *val, GHashKeyFreeFP keyfreef void *BLI_ghash_lookup(GHash *gh, const void *key) { Entry *e = ghash_lookup_entry(gh, key); + IS_GHASH_ASSERT(gh); return e ? e->val : NULL; } @@ -362,6 +373,7 @@ void *BLI_ghash_lookup(GHash *gh, const void *key) void **BLI_ghash_lookup_p(GHash *gh, const void *key) { Entry *e = ghash_lookup_entry(gh, key); + IS_GHASH_ASSERT(gh); return e ? &e->val : NULL; } @@ -399,6 +411,7 @@ void *BLI_ghash_pop(GHash *gh, void *key, GHashKeyFreeFP keyfreefp) { const unsigned int hash = ghash_keyhash(gh, key); Entry *e = ghash_remove_ex(gh, key, keyfreefp, NULL, hash); + IS_GHASH_ASSERT(gh); if (e) { void *val = e->val; BLI_mempool_free(gh->entrypool, e); @@ -462,7 +475,6 @@ void BLI_ghash_clear(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfree void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp) { BLI_assert((int)gh->nentries == BLI_mempool_count(gh->entrypool)); - if (keyfreefp || valfreefp) ghash_free_cb(gh, keyfreefp, valfreefp); @@ -773,9 +785,13 @@ GHash *BLI_ghash_pair_new(const char *info) GSet *BLI_gset_new_ex(GSetHashFP hashfp, GSetCmpFP cmpfp, const char *info, const unsigned int nentries_reserve) { - return (GSet *)ghash_new(hashfp, cmpfp, info, - nentries_reserve, - sizeof(Entry) - sizeof(void *)); + GSet *gs = (GSet *)ghash_new(hashfp, cmpfp, info, + nentries_reserve, + sizeof(Entry) - sizeof(void *)); +#ifndef NDEBUG + ((GHash *)gs)->flag |= GHASH_FLAG_IS_SET; +#endif + return gs; } GSet *BLI_gset_new(GSetHashFP hashfp, GSetCmpFP cmpfp, const char *info) diff --git a/source/blender/blenlib/intern/edgehash.c b/source/blender/blenlib/intern/edgehash.c index 8870bce435b..03822e894cf 100644 --- a/source/blender/blenlib/intern/edgehash.c +++ b/source/blender/blenlib/intern/edgehash.c @@ -55,6 +55,16 @@ static const unsigned int _ehash_hashsizes[] = { 268435459 }; +/* internal flag to ensure sets values aren't used */ +#ifndef NDEBUG +# define EDGEHASH_FLAG_IS_SET (1 << 8) +# define IS_EDGEHASH_ASSERT(eh) BLI_assert((eh->flag & EDGEHASH_FLAG_IS_SET) == 0) +// # define IS_EDGESET_ASSERT(es) BLI_assert((es->flag & EDGEHASH_FLAG_IS_SET) != 0) +#else +# define IS_EDGEHASH_ASSERT(eh) +// # define IS_EDGESET_ASSERT(es) +#endif + /* ensure v0 is smaller */ #define EDGE_ORD(v0, v1) \ if (v0 > v1) { \ @@ -242,6 +252,7 @@ void BLI_edgehash_insert(EdgeHash *eh, unsigned int v0, unsigned int v1, void *v { unsigned int hash; EdgeEntry *e; + IS_EDGEHASH_ASSERT(eh); EDGE_ORD(v0, v1); /* ensure v0 is smaller */ hash = edgehash_keyhash(eh, v0, v1); e = edgehash_insert_ex(eh, v0, v1, hash); @@ -256,6 +267,8 @@ bool BLI_edgehash_reinsert(EdgeHash *eh, unsigned int v0, unsigned int v1, void unsigned int hash; EdgeEntry *e; + IS_EDGEHASH_ASSERT(eh); + EDGE_ORD(v0, v1); /* ensure v0 is smaller */ hash = edgehash_keyhash(eh, v0, v1); @@ -278,6 +291,7 @@ bool BLI_edgehash_reinsert(EdgeHash *eh, unsigned int v0, unsigned int v1, void void **BLI_edgehash_lookup_p(EdgeHash *eh, unsigned int v0, unsigned int v1) { EdgeEntry *e = edgehash_lookup_entry(eh, v0, v1); + IS_EDGEHASH_ASSERT(eh); return e ? &e->val : NULL; } @@ -290,6 +304,7 @@ void **BLI_edgehash_lookup_p(EdgeHash *eh, unsigned int v0, unsigned int v1) void *BLI_edgehash_lookup(EdgeHash *eh, unsigned int v0, unsigned int v1) { EdgeEntry *e = edgehash_lookup_entry(eh, v0, v1); + IS_EDGEHASH_ASSERT(eh); return e ? e->val : NULL; } @@ -467,9 +482,13 @@ bool BLI_edgehashIterator_isDone(EdgeHashIterator *ehi) EdgeSet *BLI_edgeset_new_ex(const char *info, const unsigned int nentries_reserve) { - return (EdgeSet *)edgehash_new(info, - nentries_reserve, - sizeof(EdgeEntry) - sizeof(void *)); + EdgeSet *es = (EdgeSet *)edgehash_new(info, + nentries_reserve, + sizeof(EdgeEntry) - sizeof(void *)); +#ifndef NDEBUG + ((EdgeHash *)es)->flag |= EDGEHASH_FLAG_IS_SET; +#endif + return es; } EdgeSet *BLI_edgeset_new(const char *info) |