Cleanup: simplify lasso reallocation
authorCampbell Barton <ideasman42@gmail.com>
Mon, 16 Oct 2017 02:18:50 +0000 (13:18 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 16 Oct 2017 03:27:10 +0000 (14:27 +1100)
Remove unneeded define, double allocations when increasing.

source/blender/windowmanager/WM_types.h
source/blender/windowmanager/intern/wm_gesture.c
source/blender/windowmanager/intern/wm_operators.c
source/blender/windowmanager/wm.h

index 1c8d12973c6a7c58b36eb25ceaddb39cbc5f0bf4..d8ec26f55da4299b06fdb5710c2b8bab2e97e2fd 100644 (file)
@@ -412,7 +412,7 @@ typedef struct wmGesture {
        int type;               /* gesture type define */
        int swinid;             /* initial subwindow id where it started */
        int points;             /* optional, amount of points stored */
        int type;               /* gesture type define */
        int swinid;             /* initial subwindow id where it started */
        int points;             /* optional, amount of points stored */
-       int size;               /* optional, maximum amount of points stored */
+       int points_alloc;       /* optional, maximum amount of points stored */
 
        /* For modal operators which may be running idle, waiting for an event to activate the gesture.
         * Typically this is set when the user is click-dragging the gesture (border and circle select for eg). */
 
        /* For modal operators which may be running idle, waiting for an event to activate the gesture.
         * Typically this is set when the user is click-dragging the gesture (border and circle select for eg). */
index 311b501a1791a5799837763121e81b4e4d5f2183..f66efa72908a4cffc3960a9e0b0635421fda47d4 100644 (file)
@@ -97,11 +97,11 @@ wmGesture *WM_gesture_new(bContext *C, const wmEvent *event, int type)
        }
        else if (ELEM(type, WM_GESTURE_LINES, WM_GESTURE_LASSO)) {
                short *lasso;
        }
        else if (ELEM(type, WM_GESTURE_LINES, WM_GESTURE_LASSO)) {
                short *lasso;
-               gesture->customdata = lasso = MEM_callocN(2 * sizeof(short) * WM_LASSO_MIN_POINTS, "lasso points");
+               gesture->points_alloc = 1024;
+               gesture->customdata = lasso = MEM_mallocN(sizeof(short[2]) * gesture->points_alloc, "lasso points");
                lasso[0] = event->x - sx;
                lasso[1] = event->y - sy;
                gesture->points = 1;
                lasso[0] = event->x - sx;
                lasso[1] = event->y - sy;
                gesture->points = 1;
-               gesture->size = WM_LASSO_MIN_POINTS;
        }
        
        return gesture;
        }
        
        return gesture;
index 330a18eeda53f26f946299e78045aa62dc640463..6fdc0a0a8c0c4f1c623aaff84057ed75baada11c 100644 (file)
@@ -2692,28 +2692,24 @@ int WM_gesture_lasso_modal(bContext *C, wmOperator *op, const wmEvent *event)
        switch (event->type) {
                case MOUSEMOVE:
                case INBETWEEN_MOUSEMOVE:
        switch (event->type) {
                case MOUSEMOVE:
                case INBETWEEN_MOUSEMOVE:
-                       
+
                        wm_gesture_tag_redraw(C);
                        wm_gesture_tag_redraw(C);
-                       
+
                        wm_subwindow_origin_get(CTX_wm_window(C), gesture->swinid, &sx, &sy);
 
                        wm_subwindow_origin_get(CTX_wm_window(C), gesture->swinid, &sx, &sy);
 
-                       if (gesture->points == gesture->size) {
-                               short *old_lasso = gesture->customdata;
-                               gesture->customdata = MEM_callocN(2 * sizeof(short) * (gesture->size + WM_LASSO_MIN_POINTS), "lasso points");
-                               memcpy(gesture->customdata, old_lasso, 2 * sizeof(short) * gesture->size);
-                               gesture->size = gesture->size + WM_LASSO_MIN_POINTS;
-                               MEM_freeN(old_lasso);
-                               // printf("realloc\n");
+                       if (gesture->points == gesture->points_alloc) {
+                               gesture->points_alloc *= 2;
+                               gesture->customdata = MEM_reallocN(gesture->customdata, sizeof(short[2]) * gesture->points_alloc);
                        }
 
                        {
                                int x, y;
                                short *lasso = gesture->customdata;
                        }
 
                        {
                                int x, y;
                                short *lasso = gesture->customdata;
-                               
+
                                lasso += (2 * gesture->points - 2);
                                x = (event->x - sx - lasso[0]);
                                y = (event->y - sy - lasso[1]);
                                lasso += (2 * gesture->points - 2);
                                x = (event->x - sx - lasso[0]);
                                y = (event->y - sy - lasso[1]);
-                               
+
                                /* make a simple distance check to get a smoother lasso
                                 * add only when at least 2 pixels between this and previous location */
                                if ((x * x + y * y) > 4) {
                                /* make a simple distance check to get a smoother lasso
                                 * add only when at least 2 pixels between this and previous location */
                                if ((x * x + y * y) > 4) {
@@ -2724,7 +2720,7 @@ int WM_gesture_lasso_modal(bContext *C, wmOperator *op, const wmEvent *event)
                                }
                        }
                        break;
                                }
                        }
                        break;
-                       
+
                case LEFTMOUSE:
                case MIDDLEMOUSE:
                case RIGHTMOUSE:
                case LEFTMOUSE:
                case MIDDLEMOUSE:
                case RIGHTMOUSE:
index 1206c2b194a28dbdf858c399753d63b106802c01..cb88ca3a474f250c6f81672f39f9051a21341dbb 100644 (file)
@@ -60,7 +60,6 @@ void wm_window_keymap(wmKeyConfig *keyconf);
 void wm_tweakevent_test(bContext *C, const wmEvent *event, int action);
 
 /* wm_gesture.c */
 void wm_tweakevent_test(bContext *C, const wmEvent *event, int action);
 
 /* wm_gesture.c */
-#define WM_LASSO_MIN_POINTS            1024
 void wm_gesture_draw(struct wmWindow *win);
 int wm_gesture_evaluate(wmGesture *gesture);
 void wm_gesture_tag_redraw(bContext *C);
 void wm_gesture_draw(struct wmWindow *win);
 int wm_gesture_evaluate(wmGesture *gesture);
 void wm_gesture_tag_redraw(bContext *C);