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

@@ -416,12 +416,13 @@ void UpdateGamepadImageFromGamepad(GamepadImage *ctx, SDL_Gamepad *gamepad)
}
ctx->type = SDL_GetGamepadType(gamepad);
const char *mapping = SDL_GetGamepadMapping(gamepad);
char *mapping = SDL_GetGamepadMapping(gamepad);
if (mapping) {
if (SDL_strstr(mapping, "SDL_GAMECONTROLLER_USE_BUTTON_LABELS")) {
/* Just for display purposes */
ctx->type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO;
}
SDL_free(mapping);
}
for (i = 0; i < SDL_GAMEPAD_BUTTON_TOUCHPAD; ++i) {
@@ -1021,7 +1022,7 @@ void RenderGamepadDisplay(GamepadDisplay *ctx, SDL_Gamepad *gamepad)
const float arrow_extent = 48.0f;
SDL_FRect dst, rect, highlight;
Uint8 r, g, b, a;
const char *mapping = NULL;
char *mapping;
SDL_bool has_accel;
SDL_bool has_gyro;
@@ -1285,6 +1286,7 @@ void RenderGamepadDisplay(GamepadDisplay *ctx, SDL_Gamepad *gamepad)
}
}
}
SDL_free(mapping);
}
void DestroyGamepadDisplay(GamepadDisplay *ctx)

View File

@@ -27,7 +27,7 @@ int main(int argc, char *argv[])
(void)argv;
SDL_Init(0);
start = SDL_GetTicks();
SDL_GetPrefPath("libsdl", "test_filesystem");
SDL_free(SDL_GetPrefPath("libsdl", "test_filesystem"));
prequit = SDL_GetTicks();
SDL_Log("SDL_GetPrefPath took %" SDL_PRIu64 "ms", prequit - start);
SDL_Quit();

View File

@@ -20,7 +20,7 @@ print_devices(SDL_bool recording)
const char *typestr = (recording ? "recording" : "playback");
int n = 0;
int frames;
const SDL_AudioDeviceID *devices = recording ? SDL_GetAudioRecordingDevices(&n) : SDL_GetAudioPlaybackDevices(&n);
SDL_AudioDeviceID *devices = recording ? SDL_GetAudioRecordingDevices(&n) : SDL_GetAudioPlaybackDevices(&n);
if (!devices) {
SDL_Log(" Driver failed to report %s devices: %s\n\n", typestr, SDL_GetError());
@@ -46,6 +46,7 @@ print_devices(SDL_bool recording)
}
SDL_Log("\n");
}
SDL_free(devices);
}
int main(int argc, char **argv)

View File

@@ -23,7 +23,7 @@ static SDLTest_CommonState *state = NULL;
int SDL_AppInit(void **appstate, int argc, char **argv)
{
const SDL_AudioDeviceID *devices;
SDL_AudioDeviceID *devices;
SDL_AudioSpec outspec;
SDL_AudioSpec inspec;
SDL_AudioDeviceID device;
@@ -103,6 +103,7 @@ int SDL_AppInit(void **appstate, int argc, char **argv)
device = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, NULL);
if (!device) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for playback: %s!\n", SDL_GetError());
SDL_free(devices);
return SDL_APP_FAILURE;
}
SDL_PauseAudioDevice(device);
@@ -110,9 +111,11 @@ int SDL_AppInit(void **appstate, int argc, char **argv)
stream_out = SDL_CreateAudioStream(&outspec, &outspec);
if (!stream_out) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create an audio stream for playback: %s!\n", SDL_GetError());
SDL_free(devices);
return SDL_APP_FAILURE;
} else if (SDL_BindAudioStream(device, stream_out) == -1) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't bind an audio stream for playback: %s!\n", SDL_GetError());
SDL_free(devices);
return SDL_APP_FAILURE;
}
@@ -124,8 +127,10 @@ int SDL_AppInit(void **appstate, int argc, char **argv)
device = SDL_OpenAudioDevice(want_device, NULL);
if (!device) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for recording: %s!\n", SDL_GetError());
SDL_free(devices);
return SDL_APP_FAILURE;
}
SDL_free(devices);
SDL_PauseAudioDevice(device);
SDL_GetAudioDeviceFormat(device, &inspec, NULL);
stream_in = SDL_CreateAudioStream(&inspec, &inspec);

View File

@@ -366,7 +366,7 @@ static int audio_enumerateAndNameAudioDevices(void *arg)
int t;
int i, n;
const char *name;
const SDL_AudioDeviceID *devices = NULL;
SDL_AudioDeviceID *devices;
/* Iterate over types: t=0 playback device, t=1 recording device */
for (t = 0; t < 2; t++) {
@@ -388,6 +388,7 @@ static int audio_enumerateAndNameAudioDevices(void *arg)
}
}
}
SDL_free(devices);
}
return TEST_COMPLETED;

View File

