Small stack allocations fall back to malloc if they're unexpectedly large.

This commit is contained in:
Ryan C. Gordon 2018-10-22 20:50:32 -04:00
parent eedf2c965d
commit b262b0ebc9
19 changed files with 84 additions and 52 deletions

View File

@ -178,6 +178,7 @@ SDL_PromptAssertion(const SDL_assert_data *data, void *userdata)
(void) userdata; /* unused in default handler. */ (void) userdata; /* unused in default handler. */
/* !!! FIXME: why is this using SDL_stack_alloc and not just "char message[SDL_MAX_LOG_MESSAGE];" ? */
message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE); message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
if (!message) { if (!message) {
/* Uh oh, we're in real trouble now... */ /* Uh oh, we're in real trouble now... */

View File

@ -35,6 +35,10 @@
#define SDL_VARIABLE_LENGTH_ARRAY #define SDL_VARIABLE_LENGTH_ARRAY
#endif #endif
#define SDL_MAX_SMALL_ALLOC_STACKSIZE 128
#define SDL_small_alloc(type, count, pisstack) ( (*(pisstack) = ((sizeof(type)*(count)) < SDL_MAX_SMALL_ALLOC_STACKSIZE)), (*(pisstack) ? SDL_stack_alloc(type, count) : (type*)SDL_malloc(sizeof(type)*(count))) )
#define SDL_small_free(ptr, isstack) if ((isstack)) { SDL_stack_free(ptr); } else { SDL_free(ptr); }
#include "dynapi/SDL_dynapi.h" #include "dynapi/SDL_dynapi.h"
#if SDL_DYNAMIC_API #if SDL_DYNAMIC_API

View File

@ -282,6 +282,7 @@ SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list
return; return;
} }
/* !!! FIXME: why not just "char message[SDL_MAX_LOG_MESSAGE];" ? */
message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE); message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
if (!message) { if (!message) {
return; return;
@ -321,6 +322,7 @@ SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
char *output; char *output;
size_t length; size_t length;
LPTSTR tstr; LPTSTR tstr;
SDL_bool isstack;
#if !defined(HAVE_STDIO_H) && !defined(__WINRT__) #if !defined(HAVE_STDIO_H) && !defined(__WINRT__)
BOOL attachResult; BOOL attachResult;
@ -364,7 +366,7 @@ SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
#endif /* !defined(HAVE_STDIO_H) && !defined(__WINRT__) */ #endif /* !defined(HAVE_STDIO_H) && !defined(__WINRT__) */
length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1 + 1 + 1; length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1 + 1 + 1;
output = SDL_stack_alloc(char, length); output = SDL_small_alloc(char, length, &isstack);
SDL_snprintf(output, length, "%s: %s\r\n", SDL_priority_prefixes[priority], message); SDL_snprintf(output, length, "%s: %s\r\n", SDL_priority_prefixes[priority], message);
tstr = WIN_UTF8ToString(output); tstr = WIN_UTF8ToString(output);
@ -389,7 +391,7 @@ SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
#endif /* !defined(HAVE_STDIO_H) && !defined(__WINRT__) */ #endif /* !defined(HAVE_STDIO_H) && !defined(__WINRT__) */
SDL_free(tstr); SDL_free(tstr);
SDL_stack_free(output); SDL_small_free(output, isstack);
} }
#elif defined(__ANDROID__) #elif defined(__ANDROID__)
{ {
@ -404,7 +406,7 @@ SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
extern void SDL_NSLog(const char *text); extern void SDL_NSLog(const char *text);
{ {
char *text; char *text;
/* !!! FIXME: why not just "char text[SDL_MAX_LOG_MESSAGE];" ? */
text = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE); text = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
if (text) { if (text) {
SDL_snprintf(text, SDL_MAX_LOG_MESSAGE, "%s: %s", SDL_priority_prefixes[priority], message); SDL_snprintf(text, SDL_MAX_LOG_MESSAGE, "%s: %s", SDL_priority_prefixes[priority], message);

View File

@ -466,10 +466,11 @@ JNIEXPORT int JNICALL SDL_JAVA_INTERFACE(nativeRunMain)(JNIEnv* env, jclass cls,
int argc; int argc;
int len; int len;
char **argv; char **argv;
SDL_bool isstack;
/* Prepare the arguments. */ /* Prepare the arguments. */
len = (*env)->GetArrayLength(env, array); len = (*env)->GetArrayLength(env, array);
argv = SDL_stack_alloc(char*, 1 + len + 1); argv = SDL_small_alloc(char*, 1 + len + 1, &isstack); /* !!! FIXME: check for NULL */
argc = 0; argc = 0;
/* Use the name "app_process" so PHYSFS_platformCalcBaseDir() works. /* Use the name "app_process" so PHYSFS_platformCalcBaseDir() works.
https://bitbucket.org/MartinFelis/love-android-sdl2/issue/23/release-build-crash-on-start https://bitbucket.org/MartinFelis/love-android-sdl2/issue/23/release-build-crash-on-start
@ -502,7 +503,7 @@ JNIEXPORT int JNICALL SDL_JAVA_INTERFACE(nativeRunMain)(JNIEnv* env, jclass cls,
for (i = 0; i < argc; ++i) { for (i = 0; i < argc; ++i) {
SDL_free(argv[i]); SDL_free(argv[i]);
} }
SDL_stack_free(argv); SDL_small_free(argv, isstack);
} else { } else {
__android_log_print(ANDROID_LOG_ERROR, "SDL", "nativeRunMain(): Couldn't find function %s in library %s", function_name, library_file); __android_log_print(ANDROID_LOG_ERROR, "SDL", "nativeRunMain(): Couldn't find function %s in library %s", function_name, library_file);

View File

@ -528,6 +528,7 @@ SDL_RWFromFile(const char *file, const char *mode)
char *path; char *path;
FILE *fp; FILE *fp;
/* !!! FIXME: why not just "char path[PATH_MAX];" ? */
path = SDL_stack_alloc(char, PATH_MAX); path = SDL_stack_alloc(char, PATH_MAX);
if (path) { if (path) {
SDL_snprintf(path, PATH_MAX, "%s/%s", SDL_snprintf(path, PATH_MAX, "%s/%s",

View File

@ -203,13 +203,14 @@ static void UpdateEventsForDeviceRemoval()
{ {
int i, num_events; int i, num_events;
SDL_Event *events; SDL_Event *events;
SDL_bool isstack;
num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED); num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED);
if (num_events <= 0) { if (num_events <= 0) {
return; return;
} }
events = SDL_stack_alloc(SDL_Event, num_events); events = SDL_small_alloc(SDL_Event, num_events, &isstack);
if (!events) { if (!events) {
return; return;
} }
@ -220,7 +221,7 @@ static void UpdateEventsForDeviceRemoval()
} }
SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0); SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0);
SDL_stack_free(events); SDL_small_free(events, isstack);
} }
static SDL_bool HasSameOutput(SDL_ExtendedGameControllerBind *a, SDL_ExtendedGameControllerBind *b) static SDL_bool HasSameOutput(SDL_ExtendedGameControllerBind *a, SDL_ExtendedGameControllerBind *b)

View File

@ -752,13 +752,14 @@ static void UpdateEventsForDeviceRemoval()
{ {
int i, num_events; int i, num_events;
SDL_Event *events; SDL_Event *events;
SDL_bool isstack;
num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED); num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED);
if (num_events <= 0) { if (num_events <= 0) {
return; return;
} }
events = SDL_stack_alloc(SDL_Event, num_events); events = SDL_small_alloc(SDL_Event, num_events, &isstack);
if (!events) { if (!events) {
return; return;
} }
@ -769,7 +770,7 @@ static void UpdateEventsForDeviceRemoval()
} }
SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0); SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0);
SDL_stack_free(events); SDL_small_free(events, isstack);
} }
void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance) void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)

