Skip to content

Commit c1d4074

Browse files
authored
thirdparty: make fontstash and sokol C-libraries work with Boehm-GC (#9506)
1 parent 89082de commit c1d4074

File tree

4 files changed

+44
-25
lines changed

4 files changed

+44
-25
lines changed

thirdparty/fontstash/fontstash.h

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ extern "C" {
3434

3535
#define FONS_INVALID -1
3636

37+
#if !defined(FONTSTASH_MALLOC)
38+
#define FONTSTASH_MALLOC malloc
39+
#define FONTSTASH_REALLOC realloc
40+
#define FONTSTASH_FREE free
41+
#endif
42+
3743
enum FONSflags {
3844
FONS_ZERO_TOPLEFT = 1,
3945
FONS_ZERO_BOTTOMLEFT = 2,
@@ -540,24 +546,24 @@ static unsigned int fons__decutf8(unsigned int* state, unsigned int* codep, unsi
540546
static void fons__deleteAtlas(FONSatlas* atlas)
541547
{
542548
if (atlas == NULL) return;
543-
if (atlas->nodes != NULL) free(atlas->nodes);
544-
free(atlas);
549+
if (atlas->nodes != NULL) FONTSTASH_FREE(atlas->nodes);
550+
FONTSTASH_FREE(atlas);
545551
}
546552

547553
static FONSatlas* fons__allocAtlas(int w, int h, int nnodes)
548554
{
549555
FONSatlas* atlas = NULL;
550556

551557
// Allocate memory for the font stash.
552-
atlas = (FONSatlas*)malloc(sizeof(FONSatlas));
558+
atlas = (FONSatlas*)FONTSTASH_MALLOC(sizeof(FONSatlas));
553559
if (atlas == NULL) goto error;
554560
memset(atlas, 0, sizeof(FONSatlas));
555561

556562
atlas->width = w;
557563
atlas->height = h;
558564

559565
// Allocate space for skyline nodes
560-
atlas->nodes = (FONSatlasNode*)malloc(sizeof(FONSatlasNode) * nnodes);
566+
atlas->nodes = (FONSatlasNode*)FONTSTASH_MALLOC(sizeof(FONSatlasNode) * nnodes);
561567
if (atlas->nodes == NULL) goto error;
562568
memset(atlas->nodes, 0, sizeof(FONSatlasNode) * nnodes);
563569
atlas->nnodes = 0;
@@ -582,7 +588,7 @@ static int fons__atlasInsertNode(FONSatlas* atlas, int idx, int x, int y, int w)
582588
// Insert node
583589
if (atlas->nnodes+1 > atlas->cnodes) {
584590
atlas->cnodes = atlas->cnodes == 0 ? 8 : atlas->cnodes * 2;
585-
atlas->nodes = (FONSatlasNode*)realloc(atlas->nodes, sizeof(FONSatlasNode) * atlas->cnodes);
591+
atlas->nodes = (FONSatlasNode*)FONTSTASH_REALLOC(atlas->nodes, sizeof(FONSatlasNode) * atlas->cnodes);
586592
if (atlas->nodes == NULL)
587593
return 0;
588594
}
@@ -743,14 +749,14 @@ FONScontext* fonsCreateInternal(FONSparams* params)
743749
FONScontext* stash = NULL;
744750

745751
// Allocate memory for the font stash.
746-
stash = (FONScontext*)malloc(sizeof(FONScontext));
752+
stash = (FONScontext*)FONTSTASH_MALLOC(sizeof(FONScontext));
747753
if (stash == NULL) goto error;
748754
memset(stash, 0, sizeof(FONScontext));
749755

750756
stash->params = *params;
751757

752758
// Allocate scratch buffer.
753-
stash->scratch = (unsigned char*)malloc(FONS_SCRATCH_BUF_SIZE);
759+
stash->scratch = (unsigned char*)FONTSTASH_MALLOC(FONS_SCRATCH_BUF_SIZE);
754760
if (stash->scratch == NULL) goto error;
755761

756762
// Initialize implementation library
@@ -765,7 +771,7 @@ FONScontext* fonsCreateInternal(FONSparams* params)
765771
if (stash->atlas == NULL) goto error;
766772

767773
// Allocate space for fonts.
768-
stash->fonts = (FONSfont**)malloc(sizeof(FONSfont*) * FONS_INIT_FONTS);
774+
stash->fonts = (FONSfont**)FONTSTASH_MALLOC(sizeof(FONSfont*) * FONS_INIT_FONTS);
769775
if (stash->fonts == NULL) goto error;
770776
memset(stash->fonts, 0, sizeof(FONSfont*) * FONS_INIT_FONTS);
771777
stash->cfonts = FONS_INIT_FONTS;
@@ -774,7 +780,7 @@ FONScontext* fonsCreateInternal(FONSparams* params)
774780
// Create texture for the cache.
775781
stash->itw = 1.0f/stash->params.width;
776782
stash->ith = 1.0f/stash->params.height;
777-
stash->texData = (unsigned char*)malloc(stash->params.width * stash->params.height);
783+
stash->texData = (unsigned char*)FONTSTASH_MALLOC(stash->params.width * stash->params.height);
778784
if (stash->texData == NULL) goto error;
779785
memset(stash->texData, 0, stash->params.width * stash->params.height);
780786

@@ -877,25 +883,25 @@ void fonsClearState(FONScontext* stash)
877883
static void fons__freeFont(FONSfont* font)
878884
{
879885
if (font == NULL) return;
880-
if (font->glyphs) free(font->glyphs);
881-
if (font->freeData && font->data) free(font->data);
882-
free(font);
886+
if (font->glyphs) FONTSTASH_FREE(font->glyphs);
887+
if (font->freeData && font->data) FONTSTASH_FREE(font->data);
888+
FONTSTASH_FREE(font);
883889
}
884890

885891
static int fons__allocFont(FONScontext* stash)
886892
{
887893
FONSfont* font = NULL;
888894
if (stash->nfonts+1 > stash->cfonts) {
889895
stash->cfonts = stash->cfonts == 0 ? 8 : stash->cfonts * 2;
890-
stash->fonts = (FONSfont**)realloc(stash->fonts, sizeof(FONSfont*) * stash->cfonts);
896+
stash->fonts = (FONSfont**)FONTSTASH_REALLOC(stash->fonts, sizeof(FONSfont*) * stash->cfonts);
891897
if (stash->fonts == NULL)
892898
return -1;
893899
}
894-
font = (FONSfont*)malloc(sizeof(FONSfont));
900+
font = (FONSfont*)FONTSTASH_MALLOC(sizeof(FONSfont));
895901
if (font == NULL) goto error;
896902
memset(font, 0, sizeof(FONSfont));
897903

898-
font->glyphs = (FONSglyph*)malloc(sizeof(FONSglyph) * FONS_INIT_GLYPHS);
904+
font->glyphs = (FONSglyph*)FONTSTASH_MALLOC(sizeof(FONSglyph) * FONS_INIT_GLYPHS);
899905
if (font->glyphs == NULL) goto error;
900906
font->cglyphs = FONS_INIT_GLYPHS;
901907
font->nglyphs = 0;
@@ -967,7 +973,7 @@ static FONSglyph* fons__allocGlyph(FONSfont* font)
967973
{
968974
if (font->nglyphs+1 > font->cglyphs) {
969975
font->cglyphs = font->cglyphs == 0 ? 8 : font->cglyphs * 2;
970-
font->glyphs = (FONSglyph*)realloc(font->glyphs, sizeof(FONSglyph) * font->cglyphs);
976+
font->glyphs = (FONSglyph*)FONTSTASH_REALLOC(font->glyphs, sizeof(FONSglyph) * font->cglyphs);
971977
if (font->glyphs == NULL) return NULL;
972978
}
973979
font->nglyphs++;
@@ -1610,10 +1616,10 @@ FONS_DEF void fonsDeleteInternal(FONScontext* stash)
16101616
fons__freeFont(stash->fonts[i]);
16111617

16121618
if (stash->atlas) fons__deleteAtlas(stash->atlas);
1613-
if (stash->fonts) free(stash->fonts);
1614-
if (stash->texData) free(stash->texData);
1615-
if (stash->scratch) free(stash->scratch);
1616-
free(stash);
1619+
if (stash->fonts) FONTSTASH_FREE(stash->fonts);
1620+
if (stash->texData) FONTSTASH_FREE(stash->texData);
1621+
if (stash->scratch) FONTSTASH_FREE(stash->scratch);
1622+
FONTSTASH_FREE(stash);
16171623
}
16181624

16191625
FONS_DEF void fonsSetErrorCallback(FONScontext* stash, void (*callback)(void* uptr, int error, int val), void* uptr)
@@ -1651,7 +1657,7 @@ FONS_DEF int fonsExpandAtlas(FONScontext* stash, int width, int height)
16511657
return 0;
16521658
}
16531659
// Copy old texture data over.
1654-
data = (unsigned char*)malloc(width * height);
1660+
data = (unsigned char*)FONTSTASH_MALLOC(width * height);
16551661
if (data == NULL)
16561662
return 0;
16571663
for (i = 0; i < stash->params.height; i++) {
@@ -1664,7 +1670,7 @@ FONS_DEF int fonsExpandAtlas(FONScontext* stash, int width, int height)
16641670
if (height > stash->params.height)
16651671
memset(&data[stash->params.height * width], 0, (height - stash->params.height) * width);
16661672

1667-
free(stash->texData);
1673+
FONTSTASH_FREE(stash->texData);
16681674
stash->texData = data;
16691675

16701676
// Increase atlas size
@@ -1704,7 +1710,7 @@ FONS_DEF int fonsResetAtlas(FONScontext* stash, int width, int height)
17041710
fons__atlasReset(stash->atlas, width, height);
17051711

17061712
// Clear texture data.
1707-
stash->texData = (unsigned char*)realloc(stash->texData, width * height);
1713+
stash->texData = (unsigned char*)FONTSTASH_REALLOC(stash->texData, width * height);
17081714
if (stash->texData == NULL) return 0;
17091715
memset(stash->texData, 0, width * height);
17101716

thirdparty/fontstash/stb_truetype.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,8 @@ int main(int arg, char **argv)
464464
// #define your own functions "STBTT_malloc" / "STBTT_free" to avoid malloc.h
465465
#ifndef STBTT_malloc
466466
#include <stdlib.h>
467-
#define STBTT_malloc(x,u) ((void)(u),malloc(x))
468-
#define STBTT_free(x,u) ((void)(u),free(x))
467+
#define STBTT_malloc(x,u) ((void)(u),FONTSTASH_MALLOC(x))
468+
#define STBTT_free(x,u) ((void)(u),FONTSTASH_FREE(x))
469469
#endif
470470

471471
#ifndef STBTT_assert

vlib/fontstash/fontstash.v

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ module fontstash
22

33
#flag -I @VROOT/thirdparty/fontstash
44
#define FONTSTASH_IMPLEMENTATION
5+
$if gcboehm ? {
6+
#define FONTSTASH_MALLOC GC_MALLOC
7+
#define FONTSTASH_REALLOC GC_REALLOC
8+
#define FONTSTASH_FREE GC_FREE
9+
}
510
#include "fontstash.h"
611
#flag -I /usr/local/Cellar/freetype/2.10.2/include/freetype2
712
//#flag -lfreetype

vlib/sokol/c/declaration.c.v

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ pub const (
3333
#flag freebsd -DSOKOL_NO_ENTRY
3434
#flag solaris -DSOKOL_NO_ENTRY
3535
// TODO end
36+
37+
$if gcboehm ? {
38+
#define SOKOL_MALLOC GC_MALLOC
39+
#define SOKOL_CALLOC(n,m) GC_MALLOC((n)*(m))
40+
#define SOKOL_REALLOC GC_REALLOC
41+
#define SOKOL_FREE GC_FREE
42+
}
43+
3644
#include "sokol_v.h"
3745
#include "sokol_app.h"
3846
#define SOKOL_IMPL

0 commit comments

Comments
 (0)