@@ -86,9 +86,9 @@ static int clipboard_testClipboardDataFunctions(void *arg)
int last_clipboard_update_count;
int last_clipboard_callback_count;
int last_clipboard_cleanup_count;
const void *data;
void *data;
size_t size;
const char *text;
char *text;
const char *expected_text;
TestClipboardData test_data1 = {
@@ -163,11 +163,12 @@ static int clipboard_testClipboardDataFunctions(void *arg)
clipboard_cleanup_count - last_clipboard_cleanup_count);
expected_text = "TEST";
text = (char *) SDL_GetClipboardText();
text = SDL_GetClipboardText();
SDLTest_AssertCheck(
text && SDL_strcmp(text, expected_text) == 0,
"Verify clipboard text, expected \"%s\", got \"%s\"",
expected_text, text);
SDL_free(text);
boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_TEXT]);
SDLTest_AssertCheck(
@@ -186,6 +187,7 @@ static int clipboard_testClipboardDataFunctions(void *arg)
size == SDL_strlen(expected_text),
"Verify test text size, expected %d, got %d",
(int)SDL_strlen(expected_text), (int)size);
SDL_free(text);
expected_text = "CUSTOM";
boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_CUSTOM_TEXT]);
@@ -205,6 +207,7 @@ static int clipboard_testClipboardDataFunctions(void *arg)
size == SDL_strlen(expected_text),
"Verify test text size, expected %d, got %d",
(int)SDL_strlen(expected_text), (int)size);
SDL_free(text);
boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_DATA]);
SDLTest_AssertCheck(
@@ -218,6 +221,7 @@ static int clipboard_testClipboardDataFunctions(void *arg)
size == test_data1.data_size,
"Verify test data size, expected %d, got %d",
(int)test_data1.data_size, (int)size);
SDL_free(data);
boolResult = SDL_HasClipboardData("test/invalid");
SDLTest_AssertCheck(
@@ -232,6 +236,7 @@ static int clipboard_testClipboardDataFunctions(void *arg)
size == 0,
"Verify invalid data size, expected 0, got %d",
(int)size);
SDL_free(data);
#if 0 /* There's no guarantee how or when the callback is called */
SDLTest_AssertCheck(
@@ -260,11 +265,12 @@ static int clipboard_testClipboardDataFunctions(void *arg)
clipboard_cleanup_count - last_clipboard_cleanup_count);
expected_text = "TEST";
text = (char *) SDL_GetClipboardText();
text = SDL_GetClipboardText();
SDLTest_AssertCheck(
text && SDL_strcmp(text, expected_text) == 0,
"Verify clipboard text, expected \"%s\", got \"%s\"",
expected_text, text);
SDL_free(text);
boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_TEXT]);
SDLTest_AssertCheck(
@@ -283,6 +289,7 @@ static int clipboard_testClipboardDataFunctions(void *arg)
size == SDL_strlen(expected_text),
"Verify test text size, expected %d, got %d",
(int)SDL_strlen(expected_text), (int)size);
SDL_free(text);
expected_text = "CUSTOM";
boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_CUSTOM_TEXT]);
@@ -302,6 +309,7 @@ static int clipboard_testClipboardDataFunctions(void *arg)
size == SDL_strlen(expected_text),
"Verify test text size, expected %d, got %d",
(int)SDL_strlen(expected_text), (int)size);
SDL_free(text);
data = SDL_GetClipboardData(test_mime_types[TEST_MIME_TYPE_DATA], &size);
SDLTest_AssertCheck(
@@ -311,6 +319,7 @@ static int clipboard_testClipboardDataFunctions(void *arg)
size == test_data2.data_size,
"Verify test data size, expected %d, got %d",
(int)test_data2.data_size, (int)size);
SDL_free(data);
data = SDL_GetClipboardData("test/invalid", &size);
SDLTest_AssertCheck(
@@ -321,6 +330,7 @@ static int clipboard_testClipboardDataFunctions(void *arg)
size == 0,
"Verify invalid data size, expected 0, got %d",
(int)size);
SDL_free(data);
#if 0 /* There's no guarantee how or when the callback is called */
SDLTest_AssertCheck(
@@ -376,7 +386,7 @@ static int clipboard_testClipboardTextFunctions(void *arg)
char *text = SDL_strdup(textRef);
SDL_bool boolResult;
int intResult;
const char *charResult;
char *charResult;
int last_clipboard_update_count;
SDL_AddEventWatch(ClipboardEventWatch, NULL);
@@ -393,6 +403,7 @@ static int clipboard_testClipboardTextFunctions(void *arg)
charResult && SDL_strcmp(charResult, "") == 0,
"Verify SDL_GetClipboardText returned \"\", got %s",
charResult);
SDL_free(charResult);
boolResult = SDL_HasClipboardText();
SDLTest_AssertCheck(
boolResult == SDL_FALSE,
@@ -425,6 +436,7 @@ static int clipboard_testClipboardTextFunctions(void *arg)
charResult && SDL_strcmp(textRef, charResult) == 0,
"Verify SDL_GetClipboardText returned correct string, expected '%s', got '%s'",
textRef, charResult);
SDL_free(charResult);
SDLTest_AssertCheck(
clipboard_update_count == last_clipboard_update_count + 1,
"Verify clipboard update count incremented by 1, got %d",
@@ -458,7 +470,7 @@ static int clipboard_testPrimarySelectionTextFunctions(void *arg)
char *text = SDL_strdup(textRef);
SDL_bool boolResult;
int intResult;
const char *charResult;
char *charResult;
int last_clipboard_update_count;
SDL_AddEventWatch(ClipboardEventWatch, NULL);
@@ -475,6 +487,7 @@ static int clipboard_testPrimarySelectionTextFunctions(void *arg)
charResult && SDL_strcmp(charResult, "") == 0,
"Verify SDL_GetPrimarySelectionText returned \"\", got %s",
charResult);
SDL_free(charResult);
boolResult = SDL_HasPrimarySelectionText();
SDLTest_AssertCheck(
boolResult == SDL_FALSE,
@@ -506,6 +519,7 @@ static int clipboard_testPrimarySelectionTextFunctions(void *arg)
charResult && SDL_strcmp(textRef, charResult) == 0,
"Verify SDL_GetPrimarySelectionText returned correct string, expected '%s', got '%s'",
textRef, charResult);
SDL_free(charResult);
SDLTest_AssertCheck(
clipboard_update_count == last_clipboard_update_count + 1,
"Verify clipboard update count incremented by 1, got %d",

View File

@@ -175,67 +175,6 @@ static int events_addDelEventWatchWithUserdata(void *arg)
return TEST_COMPLETED;
}
/**
* Creates and validates temporary event memory
*/
static int events_temporaryMemory(void *arg)
{
SDL_Event event;
void *mem, *claimed, *tmp;
SDL_bool found;
{
/* Create and claim event memory */
mem = SDL_AllocateTemporaryMemory(1);
SDLTest_AssertCheck(mem != NULL, "SDL_AllocateTemporaryMemory()");
*(char *)mem = '1';
claimed = SDL_ClaimTemporaryMemory(mem);
SDLTest_AssertCheck(claimed != NULL, "SDL_ClaimTemporaryMemory() returned a valid pointer");
/* Verify that we can't claim it again */
tmp = SDL_ClaimTemporaryMemory(mem);
SDLTest_AssertCheck(tmp == NULL, "SDL_ClaimTemporaryMemory() can't claim memory twice");
/* Clean up */
SDL_free(claimed);
}
{
/* Create event memory and queue it */
mem = SDL_AllocateTemporaryMemory(1);
SDLTest_AssertCheck(mem != NULL, "SDL_AllocateTemporaryMemory()");
*(char *)mem = '2';
SDL_zero(event);
event.type = SDL_EVENT_USER;
event.user.data1 = mem;
SDL_PushEvent(&event);
/* Verify that we can't claim the memory once it's on the queue */
claimed = SDL_ClaimTemporaryMemory(mem);
SDLTest_AssertCheck(claimed == NULL, "SDL_ClaimTemporaryMemory() can't claim memory on the event queue");
/* Get the event and verify the memory is valid */
found = SDL_FALSE;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_USER && event.user.data1 == mem) {
found = SDL_TRUE;
}
}
SDLTest_AssertCheck(found, "SDL_PollEvent() returned queued event");
/* Verify that we can claim the memory now that we've dequeued it */
claimed = SDL_ClaimTemporaryMemory(mem);
SDLTest_AssertCheck(claimed != NULL, "SDL_ClaimTemporaryMemory() can claim memory after dequeuing event");
/* Clean up */
SDL_free(claimed);
}
return TEST_COMPLETED;
}
/* ================= Test References ================== */
/* Events test cases */
@@ -251,13 +190,9 @@ static const SDLTest_TestCaseReference eventsTest3 = {
(SDLTest_TestCaseFp)events_addDelEventWatchWithUserdata, "events_addDelEventWatchWithUserdata", "Adds and deletes an event watch function with userdata", TEST_ENABLED
};
static const SDLTest_TestCaseReference eventsTestTemporaryMemory = {
(SDLTest_TestCaseFp)events_temporaryMemory, "events_temporaryMemory", "Creates and validates temporary event memory", TEST_ENABLED
};
/* Sequence of Events test cases */
static const SDLTest_TestCaseReference *eventsTests[] = {
&eventsTest1, &eventsTest2, &eventsTest3, &eventsTestTemporaryMemory, NULL
&eventsTest1, &eventsTest2, &eventsTest3, NULL
};
/* Events test suite (global) */

