Commit b262b0ebc90d0555ec5efdb1990fe7c494296e88

Ryan C. Gordon 2018-10-22T20:50:32

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
diff --git a/src/SDL_assert.c b/src/SDL_assert.c
index 1ca083a..5a9556d 100644
--- a/src/SDL_assert.c
+++ b/src/SDL_assert.c
@@ -178,6 +178,7 @@ SDL_PromptAssertion(const SDL_assert_data *data, void *userdata)
 
     (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);
     if (!message) {
         /* Uh oh, we're in real trouble now... */
diff --git a/src/SDL_internal.h b/src/SDL_internal.h
index e0ba2a8..f8215bf 100644
--- a/src/SDL_internal.h
+++ b/src/SDL_internal.h
@@ -35,6 +35,10 @@
 #define SDL_VARIABLE_LENGTH_ARRAY
 #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"
 
 #if SDL_DYNAMIC_API
diff --git a/src/SDL_log.c b/src/SDL_log.c
index b1bf27d..b89f7ee 100644
--- a/src/SDL_log.c
+++ b/src/SDL_log.c
@@ -282,6 +282,7 @@ SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list
         return;
     }
 
+    /* !!! FIXME: why not just "char message[SDL_MAX_LOG_MESSAGE];" ? */
     message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
     if (!message) {
         return;
@@ -321,6 +322,7 @@ SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
         char *output;
         size_t length;
         LPTSTR tstr;
+        SDL_bool isstack;
 
 #if !defined(HAVE_STDIO_H) && !defined(__WINRT__)
         BOOL attachResult;
@@ -364,7 +366,7 @@ SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
 #endif /* !defined(HAVE_STDIO_H) && !defined(__WINRT__) */
 
         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);
         tstr = WIN_UTF8ToString(output);
         
@@ -389,7 +391,7 @@ SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
 #endif /* !defined(HAVE_STDIO_H) && !defined(__WINRT__) */
 
         SDL_free(tstr);
