Cleanup: blenfont & blentranslation: Replace 'void' MEM_[cm]allocN with templated, type-safe MEM_[cm]allocN<T>.

The main issue of 'type-less' standard C allocations is that there is no check on
allocated type possible.

This is a serious source of annoyance (and crashes) when making some
low-level structs non-trivial, as tracking down all usages of these
structs in higher-level other structs and their allocation is... really
painful.

MEM_[cm]allocN<T> templates on the other hand do check that the
given type is trivial, at build time (static assert), which makes such issue...
trivial to catch.

NOTE: New code should strive to use MEM_new (i.e. allocation and
construction) as much as possible, even for trivial PoD types.

Pull Request: https://projects.blender.org/blender/blender/pulls/136126
This commit is contained in:
Bastien Montagne
2025-03-18 18:25:50 +01:00
committed by Bastien Montagne
parent 4894c888ee
commit 7bf17d83ab
3 changed files with 8 additions and 12 deletions

View File

@@ -1839,8 +1839,7 @@ static bool blf_setup_face(FontBLF *font)
if (FT_HAS_KERNING(font) && !font->kerning_cache) {
/* Create kerning cache table and fill with value indicating "unset". */
font->kerning_cache = static_cast<KerningCacheBLF *>(
MEM_mallocN(sizeof(KerningCacheBLF), __func__));
font->kerning_cache = MEM_mallocN<KerningCacheBLF>(__func__);
for (uint i = 0; i < KERNING_CACHE_TABLE_SIZE; i++) {
for (uint j = 0; j < KERNING_CACHE_TABLE_SIZE; j++) {
font->kerning_cache->ascii_table[i][j] = KERNING_ENTRY_UNSET;

View File

@@ -267,7 +267,7 @@ static GlyphBLF *blf_glyph_cache_add_glyph(
}
const int buffer_size = g->dims[0] * g->dims[1] * g->num_channels;
g->bitmap = static_cast<uchar *>(MEM_mallocN(size_t(buffer_size), "glyph bitmap"));
g->bitmap = MEM_malloc_arrayN<uchar>(size_t(buffer_size), "glyph bitmap");
if (ELEM(glyph->bitmap.pixel_mode,
FT_PIXEL_MODE_GRAY,
@@ -416,7 +416,7 @@ static GlyphBLF *blf_glyph_cache_add_svg(
g->num_channels = color ? 4 : 1;
const int buffer_size = g->dims[0] * g->dims[1] * g->num_channels;
g->bitmap = static_cast<uchar *>(MEM_mallocN(size_t(buffer_size), "glyph bitmap"));
g->bitmap = MEM_malloc_arrayN<uchar>(size_t(buffer_size), "glyph bitmap");
if (color) {
memcpy(g->bitmap, render_bmp.data(), size_t(buffer_size));
@@ -1678,8 +1678,7 @@ static void blf_glyph_to_curves(const FT_Outline &ftoutline,
int contour_prev;
/* Start converting the FT data */
int *onpoints = static_cast<int *>(
MEM_callocN(size_t(ftoutline.n_contours) * sizeof(int), "onpoints"));
int *onpoints = MEM_calloc_arrayN<int>(size_t(ftoutline.n_contours), "onpoints");
/* Get number of on-curve points for bezier-triples (including conic virtual on-points). */
for (j = 0, contour_prev = -1; j < ftoutline.n_contours; j++) {
@@ -1713,9 +1712,8 @@ static void blf_glyph_to_curves(const FT_Outline &ftoutline,
contour_prev = ftoutline.contours[j];
/* add new curve */
nu = (Nurb *)MEM_callocN(sizeof(Nurb), "objfnt_nurb");
bezt = static_cast<BezTriple *>(
MEM_callocN(size_t(onpoints[j]) * sizeof(BezTriple), "objfnt_bezt"));
nu = MEM_callocN<Nurb>("objfnt_nurb");
bezt = MEM_calloc_arrayN<BezTriple>(size_t(onpoints[j]), "objfnt_bezt");
BLI_addtail(nurbsbase, nu);
nu->type = CU_BEZIER;

View File

@@ -103,13 +103,12 @@ static void fill_locales()
num_locales_menu++; /* The "closing" void item... */
/* And now, build locales and locale_menu! */
locales_menu = static_cast<EnumPropertyItem *>(
MEM_callocN(num_locales_menu * sizeof(EnumPropertyItem), __func__));
locales_menu = MEM_calloc_arrayN<EnumPropertyItem>(size_t(num_locales_menu), __func__);
line = lines;
/* Do not allocate locales with zero-sized mem,
* as LOCALE macro uses nullptr locales as invalid marker! */
if (num_locales > 0) {
locales = static_cast<const char **>(MEM_callocN(num_locales * sizeof(char *), __func__));
locales = MEM_calloc_arrayN<const char *>(size_t(num_locales), __func__);
while (line) {
const char *loc, *sep1, *sep2, *sep3;