View File

@@ -106,13 +106,13 @@ TestGUIDToString(void *arg)
SDLTest_AssertPass("Call to SDL_GUIDToString");
for (i = 0; i < NUM_TEST_GUIDS; ++i) {
const char *guid_str;
char guid_str[33];
SDL_GUID guid;
upper_lower_to_bytestring(guid.data,
test_guids[i].upper, test_guids[i].lower);
guid_str = SDL_GUIDToString(guid);
SDL_GUIDToString(guid, guid_str, sizeof(guid_str));
SDLTest_AssertCheck(SDL_strcmp(guid_str, test_guids[i].str) == 0, "Checking whether strings match, expected %s, got %s\n", test_guids[i].str, guid_str);
}

View File

@@ -64,13 +64,15 @@ static int TestVirtualJoystick(void *arg)
gamepad = SDL_OpenGamepad(SDL_GetJoystickID(joystick));
SDLTest_AssertCheck(gamepad != NULL, "SDL_OpenGamepad() succeeded");
if (gamepad) {
SDLTest_AssertCheck(SDL_strcmp(SDL_GetGamepadName(gamepad), desc.name) == 0, "SDL_GetGamepadName()");
const char *name = SDL_GetGamepadName(gamepad);
SDLTest_AssertCheck(name && SDL_strcmp(name, desc.name) == 0, "SDL_GetGamepadName()");
SDLTest_AssertCheck(SDL_GetGamepadVendor(gamepad) == desc.vendor_id, "SDL_GetGamepadVendor()");
SDLTest_AssertCheck(SDL_GetGamepadProduct(gamepad) == desc.product_id, "SDL_GetGamepadProduct()");
/* Set an explicit mapping with a different name */
SDL_SetGamepadMapping(SDL_GetJoystickID(joystick), "ff0013db5669727475616c2043007601,Virtual Gamepad,a:b0,b:b1,x:b2,y:b3,back:b4,guide:b5,start:b6,leftstick:b7,rightstick:b8,leftshoulder:b9,rightshoulder:b10,dpup:b11,dpdown:b12,dpleft:b13,dpright:b14,misc1:b15,paddle1:b16,paddle2:b17,paddle3:b18,paddle4:b19,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,");
SDLTest_AssertCheck(SDL_strcmp(SDL_GetGamepadName(gamepad), "Virtual Gamepad") == 0, "SDL_GetGamepadName() == Virtual Gamepad");
name = SDL_GetGamepadName(gamepad);
SDLTest_AssertCheck(name && SDL_strcmp(name, "Virtual Gamepad") == 0, "SDL_GetGamepadName() == Virtual Gamepad");
SDLTest_AssertCheck(SDL_GetGamepadButtonLabel(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == SDL_GAMEPAD_BUTTON_LABEL_A, "SDL_GetGamepadButtonLabel(SDL_GAMEPAD_BUTTON_SOUTH) == SDL_GAMEPAD_BUTTON_LABEL_A");
/* Set the south button and verify that the gamepad responds */
@@ -84,7 +86,8 @@ static int TestVirtualJoystick(void *arg)
/* Set an explicit mapping with legacy Nintendo style buttons */
SDL_SetGamepadMapping(SDL_GetJoystickID(joystick), "ff0013db5669727475616c2043007601,Virtual Nintendo Gamepad,a:b1,b:b0,x:b3,y:b2,back:b4,guide:b5,start:b6,leftstick:b7,rightstick:b8,leftshoulder:b9,rightshoulder:b10,dpup:b11,dpdown:b12,dpleft:b13,dpright:b14,misc1:b15,paddle1:b16,paddle2:b17,paddle3:b18,paddle4:b19,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,hint:SDL_GAMECONTROLLER_USE_BUTTON_LABELS:=1,");
SDLTest_AssertCheck(SDL_strcmp(SDL_GetGamepadName(gamepad), "Virtual Nintendo Gamepad") == 0, "SDL_GetGamepadName() == Virtual Nintendo Gamepad");
name = SDL_GetGamepadName(gamepad);
SDLTest_AssertCheck(name && SDL_strcmp(name, "Virtual Nintendo Gamepad") == 0, "SDL_GetGamepadName() == Virtual Nintendo Gamepad");
SDLTest_AssertCheck(SDL_GetGamepadButtonLabel(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == SDL_GAMEPAD_BUTTON_LABEL_B, "SDL_GetGamepadButtonLabel(SDL_GAMEPAD_BUTTON_SOUTH) == SDL_GAMEPAD_BUTTON_LABEL_B");
/* Set the south button and verify that the gamepad responds */
@@ -98,7 +101,8 @@ static int TestVirtualJoystick(void *arg)
/* Set an explicit mapping with PS4 style buttons */
SDL_SetGamepadMapping(SDL_GetJoystickID(joystick), "ff0013db5669727475616c2043007601,Virtual PS4 Gamepad,type:ps4,a:b0,b:b1,x:b2,y:b3,back:b4,guide:b5,start:b6,leftstick:b7,rightstick:b8,leftshoulder:b9,rightshoulder:b10,dpup:b11,dpdown:b12,dpleft:b13,dpright:b14,misc1:b15,paddle1:b16,paddle2:b17,paddle3:b18,paddle4:b19,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,");
SDLTest_AssertCheck(SDL_strcmp(SDL_GetGamepadName(gamepad), "Virtual PS4 Gamepad") == 0, "SDL_GetGamepadName() == Virtual PS4 Gamepad");
name = SDL_GetGamepadName(gamepad);
SDLTest_AssertCheck(name && SDL_strcmp(name, "Virtual PS4 Gamepad") == 0, "SDL_GetGamepadName() == Virtual PS4 Gamepad");
SDLTest_AssertCheck(SDL_GetGamepadButtonLabel(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == SDL_GAMEPAD_BUTTON_LABEL_CROSS, "SDL_GetGamepadButtonLabel(SDL_GAMEPAD_BUTTON_SOUTH) == SDL_GAMEPAD_BUTTON_LABEL_CROSS");
/* Set the south button and verify that the gamepad responds */

View File

@@ -38,6 +38,7 @@
#define SDL_GetPenName SDL_SUT_GetPenName
#define SDL_GetPenCapabilities SDL_SUT_GetPenCapabilities
#define SDL_GetPenType SDL_SUT_GetPenType
#define SDL_GetPersistentString(X) X
#define SDL_GetPenPtr SDL_SUT_GetPenPtr
#define SDL_PenModifyBegin SDL_SUT_PenModifyBegin

View File

@@ -13,7 +13,7 @@
static SDL_Window *createVideoSuiteTestWindow(const char *title)
{
SDL_Window *window;
SDL_Window * const *windows;
SDL_Window **windows;
SDL_Event event;
int w, h;
int count;
@@ -34,6 +34,7 @@ static SDL_Window *createVideoSuiteTestWindow(const char *title)
windows = SDL_GetWindows(&count);
SDLTest_AssertCheck(windows != NULL, "Validate that returned window list is not NULL");
SDLTest_AssertCheck(windows[0] == window, "Validate that the window is first in the window list");
SDL_free(windows);
/* Wayland and XWayland windows require that a frame be presented before they are fully mapped and visible onscreen.
* This is required for the mouse/keyboard grab tests to pass.
@@ -305,8 +306,8 @@ static int video_getWindowFlags(void *arg)
*/
static int video_getFullscreenDisplayModes(void *arg)
{
const SDL_DisplayID *displays;
const SDL_DisplayMode * const *modes;
SDL_DisplayID *displays;
SDL_DisplayMode **modes;
int count;
int i;
@@ -321,7 +322,9 @@ static int video_getFullscreenDisplayModes(void *arg)
SDLTest_AssertPass("Call to SDL_GetFullscreenDisplayModes(%" SDL_PRIu32 ")", displays[i]);
SDLTest_AssertCheck(modes != NULL, "Validate returned value from function; expected != NULL; got: %p", modes);
SDLTest_AssertCheck(count >= 0, "Validate number of modes; expected: >= 0; got: %d", count);
SDL_free(modes);
}
SDL_free(displays);
}
return TEST_COMPLETED;
@@ -332,11 +335,11 @@ static int video_getFullscreenDisplayModes(void *arg)
*/
static int video_getClosestDisplayModeCurrentResolution(void *arg)
{
const SDL_DisplayID *displays;
const SDL_DisplayMode * const *modes;
SDL_DisplayID *displays;
SDL_DisplayMode **modes;
SDL_DisplayMode current;
const SDL_DisplayMode *closest;
int i, num_modes;
SDL_DisplayMode closest;
int i, result, num_modes;
/* Get number of displays */
displays = SDL_GetDisplays(NULL);
@@ -355,21 +358,23 @@ static int video_getClosestDisplayModeCurrentResolution(void *arg)
SDL_memcpy(&current, modes[0], sizeof(current));
/* Make call */
closest = SDL_GetClosestFullscreenDisplayMode(displays[i], current.w, current.h, current.refresh_rate, SDL_FALSE);
result = SDL_GetClosestFullscreenDisplayMode(displays[i], current.w, current.h, current.refresh_rate, SDL_FALSE, &closest);
SDLTest_AssertPass("Call to SDL_GetClosestFullscreenDisplayMode(target=current)");
SDLTest_Assert(closest != NULL, "Verify returned value is not NULL");
SDLTest_AssertCheck(result == 0, "Verify return value; expected: 0, got: %d", result);
/* Check that one gets the current resolution back again */
if (closest) {
SDLTest_AssertCheck(closest->w == current.w,
if (result == 0) {
SDLTest_AssertCheck(closest.w == current.w,
"Verify returned width matches current width; expected: %d, got: %d",
current.w, closest->w);
SDLTest_AssertCheck(closest->h == current.h,
current.w, closest.w);
SDLTest_AssertCheck(closest.h == current.h,
"Verify returned height matches current height; expected: %d, got: %d",
current.h, closest->h);
current.h, closest.h);
}
}
SDL_free(modes);
}
SDL_free(displays);
}
return TEST_COMPLETED;
@@ -380,8 +385,9 @@ static int video_getClosestDisplayModeCurrentResolution(void *arg)
*/
static int video_getClosestDisplayModeRandomResolution(void *arg)
{
const SDL_DisplayID *displays;
SDL_DisplayID *displays;
SDL_DisplayMode target;
SDL_DisplayMode closest;
int i;
int variation;
@@ -403,10 +409,11 @@ static int video_getClosestDisplayModeRandomResolution(void *arg)
target.refresh_rate = (variation & 8) ? (float)SDLTest_RandomIntegerInRange(25, 120) : 0.0f;
/* Make call; may or may not find anything, so don't validate any further */
SDL_GetClosestFullscreenDisplayMode(displays[i], target.w, target.h, target.refresh_rate, SDL_FALSE);
SDL_GetClosestFullscreenDisplayMode(displays[i], target.w, target.h, target.refresh_rate, SDL_FALSE, &closest);
SDLTest_AssertPass("Call to SDL_GetClosestFullscreenDisplayMode(target=random/variation%d)", variation);
}
}
SDL_free(displays);
}
return TEST_COMPLETED;
@@ -1668,7 +1675,7 @@ cleanup:
*/
static int video_setWindowCenteredOnDisplay(void *arg)
{
const SDL_DisplayID *displays;
SDL_DisplayID *displays;
SDL_Window *window;
const char *title = "video_setWindowCenteredOnDisplay Test Window";
int x, y, w, h;
@@ -1864,6 +1871,7 @@ static int video_setWindowCenteredOnDisplay(void *arg)
destroyVideoSuiteTestWindow(window);
}
}
SDL_free(displays);
}
return TEST_COMPLETED;

