Skip to content

Commit

Permalink
ci: fix gcc not understanding array is not dynamic
Browse files Browse the repository at this point in the history
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] = {};
      |          ^~~~~~~
  • Loading branch information
e2dk4r committed Oct 21, 2024
1 parent 285943d commit f8a1b20
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) { \
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f8a1b20

Please sign in to comment.