GHash: no need to malloc iterators
authorCampbell Barton <ideasman42@gmail.com>
Fri, 6 Feb 2015 04:31:08 +0000 (15:31 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Fri, 6 Feb 2015 04:34:36 +0000 (15:34 +1100)
source/blender/blenkernel/intern/pbvh.c
source/blender/editors/curve/editcurve.c
source/blender/editors/sculpt_paint/sculpt_uv.c
source/blender/editors/space_text/text_autocomplete.c
source/blender/editors/uvedit/uvedit_smart_stitch.c
source/blender/imbuf/intern/moviecache.c
source/blender/modifiers/intern/MOD_build.c
source/blender/modifiers/intern/MOD_mask.c
source/blender/render/intern/source/sss.c
source/blender/windowmanager/intern/wm.c

index bd95389..5e07437 100644 (file)
@@ -281,7 +281,7 @@ static int map_insert_vert(PBVH *bvh, GHash *map,
 /* Find vertices used by the faces in this node and update the draw buffers */
 static void build_mesh_leaf_node(PBVH *bvh, PBVHNode *node)
 {
-       GHashIterator *iter;
+       GHashIterator gh_iter;
        GHash *map;
        int i, j, totface;
        bool has_visible = false;
@@ -314,22 +314,17 @@ static void build_mesh_leaf_node(PBVH *bvh, PBVHNode *node)
                                         "bvh node vert indices");
 
        /* Build the vertex list, unique verts first */
-       for (iter = BLI_ghashIterator_new(map), i = 0;
-            BLI_ghashIterator_done(iter) == false;
-            BLI_ghashIterator_step(iter), ++i)
-       {
-               void *value = BLI_ghashIterator_getValue(iter);
+       GHASH_ITER (gh_iter, map) {
+               void *value = BLI_ghashIterator_getValue(&gh_iter);
                int ndx = GET_INT_FROM_POINTER(value);
 
                if (ndx < 0)
                        ndx = -ndx + node->uniq_verts - 1;
 
                node->vert_indices[ndx] =
-                   GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(iter));
+                       GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(&gh_iter));
        }
 
-       BLI_ghashIterator_free(iter);
-
        for (i = 0; i < totface; ++i) {
                MFace *f = bvh->faces + node->prim_indices[i];
                int sides = f->v4 ? 4 : 3;
index 51c621c..4aeeaa8 100644 (file)
@@ -643,25 +643,20 @@ static void keyData_switchDirectionNurb(Curve *cu, Nurb *nu)
 static GHash *dupli_keyIndexHash(GHash *keyindex)
 {
        GHash *gh;
-       GHashIterator *hashIter;
+       GHashIterator gh_iter;
 
        gh = BLI_ghash_ptr_new_ex("dupli_keyIndex gh", BLI_ghash_size(keyindex));
 
-       for (hashIter = BLI_ghashIterator_new(keyindex);
-            BLI_ghashIterator_done(hashIter) == false;
-            BLI_ghashIterator_step(hashIter))
-       {
-               void *cv = BLI_ghashIterator_getKey(hashIter);
-               CVKeyIndex *index = BLI_ghashIterator_getValue(hashIter);
-               CVKeyIndex *newIndex = MEM_callocN(sizeof(CVKeyIndex), "dupli_keyIndexHash index");
+       GHASH_ITER (gh_iter, keyindex) {
+               void *cv = BLI_ghashIterator_getKey(&gh_iter);
+               CVKeyIndex *index = BLI_ghashIterator_getValue(&gh_iter);
+               CVKeyIndex *newIndex = MEM_mallocN(sizeof(CVKeyIndex), "dupli_keyIndexHash index");
 
                memcpy(newIndex, index, sizeof(CVKeyIndex));
 
                BLI_ghash_insert(gh, cv, newIndex);
        }
 
-       BLI_ghashIterator_free(hashIter);
-
        return gh;
 }
 
index 23bc4a4..a9feb9f 100644 (file)
@@ -598,7 +598,7 @@ static UvSculptData *uv_sculpt_stroke_init(bContext *C, wmOperator *op, const wm
 
                UvEdge *edges;
                GHash *edgeHash;
-               GHashIterator *ghi;
+               GHashIterator gh_iter;
 
                bool do_island_optimization = !(ts->uv_sculpt_settings & UV_SCULPT_ALL_ISLANDS);
                int island_index = 0;
@@ -754,21 +754,15 @@ static UvSculptData *uv_sculpt_stroke_init(bContext *C, wmOperator *op, const wm
                        uv_sculpt_stroke_exit(C, op);
                        return NULL;
                }
-               ghi = BLI_ghashIterator_new(edgeHash);
-               if (!ghi) {
-                       BLI_ghash_free(edgeHash, NULL, NULL);
-                       MEM_freeN(edges);
-                       uv_sculpt_stroke_exit(C, op);
-                       return NULL;
-               }
+
                /* fill the edges with data */
-               for (i = 0; !BLI_ghashIterator_done(ghi); BLI_ghashIterator_step(ghi)) {
-                       data->uvedges[i++] = *((UvEdge *)BLI_ghashIterator_getKey(ghi));
+               i = 0;
+               GHASH_ITER (gh_iter, edgeHash) {
+                       data->uvedges[i++] = *((UvEdge *)BLI_ghashIterator_getKey(&gh_iter));
                }
                data->totalUvEdges = BLI_ghash_size(edgeHash);
 
                /* cleanup temporary stuff */
-               BLI_ghashIterator_free(ghi);
                BLI_ghash_free(edgeHash, NULL, NULL);
                MEM_freeN(edges);
 
index 19af71c..1637ae1 100644 (file)
@@ -207,18 +207,16 @@ static GHash *text_autocomplete_build(Text *text)
                }
 
                {
-                       GHashIterator *iter = BLI_ghashIterator_new(gh);
+                       GHashIterator gh_iter;
 
                        /* get the formatter for highlighting */
                        TextFormatType *tft;
                        tft = ED_text_format_get(text);
 
-                       for (; !BLI_ghashIterator_done(iter); BLI_ghashIterator_step(iter)) {
-                               const char *s = BLI_ghashIterator_getValue(iter);
+                       GHASH_ITER (gh_iter, gh) {
+                               const char *s = BLI_ghashIterator_getValue(&gh_iter);
                                texttool_suggest_add(s, tft->format_identifier(s));
                        }
-                       BLI_ghashIterator_free(iter);
-
                }
        }
 
index c5931cf..4cb1f89 100644 (file)
@@ -1601,7 +1601,7 @@ static int stitch_init(bContext *C, wmOperator *op)
        BMFace *efa;
        BMLoop *l;
        BMIter iter, liter;
-       GHashIterator *ghi;
+       GHashIterator gh_iter;
        UvEdge *all_edges;
        StitchState *state;
        Scene *scene = CTX_data_scene(C);
@@ -1748,14 +1748,11 @@ static int stitch_init(bContext *C, wmOperator *op)
                }
        }
 
-
-       ghi = BLI_ghashIterator_new(edge_hash);
        total_edges = BLI_ghash_size(edge_hash);
        state->edges = edges = MEM_mallocN(sizeof(*edges) * total_edges, "stitch_edges");
 
        /* I assume any system will be able to at least allocate an iterator :p */
        if (!edges) {
-               BLI_ghashIterator_free(ghi);
                state_delete(state);
                return 0;
        }
@@ -1763,12 +1760,12 @@ static int stitch_init(bContext *C, wmOperator *op)
        state->total_separate_edges = total_edges;
 
        /* fill the edges with data */
-       for (i = 0, BLI_ghashIterator_init(ghi, edge_hash); !BLI_ghashIterator_done(ghi); BLI_ghashIterator_step(ghi)) {
-               edges[i++] = *((UvEdge *)BLI_ghashIterator_getKey(ghi));
+       i = 0;
+       GHASH_ITER (gh_iter, edge_hash) {
+               edges[i++] = *((UvEdge *)BLI_ghashIterator_getKey(&gh_iter));
        }
 
        /* cleanup temporary stuff */
-       BLI_ghashIterator_free(ghi);
        MEM_freeN(all_edges);
 
        BLI_ghash_free(edge_hash, NULL, NULL);
index cb789cc..0708045 100644 (file)
@@ -142,15 +142,12 @@ static void moviecache_valfree(void *val)
 
 static void check_unused_keys(MovieCache *cache)
 {
-       GHashIterator *iter;
-
-       iter = BLI_ghashIterator_new(cache->hash);
-       while (!BLI_ghashIterator_done(iter)) {
-               MovieCacheKey *key = BLI_ghashIterator_getKey(iter);
-               MovieCacheItem *item = BLI_ghashIterator_getValue(iter);
-               int remove = 0;
+       GHashIterator gh_iter;
 
-               BLI_ghashIterator_step(iter);
+       GHASH_ITER(gh_iter, cache->hash) {
+               MovieCacheKey *key = BLI_ghashIterator_getKey(&gh_iter);
+               MovieCacheItem *item = BLI_ghashIterator_getValue(&gh_iter);
+               bool remove;
 
                remove = !item->ibuf;
 
@@ -161,8 +158,6 @@ static void check_unused_keys(MovieCache *cache)
                if (remove)
                        BLI_ghash_remove(cache->hash, key, moviecache_keyfree, moviecache_valfree);
        }
-
-       BLI_ghashIterator_free(iter);
 }
 
 static int compare_int(const void *av, const void *bv)
@@ -473,16 +468,13 @@ void IMB_moviecache_free(MovieCache *cache)
 
 void IMB_moviecache_cleanup(MovieCache *cache, bool (cleanup_check_cb) (ImBuf *ibuf, void *userkey, void *userdata), void *userdata)
 {
-       GHashIterator *iter;
+       GHashIterator gh_iter;
 
        check_unused_keys(cache);
 
-       iter = BLI_ghashIterator_new(cache->hash);
-       while (!BLI_ghashIterator_done(iter)) {
-               MovieCacheKey *key = BLI_ghashIterator_getKey(iter);
-               MovieCacheItem *item = BLI_ghashIterator_getValue(iter);
-
-               BLI_ghashIterator_step(iter);
+       GHASH_ITER(gh_iter, cache->hash) {
+               MovieCacheKey *key = BLI_ghashIterator_getKey(&gh_iter);
+               MovieCacheItem *item = BLI_ghashIterator_getValue(&gh_iter);
 
                if (cleanup_check_cb(item->ibuf, key->userkey, userdata)) {
                        PRINT("%s: cache '%s' remove item %p\n", __func__, cache->name, item);
@@ -490,8 +482,6 @@ void IMB_moviecache_cleanup(MovieCache *cache, bool (cleanup_check_cb) (ImBuf *i
                        BLI_ghash_remove(cache->hash, key, moviecache_keyfree, moviecache_valfree);
                }
        }
-
-       BLI_ghashIterator_free(iter);
 }
 
 /* get segments of cached frames. useful for debugging cache policies */
@@ -518,13 +508,12 @@ void IMB_moviecache_get_cache_segments(MovieCache *cache, int proxy, int render_
                int totframe = BLI_ghash_size(cache->hash);
                int *frames = MEM_callocN(totframe * sizeof(int), "movieclip cache frames");
                int a, totseg = 0;
-               GHashIterator *iter;
+               GHashIterator gh_iter;
 
-               iter = BLI_ghashIterator_new(cache->hash);
                a = 0;
-               while (!BLI_ghashIterator_done(iter)) {
-                       MovieCacheKey *key = BLI_ghashIterator_getKey(iter);
-                       MovieCacheItem *item = BLI_ghashIterator_getValue(iter);
+               GHASH_ITER(gh_iter, cache->hash) {
+                       MovieCacheKey *key = BLI_ghashIterator_getKey(&gh_iter);
+                       MovieCacheItem *item = BLI_ghashIterator_getValue(&gh_iter);
                        int framenr, curproxy, curflags;
 
                        if (item->ibuf) {
@@ -533,12 +522,8 @@ void IMB_moviecache_get_cache_segments(MovieCache *cache, int proxy, int render_
                                if (curproxy == proxy && curflags == render_flags)
                                        frames[a++] = framenr;
                        }
-
-                       BLI_ghashIterator_step(iter);
                }
 
-               BLI_ghashIterator_free(iter);
-
                qsort(frames, totframe, sizeof(int), compare_int);
 
                /* count */
index f30529b..1dc1a1f 100644 (file)
@@ -86,7 +86,7 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *UNUSED(ob),
        float frac;
        MPoly *mpoly_dst;
        MLoop *ml_dst, *ml_src /*, *mloop_dst */;
-       GHashIterator *hashIter;
+       GHashIterator gh_iter;
        /* maps vert indices in old mesh to indices in new mesh */
        GHash *vertHash = BLI_ghash_int_new("build ve apply gh");
        /* maps edge indices in new mesh to indices in old mesh */
@@ -230,15 +230,11 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *UNUSED(ob),
                                    BLI_ghash_size(edgeHash), 0, numLoops_dst, numFaces_dst);
 
        /* copy the vertices across */
-       for (hashIter = BLI_ghashIterator_new(vertHash);
-            BLI_ghashIterator_done(hashIter) == false;
-            BLI_ghashIterator_step(hashIter)
-            )
-       {
+       GHASH_ITER (gh_iter, vertHash) {
                MVert source;
                MVert *dest;
-               int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(hashIter));
-               int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(hashIter));
+               int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(&gh_iter));
+               int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(&gh_iter));
 
                source = mvert_src[oldIndex];
                dest = CDDM_get_vert(result, newIndex);
@@ -246,7 +242,6 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *UNUSED(ob),
                DM_copy_vert_data(dm, result, oldIndex, newIndex, 1);
                *dest = source;
        }
-       BLI_ghashIterator_free(hashIter);
        
        /* copy the edges across, remapping indices */
        for (i = 0; i < BLI_ghash_size(edgeHash); i++) {
index 254ca0b..174f3df 100644 (file)
@@ -99,7 +99,7 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
        const bool found_test = (mmd->flag & MOD_MASK_INV) == 0;
        DerivedMesh *result = NULL;
        GHash *vertHash = NULL, *edgeHash, *polyHash;
-       GHashIterator *hashIter;
+       GHashIterator gh_iter;
        MDeformVert *dvert, *dv;
        int numPolys = 0, numLoops = 0, numEdges = 0, numVerts = 0;
        int maxVerts, maxEdges, maxPolys;
@@ -291,14 +291,11 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
        
        /* using ghash-iterators, map data into new mesh */
        /* vertices */
-       for (hashIter = BLI_ghashIterator_new(vertHash);
-            BLI_ghashIterator_done(hashIter) == false;
-            BLI_ghashIterator_step(hashIter))
-       {
+       GHASH_ITER (gh_iter, vertHash) {
                MVert source;
                MVert *dest;
-               int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(hashIter));
-               int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(hashIter));
+               int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(&gh_iter));
+               int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(&gh_iter));
                
                dm->getVert(dm, oldIndex, &source);
                dest = &mvert_new[newIndex];
@@ -306,17 +303,13 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
                DM_copy_vert_data(dm, result, oldIndex, newIndex, 1);
                *dest = source;
        }
-       BLI_ghashIterator_free(hashIter);
                
        /* edges */
-       for (hashIter = BLI_ghashIterator_new(edgeHash);
-            BLI_ghashIterator_done(hashIter) == false;
-            BLI_ghashIterator_step(hashIter))
-       {
+       GHASH_ITER (gh_iter, edgeHash) {
                MEdge source;
                MEdge *dest;
-               int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(hashIter));
-               int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(hashIter));
+               int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(&gh_iter));
+               int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(&gh_iter));
                
                dm->getEdge(dm, oldIndex, &source);
                dest = &medge_new[newIndex];
@@ -327,15 +320,11 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
                DM_copy_edge_data(dm, result, oldIndex, newIndex, 1);
                *dest = source;
        }
-       BLI_ghashIterator_free(hashIter);
        
        /* faces */
-       for (hashIter = BLI_ghashIterator_new(polyHash);
-            BLI_ghashIterator_done(hashIter) == false;
-            BLI_ghashIterator_step(hashIter))
-       {
-               int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(hashIter));
-               int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(hashIter));
+       GHASH_ITER (gh_iter, polyHash) {
+               int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(&gh_iter));
+               int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(&gh_iter));
                MPoly *source = &mpoly[oldIndex];
                MPoly *dest = &mpoly_new[newIndex];
                int oldLoopIndex = source->loopstart;