View File

@@ -16,7 +16,7 @@
int main(int argc, char **argv)
{
const SDL_DisplayID *displays;
SDL_DisplayID *displays;
int i;
SDLTest_CommonState *state;
@@ -47,6 +47,7 @@ int main(int argc, char **argv)
bounds.x, bounds.y, bounds.w, bounds.h,
usable.x, usable.y, usable.w, usable.h);
}
SDL_free(displays);
}
SDL_Quit();

View File

@@ -28,7 +28,7 @@ static SDL_CameraID back_camera = 0;
static void PrintCameraSpecs(SDL_CameraID camera_id)
{
const SDL_CameraSpec *const *specs = SDL_GetCameraSupportedFormats(camera_id, NULL);
SDL_CameraSpec **specs = SDL_GetCameraSupportedFormats(camera_id, NULL);
if (specs) {
int i;
@@ -37,6 +37,7 @@ static void PrintCameraSpecs(SDL_CameraID camera_id)
const SDL_CameraSpec *s = specs[i];
SDL_Log(" %dx%d %.2f FPS %s\n", s->width, s->height, (float)s->framerate_numerator / s->framerate_denominator, SDL_GetPixelFormatName(s->format));
}
SDL_free(specs);
}
}
@@ -101,7 +102,7 @@ int SDL_AppInit(void **appstate, int argc, char *argv[])
return SDL_APP_FAILURE;
}
const SDL_CameraID *devices = SDL_GetCameras(&devcount);
SDL_CameraID *devices = SDL_GetCameras(&devcount);
if (!devices) {
SDL_Log("SDL_GetCameras failed: %s", SDL_GetError());
return SDL_APP_FAILURE;
@@ -139,6 +140,7 @@ int SDL_AppInit(void **appstate, int argc, char *argv[])
camera_id = devices[0];
}
}
SDL_free(devices);
if (!camera_id) {
SDL_Log("No cameras available?");

View File

@@ -351,7 +351,6 @@ static void RefreshControllerName(void)
}
}
SDL_free(controller_name);
if (name) {
controller_name = SDL_strdup(name);
} else {
@@ -680,7 +679,7 @@ static void CopyMapping(void)
static void PasteMapping(void)
{
if (controller) {
const char *mapping = SDL_GetClipboardText();
char *mapping = SDL_GetClipboardText();
if (MappingHasBindings(mapping)) {
StopBinding();
SDL_SetGamepadMapping(controller->id, mapping);
@@ -688,6 +687,7 @@ static void PasteMapping(void)
} else {
/* Not a valid mapping, ignore it */
}
SDL_free(mapping);
}
}
@@ -742,7 +742,7 @@ static void CopyControllerName(void)
static void PasteControllerName(void)
{
SDL_free(controller_name);
controller_name = SDL_strdup(SDL_GetClipboardText());
controller_name = SDL_GetClipboardText();
CommitControllerName();
}
@@ -918,9 +918,9 @@ static void AddController(SDL_JoystickID id, SDL_bool verbose)
if (verbose && !SDL_IsGamepad(id)) {
const char *name = SDL_GetJoystickName(joystick);
const char *path = SDL_GetJoystickPath(joystick);
const char *guid;
char guid[33];
SDL_Log("Opened joystick %s%s%s\n", name, path ? ", " : "", path ? path : "");
guid = SDL_GUIDToString(SDL_GetJoystickGUID(joystick));
SDL_GUIDToString(SDL_GetJoystickGUID(joystick), guid, sizeof(guid));
SDL_Log("No gamepad mapping for %s\n", guid);
}
} else {
@@ -973,7 +973,6 @@ static void DelController(SDL_JoystickID id)
static void HandleGamepadRemapped(SDL_JoystickID id)
{
const char *sdlmapping;
char *mapping;
int i = FindController(id);
@@ -988,8 +987,7 @@ static void HandleGamepadRemapped(SDL_JoystickID id)
}
/* Get the current mapping */
sdlmapping = SDL_GetGamepadMapping(controllers[i].gamepad);
mapping = sdlmapping ? SDL_strdup(sdlmapping) : NULL;
mapping = SDL_GetGamepadMapping(controllers[i].gamepad);
/* Make sure the mapping has a valid name */
if (mapping && !MappingHasName(mapping)) {
@@ -1065,9 +1063,10 @@ static void HandleGamepadAdded(SDL_JoystickID id, SDL_bool verbose)
}
if (verbose) {
const char *mapping = SDL_GetGamepadMapping(gamepad);
char *mapping = SDL_GetGamepadMapping(gamepad);
if (mapping) {
SDL_Log("Mapping: %s\n", mapping);
SDL_free(mapping);
}
}
} else {
@@ -1187,7 +1186,7 @@ static void OpenVirtualGamepad(void)
static void CloseVirtualGamepad(void)
{
int i;
const SDL_JoystickID *joysticks = SDL_GetJoysticks(NULL);
SDL_JoystickID *joysticks = SDL_GetJoysticks(NULL);
if (joysticks) {
for (i = 0; joysticks[i]; ++i) {
SDL_JoystickID instance_id = joysticks[i];
@@ -1195,6 +1194,7 @@ static void CloseVirtualGamepad(void)
SDL_DetachVirtualJoystick(instance_id);
}
}
SDL_free(joysticks);
}
if (virtual_joystick) {
@@ -2053,13 +2053,14 @@ int main(int argc, char *argv[])
if (show_mappings) {
int count = 0;
const char * const *mappings = SDL_GetGamepadMappings(&count);
char **mappings = SDL_GetGamepadMappings(&count);
int map_i;
SDL_Log("Supported mappings:\n");
for (map_i = 0; map_i < count; ++map_i) {
SDL_Log("\t%s\n", mappings[map_i]);
}
SDL_Log("\n");
SDL_free(mappings);
}
/* Create a window to display gamepad state */

View File

@@ -44,7 +44,8 @@ static void SDLCALL callback(void* userdata, const char* const* files, int filte
}
}
int main(int argc, char *argv[]) {
int main(int argc, char *argv[])
{
SDL_Window *w;
SDL_Renderer *r;
SDLTest_CommonState *state;

View File

@@ -33,8 +33,8 @@ print_mode(const char *prefix, const SDL_DisplayMode *mode)
int main(int argc, char *argv[])
{
const SDL_DisplayID *displays;
const SDL_DisplayMode * const *modes;
SDL_DisplayID *displays;
SDL_DisplayMode **modes;
const SDL_DisplayMode *mode;
int num_displays, i;
SDLTest_CommonState *state;
@@ -94,9 +94,11 @@ int main(int argc, char *argv[])
(void)SDL_snprintf(prefix, sizeof(prefix), " MODE %d", m);
print_mode(prefix, modes[m]);
}
SDL_free(modes);
SDL_Log("\n");
}
SDL_free(displays);
SDL_Quit();
SDLTest_CommonDestroyState(state);

View File

@@ -61,7 +61,7 @@ static int SDLCALL enum_callback(void *userdata, const char *origdir, const char
int main(int argc, char *argv[])
{
SDLTest_CommonState *state;
const char *pref_path;
char *pref_path;
const char *base_path;
/* Initialize test framework */
@@ -98,6 +98,7 @@ int main(int argc, char *argv[])
} else {
SDL_Log("pref path: '%s'\n", pref_path);
}
SDL_free(pref_path);
pref_path = SDL_GetPrefPath(NULL, "test_filesystem");
if (!pref_path) {
@@ -106,9 +107,10 @@ int main(int argc, char *argv[])
} else {
SDL_Log("pref path: '%s'\n", pref_path);
}
SDL_free(pref_path);
if (base_path) {
const char * const *globlist;
char **globlist;
SDL_IOStream *stream;
const char *text = "foo\n";
@@ -124,6 +126,7 @@ int main(int argc, char *argv[])
for (i = 0; globlist[i]; i++) {
SDL_Log("GLOB[%d]: '%s'", i, globlist[i]);
}
SDL_free(globlist);
}
/* !!! FIXME: put this in a subroutine and make it test more thoroughly (and put it in testautomation). */

View File

@@ -40,7 +40,7 @@ int main(int argc, char **argv)
int id[9];
int nefx;
unsigned int supported;
const SDL_HapticID *haptics;
SDL_HapticID *haptics;
int num_haptics;
/* Initialize test framework */
@@ -88,44 +88,47 @@ int main(int argc, char **argv)
for (i = 0; i < num_haptics; ++i) {
SDL_Log(" %s\n", SDL_GetHapticNameForID(haptics[i]));
}
if (haptics) {
if (num_haptics == 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Haptic devices found!\n");
return 1;
}
/* We'll just use index or the first force feedback device found */
if (!name) {
i = (index != -1) ? index : 0;
if (i >= num_haptics) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Index out of range, aborting.\n");
return 1;
}
}
/* Try to find matching device */
else {
for (i = 0; i < num_haptics; i++) {
if (SDL_strstr(SDL_GetHapticNameForID(haptics[i]), name) != NULL) {
break;
}
}
if (i >= num_haptics) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.\n", name);
return 1;
}
}
haptic = SDL_OpenHaptic(haptics[i]);
if (!haptic) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s\n", SDL_GetError());
return 1;
}
SDL_Log("Device: %s\n", SDL_GetHapticName(haptic));
HapticPrintSupported(haptic);
if (num_haptics == 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Haptic devices found!\n");
SDL_free(haptics);
return 1;
}
/* We'll just use index or the first force feedback device found */
if (!name) {
i = (index != -1) ? index : 0;
if (i >= num_haptics) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Index out of range, aborting.\n");
SDL_free(haptics);
return 1;
}
}
/* Try to find matching device */
else {
for (i = 0; i < num_haptics; i++) {
if (SDL_strstr(SDL_GetHapticNameForID(haptics[i]), name) != NULL) {
break;
}
}
if (i >= num_haptics) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.\n", name);
SDL_free(haptics);
return 1;
}
}
haptic = SDL_OpenHaptic(haptics[i]);
if (!haptic) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s\n", SDL_GetError());
SDL_free(haptics);
return 1;
}
SDL_Log("Device: %s\n", SDL_GetHapticName(haptic));
HapticPrintSupported(haptic);
SDL_free(haptics);
/* We only want force feedback errors. */
SDL_ClearError();

