From 029b0b937a098f1a202e0c3dfc626458b66dcb43 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Tue, 7 Jul 2020 18:02:34 -0700 Subject: [PATCH] All: Use calloc instead of malloc in several places --- include/mgba-util/vector.h | 2 +- src/core/bitmap-cache.c | 2 +- src/core/input.c | 6 +++--- src/core/tile-cache.c | 4 ++-- src/debugger/cli-debugger.c | 2 +- src/platform/3ds/gui-font.c | 2 +- src/platform/opengl/gles2.c | 4 ++-- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/include/mgba-util/vector.h b/include/mgba-util/vector.h index c18b6c8b5..c73644cfa 100644 --- a/include/mgba-util/vector.h +++ b/include/mgba-util/vector.h @@ -41,7 +41,7 @@ CXX_GUARD_START capacity = 4; \ } \ vector->capacity = capacity; \ - vector->vector = malloc(sizeof(TYPE) * capacity); \ + vector->vector = calloc(capacity, sizeof(TYPE)); \ } \ void NAME ## Deinit(struct NAME* vector) { \ free(vector->vector); \ diff --git a/src/core/bitmap-cache.c b/src/core/bitmap-cache.c index cdbc90a0e..81aa84ffe 100644 --- a/src/core/bitmap-cache.c +++ b/src/core/bitmap-cache.c @@ -42,7 +42,7 @@ static void _redoCacheSize(struct mBitmapCache* cache) { cache->cache = anonymousMemoryMap(mBitmapCacheSystemInfoGetWidth(cache->sysConfig) * size * sizeof(color_t)); cache->status = anonymousMemoryMap(size * sizeof(*cache->status)); if (mBitmapCacheSystemInfoIsUsesPalette(cache->sysConfig)) { - cache->palette = malloc((1 << (1 << mBitmapCacheSystemInfoGetEntryBPP(cache->sysConfig))) * sizeof(color_t)); + cache->palette = calloc((1 << (1 << mBitmapCacheSystemInfoGetEntryBPP(cache->sysConfig))), sizeof(color_t)); } else { cache->palette = NULL; } diff --git a/src/core/input.c b/src/core/input.c index fe24d0822..307fed216 100644 --- a/src/core/input.c +++ b/src/core/input.c @@ -88,7 +88,7 @@ static struct mInputMapImpl* _guaranteeMap(struct mInputMap* map, uint32_t type) map->numMaps = 1; impl = &map->maps[0]; impl->type = type; - impl->map = malloc(map->info->nKeys * sizeof(int)); + impl->map = calloc(map->info->nKeys, sizeof(int)); size_t i; for (i = 0; i < map->info->nKeys; ++i) { impl->map[i] = -1; @@ -108,7 +108,7 @@ static struct mInputMapImpl* _guaranteeMap(struct mInputMap* map, uint32_t type) } if (impl) { impl->type = type; - impl->map = malloc(map->info->nKeys * sizeof(int)); + impl->map = calloc(map->info->nKeys, sizeof(int)); size_t i; for (i = 0; i < map->info->nKeys; ++i) { impl->map[i] = -1; @@ -122,7 +122,7 @@ static struct mInputMapImpl* _guaranteeMap(struct mInputMap* map, uint32_t type) map->numMaps *= 2; impl = &map->maps[m]; impl->type = type; - impl->map = malloc(map->info->nKeys * sizeof(int)); + impl->map = calloc(map->info->nKeys, sizeof(int)); size_t i; for (i = 0; i < map->info->nKeys; ++i) { impl->map[i] = -1; diff --git a/src/core/tile-cache.c b/src/core/tile-cache.c index 89b4a52cb..249cd0116 100644 --- a/src/core/tile-cache.c +++ b/src/core/tile-cache.c @@ -46,8 +46,8 @@ static void _redoCacheSize(struct mTileCache* cache) { unsigned tiles = mTileCacheSystemInfoGetMaxTiles(cache->sysConfig); cache->cache = anonymousMemoryMap(8 * 8 * sizeof(color_t) * tiles * size); cache->status = anonymousMemoryMap(tiles * size * sizeof(*cache->status)); - cache->globalPaletteVersion = malloc(size * sizeof(*cache->globalPaletteVersion)); - cache->palette = malloc(size * bpp * sizeof(*cache->palette)); + cache->globalPaletteVersion = calloc(size, sizeof(*cache->globalPaletteVersion)); + cache->palette = calloc(size * bpp, sizeof(*cache->palette)); } void mTileCacheConfigure(struct mTileCache* cache, mTileCacheConfiguration config) { diff --git a/src/debugger/cli-debugger.c b/src/debugger/cli-debugger.c index ad125e04c..ddac46952 100644 --- a/src/debugger/cli-debugger.c +++ b/src/debugger/cli-debugger.c @@ -174,7 +174,7 @@ static bool _parseExpression(struct mDebugger* debugger, struct CLIDebugVector* for (accum = dv; accum; accum = accum->next) { ++args; } - const char** arglist = malloc(sizeof(const char*) * (args + 1)); + const char** arglist = calloc(args + 1, sizeof(const char*)); args = 0; for (accum = dv; accum; accum = accum->next) { arglist[args] = accum->charValue; diff --git a/src/platform/3ds/gui-font.c b/src/platform/3ds/gui-font.c index 761cd7c55..4d4a4f01e 100644 --- a/src/platform/3ds/gui-font.c +++ b/src/platform/3ds/gui-font.c @@ -31,7 +31,7 @@ struct GUIFont* GUIFontCreate(void) { guiFont->font = fontGetSystemFont(); TGLP_s* glyphInfo = fontGetGlyphInfo(guiFont->font); guiFont->size = FONT_SIZE / glyphInfo->cellHeight; - guiFont->sheets = malloc(sizeof(*guiFont->sheets) * glyphInfo->nSheets); + guiFont->sheets = calloc(glyphInfo->nSheets, sizeof(*guiFont->sheets)); int i; for (i = 0; i < glyphInfo->nSheets; ++i) { diff --git a/src/platform/opengl/gles2.c b/src/platform/opengl/gles2.c index fbb0fa601..a8052cab1 100644 --- a/src/platform/opengl/gles2.c +++ b/src/platform/opengl/gles2.c @@ -921,7 +921,7 @@ bool mGLES2ShaderLoad(struct VideoShader* shader, struct VDir* dir) { success = false; } if (success) { - struct mGLES2Shader* shaderBlock = malloc(sizeof(struct mGLES2Shader) * inShaders); + struct mGLES2Shader* shaderBlock = calloc(inShaders, sizeof(struct mGLES2Shader)); int n; for (n = 0; n < inShaders; ++n) { char passName[12]; @@ -980,7 +980,7 @@ bool mGLES2ShaderLoad(struct VideoShader* shader, struct VDir* dir) { } } u = mGLES2UniformListSize(&uniformVector); - struct mGLES2Uniform* uniformBlock = malloc(sizeof(*uniformBlock) * u); + struct mGLES2Uniform* uniformBlock = calloc(u, sizeof(*uniformBlock)); memcpy(uniformBlock, mGLES2UniformListGetPointer(&uniformVector, 0), sizeof(*uniformBlock) * u); mGLES2UniformListDeinit(&uniformVector);