From 656916e0faa23c7b8e44a9beea3b7f3f1ed34b80 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 7 Jun 2019 14:25:33 +0530 Subject: [PATCH] Use remove_i_from_array in a few more places Also simplify the REMOVER macro --- glfw/internal.h | 7 ++++--- kitty/data-types.h | 6 +++--- kitty/graphics.c | 12 ++---------- kitty/state.c | 14 ++++++-------- 4 files changed, 15 insertions(+), 24 deletions(-) diff --git a/glfw/internal.h b/glfw/internal.h index 9b6d50b9f..bc8c15781 100644 --- a/glfw/internal.h +++ b/glfw/internal.h @@ -197,11 +197,12 @@ typedef void (APIENTRY * PFN_vkVoidFunction)(void); #endif #define remove_i_from_array(array, i, count) { \ - count--; \ - if (i < count) { \ - memmove(array + i, array + i + 1, sizeof(array[0]) * (count - i)); \ + (count)--; \ + if ((i) < (count)) { \ + memmove((array) + (i), (array) + (i) + 1, sizeof((array)[0]) * ((count) - (i))); \ }} + // Constructs a version number string from the public header macros #define _GLFW_CONCAT_VERSION(m, n, r) #m "." #n "." #r #define _GLFW_MAKE_VERSION(m, n, r) _GLFW_CONCAT_VERSION(m, n, r) diff --git a/kitty/data-types.h b/kitty/data-types.h index f9ce1e834..8c46eb6a1 100644 --- a/kitty/data-types.h +++ b/kitty/data-types.h @@ -259,9 +259,9 @@ typedef struct {FONTS_DATA_HEAD} *FONTS_DATA_HANDLE; } #define remove_i_from_array(array, i, count) { \ - count--; \ - if (i < count) { \ - memmove(array + i, array + i + 1, sizeof(array[0]) * (count - i)); \ + (count)--; \ + if ((i) < (count)) { \ + memmove((array) + (i), (array) + (i) + 1, sizeof((array)[0]) * ((count) - (i))); \ }} // Global functions diff --git a/kitty/graphics.c b/kitty/graphics.c index 4f619725f..8fc2991d6 100644 --- a/kitty/graphics.c +++ b/kitty/graphics.c @@ -77,14 +77,6 @@ dealloc(GraphicsManager* self) { static size_t internal_id_counter = 1; -static inline void -remove_from_array(void *array, size_t item_size, size_t idx, size_t array_count) { - size_t num_to_right = array_count - 1 - idx; - uint8_t *p = (uint8_t*)array; - if (num_to_right > 0) memmove(p + (idx * item_size), p + ((idx + 1) * item_size), num_to_right * item_size); - memset(p + (item_size * (array_count - 1)), 0, item_size); -} - static inline Image* img_by_internal_id(GraphicsManager *self, size_t id) { for (size_t i = 0; i < self->image_count; i++) { @@ -104,7 +96,7 @@ img_by_client_id(GraphicsManager *self, uint32_t id) { static inline void remove_image(GraphicsManager *self, size_t idx) { free_image(self, self->images + idx); - remove_from_array(self->images, sizeof(Image), idx, self->image_count--); + remove_i_from_array(self->images, idx, self->image_count); self->layers_dirty = true; } @@ -607,7 +599,7 @@ filter_refs(GraphicsManager *self, const void* data, bool free_images, bool (*fi for (j = img->refcnt; j-- > 0;) { ref = img->refs + j; if (filter_func(ref, img, data, cell)) { - remove_from_array(img->refs, sizeof(ImageRef), j, img->refcnt--); + remove_i_from_array(img->refs, j, img->refcnt); } } if (img->refcnt == 0 && (free_images || img->client_id == 0)) remove_image(self, i); diff --git a/kitty/state.c b/kitty/state.c index 1102fd8f5..31e815628 100644 --- a/kitty/state.c +++ b/kitty/state.c @@ -10,14 +10,12 @@ GlobalState global_state = {{0}}; -#define REMOVER(array, qid, count, structure, destroy, capacity) { \ +#define REMOVER(array, qid, count, destroy, capacity) { \ for (size_t i = 0; i < count; i++) { \ if (array[i].id == qid) { \ destroy(array + i); \ - memset(array + i, 0, sizeof(structure)); \ - size_t num_to_right = count - 1 - i; \ - if (num_to_right) memmove(array + i, array + i + 1, num_to_right * sizeof(structure)); \ - (count)--; \ + memset(array + i, 0, sizeof(array[0])); \ + remove_i_from_array(array, i, count); \ break; \ } \ }} @@ -140,7 +138,7 @@ destroy_window(Window *w) { static inline void remove_window_inner(Tab *tab, id_type id) { - REMOVER(tab->windows, id, tab->num_windows, Window, destroy_window, tab->capacity); + REMOVER(tab->windows, id, tab->num_windows, destroy_window, tab->capacity); } static inline void @@ -162,7 +160,7 @@ destroy_tab(Tab *tab) { static inline void remove_tab_inner(OSWindow *os_window, id_type id) { make_os_window_context_current(os_window); - REMOVER(os_window->tabs, id, os_window->num_tabs, Tab, destroy_tab, os_window->capacity); + REMOVER(os_window->tabs, id, os_window->num_tabs, destroy_tab, os_window->capacity); } static inline void @@ -194,7 +192,7 @@ remove_os_window(id_type os_window_id) { END_WITH_OS_WINDOW if (found) { WITH_OS_WINDOW_REFS - REMOVER(global_state.os_windows, os_window_id, global_state.num_os_windows, OSWindow, destroy_os_window_item, global_state.capacity); + REMOVER(global_state.os_windows, os_window_id, global_state.num_os_windows, destroy_os_window_item, global_state.capacity); END_WITH_OS_WINDOW_REFS update_os_window_references(); }