View File

@@ -80,18 +80,18 @@ int main(int argc, char *argv[])
//SDL_CreateWindow("Dummy", 128, 128, 0);
*/
SDL_GetKeyboards(&num_keyboards);
SDL_free(SDL_GetKeyboards(&num_keyboards));
SDL_Log("There are %d keyboards at startup\n", num_keyboards);
SDL_GetMice(&num_mice);
SDL_free(SDL_GetMice(&num_mice));
SDL_Log("There are %d mice at startup\n", num_mice);
SDL_GetJoysticks(&num_joysticks);
SDL_free(SDL_GetJoysticks(&num_joysticks));
SDL_Log("There are %d joysticks at startup\n", num_joysticks);
if (enable_haptic) {
int num_haptics;
SDL_GetHaptics(&num_haptics);
SDL_free(SDL_GetHaptics(&num_haptics));
SDL_Log("There are %d haptic devices at startup\n", num_haptics);
}

View File

@@ -53,7 +53,7 @@ static int cursor_length = 0;
static SDL_bool cursor_visible;
static Uint64 last_cursor_change;
static SDL_BlendMode highlight_mode;
static const char **candidates;
static char **candidates;
static int num_candidates;
static int selected_candidate;
static SDL_bool horizontal_candidates;
@@ -476,6 +476,11 @@ static void InitInput(void)
static void ClearCandidates(void)
{
int i;
for (i = 0; i < num_candidates; ++i) {
SDL_free(candidates[i]);
}
SDL_free(candidates);
candidates = NULL;
num_candidates = 0;
@@ -483,12 +488,20 @@ static void ClearCandidates(void)
static void SaveCandidates(SDL_Event *event)
{
int i;
ClearCandidates();
num_candidates = event->edit_candidates.num_candidates;
if (num_candidates > 0) {
candidates = (const char **)SDL_ClaimTemporaryMemory(event->edit_candidates.candidates);
SDL_assert(candidates);
candidates = (char **)SDL_malloc(num_candidates * sizeof(*candidates));
if (!candidates) {
num_candidates = 0;
return;
}
for (i = 0; i < num_candidates; ++i) {
candidates[i] = SDL_strdup(event->edit_candidates.candidates[i]);
}
selected_candidate = event->edit_candidates.selected_candidate;
horizontal_candidates = event->edit_candidates.horizontal;
}

View File

@@ -15,7 +15,7 @@
static void log_locales(void)
{
const SDL_Locale * const *locales = SDL_GetPreferredLocales(NULL);
SDL_Locale **locales = SDL_GetPreferredLocales(NULL);
if (!locales) {
SDL_Log("Couldn't determine locales: %s", SDL_GetError());
} else {
@@ -29,6 +29,7 @@ static void log_locales(void)
total++;
}
SDL_Log("%u locales seen.", total);
SDL_free(locales);
}
}

View File

@@ -135,7 +135,7 @@ test_multi_audio(const SDL_AudioDeviceID *devices, int devcount)
int main(int argc, char **argv)
{
const SDL_AudioDeviceID *devices = NULL;
SDL_AudioDeviceID *devices;
int devcount = 0;
int i;
char *filename = NULL;
@@ -192,6 +192,7 @@ int main(int argc, char **argv)
test_multi_audio(devices, devcount);
SDL_free(sound);
}
SDL_free(devices);
}
SDL_free(filename);