-        SDL_stack_free(output);
+        SDL_small_free(output, isstack);
     }
 #elif defined(__ANDROID__)
     {
@@ -404,7 +406,7 @@ SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
     extern void SDL_NSLog(const char *text);
     {
         char *text;
-
+        /* !!! FIXME: why not just "char text[SDL_MAX_LOG_MESSAGE];" ? */
         text = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
         if (text) {
             SDL_snprintf(text, SDL_MAX_LOG_MESSAGE, "%s: %s", SDL_priority_prefixes[priority], message);
diff --git a/src/core/android/SDL_android.c b/src/core/android/SDL_android.c
index 9178604..81ccd4a 100644
--- a/src/core/android/SDL_android.c
+++ b/src/core/android/SDL_android.c
@@ -466,10 +466,11 @@ JNIEXPORT int JNICALL SDL_JAVA_INTERFACE(nativeRunMain)(JNIEnv* env, jclass cls,
             int argc;
             int len;
             char **argv;
+            SDL_bool isstack;
 
             /* Prepare the arguments. */
             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;
             /* Use the name "app_process" so PHYSFS_platformCalcBaseDir() works.
                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) {
                 SDL_free(argv[i]);
             }
-            SDL_stack_free(argv);
+            SDL_small_free(argv, isstack);
 
         } else {
             __android_log_print(ANDROID_LOG_ERROR, "SDL", "nativeRunMain(): Couldn't find function %s in library %s", function_name, library_file);
diff --git a/src/file/SDL_rwops.c b/src/file/SDL_rwops.c
index cf5d3aa..5b0969d 100644
--- a/src/file/SDL_rwops.c
+++ b/src/file/SDL_rwops.c
@@ -528,6 +528,7 @@ SDL_RWFromFile(const char *file, const char *mode)
         char *path;
         FILE *fp;
 
+        /* !!! FIXME: why not just "char path[PATH_MAX];" ? */
         path = SDL_stack_alloc(char, PATH_MAX);
         if (path) {
             SDL_snprintf(path, PATH_MAX, "%s/%s",
diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index fa64781..41100a4 100644
--- a/src/joystick/SDL_gamecontroller.c
+++ b/src/joystick/SDL_gamecontroller.c
@@ -203,13 +203,14 @@ static void UpdateEventsForDeviceRemoval()
 {
     int i, num_events;
     SDL_Event *events;
+    SDL_bool isstack;
 
     num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED);
     if (num_events <= 0) {
         return;
     }
 
-    events = SDL_stack_alloc(SDL_Event, num_events);
+    events = SDL_small_alloc(SDL_Event, num_events, &isstack);
     if (!events) {
         return;
     }
@@ -220,7 +221,7 @@ static void UpdateEventsForDeviceRemoval()
     }
     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)
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index 72c5656..9d22122 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -752,13 +752,14 @@ static void UpdateEventsForDeviceRemoval()
 {
     int i, num_events;
     SDL_Event *events;
+    SDL_bool isstack;
 
     num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED);
     if (num_events <= 0) {
         return;
     }
 
-    events = SDL_stack_alloc(SDL_Event, num_events);
+    events = SDL_small_alloc(SDL_Event, num_events, &isstack);
     if (!events) {
         return;
     }
@@ -769,7 +770,7 @@ static void UpdateEventsForDeviceRemoval()
     }
     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)
diff --git a/src/loadso/dlopen/SDL_sysloadso.c b/src/loadso/dlopen/SDL_sysloadso.c
index 18b4b84..ed89a21 100644
--- a/src/loadso/dlopen/SDL_sysloadso.c
+++ b/src/loadso/dlopen/SDL_sysloadso.c
@@ -61,12 +61,13 @@ SDL_LoadFunction(void *handle, const char *name)
     void *symbol = dlsym(handle, name);
     if (symbol == NULL) {
         /* append an underscore for platforms that need that. */
+        SDL_bool isstack;
         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] = '_';
         SDL_strlcpy(&_name[1], name, len);
         symbol = dlsym(handle, _name);
-        SDL_stack_free(_name);
+        SDL_small_free(_name, isstack);
         if (symbol == NULL) {
             SDL_SetError("Failed loading %s: %s", name,
                          (const char *) dlerror());
diff --git a/src/main/windows/SDL_windows_main.c b/src/main/windows/SDL_windows_main.c
index 5e643a4..212a6a3 100644
--- a/src/main/windows/SDL_windows_main.c
+++ b/src/main/windows/SDL_windows_main.c
@@ -131,6 +131,7 @@ main_utf8(int argc, char *argv[])
 static int
 main_getcmdline()
 {
+    SDL_bool isstack;
     char **argv;
     int argc;
     char *cmdline;
@@ -150,7 +151,7 @@ main_getcmdline()
 
     /* Parse it into argv and argc */
     argc = ParseCommandLine(cmdline, NULL);
-    argv = SDL_stack_alloc(char *, argc + 1);
+    argv = SDL_small_alloc(char *, argc + 1, &isstack);
     if (argv == NULL) {
         return OutOfMemory();
     }
@@ -158,7 +159,7 @@ main_getcmdline()
 
     retval = main_utf8(argc, argv);
 
-    SDL_stack_free(argv);
+    SDL_small_free(argv, isstack);
     SDL_free(cmdline);
 
     return retval;
@@ -177,8 +178,9 @@ console_ansi_main(int argc, char *argv[])
 int
 console_wmain(int argc, wchar_t *wargv[], wchar_t *wenvp)
 {
+    SDL_bool isstack;
     int retval = 0;
-    char **argv = SDL_stack_alloc(char*, argc + 1);
+    char **argv = SDL_small_alloc(char*, argc + 1, &isstack);
     int 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);
 
     /* !!! FIXME: we are leaking all the elements of argv we allocated. */
-    SDL_stack_free(argv);
+    SDL_small_free(argv, isstack);
 
     return retval;
 }
diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index 5bbcd79..b03524b 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -2176,8 +2176,9 @@ RenderDrawPointsWithRects(SDL_Renderer * renderer,
     SDL_FRect *frects;
     int i;
     int retval = -1;
+    SDL_bool isstack;
 
-    frects = SDL_stack_alloc(SDL_FRect, count);
+    frects = SDL_small_alloc(SDL_FRect, count, &isstack);
     if (!frects) {
         return SDL_OutOfMemory();
     }
@@ -2190,7 +2191,7 @@ RenderDrawPointsWithRects(SDL_Renderer * renderer,
 
     retval = QueueCmdFillRects(renderer, frects, count);
 
-    SDL_stack_free(frects);
+    SDL_small_free(frects, isstack);
 
     return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
 }
@@ -2202,6 +2203,7 @@ SDL_RenderDrawPoints(SDL_Renderer * renderer,
     SDL_FPoint *fpoints;
     int i;
     int retval;
+    SDL_bool isstack;
 
     CHECK_RENDERER_MAGIC(renderer, -1);
 
@@ -2221,7 +2223,7 @@ SDL_RenderDrawPoints(SDL_Renderer * renderer,
         return RenderDrawPointsWithRects(renderer, points, count);
     }
 
-    fpoints = SDL_stack_alloc(SDL_FPoint, count);
+    fpoints = SDL_small_alloc(SDL_FPoint, count, &isstack);
     if (!fpoints) {
         return SDL_OutOfMemory();
     }
@@ -2232,7 +2234,7 @@ SDL_RenderDrawPoints(SDL_Renderer * renderer,
 
     retval = QueueCmdDrawPoints(renderer, fpoints, count);
 
-    SDL_stack_free(fpoints);
+    SDL_small_free(fpoints, isstack);
 
     return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
 }
@@ -2258,8 +2260,9 @@ RenderDrawLinesWithRects(SDL_Renderer * renderer,
     SDL_FPoint fpoints[2];
     int i, nrects = 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) {
         return SDL_OutOfMemory();
     }
@@ -2295,7 +2298,7 @@ RenderDrawLinesWithRects(SDL_Renderer * renderer,
 
     retval += QueueCmdFillRects(renderer, frects, nrects);
 
-    SDL_stack_free(frects);
+    SDL_small_free(frects, isstack);
 
     if (retval < 0) {
         retval = -1;
@@ -2310,6 +2313,7 @@ SDL_RenderDrawLines(SDL_Renderer * renderer,
     SDL_FPoint *fpoints;
     int i;
     int retval;
+    SDL_bool isstack;
 
     CHECK_RENDERER_MAGIC(renderer, -1);
 
@@ -2329,7 +2333,7 @@ SDL_RenderDrawLines(SDL_Renderer * renderer,
         return RenderDrawLinesWithRects(renderer, points, count);
     }
 
-    fpoints = SDL_stack_alloc(SDL_FPoint, count);
+    fpoints = SDL_small_alloc(SDL_FPoint, count, &isstack);
     if (!fpoints) {
         return SDL_OutOfMemory();
     }
@@ -2340,7 +2344,7 @@ SDL_RenderDrawLines(SDL_Renderer * renderer,
 
     retval = QueueCmdDrawLines(renderer, fpoints, count);
 
-    SDL_stack_free(fpoints);
+    SDL_small_free(fpoints, isstack);
 
     return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
 }
@@ -2426,6 +2430,7 @@ SDL_RenderFillRects(SDL_Renderer * renderer,
     SDL_FRect *frects;
     int i;
     int retval;
+    SDL_bool isstack;
 
     CHECK_RENDERER_MAGIC(renderer, -1);
 
@@ -2441,7 +2446,7 @@ SDL_RenderFillRects(SDL_Renderer * renderer,
         return 0;
     }
 
-    frects = SDL_stack_alloc(SDL_FRect, count);
+    frects = SDL_small_alloc(SDL_FRect, count, &isstack);
     if (!frects) {
         return SDL_OutOfMemory();
     }
@@ -2454,7 +2459,7 @@ SDL_RenderFillRects(SDL_Renderer * renderer,
 
     retval = QueueCmdFillRects(renderer, frects, count);
 
-    SDL_stack_free(frects);
+    SDL_small_free(frects, isstack);
 
     return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
 }
diff --git a/src/render/opengl/SDL_render_gl.c b/src/render/opengl/SDL_render_gl.c
index ef8507e..7d0f08d 100644
--- a/src/render/opengl/SDL_render_gl.c
+++ b/src/render/opengl/SDL_render_gl.c
@@ -1348,10 +1348,11 @@ GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
 
     /* Flip the rows to be top-down if necessary */
     if (!renderer->target) {
+        SDL_bool isstack;
         length = rect->w * SDL_BYTESPERPIXEL(temp_format);
         src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
         dst = (Uint8*)temp_pixels;
-        tmp = SDL_stack_alloc(Uint8, length);
+        tmp = SDL_small_alloc(Uint8, length, &isstack);
         rows = rect->h / 2;
         while (rows--) {
             SDL_memcpy(tmp, dst, length);
@@ -1360,7 +1361,7 @@ GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
             dst += temp_pitch;
             src -= temp_pitch;
         }
-        SDL_stack_free(tmp);
+        SDL_small_free(tmp, isstack);
     }
 
     status = SDL_ConvertPixels(rect->w, rect->h,
diff --git a/src/render/opengl/SDL_shaders_gl.c b/src/render/opengl/SDL_shaders_gl.c
index 251b54d..650b244 100644
--- a/src/render/opengl/SDL_shaders_gl.c
+++ b/src/render/opengl/SDL_shaders_gl.c
@@ -340,11 +340,12 @@ CompileShader(GL_ShaderContext *ctx, GLhandleARB shader, const char *defines, co
     ctx->glCompileShaderARB(shader);
     ctx->glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
     if (status == 0) {
+        SDL_bool isstack;
         GLint length;
         char *info;
 
         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);
         SDL_LogError(SDL_LOG_CATEGORY_RENDER,
             "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,
             "Failed to compile shader:\n%s%s\n%s", defines, source, info);
 #endif
-        SDL_stack_free(info);
+        SDL_small_free(info, isstack);
 
         return SDL_FALSE;
     } else {
diff --git a/src/render/opengles/SDL_render_gles.c b/src/render/opengles/SDL_render_gles.c
index 6f662af..a410152 100644
--- a/src/render/opengles/SDL_render_gles.c
+++ b/src/render/opengles/SDL_render_gles.c
@@ -972,10 +972,11 @@ GLES_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
 
     /* Flip the rows to be top-down if necessary */
     if (!renderer->target) {
+        SDL_bool isstack;
         length = rect->w * SDL_BYTESPERPIXEL(temp_format);
         src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
         dst = (Uint8*)temp_pixels;
-        tmp = SDL_stack_alloc(Uint8, length);
+        tmp = SDL_small_alloc(Uint8, length, &isstack);
         rows = rect->h / 2;
         while (rows--) {
             SDL_memcpy(tmp, dst, length);
@@ -984,7 +985,7 @@ GLES_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
             dst += temp_pitch;
             src -= temp_pitch;
         }
-        SDL_stack_free(tmp);
+        SDL_small_free(tmp, isstack);
     }
 
     status = SDL_ConvertPixels(rect->w, rect->h,
diff --git a/src/render/opengles2/SDL_render_gles2.c b/src/render/opengles2/SDL_render_gles2.c
index 97d1d44..c6c9f80 100644
--- a/src/render/opengles2/SDL_render_gles2.c
+++ b/src/render/opengles2/SDL_render_gles2.c
@@ -601,19 +601,20 @@ GLES2_CacheShader(GLES2_RenderData *data, GLES2_ShaderType type)
         compileSuccessful = GL_TRUE;
     }
     if (!compileSuccessful) {
+        SDL_bool isstack = SDL_FALSE;
         char *info = NULL;
         int length = 0;
 
         data->glGetShaderiv(entry->id, GL_INFO_LOG_LENGTH, &length);
         if (length > 0) {
-            info = SDL_stack_alloc(char, length);
+            info = SDL_small_alloc(char, length, &isstack);
             if (info) {
                 data->glGetShaderInfoLog(entry->id, length, &length, info);
             }
         }
         if (info) {
             SDL_SetError("Failed to load the shader: %s", info);
-            SDL_stack_free(info);
+            SDL_small_free(info, isstack);
         } else {
             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 */
     if (!renderer->target) {
+        SDL_bool isstack;
         length = rect->w * SDL_BYTESPERPIXEL(temp_format);
         src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
         dst = (Uint8*)temp_pixels;
-        tmp = SDL_stack_alloc(Uint8, length);
+        tmp = SDL_small_alloc(Uint8, length, &isstack);
         rows = rect->h / 2;
         while (rows--) {
             SDL_memcpy(tmp, dst, length);
@@ -1826,7 +1828,7 @@ GLES2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
             dst += temp_pitch;
             src -= temp_pitch;
         }
-        SDL_stack_free(tmp);
+        SDL_small_free(tmp, isstack);
     }
 
     status = SDL_ConvertPixels(rect->w, rect->h,
diff --git a/src/video/cocoa/SDL_cocoamodes.m b/src/video/cocoa/SDL_cocoamodes.m
index 97ccd94..b614b79 100644
--- a/src/video/cocoa/SDL_cocoamodes.m
+++ b/src/video/cocoa/SDL_cocoamodes.m
@@ -187,6 +187,7 @@ Cocoa_InitModes(_THIS)
     CGDisplayErr result;
     CGDirectDisplayID *displays;
     CGDisplayCount numDisplays;
+    SDL_bool isstack;
     int pass, i;
 
     result = CGGetOnlineDisplayList(0, NULL, &numDisplays);
@@ -194,11 +195,11 @@ Cocoa_InitModes(_THIS)
         CG_SetError("CGGetOnlineDisplayList()", result);
         return;
     }
-    displays = SDL_stack_alloc(CGDirectDisplayID, numDisplays);
+    displays = SDL_small_alloc(CGDirectDisplayID, numDisplays, &isstack);
     result = CGGetOnlineDisplayList(numDisplays, displays, &numDisplays);
     if (result != kCGErrorSuccess) {
         CG_SetError("CGGetOnlineDisplayList()", result);
-        SDL_stack_free(displays);
+        SDL_small_free(displays, isstack);
         return;
     }
 
@@ -260,7 +261,7 @@ Cocoa_InitModes(_THIS)
             SDL_free(display.name);
         }
     }
-    SDL_stack_free(displays);
+    SDL_small_free(displays, isstack);
 }}
 
 int
diff --git a/src/video/windows/SDL_windowsevents.c b/src/video/windows/SDL_windowsevents.c
index 5773f39..6505b89 100644
--- a/src/video/windows/SDL_windowsevents.c
+++ b/src/video/windows/SDL_windowsevents.c
@@ -901,7 +901,8 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
     case WM_TOUCH:
         if (data->videodata->GetTouchInputInfo && data->videodata->CloseTouchInputHandle) {
             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))) {
                 RECT rect;
                 float x, y;
@@ -909,7 +910,7 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
                 if (!GetClientRect(hwnd, &rect) ||
                     (rect.right == rect.left && rect.bottom == rect.top)) {
                     if (inputs) {
-                        SDL_stack_free(inputs);
+                        SDL_small_free(inputs, isstack);
                     }
                     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);
             return 0;
@@ -954,17 +955,19 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
         {
             UINT i;
             HDROP drop = (HDROP) wParam;
+            SDL_bool isstack;
             UINT count = DragQueryFile(drop, 0xFFFFFFFF, NULL, 0);
             for (i = 0; i < count; ++i) {
+                SDL_bool isstack;
                 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 (DragQueryFile(drop, i, buffer, size)) {
                         char *file = WIN_StringToUTF8(buffer);
                         SDL_SendDropFile(data->window, file);
                         SDL_free(file);
                     }
-                    SDL_stack_free(buffer);
+                    SDL_small_free(buffer, isstack);
                 }
             }
             SDL_SendDropComplete(data->window);
diff --git a/src/video/windows/SDL_windowsframebuffer.c b/src/video/windows/SDL_windowsframebuffer.c
index e07d9c4..f884f7f 100644
--- a/src/video/windows/SDL_windowsframebuffer.c
+++ b/src/video/windows/SDL_windowsframebuffer.c
@@ -27,6 +27,7 @@
 int WIN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
 {
     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
+    SDL_bool isstack;
     size_t size;
     LPBITMAPINFO info;
     HBITMAP hbm;
@@ -41,7 +42,7 @@ int WIN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, voi
 
     /* Find out the format of the screen */
     size = sizeof(BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD);
-    info = (LPBITMAPINFO)SDL_stack_alloc(Uint8, size);
+    info = (LPBITMAPINFO)SDL_small_alloc(Uint8, size, &isstack);
     if (!info) {
         return SDL_OutOfMemory();
     }
@@ -85,7 +86,7 @@ int WIN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, voi
 
     data->mdc = CreateCompatibleDC(data->hdc);
     data->hbm = CreateDIBSection(data->hdc, info, DIB_RGB_COLORS, pixels, NULL, 0);
-    SDL_stack_free(info);
+    SDL_small_free(info, isstack);
 
     if (!data->hbm) {
         return WIN_SetError("Unable to create DIB");
diff --git a/src/video/windows/SDL_windowsmouse.c b/src/video/windows/SDL_windowsmouse.c
index 1ddeae2..a8abc48 100644
--- a/src/video/windows/SDL_windowsmouse.c
+++ b/src/video/windows/SDL_windowsmouse.c
@@ -97,6 +97,7 @@ WIN_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
     LPVOID pixels;
     LPVOID maskbits;
     size_t maskbitslen;
+    SDL_bool isstack;
     ICONINFO ii;
 
     SDL_zero(bmh);
@@ -112,7 +113,7 @@ WIN_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
     bmh.bV4BlueMask  = 0x000000FF;
 
     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) {
         SDL_OutOfMemory();
         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.hbmMask = CreateBitmap(surface->w, surface->h, 1, 1, maskbits);
     ReleaseDC(NULL, hdc);
-    SDL_stack_free(maskbits);
+    SDL_small_free(maskbits, isstack);
 
     SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
     SDL_assert(surface->pitch == surface->w * 4);
diff --git a/src/video/windows/SDL_windowswindow.c b/src/video/windows/SDL_windowswindow.c
index ba5ef05..e6ce763 100644
--- a/src/video/windows/SDL_windowswindow.c
+++ b/src/video/windows/SDL_windowswindow.c
@@ -380,10 +380,11 @@ WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
     HWND hwnd = (HWND) data;
     LPTSTR title;
     int titleLen;
+    SDL_bool isstack;
 
     /* Query the title from the existing window */
     titleLen = GetWindowTextLength(hwnd);
-    title = SDL_stack_alloc(TCHAR, titleLen + 1);
+    title = SDL_small_alloc(TCHAR, titleLen + 1, &isstack);
     if (title) {
         titleLen = GetWindowText(hwnd, title, titleLen);
     } else {
@@ -393,7 +394,7 @@ WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
         window->title = WIN_StringToUTF8(title);
     }
     if (title) {
-        SDL_stack_free(title);
+        SDL_small_free(title, isstack);
     }
 
     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;
     int icon_len, mask_len, y;
     SDL_RWops *dst;
+    SDL_bool isstack;
 
     /* Create temporary buffer for ICONIMAGE structure */
     mask_len = (icon->h * (icon->w + 7)/8);
     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);
     if (!dst) {
-        SDL_stack_free(icon_bmp);
+        SDL_small_free(icon_bmp, isstack);
         return;
     }
 
@@ -481,7 +483,7 @@ WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
     hicon = CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
 
     SDL_RWclose(dst);
-    SDL_stack_free(icon_bmp);
+    SDL_small_free(icon_bmp, isstack);
 
     /* Set the icon for the window */
     SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hicon);