mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-10-01 07:28:30 +00:00
error: SDL's allocators now call SDL_OutOfMemory on error.
This means the allocator's caller doesn't need to use SDL_OutOfMemory directly if the allocation fails. This applies to the usual allocators: SDL_malloc, SDL_calloc, SDL_realloc (all of these regardless of if the app supplied a custom allocator or we're using system malloc() or an internal copy of dlmalloc under the hood), SDL_aligned_alloc, SDL_small_alloc, SDL_strdup, SDL_asprintf, SDL_wcsdup... probably others. If it returns something you can pass to SDL_free, it should work. The caller might still need to use SDL_OutOfMemory if something that wasn't SDL allocated the memory: operator new in C++ code, Objective-C's alloc message, win32 GlobalAlloc, etc. Fixes #8642.
This commit is contained in:
@@ -41,7 +41,6 @@ char *SDL_GetPrefPath(const char *org, const char *app)
|
||||
size_t pathlen = SDL_strlen(path) + 2;
|
||||
char *fullpath = (char *)SDL_malloc(pathlen);
|
||||
if (!fullpath) {
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
SDL_snprintf(fullpath, pathlen, "%s/", path);
|
||||
|
@@ -52,9 +52,7 @@ char *SDL_GetBasePath(void)
|
||||
if (base) {
|
||||
const size_t len = SDL_strlen(base) + 2;
|
||||
retval = (char *)SDL_malloc(len);
|
||||
if (retval == NULL) {
|
||||
SDL_OutOfMemory();
|
||||
} else {
|
||||
if (retval != NULL) {
|
||||
SDL_snprintf(retval, len, "%s/", base);
|
||||
}
|
||||
}
|
||||
@@ -105,9 +103,7 @@ char *SDL_GetPrefPath(const char *org, const char *app)
|
||||
if (base) {
|
||||
const size_t len = SDL_strlen(base) + SDL_strlen(org) + SDL_strlen(app) + 4;
|
||||
retval = (char *)SDL_malloc(len);
|
||||
if (retval == NULL) {
|
||||
SDL_OutOfMemory();
|
||||
} else {
|
||||
if (retval != NULL) {
|
||||
char *ptr;
|
||||
if (*org) {
|
||||
SDL_snprintf(retval, len, "%s/%s/%s/", base, org, app);
|
||||
@@ -152,13 +148,7 @@ char *SDL_GetUserFolder(SDL_Folder folder)
|
||||
SDL_SetError("No $HOME environment variable available");
|
||||
}
|
||||
|
||||
retval = SDL_strdup(base);
|
||||
|
||||
if (!retval) {
|
||||
SDL_OutOfMemory();
|
||||
}
|
||||
|
||||
return retval;
|
||||
return SDL_strdup(base);
|
||||
|
||||
case SDL_FOLDER_DESKTOP:
|
||||
dir = NSDesktopDirectory;
|
||||
@@ -221,7 +211,6 @@ char *SDL_GetUserFolder(SDL_Folder folder)
|
||||
|
||||
retval = SDL_strdup(base);
|
||||
if (retval == NULL) {
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@@ -53,7 +53,6 @@ char *SDL_GetPrefPath(const char *org, const char *app)
|
||||
len = SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3;
|
||||
retval = (char *)SDL_malloc(len);
|
||||
if (!retval) {
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -86,7 +85,6 @@ char *SDL_GetPrefPath(const char *org, const char *app)
|
||||
char *SDL_GetUserFolder(SDL_Folder folder)
|
||||
{
|
||||
const char *home = NULL;
|
||||
char *retval;
|
||||
|
||||
if (folder != SDL_FOLDER_HOME) {
|
||||
SDL_SetError("Emscripten only supports the home folder");
|
||||
@@ -99,13 +97,7 @@ char *SDL_GetUserFolder(SDL_Folder folder)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
retval = SDL_strdup(home);
|
||||
|
||||
if (!retval) {
|
||||
SDL_OutOfMemory();
|
||||
}
|
||||
|
||||
return retval;
|
||||
return SDL_strdup(home);
|
||||
}
|
||||
|
||||
#endif /* SDL_FILESYSTEM_EMSCRIPTEN */
|
||||
|
@@ -46,7 +46,6 @@ SDL_GetBasePath(void)
|
||||
void *ptr = SDL_realloc(path, buflen * sizeof(CHAR));
|
||||
if (!ptr) {
|
||||
SDL_free(path);
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@@ -52,7 +52,6 @@ char *SDL_GetBasePath(void)
|
||||
const size_t len = SDL_strlen(str);
|
||||
char *retval = (char *) SDL_malloc(len + 2);
|
||||
if (!retval) {
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -83,9 +82,7 @@ char *SDL_GetPrefPath(const char *org, const char *app)
|
||||
}
|
||||
len += SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3;
|
||||
char *retval = (char *) SDL_malloc(len);
|
||||
if (!retval) {
|
||||
SDL_OutOfMemory();
|
||||
} else {
|
||||
if (retval) {
|
||||
if (*org) {
|
||||
SDL_snprintf(retval, len, "%s%s%s/%s/", home, append, org, app);
|
||||
} else {
|
||||
@@ -110,25 +107,17 @@ char *SDL_GetUserFolder(SDL_Folder folder)
|
||||
|
||||
switch (folder) {
|
||||
case SDL_FOLDER_HOME:
|
||||
retval = SDL_strdup(home);
|
||||
|
||||
if (!retval) {
|
||||
SDL_OutOfMemory();
|
||||
}
|
||||
|
||||
return retval;
|
||||
return SDL_strdup(home);
|
||||
|
||||
/* TODO: Is Haiku's desktop folder always ~/Desktop/ ? */
|
||||
case SDL_FOLDER_DESKTOP:
|
||||
retval = (char *) SDL_malloc(SDL_strlen(home) + 10);
|
||||
|
||||
if (!retval) {
|
||||
SDL_OutOfMemory();
|
||||
if (retval) {
|
||||
SDL_strlcpy(retval, home, SDL_strlen(home) + 10);
|
||||
SDL_strlcat(retval, "/Desktop/", SDL_strlen(home) + 10);
|
||||
}
|
||||
|
||||
SDL_strlcpy(retval, home, SDL_strlen(home) + 10);
|
||||
SDL_strlcat(retval, "/Desktop/", SDL_strlen(home) + 10);
|
||||
|
||||
return retval;
|
||||
|
||||
case SDL_FOLDER_DOCUMENTS:
|
||||
|
@@ -70,7 +70,6 @@ static char *MakePrefPath(const char *app)
|
||||
{
|
||||
char *pref_path;
|
||||
if (SDL_asprintf(&pref_path, "sdmc:/3ds/%s/", app) < 0) {
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
return pref_path;
|
||||
|
@@ -42,7 +42,6 @@ static char *SDL_unixify_std(const char *ro_path, char *buffer, size_t buf_len,
|
||||
buffer = SDL_malloc(buf_len);
|
||||
|
||||
if (!buffer) {
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@@ -89,7 +88,6 @@ static char *canonicalisePath(const char *path, const char *pathVar)
|
||||
regs.r[5] = 1 - regs.r[5];
|
||||
buf = SDL_malloc(regs.r[5]);
|
||||
if (!buf) {
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
regs.r[2] = (int)buf;
|
||||
@@ -174,7 +172,6 @@ char *SDL_GetPrefPath(const char *org, const char *app)
|
||||
len = SDL_strlen(canon) + SDL_strlen(org) + SDL_strlen(app) + 4;
|
||||
dir = (char *)SDL_malloc(len);
|
||||
if (!dir) {
|
||||
SDL_OutOfMemory();
|
||||
SDL_free(canon);
|
||||
return NULL;
|
||||
}
|
||||
|
@@ -48,7 +48,6 @@ static char *readSymLink(const char *path)
|
||||
while (1) {
|
||||
char *ptr = (char *)SDL_realloc(retval, (size_t)len);
|
||||
if (!ptr) {
|
||||
SDL_OutOfMemory();
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -85,7 +84,6 @@ static char *search_path_for_binary(const char *bin)
|
||||
|
||||
envr = SDL_strdup(envr);
|
||||
if (!envr) {
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -131,7 +129,6 @@ char *SDL_GetBasePath(void)
|
||||
if (sysctl(mib, SDL_arraysize(mib), fullpath, &buflen, NULL, 0) != -1) {
|
||||
retval = SDL_strdup(fullpath);
|
||||
if (!retval) {
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@@ -145,14 +142,12 @@ char *SDL_GetBasePath(void)
|
||||
char *exe, *pwddst;
|
||||
char *realpathbuf = (char *)SDL_malloc(PATH_MAX + 1);
|
||||
if (!realpathbuf) {
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cmdline = SDL_malloc(len);
|
||||
if (!cmdline) {
|
||||
SDL_free(realpathbuf);
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -228,7 +223,6 @@ char *SDL_GetBasePath(void)
|
||||
if ((path) && (path[0] == '/')) { /* must be absolute path... */
|
||||
retval = SDL_strdup(path);
|
||||
if (!retval) {
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@@ -302,7 +296,6 @@ char *SDL_GetPrefPath(const char *org, const char *app)
|
||||
len += SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3;
|
||||
retval = (char *)SDL_malloc(len);
|
||||
if (!retval) {
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -541,13 +534,7 @@ char *SDL_GetUserFolder(SDL_Folder folder)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
retval = SDL_strdup(param);
|
||||
|
||||
if (!retval) {
|
||||
SDL_OutOfMemory();
|
||||
}
|
||||
|
||||
return retval;
|
||||
return SDL_strdup(param);
|
||||
|
||||
case SDL_FOLDER_DESKTOP:
|
||||
param = "DESKTOP";
|
||||
|
@@ -61,7 +61,6 @@ char *SDL_GetPrefPath(const char *org, const char *app)
|
||||
len += SDL_strlen(org) + SDL_strlen(app) + 3;
|
||||
retval = (char *)SDL_malloc(len);
|
||||
if (!retval) {
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@@ -53,7 +53,6 @@ char *SDL_GetBasePath(void)
|
||||
void *ptr = SDL_realloc(path, buflen * sizeof(WCHAR));
|
||||
if (!ptr) {
|
||||
SDL_free(path);
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -123,14 +122,12 @@ char *SDL_GetPrefPath(const char *org, const char *app)
|
||||
|
||||
worg = WIN_UTF8ToStringW(org);
|
||||
if (!worg) {
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
wapp = WIN_UTF8ToStringW(app);
|
||||
if (!wapp) {
|
||||
SDL_free(worg);
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@@ -131,7 +131,6 @@ SDL_GetBasePath(void)
|
||||
destPathLen = SDL_strlen(srcPath) + 2;
|
||||
destPath = (char *)SDL_malloc(destPathLen);
|
||||
if (!destPath) {
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -178,14 +177,12 @@ SDL_GetPrefPath(const char *org, const char *app)
|
||||
|
||||
worg = WIN_UTF8ToString(org);
|
||||
if (!worg) {
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
wapp = WIN_UTF8ToString(app);
|
||||
if (!wapp) {
|
||||
SDL_free(worg);
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user