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

@@ -320,3 +320,48 @@ int SDL_URIToLocal(const char *src, char *dst)
}
return -1;
}
// This is a set of per-thread persistent strings that we can return from the SDL API.
// This is used for short strings that might persist past the lifetime of the object
// they are related to.
static SDL_TLSID SDL_string_storage;
static void SDL_FreePersistentStrings( void *value )
{
SDL_HashTable *strings = (SDL_HashTable *)value;
SDL_DestroyHashTable(strings);
}
const char *SDL_GetPersistentString(const char *string)
{
if (!string) {
return NULL;
}
if (!*string) {
return "";
}
SDL_HashTable *strings = (SDL_HashTable *)SDL_GetTLS(&SDL_string_storage);
if (!strings) {
strings = SDL_CreateHashTable(NULL, 32, SDL_HashString, SDL_KeyMatchString, SDL_NukeFreeValue, SDL_FALSE);
if (!strings) {
return NULL;
}
SDL_SetTLS(&SDL_string_storage, strings, SDL_FreePersistentStrings);
}
const void *retval;
if (!SDL_FindInHashTable(strings, string, &retval)) {
char *new_string = SDL_strdup(string);
if (!new_string) {
return NULL;
}
// If the hash table insert fails, at least we can return the string we allocated
retval = new_string;
SDL_InsertIntoHashTable(strings, string, retval);
}
return (const char *)retval;
}