Use remove_i_from_array in a few more places

Also simplify the REMOVER macro
This commit is contained in:
Kovid Goyal 2019-06-07 14:25:33 +05:30
parent 4a55eb9e7f
commit 656916e0fa
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 15 additions and 24 deletions

7
glfw/internal.h vendored
View File

@ -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)

View File

@ -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

View File

@ -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);

View File

@ -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();
}