From 3c7a71772c2b25aa686d29e88b87c82dc956cad6 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 7 Jun 2019 10:01:10 +0530 Subject: [PATCH] Wrap memmove in glfw with a nice safe macro --- glfw/cocoa_init.m | 5 +---- glfw/internal.h | 6 ++++++ glfw/monitor.c | 5 +---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/glfw/cocoa_init.m b/glfw/cocoa_init.m index f1441a9bd..44c40e732 100644 --- a/glfw/cocoa_init.m +++ b/glfw/cocoa_init.m @@ -516,10 +516,7 @@ remove_timer_at(size_t idx) { Timer *t = timers + idx; if (t->os_timer) { [t->os_timer invalidate]; t->os_timer = NULL; } if (t->callback_data && t->free_callback_data) { t->free_callback_data(t->id, t->callback_data); t->callback_data = NULL; } - num_timers--; - if (idx < num_timers) { - memmove(timers + idx, timers + idx + 1, sizeof(timers[0]) * (num_timers - idx)); - } + remove_i_from_array(timers, idx, num_timers); } } diff --git a/glfw/internal.h b/glfw/internal.h index e93d8c157..7cee164af 100644 --- a/glfw/internal.h +++ b/glfw/internal.h @@ -196,6 +196,12 @@ typedef void (APIENTRY * PFN_vkVoidFunction)(void); #error "No supported window creation API selected" #endif +#define remove_i_from_array(array, i, count) { \ + count--; \ + if (i < count) { \ + memmove(array + i, array + i + 1, sizeof(array[0]) * (count - 1)); \ + }} + // 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/glfw/monitor.c b/glfw/monitor.c index ae0ce3c16..3fe9c03c0 100644 --- a/glfw/monitor.c +++ b/glfw/monitor.c @@ -127,10 +127,7 @@ void _glfwInputMonitor(_GLFWmonitor* monitor, int action, int placement) { if (_glfw.monitors[i] == monitor) { - _glfw.monitorCount--; - memmove(_glfw.monitors + i, - _glfw.monitors + i + 1, - (_glfw.monitorCount - i) * sizeof(_GLFWmonitor*)); + remove_i_from_array(_glfw.monitors, i, _glfw.monitorCount); break; } }