View File

@ -61,12 +61,13 @@ SDL_LoadFunction(void *handle, const char *name)
void *symbol = dlsym(handle, name); void *symbol = dlsym(handle, name);
if (symbol == NULL) { if (symbol == NULL) {
/* append an underscore for platforms that need that. */ /* append an underscore for platforms that need that. */
SDL_bool isstack;
size_t len = 1 + SDL_strlen(name) + 1; size_t len = 1 + SDL_strlen(name) + 1;
char *_name = SDL_stack_alloc(char, len); char *_name = SDL_small_alloc(char, len, &isstack);
_name[0] = '_'; _name[0] = '_';
SDL_strlcpy(&_name[1], name, len); SDL_strlcpy(&_name[1], name, len);
symbol = dlsym(handle, _name); symbol = dlsym(handle, _name);
SDL_stack_free(_name); SDL_small_free(_name, isstack);
if (symbol == NULL) { if (symbol == NULL) {
SDL_SetError("Failed loading %s: %s", name, SDL_SetError("Failed loading %s: %s", name,
(const char *) dlerror()); (const char *) dlerror());

View File

@ -131,6 +131,7 @@ main_utf8(int argc, char *argv[])
static int static int
main_getcmdline() main_getcmdline()
{ {
SDL_bool isstack;
char **argv; char **argv;
int argc; int argc;
char *cmdline; char *cmdline;
@ -150,7 +151,7 @@ main_getcmdline()
/* Parse it into argv and argc */ /* Parse it into argv and argc */
argc = ParseCommandLine(cmdline, NULL); argc = ParseCommandLine(cmdline, NULL);
argv = SDL_stack_alloc(char *, argc + 1); argv = SDL_small_alloc(char *, argc + 1, &isstack);
if (argv == NULL) { if (argv == NULL) {
return OutOfMemory(); return OutOfMemory();
} }
@ -158,7 +159,7 @@ main_getcmdline()
retval = main_utf8(argc, argv); retval = main_utf8(argc, argv);
SDL_stack_free(argv); SDL_small_free(argv, isstack);
SDL_free(cmdline); SDL_free(cmdline);
return retval; return retval;
@ -177,8 +178,9 @@ console_ansi_main(int argc, char *argv[])
int int
console_wmain(int argc, wchar_t *wargv[], wchar_t *wenvp) console_wmain(int argc, wchar_t *wargv[], wchar_t *wenvp)
{ {
SDL_bool isstack;
int retval = 0; int retval = 0;
char **argv = SDL_stack_alloc(char*, argc + 1); char **argv = SDL_small_alloc(char*, argc + 1, &isstack);
int i; int i;
for (i = 0; i < argc; ++i) { for (i = 0; i < argc; ++i) {
@ -189,7 +191,7 @@ console_wmain(int argc, wchar_t *wargv[], wchar_t *wenvp)
retval = main_utf8(argc, argv); retval = main_utf8(argc, argv);
/* !!! FIXME: we are leaking all the elements of argv we allocated. */ /* !!! FIXME: we are leaking all the elements of argv we allocated. */
SDL_stack_free(argv); SDL_small_free(argv, isstack);
return retval; return retval;
} }

View File

@ -2176,8 +2176,9 @@ RenderDrawPointsWithRects(SDL_Renderer * renderer,
SDL_FRect *frects; SDL_FRect *frects;
int i; int i;
int retval = -1; int retval = -1;
SDL_bool isstack;
frects = SDL_stack_alloc(SDL_FRect, count); frects = SDL_small_alloc(SDL_FRect, count, &isstack);
if (!frects) { if (!frects) {
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
@ -2190,7 +2191,7 @@ RenderDrawPointsWithRects(SDL_Renderer * renderer,
retval = QueueCmdFillRects(renderer, frects, count); retval = QueueCmdFillRects(renderer, frects, count);
SDL_stack_free(frects); SDL_small_free(frects, isstack);
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer); return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
} }
@ -2202,6 +2203,7 @@ SDL_RenderDrawPoints(SDL_Renderer * renderer,
SDL_FPoint *fpoints; SDL_FPoint *fpoints;
int i; int i;
int retval; int retval;
SDL_bool isstack;
CHECK_RENDERER_MAGIC(renderer, -1); CHECK_RENDERER_MAGIC(renderer, -1);
@ -2221,7 +2223,7 @@ SDL_RenderDrawPoints(SDL_Renderer * renderer,
return RenderDrawPointsWithRects(renderer, points, count); return RenderDrawPointsWithRects(renderer, points, count);
} }
fpoints = SDL_stack_alloc(SDL_FPoint, count); fpoints = SDL_small_alloc(SDL_FPoint, count, &isstack);
if (!fpoints) { if (!fpoints) {
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
@ -2232,7 +2234,7 @@ SDL_RenderDrawPoints(SDL_Renderer * renderer,
retval = QueueCmdDrawPoints(renderer, fpoints, count); retval = QueueCmdDrawPoints(renderer, fpoints, count);
SDL_stack_free(fpoints); SDL_small_free(fpoints, isstack);
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer); return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
} }
@ -2258,8 +2260,9 @@ RenderDrawLinesWithRects(SDL_Renderer * renderer,
SDL_FPoint fpoints[2]; SDL_FPoint fpoints[2];
int i, nrects = 0; int i, nrects = 0;
int retval = 0; int retval = 0;
SDL_bool isstack;
frects = SDL_stack_alloc(SDL_FRect, count-1); frects = SDL_small_alloc(SDL_FRect, count-1, &isstack);
if (!frects) { if (!frects) {
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
@ -2295,7 +2298,7 @@ RenderDrawLinesWithRects(SDL_Renderer * renderer,
retval += QueueCmdFillRects(renderer, frects, nrects); retval += QueueCmdFillRects(renderer, frects, nrects);
SDL_stack_free(frects); SDL_small_free(frects, isstack);
if (retval < 0) { if (retval < 0) {
retval = -1; retval = -1;
@ -2310,6 +2313,7 @@ SDL_RenderDrawLines(SDL_Renderer * renderer,
SDL_FPoint *fpoints; SDL_FPoint *fpoints;
int i; int i;
int retval; int retval;
SDL_bool isstack;
CHECK_RENDERER_MAGIC(renderer, -1); CHECK_RENDERER_MAGIC(renderer, -1);
@ -2329,7 +2333,7 @@ SDL_RenderDrawLines(SDL_Renderer * renderer,
return RenderDrawLinesWithRects(renderer, points, count); return RenderDrawLinesWithRects(renderer, points, count);
} }
fpoints = SDL_stack_alloc(SDL_FPoint, count); fpoints = SDL_small_alloc(SDL_FPoint, count, &isstack);
if (!fpoints) { if (!fpoints) {
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
@ -2340,7 +2344,7 @@ SDL_RenderDrawLines(SDL_Renderer * renderer,
retval = QueueCmdDrawLines(renderer, fpoints, count); retval = QueueCmdDrawLines(renderer, fpoints, count);
SDL_stack_free(fpoints); SDL_small_free(fpoints, isstack);
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer); return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
} }
@ -2426,6 +2430,7 @@ SDL_RenderFillRects(SDL_Renderer * renderer,
SDL_FRect *frects; SDL_FRect *frects;
int i; int i;
int retval; int retval;
SDL_bool isstack;
CHECK_RENDERER_MAGIC(renderer, -1); CHECK_RENDERER_MAGIC(renderer, -1);
@ -2441,7 +2446,7 @@ SDL_RenderFillRects(SDL_Renderer * renderer,
return 0; return 0;
} }
frects = SDL_stack_alloc(SDL_FRect, count); frects = SDL_small_alloc(SDL_FRect, count, &isstack);
if (!frects) { if (!frects) {
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
@ -2454,7 +2459,7 @@ SDL_RenderFillRects(SDL_Renderer * renderer,
retval = QueueCmdFillRects(renderer, frects, count); retval = QueueCmdFillRects(renderer, frects, count);
SDL_stack_free(frects); SDL_small_free(frects, isstack);
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer); return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
} }

View File

@ -1348,10 +1348,11 @@ GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
/* Flip the rows to be top-down if necessary */ /* Flip the rows to be top-down if necessary */
if (!renderer->target) { if (!renderer->target) {
SDL_bool isstack;
length = rect->w * SDL_BYTESPERPIXEL(temp_format); length = rect->w * SDL_BYTESPERPIXEL(temp_format);
src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch; src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
dst = (Uint8*)temp_pixels; dst = (Uint8*)temp_pixels;
tmp = SDL_stack_alloc(Uint8, length); tmp = SDL_small_alloc(Uint8, length, &isstack);
rows = rect->h / 2; rows = rect->h / 2;
while (rows--) { while (rows--) {
SDL_memcpy(tmp, dst, length); SDL_memcpy(tmp, dst, length);
@ -1360,7 +1361,7 @@ GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
dst += temp_pitch; dst += temp_pitch;
src -= temp_pitch; src -= temp_pitch;
} }
SDL_stack_free(tmp); SDL_small_free(tmp, isstack);
} }
status = SDL_ConvertPixels(rect->w, rect->h, status = SDL_ConvertPixels(rect->w, rect->h,

View File

@ -340,11 +340,12 @@ CompileShader(GL_ShaderContext *ctx, GLhandleARB shader, const char *defines, co
ctx->glCompileShaderARB(shader); ctx->glCompileShaderARB(shader);
ctx->glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status); ctx->glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
if (status == 0) { if (status == 0) {
SDL_bool isstack;
GLint length; GLint length;
char *info; char *info;
ctx->glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length); ctx->glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
info = SDL_stack_alloc(char, length+1); info = SDL_small_alloc(char, length+1, &isstack);
ctx->glGetInfoLogARB(shader, length, NULL, info); ctx->glGetInfoLogARB(shader, length, NULL, info);
SDL_LogError(SDL_LOG_CATEGORY_RENDER, SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"Failed to compile shader:\n%s%s\n%s", defines, source, info); "Failed to compile shader:\n%s%s\n%s", defines, source, info);
@ -352,7 +353,7 @@ CompileShader(GL_ShaderContext *ctx, GLhandleARB shader, const char *defines, co
fprintf(stderr, fprintf(stderr,
"Failed to compile shader:\n%s%s\n%s", defines, source, info); "Failed to compile shader:\n%s%s\n%s", defines, source, info);
#endif #endif
SDL_stack_free(info); SDL_small_free(info, isstack);
return SDL_FALSE; return SDL_FALSE;
} else { } else {

View File

@ -972,10 +972,11 @@ GLES_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
/* Flip the rows to be top-down if necessary */ /* Flip the rows to be top-down if necessary */
if (!renderer->target) { if (!renderer->target) {
SDL_bool isstack;
length = rect->w * SDL_BYTESPERPIXEL(temp_format); length = rect->w * SDL_BYTESPERPIXEL(temp_format);
src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch; src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
dst = (Uint8*)temp_pixels; dst = (Uint8*)temp_pixels;
tmp = SDL_stack_alloc(Uint8, length); tmp = SDL_small_alloc(Uint8, length, &isstack);
rows = rect->h / 2; rows = rect->h / 2;
while (rows--) { while (rows--) {
SDL_memcpy(tmp, dst, length); SDL_memcpy(tmp, dst, length);
@ -984,7 +985,7 @@ GLES_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
dst += temp_pitch; dst += temp_pitch;
src -= temp_pitch; src -= temp_pitch;
} }
SDL_stack_free(tmp); SDL_small_free(tmp, isstack);
} }
status = SDL_ConvertPixels(rect->w, rect->h, status = SDL_ConvertPixels(rect->w, rect->h,

View File

@ -601,19 +601,20 @@ GLES2_CacheShader(GLES2_RenderData *data, GLES2_ShaderType type)
compileSuccessful = GL_TRUE; compileSuccessful = GL_TRUE;
} }
if (!compileSuccessful) { if (!compileSuccessful) {
SDL_bool isstack = SDL_FALSE;
char *info = NULL; char *info = NULL;
int length = 0; int length = 0;
data->glGetShaderiv(entry->id, GL_INFO_LOG_LENGTH, &length); data->glGetShaderiv(entry->id, GL_INFO_LOG_LENGTH, &length);
if (length > 0) { if (length > 0) {
info = SDL_stack_alloc(char, length); info = SDL_small_alloc(char, length, &isstack);
if (info) { if (info) {
data->glGetShaderInfoLog(entry->id, length, &length, info); data->glGetShaderInfoLog(entry->id, length, &length, info);
} }
} }
if (info) { if (info) {
SDL_SetError("Failed to load the shader: %s", info); SDL_SetError("Failed to load the shader: %s", info);
SDL_stack_free(info); SDL_small_free(info, isstack);
} else { } else {
SDL_SetError("Failed to load the shader"); SDL_SetError("Failed to load the shader");
} }
@ -1814,10 +1815,11 @@ GLES2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
/* Flip the rows to be top-down if necessary */ /* Flip the rows to be top-down if necessary */
if (!renderer->target) { if (!renderer->target) {
SDL_bool isstack;
length = rect->w * SDL_BYTESPERPIXEL(temp_format); length = rect->w * SDL_BYTESPERPIXEL(temp_format);
src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch; src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
dst = (Uint8*)temp_pixels; dst = (Uint8*)temp_pixels;
tmp = SDL_stack_alloc(Uint8, length); tmp = SDL_small_alloc(Uint8, length, &isstack);
rows = rect->h / 2; rows = rect->h / 2;
while (rows--) { while (rows--) {
SDL_memcpy(tmp, dst, length); SDL_memcpy(tmp, dst, length);
@ -1826,7 +1828,7 @@ GLES2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
dst += temp_pitch; dst += temp_pitch;
src -= temp_pitch; src -= temp_pitch;
} }
SDL_stack_free(tmp); SDL_small_free(tmp, isstack);
} }
status = SDL_ConvertPixels(rect->w, rect->h, status = SDL_ConvertPixels(rect->w, rect->h,

View File

@ -187,6 +187,7 @@ Cocoa_InitModes(_THIS)
CGDisplayErr result; CGDisplayErr result;
CGDirectDisplayID *displays; CGDirectDisplayID *displays;
CGDisplayCount numDisplays; CGDisplayCount numDisplays;
SDL_bool isstack;
int pass, i; int pass, i;
result = CGGetOnlineDisplayList(0, NULL, &numDisplays); result = CGGetOnlineDisplayList(0, NULL, &numDisplays);
@ -194,11 +195,11 @@ Cocoa_InitModes(_THIS)
CG_SetError("CGGetOnlineDisplayList()", result); CG_SetError("CGGetOnlineDisplayList()", result);
return; return;
} }
displays = SDL_stack_alloc(CGDirectDisplayID, numDisplays); displays = SDL_small_alloc(CGDirectDisplayID, numDisplays, &isstack);
result = CGGetOnlineDisplayList(numDisplays, displays, &numDisplays); result = CGGetOnlineDisplayList(numDisplays, displays, &numDisplays);
if (result != kCGErrorSuccess) { if (result != kCGErrorSuccess) {
CG_SetError("CGGetOnlineDisplayList()", result); CG_SetError("CGGetOnlineDisplayList()", result);
SDL_stack_free(displays); SDL_small_free(displays, isstack);
return; return;
} }
@ -260,7 +261,7 @@ Cocoa_InitModes(_THIS)
SDL_free(display.name); SDL_free(display.name);
} }
} }
SDL_stack_free(displays); SDL_small_free(displays, isstack);
}} }}
int int

View File

@ -901,7 +901,8 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
case WM_TOUCH: case WM_TOUCH:
if (data->videodata->GetTouchInputInfo && data->videodata->CloseTouchInputHandle) { if (data->videodata->GetTouchInputInfo && data->videodata->CloseTouchInputHandle) {
UINT i, num_inputs = LOWORD(wParam); UINT i, num_inputs = LOWORD(wParam);
PTOUCHINPUT inputs = SDL_stack_alloc(TOUCHINPUT, num_inputs); SDL_bool isstack;
PTOUCHINPUT inputs = SDL_small_alloc(TOUCHINPUT, num_inputs, &isstack);
if (data->videodata->GetTouchInputInfo((HTOUCHINPUT)lParam, num_inputs, inputs, sizeof(TOUCHINPUT))) { if (data->videodata->GetTouchInputInfo((HTOUCHINPUT)lParam, num_inputs, inputs, sizeof(TOUCHINPUT))) {
RECT rect; RECT rect;
float x, y; float x, y;
@ -909,7 +910,7 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
if (!GetClientRect(hwnd, &rect) || if (!GetClientRect(hwnd, &rect) ||
(rect.right == rect.left && rect.bottom == rect.top)) { (rect.right == rect.left && rect.bottom == rect.top)) {
if (inputs) { if (inputs) {
SDL_stack_free(inputs); SDL_small_free(inputs, isstack);
} }
break; break;
} }
@ -943,7 +944,7 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
} }
} }
} }
SDL_stack_free(inputs); SDL_small_free(inputs, isstack);
data->videodata->CloseTouchInputHandle((HTOUCHINPUT)lParam); data->videodata->CloseTouchInputHandle((HTOUCHINPUT)lParam);
return 0; return 0;
@ -954,17 +955,19 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{ {
UINT i; UINT i;
HDROP drop = (HDROP) wParam; HDROP drop = (HDROP) wParam;
SDL_bool isstack;
UINT count = DragQueryFile(drop, 0xFFFFFFFF, NULL, 0); UINT count = DragQueryFile(drop, 0xFFFFFFFF, NULL, 0);
for (i = 0; i < count; ++i) { for (i = 0; i < count; ++i) {
SDL_bool isstack;
UINT size = DragQueryFile(drop, i, NULL, 0) + 1; UINT size = DragQueryFile(drop, i, NULL, 0) + 1;
LPTSTR buffer = SDL_stack_alloc(TCHAR, size); LPTSTR buffer = SDL_small_alloc(TCHAR, size, &isstack);
if (buffer) { if (buffer) {
if (DragQueryFile(drop, i, buffer, size)) { if (DragQueryFile(drop, i, buffer, size)) {
char *file = WIN_StringToUTF8(buffer); char *file = WIN_StringToUTF8(buffer);
SDL_SendDropFile(data->window, file); SDL_SendDropFile(data->window, file);
SDL_free(file); SDL_free(file);
} }
SDL_stack_free(buffer); SDL_small_free(buffer, isstack);
} }
} }
SDL_SendDropComplete(data->window); SDL_SendDropComplete(data->window);

View File

@ -27,6 +27,7 @@
int WIN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch) int WIN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
{ {
SDL_WindowData *data = (SDL_WindowData *) window->driverdata; SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
SDL_bool isstack;
size_t size; size_t size;
LPBITMAPINFO info; LPBITMAPINFO info;
HBITMAP hbm; HBITMAP hbm;
@ -41,7 +42,7 @@ int WIN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, voi
/* Find out the format of the screen */ /* Find out the format of the screen */
size = sizeof(BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD); size = sizeof(BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD);
info = (LPBITMAPINFO)SDL_stack_alloc(Uint8, size); info = (LPBITMAPINFO)SDL_small_alloc(Uint8, size, &isstack);
if (!info) { if (!info) {
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
@ -85,7 +86,7 @@ int WIN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, voi
data->mdc = CreateCompatibleDC(data->hdc); data->mdc = CreateCompatibleDC(data->hdc);
data->hbm = CreateDIBSection(data->hdc, info, DIB_RGB_COLORS, pixels, NULL, 0); data->hbm = CreateDIBSection(data->hdc, info, DIB_RGB_COLORS, pixels, NULL, 0);
SDL_stack_free(info); SDL_small_free(info, isstack);
if (!data->hbm) { if (!data->hbm) {
return WIN_SetError("Unable to create DIB"); return WIN_SetError("Unable to create DIB");

View File

@ -97,6 +97,7 @@ WIN_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
LPVOID pixels; LPVOID pixels;
LPVOID maskbits; LPVOID maskbits;
size_t maskbitslen; size_t maskbitslen;
SDL_bool isstack;
ICONINFO ii; ICONINFO ii;
SDL_zero(bmh); SDL_zero(bmh);
@ -112,7 +113,7 @@ WIN_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
bmh.bV4BlueMask = 0x000000FF; bmh.bV4BlueMask = 0x000000FF;
maskbitslen = ((surface->w + (pad - (surface->w % pad))) / 8) * surface->h; maskbitslen = ((surface->w + (pad - (surface->w % pad))) / 8) * surface->h;
maskbits = SDL_stack_alloc(Uint8,maskbitslen); maskbits = SDL_small_alloc(Uint8,maskbitslen);
if (maskbits == NULL) { if (maskbits == NULL) {
SDL_OutOfMemory(); SDL_OutOfMemory();
return NULL; return NULL;
@ -129,7 +130,7 @@ WIN_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
ii.hbmColor = CreateDIBSection(hdc, (BITMAPINFO*)&bmh, DIB_RGB_COLORS, &pixels, NULL, 0); ii.hbmColor = CreateDIBSection(hdc, (BITMAPINFO*)&bmh, DIB_RGB_COLORS, &pixels, NULL, 0);
ii.hbmMask = CreateBitmap(surface->w, surface->h, 1, 1, maskbits); ii.hbmMask = CreateBitmap(surface->w, surface->h, 1, 1, maskbits);
ReleaseDC(NULL, hdc); ReleaseDC(NULL, hdc);
SDL_stack_free(maskbits); SDL_small_free(maskbits, isstack);
SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888); SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
SDL_assert(surface->pitch == surface->w * 4); SDL_assert(surface->pitch == surface->w * 4);

View File

@ -380,10 +380,11 @@ WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
HWND hwnd = (HWND) data; HWND hwnd = (HWND) data;
LPTSTR title; LPTSTR title;
int titleLen; int titleLen;
SDL_bool isstack;
/* Query the title from the existing window */ /* Query the title from the existing window */
titleLen = GetWindowTextLength(hwnd); titleLen = GetWindowTextLength(hwnd);
title = SDL_stack_alloc(TCHAR, titleLen + 1); title = SDL_small_alloc(TCHAR, titleLen + 1, &isstack);
if (title) { if (title) {
titleLen = GetWindowText(hwnd, title, titleLen); titleLen = GetWindowText(hwnd, title, titleLen);
} else { } else {
@ -393,7 +394,7 @@ WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
window->title = WIN_StringToUTF8(title); window->title = WIN_StringToUTF8(title);
} }
if (title) { if (title) {
SDL_stack_free(title); SDL_small_free(title, isstack);
} }
if (SetupWindowData(_this, window, hwnd, GetParent(hwnd), SDL_FALSE) < 0) { if (SetupWindowData(_this, window, hwnd, GetParent(hwnd), SDL_FALSE) < 0) {
@ -443,14 +444,15 @@ WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
BYTE *icon_bmp; BYTE *icon_bmp;
int icon_len, mask_len, y; int icon_len, mask_len, y;
SDL_RWops *dst; SDL_RWops *dst;
SDL_bool isstack;
/* Create temporary buffer for ICONIMAGE structure */ /* Create temporary buffer for ICONIMAGE structure */
mask_len = (icon->h * (icon->w + 7)/8); mask_len = (icon->h * (icon->w + 7)/8);
icon_len = 40 + icon->h * icon->w * sizeof(Uint32) + mask_len; icon_len = 40 + icon->h * icon->w * sizeof(Uint32) + mask_len;
icon_bmp = SDL_stack_alloc(BYTE, icon_len); icon_bmp = SDL_small_alloc(BYTE, icon_len, &isstack);
dst = SDL_RWFromMem(icon_bmp, icon_len); dst = SDL_RWFromMem(icon_bmp, icon_len);
if (!dst) { if (!dst) {
SDL_stack_free(icon_bmp); SDL_small_free(icon_bmp, isstack);
return; return;
} }
@ -481,7 +483,7 @@ WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
hicon = CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000); hicon = CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
SDL_RWclose(dst); SDL_RWclose(dst);
SDL_stack_free(icon_bmp); SDL_small_free(icon_bmp, isstack);
/* Set the icon for the window */ /* Set the icon for the window */
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hicon); SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hicon);