Commit 2fe56161 authored by Sam Lantinga's avatar Sam Lantinga

Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating...

Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
OpenGL renderer in progress

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%401965
parent 2b538200
...@@ -191,7 +191,7 @@ typedef struct SDL_RendererInfo ...@@ -191,7 +191,7 @@ typedef struct SDL_RendererInfo
Uint32 blend_modes; /**< A mask of supported blend modes */ Uint32 blend_modes; /**< A mask of supported blend modes */
Uint32 scale_modes; /**< A mask of supported scale modes */ Uint32 scale_modes; /**< A mask of supported scale modes */
Uint32 num_texture_formats; /**< The number of available texture formats */ Uint32 num_texture_formats; /**< The number of available texture formats */
Uint32 texture_formats[16]; /**< The available texture formats */ Uint32 texture_formats[20]; /**< The available texture formats */
int max_texture_width; /**< The maximimum texture width */ int max_texture_width; /**< The maximimum texture width */
int max_texture_height; /**< The maximimum texture height */ int max_texture_height; /**< The maximimum texture height */
} SDL_RendererInfo; } SDL_RendererInfo;
......
...@@ -346,12 +346,11 @@ SDL_AllocFormat(int bpp, ...@@ -346,12 +346,11 @@ SDL_AllocFormat(int bpp,
Uint32 mask; Uint32 mask;
/* Allocate an empty pixel format structure */ /* Allocate an empty pixel format structure */
format = SDL_malloc(sizeof(*format)); format = SDL_calloc(1, sizeof(*format));
if (format == NULL) { if (format == NULL) {
SDL_OutOfMemory(); SDL_OutOfMemory();
return (NULL); return (NULL);
} }
SDL_memset(format, 0, sizeof(*format));
format->alpha = SDL_ALPHA_OPAQUE; format->alpha = SDL_ALPHA_OPAQUE;
/* Set up the format */ /* Set up the format */
...@@ -714,22 +713,20 @@ SDL_AllocBlitMap(void) ...@@ -714,22 +713,20 @@ SDL_AllocBlitMap(void)
SDL_BlitMap *map; SDL_BlitMap *map;
/* Allocate the empty map */ /* Allocate the empty map */
map = (SDL_BlitMap *) SDL_malloc(sizeof(*map)); map = (SDL_BlitMap *) SDL_calloc(1, sizeof(*map));
if (map == NULL) { if (map == NULL) {
SDL_OutOfMemory(); SDL_OutOfMemory();
return (NULL); return (NULL);
} }
SDL_memset(map, 0, sizeof(*map));
/* Allocate the software blit data */ /* Allocate the software blit data */
map->sw_data = map->sw_data =
(struct private_swaccel *) SDL_malloc(sizeof(*map->sw_data)); (struct private_swaccel *) SDL_calloc(1, sizeof(*map->sw_data));
if (map->sw_data == NULL) { if (map->sw_data == NULL) {
SDL_FreeBlitMap(map); SDL_FreeBlitMap(map);
SDL_OutOfMemory(); SDL_OutOfMemory();
return (NULL); return (NULL);
} }
SDL_memset(map->sw_data, 0, sizeof(*map->sw_data));
/* It's ready to go */ /* It's ready to go */
return (map); return (map);
......
This diff is collapsed.
...@@ -105,13 +105,12 @@ CreateTexture(SDL_Renderer * renderer, Uint32 format, int w, int h) ...@@ -105,13 +105,12 @@ CreateTexture(SDL_Renderer * renderer, Uint32 format, int w, int h)
{ {
SDL_Texture *texture; SDL_Texture *texture;
texture = (SDL_Texture *) SDL_malloc(sizeof(*texture)); texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
if (!texture) { if (!texture) {
SDL_OutOfMemory(); SDL_OutOfMemory();
return NULL; return NULL;
} }
SDL_zerop(texture);
texture->format = format; texture->format = format;
texture->access = SDL_TextureAccess_Local; texture->access = SDL_TextureAccess_Local;
texture->w = w; texture->w = w;
...@@ -173,13 +172,12 @@ SW_CreateRenderer(SDL_Window * window, Uint32 flags) ...@@ -173,13 +172,12 @@ SW_CreateRenderer(SDL_Window * window, Uint32 flags)
return NULL; return NULL;
} }
data = (SW_RenderData *) SDL_malloc(sizeof(*data)); data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
if (!data) { if (!data) {
SW_DestroyRenderer(renderer); SW_DestroyRenderer(renderer);
SDL_OutOfMemory(); SDL_OutOfMemory();
return NULL; return NULL;
} }
SDL_zerop(data);
renderer->CreateTexture = SW_CreateTexture; renderer->CreateTexture = SW_CreateTexture;
renderer->QueryTexturePixels = SW_QueryTexturePixels; renderer->QueryTexturePixels = SW_QueryTexturePixels;
......
...@@ -42,12 +42,11 @@ SDL_CreateRGBSurface(Uint32 flags, ...@@ -42,12 +42,11 @@ SDL_CreateRGBSurface(Uint32 flags,
SDL_Surface *surface; SDL_Surface *surface;
/* Allocate the surface */ /* Allocate the surface */
surface = (SDL_Surface *) SDL_malloc(sizeof(*surface)); surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
if (surface == NULL) { if (surface == NULL) {
SDL_OutOfMemory(); SDL_OutOfMemory();
return NULL; return NULL;
} }
SDL_zerop(surface);
surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask); surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask);
if (!surface->format) { if (!surface->format) {
......
...@@ -279,10 +279,10 @@ SDL_VideoInit(const char *driver_name, Uint32 flags) ...@@ -279,10 +279,10 @@ SDL_VideoInit(const char *driver_name, Uint32 flags)
/* The software renderer is always available */ /* The software renderer is always available */
for (i = 0; i < _this->num_displays; ++i) { for (i = 0; i < _this->num_displays; ++i) {
if (_this->displays[i].num_render_drivers > 0) { #if SDL_VIDEO_OPENGL
#if 0 //SDL_VIDEO_OPENGL SDL_AddRenderDriver(i, &GL_RenderDriver);
SDL_AddRenderDriver(i, &GL_RenderDriver);
#endif #endif
if (_this->displays[i].num_render_drivers > 0) {
SDL_AddRenderDriver(i, &SW_RenderDriver); SDL_AddRenderDriver(i, &SW_RenderDriver);
} }
} }
...@@ -1403,13 +1403,12 @@ SDL_CreateTexture(Uint32 format, int access, int w, int h) ...@@ -1403,13 +1403,12 @@ SDL_CreateTexture(Uint32 format, int access, int w, int h)
return 0; return 0;
} }
texture = (SDL_Texture *) SDL_malloc(sizeof(*texture)); texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
if (!texture) { if (!texture) {
SDL_OutOfMemory(); SDL_OutOfMemory();
return 0; return 0;
} }
SDL_zerop(texture);
texture->id = _this->next_object_id++; texture->id = _this->next_object_id++;
texture->format = format; texture->format = format;
texture->access = access; texture->access = access;
......
...@@ -1054,12 +1054,11 @@ SDL_SW_CreateYUVTexture(SDL_Texture * texture) ...@@ -1054,12 +1054,11 @@ SDL_SW_CreateYUVTexture(SDL_Texture * texture)
int i; int i;
int CR, CB; int CR, CB;
swdata = (SDL_SW_YUVTexture *) SDL_malloc(sizeof(*swdata)); swdata = (SDL_SW_YUVTexture *) SDL_calloc(1, sizeof(*swdata));
if (!swdata) { if (!swdata) {
SDL_OutOfMemory(); SDL_OutOfMemory();
return NULL; return NULL;
} }
SDL_zerop(swdata);
switch (texture->format) { switch (texture->format) {
case SDL_PixelFormat_YV12: case SDL_PixelFormat_YV12:
......
...@@ -239,20 +239,18 @@ D3D_CreateRenderer(SDL_Window * window, Uint32 flags) ...@@ -239,20 +239,18 @@ D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
D3DPRESENT_PARAMETERS pparams; D3DPRESENT_PARAMETERS pparams;
IDirect3DSwapChain9 *chain; IDirect3DSwapChain9 *chain;
renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer)); renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
if (!renderer) { if (!renderer) {
SDL_OutOfMemory(); SDL_OutOfMemory();
return NULL; return NULL;
} }
SDL_zerop(renderer);
data = (D3D_RenderData *) SDL_malloc(sizeof(*data)); data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
if (!data) { if (!data) {
D3D_DestroyRenderer(renderer); D3D_DestroyRenderer(renderer);
SDL_OutOfMemory(); SDL_OutOfMemory();
return NULL; return NULL;
} }
SDL_zerop(data);
renderer->CreateTexture = D3D_CreateTexture; renderer->CreateTexture = D3D_CreateTexture;
renderer->SetTexturePalette = D3D_SetTexturePalette; renderer->SetTexturePalette = D3D_SetTexturePalette;
...@@ -379,12 +377,11 @@ D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture) ...@@ -379,12 +377,11 @@ D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
D3DPOOL pool; D3DPOOL pool;
HRESULT result; HRESULT result;
data = (D3D_TextureData *) SDL_malloc(sizeof(*data)); data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
if (!data) { if (!data) {
SDL_OutOfMemory(); SDL_OutOfMemory();
return -1; return -1;
} }
SDL_zerop(data);
texture->driverdata = data; texture->driverdata = data;
......
...@@ -142,20 +142,18 @@ GDI_CreateRenderer(SDL_Window * window, Uint32 flags) ...@@ -142,20 +142,18 @@ GDI_CreateRenderer(SDL_Window * window, Uint32 flags)
HBITMAP hbm; HBITMAP hbm;
int i, n; int i, n;
renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer)); renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
if (!renderer) { if (!renderer) {
SDL_OutOfMemory(); SDL_OutOfMemory();
return NULL; return NULL;
} }
SDL_zerop(renderer);
data = (GDI_RenderData *) SDL_malloc(sizeof(*data)); data = (GDI_RenderData *) SDL_calloc(1, sizeof(*data));
if (!data) { if (!data) {
GDI_DestroyRenderer(renderer); GDI_DestroyRenderer(renderer);
SDL_OutOfMemory(); SDL_OutOfMemory();
return NULL; return NULL;
} }
SDL_zerop(data);
renderer->CreateTexture = GDI_CreateTexture; renderer->CreateTexture = GDI_CreateTexture;
renderer->QueryTexturePixels = GDI_QueryTexturePixels; renderer->QueryTexturePixels = GDI_QueryTexturePixels;
...@@ -183,13 +181,12 @@ GDI_CreateRenderer(SDL_Window * window, Uint32 flags) ...@@ -183,13 +181,12 @@ GDI_CreateRenderer(SDL_Window * window, Uint32 flags)
/* Fill in the compatible bitmap info */ /* Fill in the compatible bitmap info */
bmi_size = sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD); bmi_size = sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);
data->bmi = (LPBITMAPINFO) SDL_malloc(bmi_size); data->bmi = (LPBITMAPINFO) SDL_calloc(1, bmi_size);
if (!data->bmi) { if (!data->bmi) {
GDI_DestroyRenderer(renderer); GDI_DestroyRenderer(renderer);
SDL_OutOfMemory(); SDL_OutOfMemory();
return NULL; return NULL;
} }
SDL_memset(data->bmi, 0, bmi_size);
data->bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); data->bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
hbm = CreateCompatibleBitmap(data->window_hdc, 1, 1); hbm = CreateCompatibleBitmap(data->window_hdc, 1, 1);
...@@ -241,12 +238,11 @@ GDI_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture) ...@@ -241,12 +238,11 @@ GDI_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window); SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
GDI_TextureData *data; GDI_TextureData *data;
data = (GDI_TextureData *) SDL_malloc(sizeof(*data)); data = (GDI_TextureData *) SDL_calloc(1, sizeof(*data));
if (!data) { if (!data) {
SDL_OutOfMemory(); SDL_OutOfMemory();
return -1; return -1;
} }
SDL_zerop(data);
texture->driverdata = data; texture->driverdata = data;
...@@ -268,13 +264,12 @@ GDI_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture) ...@@ -268,13 +264,12 @@ GDI_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
LPBITMAPINFO bmi; LPBITMAPINFO bmi;
bmi_size = sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD); bmi_size = sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);
bmi = (LPBITMAPINFO) SDL_malloc(bmi_size); bmi = (LPBITMAPINFO) SDL_calloc(1, bmi_size);
if (!bmi) { if (!bmi) {
GDI_DestroyTexture(renderer, texture); GDI_DestroyTexture(renderer, texture);
SDL_OutOfMemory(); SDL_OutOfMemory();
return -1; return -1;
} }
SDL_memset(bmi, 0, bmi_size);
bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi->bmiHeader.biWidth = texture->w; bmi->bmiHeader.biWidth = texture->w;
bmi->bmiHeader.biHeight = -texture->h; /* topdown bitmap */ bmi->bmiHeader.biHeight = -texture->h; /* topdown bitmap */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment