segatools/games/mai2io
Gl0w1amp 2c2b8b11b4 Fix mai2 memory leak via LED 15070 rate-limiting and enhance touch handling (#101)
## Overview
This PR addresses the severe memory leak and performance issues observed in `mai2`, while also introducing improvements to the touch emulation logic.

## Root Cause Analysis
As discussed previously, the root cause of the `mai2` memory leak is not a global `segatools` buffer bug. Instead, the game aggressively spams overlapped empty reads specifically on the `LED 15070` UART. On real hardware, the serial driver naturally throttles this. Under emulation, without a throttle, it hits approximately **260kHz of empty async reads**, which causes the memory usage to explode.

## The Fix
Instead of introducing complex locking mechanisms and condition variables globally in `uart.c`, this PR applies a targeted fix:
* Added a local `Sleep(1)` directly in `common/board/led15070.c` to rate-limit empty reads on the LED path.
* Because this is isolated to LED communications, it completely resolves the memory leak without introducing any lag, livelocks, or overhead to other critical inputs.

## Additional Changes in this PR
Alongside the memory leak fix, this PR includes a few touch-related improvements (as touch emulation was reviewed during the debugging process):
* Enhanced touch input handling and improved auto-scan state management.
* Implemented IOCTL handling for touch input to properly manage communication status.

## Testing
* **mai2:** Tested successfully on multiple machines. The memory leak is completely gone, and the game runs smoothly.
* **chusan:** Tested to ensure no regressions. Sliders and inputs work flawlessly without the lag.

Reviewed-on: https://gitea.tendokyu.moe/TeamTofuShop/segatools/pulls/101
Co-authored-by: Gl0w1amp <gl0w1amp@noreply.gitea.tendokyu.moe>
Co-committed-by: Gl0w1amp <gl0w1amp@noreply.gitea.tendokyu.moe>
2026-04-03 15:13:27 +00:00
..
config.c mai2io: change 2p select button default key 2026-01-13 06:57:52 +00:00
config.h refactor all common parts and games 2025-04-17 19:40:40 +02:00
mai2io.c Fix mai2 memory leak via LED 15070 rate-limiting and enhance touch handling (#101) 2026-04-03 15:13:27 +00:00
mai2io.h Fix mai2 memory leak via LED 15070 rate-limiting and enhance touch handling (#101) 2026-04-03 15:13:27 +00:00
meson.build refactor all common parts and games 2025-04-17 19:40:40 +02:00