SDL: Removing function macro SDL_TABLESIZE()

From 7bfecacc025692948ad664502e3b7ccdc04d2f0b Mon Sep 17 00:00:00 2001
From: Petar Popovic <[EMAIL REDACTED]>
Date: Mon, 29 Apr 2024 14:38:56 +0200
Subject: [PATCH] Removing function macro SDL_TABLESIZE()

---
 build-scripts/SDL_migration.cocci  | 4 ++++
 docs/README-migration.md           | 3 +++
 include/SDL3/SDL_oldnames.h        | 2 ++
 include/SDL3/SDL_stdinc.h          | 5 +++--
 src/video/haiku/SDL_bkeyboard.cc   | 4 ++--
 src/video/kmsdrm/SDL_kmsdrmdyn.c   | 6 +++---
 src/video/psp/SDL_pspevents.c      | 2 +-
 src/video/qnx/SDL_qnxkeyboard.c    | 2 +-
 src/video/wayland/SDL_waylanddyn.c | 4 ++--
 src/video/x11/SDL_x11dyn.c         | 6 +++---
 10 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/build-scripts/SDL_migration.cocci b/build-scripts/SDL_migration.cocci
index 1207b8f33bbd4..dae96a779a6bc 100644
--- a/build-scripts/SDL_migration.cocci
+++ b/build-scripts/SDL_migration.cocci
@@ -3142,6 +3142,10 @@ typedef SDL_Colour, SDL_Color;
   (...)
 @@
 @@
+- SDL_TABLESIZE
++ SDL_arraysize
+@@
+@@
 - SDL_iPhoneSetEventPump
 + SDL_iOSSetEventPump
   (...)
diff --git a/docs/README-migration.md b/docs/README-migration.md
index 7d45e93e7e3a2..f85db015d3fe7 100644
--- a/docs/README-migration.md
+++ b/docs/README-migration.md
@@ -1425,6 +1425,9 @@ SDL3 attempts to apply consistency to case-insensitive string functions. In SDL2
 
 Please note that the case-folding technique used by SDL3 will not produce correct results for the "Turkish 'I'"; this one letter is a surprisingly hard problem in the Unicode world, and since these functions do not specify the human language in use, we have chosen to ignore this problem.
 
+The following macros have been removed:
+* SDL_TABLESIZE() - use SDL_arraysize() instead
+
 The following functions have been renamed:
 * SDL_strtokr() => SDL_strtok_r()
 
diff --git a/include/SDL3/SDL_oldnames.h b/include/SDL3/SDL_oldnames.h
index 75e08e29794c9..4b9bc3893adb8 100644
--- a/include/SDL3/SDL_oldnames.h
+++ b/include/SDL3/SDL_oldnames.h
@@ -492,6 +492,7 @@
 #define SDL_SensorUpdate SDL_UpdateSensors
 
 /* ##SDL_stdinc.h */
+#define SDL_TABLESIZE SDL_arraysize
 #define SDL_strtokr SDL_strtok_r
 
 /* ##SDL_surface.h */
@@ -999,6 +1000,7 @@
 #define SDL_SensorUpdate SDL_SensorUpdate_renamed_SDL_UpdateSensors
 
 /* ##SDL_stdinc.h */
+#define SDL_TABLESIZE SDL_TABLESIZE_renamed_SDL_arraysize
 #define SDL_strtokr SDL_strtokr_renamed_SDL_strtok_r
 
 /* ##SDL_surface.h */
diff --git a/include/SDL3/SDL_stdinc.h b/include/SDL3/SDL_stdinc.h
index f3031e6f2a483..48812cb173a75 100644
--- a/include/SDL3/SDL_stdinc.h
+++ b/include/SDL3/SDL_stdinc.h
@@ -91,10 +91,11 @@ void *alloca(size_t);
 /**
  * The number of elements in an array.
  *
+ * NOTE: This macro double-evaluates the argument, so you should never have side effects in the parameter.
+ * 
  * \since This macro is available since SDL 3.0.0.
  */