View File

@@ -222,7 +222,7 @@ static void dump_state(void)
for (i = 0; i < pens_nr; ++i) {
SDL_PenID penid = pens[i];
SDL_GUID guid = SDL_GetPenGUID(penid);
const char *guid_str;
char guid_str[33];
float axes[SDL_PEN_NUM_AXES];
float x, y;
int k;
@@ -232,7 +232,7 @@ static void dump_state(void)
char *type;
char *buttons_str;
guid_str = SDL_GUIDToString(guid);
SDL_GUIDToString(guid, guid_str, sizeof(guid_str));
switch (SDL_GetPenType(penid)) {
case SDL_PEN_TYPE_ERASER:

View File

@@ -39,7 +39,7 @@ int main(int argc, char **argv)
char *name = NULL;
int index;
SDLTest_CommonState *state;
const SDL_HapticID *haptics;
SDL_HapticID *haptics;
int num_haptics;
/* Initialize test framework */
@@ -89,43 +89,46 @@ int main(int argc, char **argv)
SDL_INIT_HAPTIC);
haptics = SDL_GetHaptics(&num_haptics);
SDL_Log("%d Haptic devices detected.\n", num_haptics);
if (haptics) {
if (num_haptics == 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Haptic devices found!\n");
return 1;
}
/* We'll just use index or the first force feedback device found */
if (!name) {
i = (index != -1) ? index : 0;
if (i >= num_haptics) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Index out of range, aborting.\n");
return 1;
}
}
/* Try to find matching device */
else {
for (i = 0; i < num_haptics; i++) {
if (SDL_strstr(SDL_GetHapticNameForID(haptics[i]), name) != NULL) {
break;
}
}
if (i >= num_haptics) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.\n", name);
return 1;
}
}
haptic = SDL_OpenHaptic(haptics[i]);
if (!haptic) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s\n", SDL_GetError());
return 1;
}
SDL_Log("Device: %s\n", SDL_GetHapticName(haptic));
if (num_haptics == 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Haptic devices found!\n");
SDL_free(haptics);
return 1;
}
/* We'll just use index or the first force feedback device found */
if (!name) {
i = (index != -1) ? index : 0;
if (i >= num_haptics) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Index out of range, aborting.\n");
SDL_free(haptics);
return 1;
}
}
/* Try to find matching device */
else {
for (i = 0; i < num_haptics; i++) {
if (SDL_strstr(SDL_GetHapticNameForID(haptics[i]), name) != NULL) {
break;
}
}
if (i >= num_haptics) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.\n", name);
SDL_free(haptics);
return 1;
}
}
haptic = SDL_OpenHaptic(haptics[i]);
if (!haptic) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s\n", SDL_GetError());
SDL_free(haptics);
return 1;
}
SDL_Log("Device: %s\n", SDL_GetHapticName(haptic));
SDL_free(haptics);
/* We only want force feedback errors. */
SDL_ClearError();

