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:
Ryan C. Gordon
2023-11-30 00:14:27 -05:00
parent 70b65d4170
commit 447b508a77
197 changed files with 313 additions and 742 deletions

View File

@@ -293,7 +293,6 @@ void *SDL_AllocateRenderVertices(SDL_Renderer *renderer, const size_t numbytes,
ptr = SDL_realloc(renderer->vertex_data, newsize);
if (!ptr) {
SDL_OutOfMemory();
return NULL;
}
renderer->vertex_data = ptr;
@@ -321,7 +320,6 @@ static SDL_RenderCommand *AllocateRenderCommand(SDL_Renderer *renderer)
} else {
retval = SDL_calloc(1, sizeof(*retval));
if (!retval) {
SDL_OutOfMemory();
return NULL;
}
}
@@ -1158,7 +1156,6 @@ SDL_Texture *SDL_CreateTextureWithProperties(SDL_Renderer *renderer, SDL_Propert
}
texture = (SDL_Texture *)SDL_calloc(1, sizeof(*texture));
if (!texture) {
SDL_OutOfMemory();
return NULL;
}
texture->magic = &SDL_texture_magic;
@@ -1566,7 +1563,7 @@ static int SDL_UpdateTextureYUV(SDL_Texture *texture, const SDL_Rect *rect,
if (alloclen > 0) {
void *temp_pixels = SDL_malloc(alloclen);
if (!temp_pixels) {
return SDL_OutOfMemory();
return -1;
}
SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
rect->w, rect->h, temp_pixels, temp_pitch);
@@ -1606,7 +1603,7 @@ static int SDL_UpdateTextureNative(SDL_Texture *texture, const SDL_Rect *rect,
if (alloclen > 0) {
void *temp_pixels = SDL_malloc(alloclen);
if (!temp_pixels) {
return SDL_OutOfMemory();
return -1;
}
SDL_ConvertPixels(rect->w, rect->h,
texture->format, pixels, pitch,
@@ -1699,7 +1696,7 @@ static int SDL_UpdateTextureYUVPlanar(SDL_Texture *texture, const SDL_Rect *rect
if (alloclen > 0) {
void *temp_pixels = SDL_malloc(alloclen);
if (!temp_pixels) {
return SDL_OutOfMemory();
return -1;
}
SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
rect->w, rect->h, temp_pixels, temp_pitch);
@@ -1749,7 +1746,7 @@ static int SDL_UpdateTextureNVPlanar(SDL_Texture *texture, const SDL_Rect *rect,
if (alloclen > 0) {
void *temp_pixels = SDL_malloc(alloclen);
if (!temp_pixels) {
return SDL_OutOfMemory();
return -1;
}
SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
rect->w, rect->h, temp_pixels, temp_pitch);
@@ -2694,7 +2691,7 @@ static int RenderPointsWithRects(SDL_Renderer *renderer, const SDL_FPoint *fpoin
frects = SDL_small_alloc(SDL_FRect, count, &isstack);
if (!frects) {
return SDL_OutOfMemory();
return -1;
}
for (i = 0; i < count; ++i) {
@@ -2814,7 +2811,7 @@ static int RenderLineBresenham(SDL_Renderer *renderer, int x1, int y1, int x2, i
points = SDL_small_alloc(SDL_FPoint, numpixels, &isstack);
if (!points) {
return SDL_OutOfMemory();
return -1;
}
for (i = 0; i < numpixels; ++i) {
points[i].x = (float)x;
@@ -2857,7 +2854,7 @@ static int RenderLinesWithRectsF(SDL_Renderer *renderer,
frects = SDL_small_alloc(SDL_FRect, count - 1, &isstack);
if (!frects) {
return SDL_OutOfMemory();
return -1;
}
for (i = 0; i < count - 1; ++i) {
@@ -3160,7 +3157,7 @@ int SDL_RenderFillRects(SDL_Renderer *renderer, const SDL_FRect *rects, int coun
frects = SDL_small_alloc(SDL_FRect, count, &isstack);
if (!frects) {
return SDL_OutOfMemory();
return -1;
}
for (i = 0; i < count; ++i) {
frects[i].x = rects[i].x * renderer->view->scale.x;

View File

@@ -47,7 +47,6 @@ SDL_SW_YUVTexture *SDL_SW_CreateYUVTexture(Uint32 format, int w, int h)
swdata = (SDL_SW_YUVTexture *)SDL_calloc(1, sizeof(*swdata));
if (!swdata) {
SDL_OutOfMemory();
return NULL;
}
@@ -59,13 +58,11 @@ SDL_SW_YUVTexture *SDL_SW_CreateYUVTexture(Uint32 format, int w, int h)
size_t dst_size;
if (SDL_CalculateYUVSize(format, w, h, &dst_size, NULL) < 0) {
SDL_SW_DestroyYUVTexture(swdata);
SDL_OutOfMemory();
return NULL;
}
swdata->pixels = (Uint8 *)SDL_aligned_alloc(SDL_SIMDGetAlignment(), dst_size);
if (!swdata->pixels) {
SDL_SW_DestroyYUVTexture(swdata);
SDL_OutOfMemory();
return NULL;
}
}

View File

@@ -525,7 +525,7 @@ static int D3D_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL_P
texturedata = (D3D_TextureData *)SDL_calloc(1, sizeof(*texturedata));
if (!texturedata) {
return SDL_OutOfMemory();
return -1;
}
texturedata->scaleMode = (texture->scaleMode == SDL_SCALEMODE_NEAREST) ? D3DTEXF_POINT : D3DTEXF_LINEAR;
@@ -661,7 +661,7 @@ static int D3D_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture,
texturedata->pitch = texture->w;
texturedata->pixels = (Uint8 *)SDL_malloc((texture->h * texturedata->pitch * 3) / 2);
if (!texturedata->pixels) {
return SDL_OutOfMemory();
return -1;
}
}
*pixels =
@@ -1562,7 +1562,6 @@ SDL_Renderer *D3D_CreateRenderer(SDL_Window *window, SDL_PropertiesID create_pro
renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
renderer->magic = &SDL_renderer_magic;
@@ -1570,7 +1569,6 @@ SDL_Renderer *D3D_CreateRenderer(SDL_Window *window, SDL_PropertiesID create_pro
data = (D3D_RenderData *)SDL_calloc(1, sizeof(*data));
if (!data) {
SDL_free(renderer);
SDL_OutOfMemory();
return NULL;
}

View File

@@ -399,7 +399,6 @@ static ID3D11BlendState *D3D11_CreateBlendState(SDL_Renderer *renderer, SDL_Blen
blendModes = (D3D11_BlendMode *)SDL_realloc(data->blendModes, (data->blendModesCount + 1) * sizeof(*blendModes));
if (!blendModes) {
SAFE_RELEASE(blendState);
SDL_OutOfMemory();
return NULL;
}
blendModes[data->blendModesCount].blendMode = blendMode;
@@ -1095,7 +1094,6 @@ static int D3D11_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL
textureData = (D3D11_TextureData *)SDL_calloc(1, sizeof(*textureData));
if (!textureData) {
SDL_OutOfMemory();
return -1;
}
textureData->scaleMode = (texture->scaleMode == SDL_SCALEMODE_NEAREST) ? D3D11_FILTER_MIN_MAG_MIP_POINT : D3D11_FILTER_MIN_MAG_MIP_LINEAR;
@@ -1554,7 +1552,7 @@ static int D3D11_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture,
textureData->pitch = texture->w;
textureData->pixels = (Uint8 *)SDL_malloc((texture->h * textureData->pitch * 3) / 2);
if (!textureData->pixels) {
return SDL_OutOfMemory();
return -1;
}
}
textureData->locked_rect = *rect;
@@ -2436,7 +2434,6 @@ SDL_Renderer *D3D11_CreateRenderer(SDL_Window *window, SDL_PropertiesID create_p
renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
renderer->magic = &SDL_renderer_magic;
@@ -2444,7 +2441,6 @@ SDL_Renderer *D3D11_CreateRenderer(SDL_Window *window, SDL_PropertiesID create_p
data = (D3D11_RenderData *)SDL_calloc(1, sizeof(*data));
if (!data) {
SDL_free(renderer);
SDL_OutOfMemory();
return NULL;
}

View File

@@ -644,7 +644,6 @@ static D3D12_PipelineState *D3D12_CreatePipelineState(SDL_Renderer *renderer,
pipelineStates = (D3D12_PipelineState *)SDL_realloc(data->pipelineStates, (data->pipelineStateCount + 1) * sizeof(*pipelineStates));
if (!pipelineStates) {
SAFE_RELEASE(pipelineState);
SDL_OutOfMemory();
return NULL;
}
@@ -1460,7 +1459,6 @@ static int D3D12_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL
textureData = (D3D12_TextureData *)SDL_calloc(1, sizeof(*textureData));
if (!textureData) {
SDL_OutOfMemory();
return -1;
}
textureData->scaleMode = (texture->scaleMode == SDL_SCALEMODE_NEAREST) ? D3D12_FILTER_MIN_MAG_MIP_POINT : D3D12_FILTER_MIN_MAG_MIP_LINEAR;
@@ -1910,7 +1908,7 @@ static int D3D12_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture,
textureData->pitch = texture->w;
textureData->pixels = (Uint8 *)SDL_malloc((texture->h * textureData->pitch * 3) / 2);
if (!textureData->pixels) {
return SDL_OutOfMemory();
return -1;
}
}
textureData->lockedRect = *rect;
@@ -2987,7 +2985,6 @@ SDL_Renderer *D3D12_CreateRenderer(SDL_Window *window, SDL_PropertiesID create_p
renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
renderer->magic = &SDL_renderer_magic;
@@ -2995,7 +2992,6 @@ SDL_Renderer *D3D12_CreateRenderer(SDL_Window *window, SDL_PropertiesID create_p
data = (D3D12_RenderData *)SDL_calloc(1, sizeof(*data));
if (!data) {
SDL_free(renderer);
SDL_OutOfMemory();
return NULL;
}

View File

@@ -339,7 +339,6 @@ static id<MTLRenderPipelineState> MakePipelineState(METAL_RenderData *data, META
return (__bridge id<MTLRenderPipelineState>)pipeline.pipe;
} else {
CFBridgingRelease(pipeline.pipe);
SDL_OutOfMemory();
return NULL;
}
}
@@ -401,7 +400,6 @@ static METAL_ShaderPipelines *ChooseShaderPipelines(METAL_RenderData *data, MTLP
allpipelines = SDL_realloc(allpipelines, (count + 1) * sizeof(METAL_ShaderPipelines));
if (allpipelines == NULL) {
SDL_OutOfMemory();
return NULL;
}
@@ -1507,7 +1505,7 @@ static int METAL_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *rect,
temp_pitch = rect->w * 4UL;
temp_pixels = SDL_malloc(temp_pitch * rect->h);
if (!temp_pixels) {
return SDL_OutOfMemory();
return -1;
}
[mtltexture getBytes:temp_pixels bytesPerRow:temp_pitch fromRegion:mtlregion mipmapLevel:0];
@@ -1747,7 +1745,6 @@ static SDL_Renderer *METAL_CreateRenderer(SDL_Window *window, SDL_PropertiesID c
renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}

View File

@@ -468,7 +468,7 @@ static int GL_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL_Pr
data = (GL_TextureData *)SDL_calloc(1, sizeof(*data));
if (!data) {
return SDL_OutOfMemory();
return -1;
}
if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
@@ -488,7 +488,7 @@ static int GL_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL_Pr
data->pixels = SDL_calloc(1, size);
if (!data->pixels) {
SDL_free(data);
return SDL_OutOfMemory();
return -1;
}
}
@@ -1483,7 +1483,7 @@ static int GL_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *rect,
temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format);
temp_pixels = SDL_malloc((size_t)rect->h * temp_pitch);
if (!temp_pixels) {
return SDL_OutOfMemory();
return -1;
}
SDL_GetCurrentRenderOutputSize(renderer, &w, &h);
@@ -1794,14 +1794,12 @@ static SDL_Renderer *GL_CreateRenderer(SDL_Window *window, SDL_PropertiesID crea
renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
goto error;
}
data = (GL_RenderData *)SDL_calloc(1, sizeof(*data));
if (!data) {
SDL_free(renderer);
SDL_OutOfMemory();
goto error;
}

View File

@@ -421,7 +421,6 @@ static GLES2_ProgramCacheEntry *GLES2_CacheProgram(GLES2_RenderData *data, GLuin
/* Create a program cache entry */
entry = (GLES2_ProgramCacheEntry *)SDL_calloc(1, sizeof(GLES2_ProgramCacheEntry));
if (!entry) {
SDL_OutOfMemory();
return NULL;
}
entry->vertex_shader = vertex;
@@ -1466,7 +1465,7 @@ static int GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL
/* Allocate a texture struct */
data = (GLES2_TextureData *)SDL_calloc(1, sizeof(GLES2_TextureData));
if (!data) {
return SDL_OutOfMemory();
return -1;
}
data->texture = 0;
#ifdef GL_TEXTURE_EXTERNAL_OES
@@ -1501,7 +1500,7 @@ static int GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL
data->pixel_data = SDL_calloc(1, size);
if (!data->pixel_data) {
SDL_free(data);
return SDL_OutOfMemory();
return -1;
}
}
@@ -1623,7 +1622,7 @@ static int GLES2_TexSubImage2D(GLES2_RenderData *data, GLenum target, GLint xoff
if ((size_t)pitch != src_pitch) {
blob = (Uint8 *)SDL_malloc(src_pitch * height);
if (!blob) {
return SDL_OutOfMemory();
return -1;
}
src = blob;
for (y = 0; y < height; ++y) {
@@ -1946,7 +1945,7 @@ static int GLES2_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *rect,
temp_pixels = SDL_malloc(buflen);
if (!temp_pixels) {
return SDL_OutOfMemory();
return -1;
}
SDL_GetCurrentRenderOutputSize(renderer, &w, &h);
@@ -2109,14 +2108,12 @@ static SDL_Renderer *GLES2_CreateRenderer(SDL_Window *window, SDL_PropertiesID c
/* Create the renderer struct */
renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(SDL_Renderer));
if (!renderer) {
SDL_OutOfMemory();
goto error;
}
data = (GLES2_RenderData *)SDL_calloc(1, sizeof(GLES2_RenderData));
if (!data) {
SDL_free(renderer);
SDL_OutOfMemory();
goto error;
}
renderer->info = GLES2_RenderDriver.info;

View File

@@ -111,7 +111,7 @@ static int PS2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL_P
GSTEXTURE *ps2_tex = (GSTEXTURE *)SDL_calloc(1, sizeof(GSTEXTURE));
if (!ps2_tex) {
return SDL_OutOfMemory();
return -1;
}
ps2_tex->Width = texture->w;
@@ -121,7 +121,7 @@ static int PS2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL_P
if (!ps2_tex->Mem) {
SDL_free(ps2_tex);
return SDL_OutOfMemory();
return -1;
}
texture->driverdata = ps2_tex;
@@ -595,14 +595,12 @@ static SDL_Renderer *PS2_CreateRenderer(SDL_Window *window, SDL_PropertiesID cre
renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
data = (PS2_RenderData *)SDL_calloc(1, sizeof(*data));
if (!data) {
PS2_DestroyRenderer(renderer);
SDL_OutOfMemory();
return NULL;
}

View File

@@ -286,7 +286,7 @@ static int TextureSwizzle(PSP_TextureData *psp_texture, void *dst)
}
if (!data) {
return SDL_OutOfMemory();
return -1;
}
for (j = 0; j < height; j++, blockaddress += 16) {
@@ -348,7 +348,7 @@ static int TextureUnswizzle(PSP_TextureData *psp_texture, void *dst)
}
if (!data) {
return SDL_OutOfMemory();
return -1;
}
ydst = (unsigned char *)data;
@@ -392,7 +392,7 @@ static int TextureSpillToSram(PSP_RenderData *data, PSP_TextureData *psp_texture
// Texture was swizzled in vram, just copy to system memory
void *sdata = SDL_malloc(psp_texture->size);
if (!sdata) {
return SDL_OutOfMemory();
return -1;
}
SDL_memcpy(sdata, psp_texture->data, psp_texture->size);
@@ -484,7 +484,7 @@ static int PSP_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL_P
PSP_TextureData *psp_texture = (PSP_TextureData *)SDL_calloc(1, sizeof(*psp_texture));
if (!psp_texture) {
return SDL_OutOfMemory();
return -1;
}
psp_texture->swizzled = SDL_FALSE;
@@ -527,7 +527,7 @@ static int PSP_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL_P
if (!psp_texture->data) {
SDL_free(psp_texture);
return SDL_OutOfMemory();
return -1;
}
texture->driverdata = psp_texture;
@@ -1304,14 +1304,12 @@ SDL_Renderer *PSP_CreateRenderer(SDL_Window *window, SDL_PropertiesID create_pro
renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
data = (PSP_RenderData *)SDL_calloc(1, sizeof(*data));
if (!data) {
PSP_DestroyRenderer(renderer);
SDL_OutOfMemory();
return NULL;
}

View File

@@ -1111,14 +1111,12 @@ SDL_Renderer *SW_CreateRendererForSurface(SDL_Surface *surface)
renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
data = (SW_RenderData *)SDL_calloc(1, sizeof(*data));
if (!data) {
SW_DestroyRenderer(renderer);
SDL_OutOfMemory();
return NULL;
}
data->surface = surface;

View File

@@ -218,14 +218,12 @@ SDL_Renderer *VITA_GXM_CreateRenderer(SDL_Window *window, SDL_PropertiesID creat
renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
data = (VITA_GXM_RenderData *)SDL_calloc(1, sizeof(VITA_GXM_RenderData));
if (!data) {
SDL_free(renderer);
SDL_OutOfMemory();
return NULL;
}
@@ -298,7 +296,7 @@ static int VITA_GXM_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture,
VITA_GXM_TextureData *vita_texture = (VITA_GXM_TextureData *)SDL_calloc(1, sizeof(VITA_GXM_TextureData));
if (!vita_texture) {
return SDL_OutOfMemory();
return -1;
}
vita_texture->tex = create_gxm_texture(
@@ -1111,7 +1109,7 @@ static int VITA_GXM_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *rec
temp_pixels = SDL_malloc(buflen);
if (!temp_pixels) {
return SDL_OutOfMemory();
return -1;
}
SDL_GetCurrentRenderOutputSize(renderer, &w, &h);