Removed temporary memory from the API

It was intended to make the API easier to use, but various automatic garbage collection all had flaws, and making the application periodically clean up temporary memory added cognitive load to using the API, and in many cases was it was difficult to restructure threaded code to handle this.

So, we're largely going back to the original system, where the API returns allocated results and you free them.

In addition, to solve the problems we originally wanted temporary memory for:
* Short strings with a finite count, like device names, get stored in a per-thread string pool.
* Events continue to use temporary memory internally, which is cleaned up on the next event processing cycle.
This commit is contained in:
Sam Lantinga 2024-07-26 18:57:18 -07:00
parent 21411c6418
commit 4f55271571
100 changed files with 737 additions and 853 deletions

View file

@ -714,7 +714,7 @@ SDL_bool SDL_HasJoystick(void)
return SDL_FALSE;
}
const SDL_JoystickID *SDL_GetJoysticks(int *count)
SDL_JoystickID *SDL_GetJoysticks(int *count)
{
int i, num_joysticks, device_index;
int joystick_index = 0, total_joysticks = 0;
@ -751,7 +751,7 @@ const SDL_JoystickID *SDL_GetJoysticks(int *count)
}
SDL_UnlockJoysticks();
return SDL_FreeLater(joysticks);
return joysticks;
}
const SDL_SteamVirtualGamepadInfo *SDL_GetJoystickVirtualGamepadInfoForID(SDL_JoystickID instance_id)
@ -780,9 +780,9 @@ const char *SDL_GetJoystickNameForID(SDL_JoystickID instance_id)
SDL_LockJoysticks();
info = SDL_GetJoystickVirtualGamepadInfoForID(instance_id);
if (info) {
name = SDL_CreateTemporaryString(info->name);
name = SDL_GetPersistentString(info->name);
} else if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
name = SDL_CreateTemporaryString(driver->GetDeviceName(device_index));
name = SDL_GetPersistentString(driver->GetDeviceName(device_index));
}
SDL_UnlockJoysticks();
@ -800,7 +800,7 @@ const char *SDL_GetJoystickPathForID(SDL_JoystickID instance_id)
SDL_LockJoysticks();
if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
path = SDL_CreateTemporaryString(driver->GetDevicePath(device_index));
path = SDL_GetPersistentString(driver->GetDevicePath(device_index));
}
SDL_UnlockJoysticks();
@ -858,7 +858,7 @@ static SDL_bool IsROGAlly(SDL_Joystick *joystick)
SDL_bool has_ally_gyro = SDL_FALSE;
if (SDL_InitSubSystem(SDL_INIT_SENSOR) == 0) {
const SDL_SensorID *sensors = SDL_GetSensors(NULL);
SDL_SensorID *sensors = SDL_GetSensors(NULL);
if (sensors) {
int i;
for (i = 0; sensors[i]; ++i) {
@ -877,6 +877,7 @@ static SDL_bool IsROGAlly(SDL_Joystick *joystick)
}
}
}
SDL_free(sensors);
}
SDL_QuitSubSystem(SDL_INIT_SENSOR);
}
@ -951,7 +952,7 @@ static SDL_bool ShouldAttemptSensorFusion(SDL_Joystick *joystick, SDL_bool *inve
static void AttemptSensorFusion(SDL_Joystick *joystick, SDL_bool invert_sensors)
{
const SDL_SensorID *sensors;
SDL_SensorID *sensors;
unsigned int i, j;
SDL_AssertJoysticksLocked();
@ -980,6 +981,7 @@ static void AttemptSensorFusion(SDL_Joystick *joystick, SDL_bool invert_sensors)
SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, 0.0f);
}
}
SDL_free(sensors);
}
SDL_QuitSubSystem(SDL_INIT_SENSOR);
@ -1651,9 +1653,9 @@ const char *SDL_GetJoystickName(SDL_Joystick *joystick)
info = SDL_GetJoystickVirtualGamepadInfoForID(joystick->instance_id);
if (info) {
retval = SDL_CreateTemporaryString(info->name);
retval = SDL_GetPersistentString(info->name);
} else {
retval = SDL_CreateTemporaryString(joystick->name);
retval = SDL_GetPersistentString(joystick->name);
}
}
SDL_UnlockJoysticks();
@ -1673,7 +1675,7 @@ const char *SDL_GetJoystickPath(SDL_Joystick *joystick)
CHECK_JOYSTICK_MAGIC(joystick, NULL);
if (joystick->path) {
retval = SDL_CreateTemporaryString(joystick->path);
retval = SDL_GetPersistentString(joystick->path);
} else {
SDL_Unsupported();
retval = NULL;
@ -1903,7 +1905,7 @@ void SDL_CloseJoystick(SDL_Joystick *joystick)
void SDL_QuitJoysticks(void)
{
int i;
const SDL_JoystickID *joysticks;
SDL_JoystickID *joysticks;
SDL_LockJoysticks();
@ -1914,6 +1916,7 @@ void SDL_QuitJoysticks(void)
for (i = 0; joysticks[i]; ++i) {
SDL_PrivateJoystickRemoved(joysticks[i]);
}
SDL_free(joysticks);
}
while (SDL_joysticks) {
@ -3428,7 +3431,7 @@ const char *SDL_GetJoystickSerial(SDL_Joystick *joystick)
{
CHECK_JOYSTICK_MAGIC(joystick, NULL);
retval = SDL_CreateTemporaryString(joystick->serial);
retval = SDL_GetPersistentString(joystick->serial);
}
SDL_UnlockJoysticks();