fontstash: define and use FONTSTASH_MALLOC_ATOMIC for the allocated texture and atlas nodes (which do not contain pointers)

This commit is contained in:
Delyan Angelov 2025-09-07 14:05:40 +03:00
parent 6e0fd17a72
commit e89839c845
No known key found for this signature in database
GPG key ID: 66886C0F12D595ED
2 changed files with 8 additions and 4 deletions

View file

@ -38,6 +38,9 @@ extern "C" {
#define FONTSTASH_MALLOC malloc #define FONTSTASH_MALLOC malloc
#define FONTSTASH_REALLOC realloc #define FONTSTASH_REALLOC realloc
#define FONTSTASH_FREE free #define FONTSTASH_FREE free
// __v_ start
#define FONTSTASH_MALLOC_ATOMIC malloc
// __v_ end
#endif #endif
enum FONSflags { enum FONSflags {
@ -563,7 +566,7 @@ static FONSatlas* fons__allocAtlas(int w, int h, int nnodes)
atlas->height = h; atlas->height = h;
// Allocate space for skyline nodes // Allocate space for skyline nodes
atlas->nodes = (FONSatlasNode*)FONTSTASH_MALLOC(sizeof(FONSatlasNode) * nnodes); atlas->nodes = (FONSatlasNode*)FONTSTASH_MALLOC_ATOMIC(sizeof(FONSatlasNode) * nnodes);
if (atlas->nodes == NULL) goto error; if (atlas->nodes == NULL) goto error;
memset(atlas->nodes, 0, sizeof(FONSatlasNode) * nnodes); memset(atlas->nodes, 0, sizeof(FONSatlasNode) * nnodes);
atlas->nnodes = 0; atlas->nnodes = 0;
@ -780,7 +783,7 @@ FONScontext* fonsCreateInternal(FONSparams* params)
// Create texture for the cache. // Create texture for the cache.
stash->itw = 1.0f/stash->params.width; stash->itw = 1.0f/stash->params.width;
stash->ith = 1.0f/stash->params.height; stash->ith = 1.0f/stash->params.height;
stash->texData = (unsigned char*)FONTSTASH_MALLOC(stash->params.width * stash->params.height); stash->texData = (unsigned char*)FONTSTASH_MALLOC_ATOMIC(stash->params.width * stash->params.height);
if (stash->texData == NULL) goto error; if (stash->texData == NULL) goto error;
memset(stash->texData, 0, stash->params.width * stash->params.height); memset(stash->texData, 0, stash->params.width * stash->params.height);
@ -901,7 +904,7 @@ static int fons__allocFont(FONScontext* stash)
if (font == NULL) goto error; if (font == NULL) goto error;
memset(font, 0, sizeof(FONSfont)); memset(font, 0, sizeof(FONSfont));
font->glyphs = (FONSglyph*)FONTSTASH_MALLOC(sizeof(FONSglyph) * FONS_INIT_GLYPHS); font->glyphs = (FONSglyph*)FONTSTASH_MALLOC_ATOMIC(sizeof(FONSglyph) * FONS_INIT_GLYPHS);
if (font->glyphs == NULL) goto error; if (font->glyphs == NULL) goto error;
font->cglyphs = FONS_INIT_GLYPHS; font->cglyphs = FONS_INIT_GLYPHS;
font->nglyphs = 0; font->nglyphs = 0;
@ -1657,7 +1660,7 @@ FONS_DEF int fonsExpandAtlas(FONScontext* stash, int width, int height)
return 0; return 0;
} }
// Copy old texture data over. // Copy old texture data over.
data = (unsigned char*)FONTSTASH_MALLOC(width * height); data = (unsigned char*)FONTSTASH_MALLOC_ATOMIC(width * height);
if (data == NULL) if (data == NULL)
return 0; return 0;
for (i = 0; i < stash->params.height; i++) { for (i = 0; i < stash->params.height; i++) {

View file

@ -8,6 +8,7 @@ $if gcboehm ? {
#define FONTSTASH_MALLOC GC_MALLOC #define FONTSTASH_MALLOC GC_MALLOC
#define FONTSTASH_REALLOC GC_REALLOC #define FONTSTASH_REALLOC GC_REALLOC
#define FONTSTASH_FREE GC_FREE #define FONTSTASH_FREE GC_FREE
#define FONTSTASH_MALLOC_ATOMIC GC_MALLOC_ATOMIC
} }
#include "fontstash.h" #include "fontstash.h"
#flag darwin -I/usr/local/Cellar/freetype/2.10.2/include/freetype2 #flag darwin -I/usr/local/Cellar/freetype/2.10.2/include/freetype2