From f8a1b20fea3524c8f5562e1b8eb5384dcbe1979d Mon Sep 17 00:00:00 2001 From: e2dk4r <43293320+e2dk4r@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:56:52 +0300 Subject: [PATCH] ci: fix gcc not understanding array is not dynamic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use alloca() to continue to allocate on stack. This requires alloca() to be supported by compiler. Issue fixed at gcc v13.1+. Tested gcc versions: - 11.2, 11.3, 11.4 - 12.1, 12.2, 12.3, 12.4 - 13.1 ../src/main.c: In function ‘main’: ../src/main.c:277:10: error: variable-sized object may not be initialized 277 | struct gamepad gamepads[maxSupportedControllers] = {}; | ^~~~~~~ --- src/main.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index 672ddab..f8a6dbe 100644 --- a/src/main.c +++ b/src/main.c @@ -17,6 +17,12 @@ #define IORING_ASYNC_CANCEL_ALL (1U << 0) #endif +#if __has_builtin(__builtin_alloca) +#define alloca(size) __builtin_alloca(size) +#else +#error alloca must be supported +#endif + #if GAMEPAD_IDLE_INHIBIT_DEBUG #define assert(x) \ if (!(x)) { \ @@ -274,7 +280,7 @@ main(void) // TODO: make maxSupportedControllers configurable u32 maxSupportedControllers = 4; - struct gamepad gamepads[maxSupportedControllers] = {}; + struct gamepad *gamepads = alloca(maxSupportedControllers * sizeof(*gamepads)); /* wayland */ context.wl_display = wl_display_connect(0); @@ -462,7 +468,7 @@ main(void) stagedOp.triggerMinimum = triggerAbsInfo.minimum; printf("Input device ID: bus %#x vendor %#x product %#x\n", id.bustype, id.vendor, id.product); - stagedOp.gamepad = GamepadGetNotConnected(gamepads, ARRAY_COUNT(gamepads)); + stagedOp.gamepad = GamepadGetNotConnected(gamepads, maxSupportedControllers); if (!stagedOp.gamepad) { warning("Maximum number of gamepads connected! So not registering this one.\n"); close(stagedOp.fd); @@ -678,7 +684,7 @@ main(void) stagedOp.triggerMinimum = triggerAbsInfo.minimum; printf("Input device ID: bus %#x vendor %#x product %#x\n", id.bustype, id.vendor, id.product); - stagedOp.gamepad = GamepadGetNotConnected(gamepads, ARRAY_COUNT(gamepads)); + stagedOp.gamepad = GamepadGetNotConnected(gamepads, maxSupportedControllers); if (!stagedOp.gamepad) { warning("Maximum number of gamepads connected! So not registering this one.\n"); struct io_uring_sqe *sqe = io_uring_get_sqe(&ring);