-#define SDL_arraysize(array)    (sizeof(array)/sizeof(array[0]))
-#define SDL_TABLESIZE(table)    SDL_arraysize(table)
+#define SDL_arraysize(array) (sizeof(array)/sizeof(array[0]))
 
 /**
  * Macro useful for building other macros with strings in them.
diff --git a/src/video/haiku/SDL_bkeyboard.cc b/src/video/haiku/SDL_bkeyboard.cc
index db64fd054e627..0752df16a2374 100644
--- a/src/video/haiku/SDL_bkeyboard.cc
+++ b/src/video/haiku/SDL_bkeyboard.cc
@@ -40,7 +40,7 @@ static SDL_Scancode keymap[KEYMAP_SIZE];
 static int8 keystate[KEYMAP_SIZE];
 
 void HAIKU_InitOSKeymap(void) {
-        for ( uint i = 0; i < SDL_TABLESIZE(keymap); ++i ) {
+        for ( uint i = 0; i < SDL_arraysize(keymap); ++i ) {
             keymap[i] = SDL_SCANCODE_UNKNOWN;
         }
 
@@ -158,7 +158,7 @@ void HAIKU_InitOSKeymap(void) {
 }
 
 SDL_Scancode HAIKU_GetScancodeFromBeKey(int32 bkey) {
-    if (bkey > 0 && bkey < (int32)SDL_TABLESIZE(keymap)) {
+    if (bkey > 0 && bkey < (int32)SDL_arraysize(keymap)) {
         return keymap[bkey];
     } else {
         return SDL_SCANCODE_UNKNOWN;
diff --git a/src/video/kmsdrm/SDL_kmsdrmdyn.c b/src/video/kmsdrm/SDL_kmsdrmdyn.c
index b03ede6df9634..d50b5e413ff2b 100644
--- a/src/video/kmsdrm/SDL_kmsdrmdyn.c
+++ b/src/video/kmsdrm/SDL_kmsdrmdyn.c
@@ -48,7 +48,7 @@ static void *KMSDRM_GetSym(const char *fnname, int *pHasModule)
 {
     int i;
     void *fn = NULL;
-    for (i = 0; i < SDL_TABLESIZE(kmsdrmlibs); i++) {
+    for (i = 0; i < SDL_arraysize(kmsdrmlibs); i++) {
         if (kmsdrmlibs[i].lib) {
             fn = SDL_LoadFunction(kmsdrmlibs[i].lib, fnname);
             if (fn) {
@@ -97,7 +97,7 @@ void SDL_KMSDRM_UnloadSymbols(void)
 #include "SDL_kmsdrmsym.h"
 
 #ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
-            for (i = 0; i < SDL_TABLESIZE(kmsdrmlibs); i++) {
+            for (i = 0; i < SDL_arraysize(kmsdrmlibs); i++) {
                 if (kmsdrmlibs[i].lib) {
                     SDL_UnloadObject(kmsdrmlibs[i].lib);
                     kmsdrmlibs[i].lib = NULL;
@@ -118,7 +118,7 @@ int SDL_KMSDRM_LoadSymbols(void)
 #ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
         int i;
         int *thismod = NULL;
-        for (i = 0; i < SDL_TABLESIZE(kmsdrmlibs); i++) {
+        for (i = 0; i < SDL_arraysize(kmsdrmlibs); i++) {
             if (kmsdrmlibs[i].libname) {
                 kmsdrmlibs[i].lib = SDL_LoadObject(kmsdrmlibs[i].libname);
             }
diff --git a/src/video/psp/SDL_pspevents.c b/src/video/psp/SDL_pspevents.c
index 64b17ae9b85db..107284be8a12f 100644
--- a/src/video/psp/SDL_pspevents.c
+++ b/src/video/psp/SDL_pspevents.c
@@ -128,7 +128,7 @@ void PSP_InitOSKeymap(SDL_VideoDevice *_this)
 {
 #ifdef PSPIRKEYB
     int i;
-    for (i = 0; i < SDL_TABLESIZE(keymap); ++i) {
+    for (i = 0; i < SDL_arraysize(keymap); ++i) {
         keymap[i] = SDLK_UNKNOWN;
     }
 
diff --git a/src/video/qnx/SDL_qnxkeyboard.c b/src/video/qnx/SDL_qnxkeyboard.c
index 61b0e5aabfdbe..706b1fff9b05f 100644
--- a/src/video/qnx/SDL_qnxkeyboard.c
+++ b/src/video/qnx/SDL_qnxkeyboard.c
@@ -106,7 +106,7 @@ void handleKeyboardEvent(screen_event_t event)
     }
 
     // Skip unrecognized keys.
-    if ((val < 0) || (val >= SDL_TABLESIZE(key_to_sdl))) {
+    if ((val < 0) || (val >= SDL_arraysize(key_to_sdl))) {
         return;
     }
 
diff --git a/src/video/wayland/SDL_waylanddyn.c b/src/video/wayland/SDL_waylanddyn.c
index e6021f08cbc29..6cb4061587020 100644
--- a/src/video/wayland/SDL_waylanddyn.c
+++ b/src/video/wayland/SDL_waylanddyn.c
@@ -111,7 +111,7 @@ void SDL_WAYLAND_UnloadSymbols(void)
 #include "SDL_waylandsym.h"
 
 #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC
-            for (i = 0; i < SDL_TABLESIZE(waylandlibs); i++) {
+            for (i = 0; i < SDL_arraysize(waylandlibs); i++) {
                 if (waylandlibs[i].lib) {
                     SDL_UnloadObject(waylandlibs[i].lib);
                     waylandlibs[i].lib = NULL;
@@ -132,7 +132,7 @@ int SDL_WAYLAND_LoadSymbols(void)
 #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC
         int i;
         int *thismod = NULL;
-        for (i = 0; i < SDL_TABLESIZE(waylandlibs); i++) {
+        for (i = 0; i < SDL_arraysize(waylandlibs); i++) {
             if (waylandlibs[i].libname) {
                 waylandlibs[i].lib = SDL_LoadObject(waylandlibs[i].libname);
             }
diff --git a/src/video/x11/SDL_x11dyn.c b/src/video/x11/SDL_x11dyn.c
index 0ae1c5a94a0ba..aaf77d28a673e 100644
--- a/src/video/x11/SDL_x11dyn.c
+++ b/src/video/x11/SDL_x11dyn.c
@@ -71,7 +71,7 @@ static void *X11_GetSym(const char *fnname, int *pHasModule)
 {
     int i;
     void *fn = NULL;
-    for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
+    for (i = 0; i < SDL_arraysize(x11libs); i++) {
         if (x11libs[i].lib) {
             fn = SDL_LoadFunction(x11libs[i].lib, fnname);
             if (fn) {
@@ -132,7 +132,7 @@ void SDL_X11_UnloadSymbols(void)
 #endif
 
 #ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
-            for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
+            for (i = 0; i < SDL_arraysize(x11libs); i++) {
                 if (x11libs[i].lib) {
                     SDL_UnloadObject(x11libs[i].lib);
                     x11libs[i].lib = NULL;
@@ -153,7 +153,7 @@ int SDL_X11_LoadSymbols(void)
 #ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
         int i;
         int *thismod = NULL;
-        for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
+        for (i = 0; i < SDL_arraysize(x11libs); i++) {
             if (x11libs[i].libname) {
                 x11libs[i].lib = SDL_LoadObject(x11libs[i].libname);
             }