View File

@@ -58,7 +58,7 @@ static void HandleSensorEvent(SDL_SensorEvent *event)
int main(int argc, char **argv)
{
const SDL_SensorID *sensors;
SDL_SensorID *sensors;
int i, num_sensors, num_opened;
SDLTest_CommonState *state;
@@ -104,6 +104,7 @@ int main(int argc, char **argv)
}
}
}
SDL_free(sensors);
}
SDL_Log("Opened %d sensors\n", num_opened);

View File

@@ -146,7 +146,7 @@ static void SDLCALL fill_buffer(void *userdata, SDL_AudioStream *stream, int len
int main(int argc, char *argv[])
{
const SDL_AudioDeviceID *devices = NULL;
SDL_AudioDeviceID *devices;
SDLTest_CommonState *state;
int devcount = 0;
int i;
@@ -181,7 +181,6 @@ int main(int argc, char *argv[])
devices = SDL_GetAudioPlaybackDevices(&devcount);
if (!devices) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GetAudioPlaybackDevices() failed: %s\n", SDL_GetError());
devcount = 0;
}
SDL_Log("Available audio devices:");
@@ -233,6 +232,7 @@ int main(int argc, char *argv[])
SDL_DestroyAudioStream(stream);
}
SDL_free(devices);
SDL_Quit();
return 0;

View File

@@ -53,7 +53,7 @@ static const SDL_DisplayMode *highlighted_mode = NULL;
static void
draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_FRect viewport)
{
const SDL_DisplayMode * const *modes;
SDL_DisplayMode **modes;
char text[1024];
const int lineHeight = 10;
int i, j;
@@ -62,7 +62,7 @@ draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_FRect viewport)
float x, y;
float table_top;
SDL_FPoint mouse_pos = { -1.0f, -1.0f };
const SDL_DisplayID *displays;
SDL_DisplayID *displays;
/* Get mouse position */
if (SDL_GetMouseFocus() == window) {
@@ -99,7 +99,6 @@ draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_FRect viewport)
}
displays = SDL_GetDisplays(NULL);
if (displays) {
for (i = 0; displays[i]; ++i) {
SDL_DisplayID display = displays[i];
@@ -143,7 +142,9 @@ draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_FRect viewport)
column_chars = 0;
}
}
SDL_free(modes);
}
SDL_free(displays);
}
}