@@ -354,8 +343,6 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
                }
        }
 
-       BLI_ghashIterator_free(hashIter);
-
        MEM_freeN(loop_mapping);
 
        /* why is this needed? - campbell */
index 8ea3a75..890b044 100644 (file)
@@ -1031,14 +1031,12 @@ void make_sss_tree(Render *re)
 void free_sss(Render *re)
 {
        if (re->sss_hash) {
-               GHashIterator *it= BLI_ghashIterator_new(re->sss_hash);
+               GHashIterator gh_iter;
 
-               while (!BLI_ghashIterator_done(it)) {
-                       sss_free_tree(BLI_ghashIterator_getValue(it));
-                       BLI_ghashIterator_step(it);
+               GHASH_ITER (gh_iter, re->sss_hash) {
+                       sss_free_tree(BLI_ghashIterator_getValue(&gh_iter));
                }
 
-               BLI_ghashIterator_free(it);
                BLI_ghash_free(re->sss_hash, NULL, NULL);
                re->sss_hash= NULL;
        }
index a6a3c6b..34069e0 100644 (file)
@@ -250,15 +250,14 @@ void WM_uilisttype_init(void)
 
 void WM_uilisttype_free(void)
 {
-       GHashIterator *iter = BLI_ghashIterator_new(uilisttypes_hash);
+       GHashIterator gh_iter;
 
-       for (; !BLI_ghashIterator_done(iter); BLI_ghashIterator_step(iter)) {
-               uiListType *ult = BLI_ghashIterator_getValue(iter);
+       GHASH_ITER (gh_iter, uilisttypes_hash) {
+               uiListType *ult = BLI_ghashIterator_getValue(&gh_iter);
                if (ult->ext.free) {
                        ult->ext.free(ult->ext.data);
                }
        }
-       BLI_ghashIterator_free(iter);
 
        BLI_ghash_free(uilisttypes_hash, NULL, MEM_freeN);
        uilisttypes_hash = NULL;
@@ -309,15 +308,14 @@ void WM_menutype_init(void)
 
 void WM_menutype_free(void)
 {
-       GHashIterator *iter = BLI_ghashIterator_new(menutypes_hash);
+       GHashIterator gh_iter;
 
-       for (; !BLI_ghashIterator_done(iter); BLI_ghashIterator_step(iter)) {
-               MenuType *mt = BLI_ghashIterator_getValue(iter);
+       GHASH_ITER (gh_iter, menutypes_hash) {
+               MenuType *mt = BLI_ghashIterator_getValue(&gh_iter);
                if (mt->ext.free) {
                        mt->ext.free(mt->ext.data);
                }
        }
-       BLI_ghashIterator_free(iter);
 
        BLI_ghash_free(menutypes_hash, NULL, MEM_freeN);
        menutypes_hash = NULL;