1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

thirdparty: make fontstash and sokol C-libraries work with Boehm-GC (#9506)

This commit is contained in:
Uwe Krüger 2021-03-29 11:27:34 +02:00 committed by GitHub
parent 89082de5d1
commit c1d4074bc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 25 deletions

View File

@ -34,6 +34,12 @@ extern "C" {
#define FONS_INVALID -1 #define FONS_INVALID -1
#if !defined(FONTSTASH_MALLOC)
#define FONTSTASH_MALLOC malloc
#define FONTSTASH_REALLOC realloc
#define FONTSTASH_FREE free
#endif
enum FONSflags { enum FONSflags {
FONS_ZERO_TOPLEFT = 1, FONS_ZERO_TOPLEFT = 1,
FONS_ZERO_BOTTOMLEFT = 2, FONS_ZERO_BOTTOMLEFT = 2,
@ -540,8 +546,8 @@ static unsigned int fons__decutf8(unsigned int* state, unsigned int* codep, unsi
static void fons__deleteAtlas(FONSatlas* atlas) static void fons__deleteAtlas(FONSatlas* atlas)
{ {
if (atlas == NULL) return; if (atlas == NULL) return;
if (atlas->nodes != NULL) free(atlas->nodes); if (atlas->nodes != NULL) FONTSTASH_FREE(atlas->nodes);
free(atlas); FONTSTASH_FREE(atlas);
} }
static FONSatlas* fons__allocAtlas(int w, int h, int nnodes) static FONSatlas* fons__allocAtlas(int w, int h, int nnodes)
@ -549,7 +555,7 @@ static FONSatlas* fons__allocAtlas(int w, int h, int nnodes)
FONSatlas* atlas = NULL; FONSatlas* atlas = NULL;
// Allocate memory for the font stash. // Allocate memory for the font stash.
atlas = (FONSatlas*)malloc(sizeof(FONSatlas)); atlas = (FONSatlas*)FONTSTASH_MALLOC(sizeof(FONSatlas));
if (atlas == NULL) goto error; if (atlas == NULL) goto error;
memset(atlas, 0, sizeof(FONSatlas)); memset(atlas, 0, sizeof(FONSatlas));
@ -557,7 +563,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*)malloc(sizeof(FONSatlasNode) * nnodes); atlas->nodes = (FONSatlasNode*)FONTSTASH_MALLOC(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;
@ -582,7 +588,7 @@ static int fons__atlasInsertNode(FONSatlas* atlas, int idx, int x, int y, int w)
// Insert node // Insert node
if (atlas->nnodes+1 > atlas->cnodes) { if (atlas->nnodes+1 > atlas->cnodes) {
atlas->cnodes = atlas->cnodes == 0 ? 8 : atlas->cnodes * 2; atlas->cnodes = atlas->cnodes == 0 ? 8 : atlas->cnodes * 2;
atlas->nodes = (FONSatlasNode*)realloc(atlas->nodes, sizeof(FONSatlasNode) * atlas->cnodes); atlas->nodes = (FONSatlasNode*)FONTSTASH_REALLOC(atlas->nodes, sizeof(FONSatlasNode) * atlas->cnodes);
if (atlas->nodes == NULL) if (atlas->nodes == NULL)
return 0; return 0;
} }
@ -743,14 +749,14 @@ FONScontext* fonsCreateInternal(FONSparams* params)
FONScontext* stash = NULL; FONScontext* stash = NULL;
// Allocate memory for the font stash. // Allocate memory for the font stash.
stash = (FONScontext*)malloc(sizeof(FONScontext)); stash = (FONScontext*)FONTSTASH_MALLOC(sizeof(FONScontext));
if (stash == NULL) goto error; if (stash == NULL) goto error;
memset(stash, 0, sizeof(FONScontext)); memset(stash, 0, sizeof(FONScontext));
stash->params = *params; stash->params = *params;
// Allocate scratch buffer. // Allocate scratch buffer.
stash->scratch = (unsigned char*)malloc(FONS_SCRATCH_BUF_SIZE); stash->scratch = (unsigned char*)FONTSTASH_MALLOC(FONS_SCRATCH_BUF_SIZE);
if (stash->scratch == NULL) goto error; if (stash->scratch == NULL) goto error;
// Initialize implementation library // Initialize implementation library
@ -765,7 +771,7 @@ FONScontext* fonsCreateInternal(FONSparams* params)
if (stash->atlas == NULL) goto error; if (stash->atlas == NULL) goto error;
// Allocate space for fonts. // Allocate space for fonts.
stash->fonts = (FONSfont**)malloc(sizeof(FONSfont*) * FONS_INIT_FONTS); stash->fonts = (FONSfont**)FONTSTASH_MALLOC(sizeof(FONSfont*) * FONS_INIT_FONTS);
if (stash->fonts == NULL) goto error; if (stash->fonts == NULL) goto error;
memset(stash->fonts, 0, sizeof(FONSfont*) * FONS_INIT_FONTS); memset(stash->fonts, 0, sizeof(FONSfont*) * FONS_INIT_FONTS);
stash->cfonts = FONS_INIT_FONTS; stash->cfonts = FONS_INIT_FONTS;
@ -774,7 +780,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*)malloc(stash->params.width * stash->params.height); stash->texData = (unsigned char*)FONTSTASH_MALLOC(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);
@ -877,9 +883,9 @@ void fonsClearState(FONScontext* stash)
static void fons__freeFont(FONSfont* font) static void fons__freeFont(FONSfont* font)
{ {
if (font == NULL) return; if (font == NULL) return;
if (font->glyphs) free(font->glyphs); if (font->glyphs) FONTSTASH_FREE(font->glyphs);
if (font->freeData && font->data) free(font->data); if (font->freeData && font->data) FONTSTASH_FREE(font->data);
free(font); FONTSTASH_FREE(font);
} }
static int fons__allocFont(FONScontext* stash) static int fons__allocFont(FONScontext* stash)
@ -887,15 +893,15 @@ static int fons__allocFont(FONScontext* stash)
FONSfont* font = NULL; FONSfont* font = NULL;
if (stash->nfonts+1 > stash->cfonts) { if (stash->nfonts+1 > stash->cfonts) {
stash->cfonts = stash->cfonts == 0 ? 8 : stash->cfonts * 2; stash->cfonts = stash->cfonts == 0 ? 8 : stash->cfonts * 2;
stash->fonts = (FONSfont**)realloc(stash->fonts, sizeof(FONSfont*) * stash->cfonts); stash->fonts = (FONSfont**)FONTSTASH_REALLOC(stash->fonts, sizeof(FONSfont*) * stash->cfonts);
if (stash->fonts == NULL) if (stash->fonts == NULL)
return -1; return -1;
} }
font = (FONSfont*)malloc(sizeof(FONSfont)); font = (FONSfont*)FONTSTASH_MALLOC(sizeof(FONSfont));
if (font == NULL) goto error; if (font == NULL) goto error;
memset(font, 0, sizeof(FONSfont)); memset(font, 0, sizeof(FONSfont));
font->glyphs = (FONSglyph*)malloc(sizeof(FONSglyph) * FONS_INIT_GLYPHS); font->glyphs = (FONSglyph*)FONTSTASH_MALLOC(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;
@ -967,7 +973,7 @@ static FONSglyph* fons__allocGlyph(FONSfont* font)
{ {
if (font->nglyphs+1 > font->cglyphs) { if (font->nglyphs+1 > font->cglyphs) {
font->cglyphs = font->cglyphs == 0 ? 8 : font->cglyphs * 2; font->cglyphs = font->cglyphs == 0 ? 8 : font->cglyphs * 2;
font->glyphs = (FONSglyph*)realloc(font->glyphs, sizeof(FONSglyph) * font->cglyphs); font->glyphs = (FONSglyph*)FONTSTASH_REALLOC(font->glyphs, sizeof(FONSglyph) * font->cglyphs);
if (font->glyphs == NULL) return NULL; if (font->glyphs == NULL) return NULL;
} }
font->nglyphs++; font->nglyphs++;
@ -1610,10 +1616,10 @@ FONS_DEF void fonsDeleteInternal(FONScontext* stash)
fons__freeFont(stash->fonts[i]); fons__freeFont(stash->fonts[i]);
if (stash->atlas) fons__deleteAtlas(stash->atlas); if (stash->atlas) fons__deleteAtlas(stash->atlas);
if (stash->fonts) free(stash->fonts); if (stash->fonts) FONTSTASH_FREE(stash->fonts);
if (stash->texData) free(stash->texData); if (stash->texData) FONTSTASH_FREE(stash->texData);
if (stash->scratch) free(stash->scratch); if (stash->scratch) FONTSTASH_FREE(stash->scratch);
free(stash); FONTSTASH_FREE(stash);
} }
FONS_DEF void fonsSetErrorCallback(FONScontext* stash, void (*callback)(void* uptr, int error, int val), void* uptr) 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)
return 0; return 0;
} }
// Copy old texture data over. // Copy old texture data over.
data = (unsigned char*)malloc(width * height); data = (unsigned char*)FONTSTASH_MALLOC(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++) {
@ -1664,7 +1670,7 @@ FONS_DEF int fonsExpandAtlas(FONScontext* stash, int width, int height)
if (height > stash->params.height) if (height > stash->params.height)
memset(&data[stash->params.height * width], 0, (height - stash->params.height) * width); memset(&data[stash->params.height * width], 0, (height - stash->params.height) * width);
free(stash->texData); FONTSTASH_FREE(stash->texData);
stash->texData = data; stash->texData = data;
// Increase atlas size // Increase atlas size
@ -1704,7 +1710,7 @@ FONS_DEF int fonsResetAtlas(FONScontext* stash, int width, int height)
fons__atlasReset(stash->atlas, width, height); fons__atlasReset(stash->atlas, width, height);
// Clear texture data. // Clear texture data.
stash->texData = (unsigned char*)realloc(stash->texData, width * height); stash->texData = (unsigned char*)FONTSTASH_REALLOC(stash->texData, width * height);
if (stash->texData == NULL) return 0; if (stash->texData == NULL) return 0;
memset(stash->texData, 0, width * height); memset(stash->texData, 0, width * height);

View File

@ -464,8 +464,8 @@ int main(int arg, char **argv)
// #define your own functions "STBTT_malloc" / "STBTT_free" to avoid malloc.h // #define your own functions "STBTT_malloc" / "STBTT_free" to avoid malloc.h
#ifndef STBTT_malloc #ifndef STBTT_malloc
#include <stdlib.h> #include <stdlib.h>
#define STBTT_malloc(x,u) ((void)(u),malloc(x)) #define STBTT_malloc(x,u) ((void)(u),FONTSTASH_MALLOC(x))
#define STBTT_free(x,u) ((void)(u),free(x)) #define STBTT_free(x,u) ((void)(u),FONTSTASH_FREE(x))
#endif #endif
#ifndef STBTT_assert #ifndef STBTT_assert

View File

@ -2,6 +2,11 @@ module fontstash
#flag -I @VROOT/thirdparty/fontstash #flag -I @VROOT/thirdparty/fontstash
#define FONTSTASH_IMPLEMENTATION #define FONTSTASH_IMPLEMENTATION
$if gcboehm ? {
#define FONTSTASH_MALLOC GC_MALLOC
#define FONTSTASH_REALLOC GC_REALLOC
#define FONTSTASH_FREE GC_FREE
}
#include "fontstash.h" #include "fontstash.h"
#flag -I /usr/local/Cellar/freetype/2.10.2/include/freetype2 #flag -I /usr/local/Cellar/freetype/2.10.2/include/freetype2
//#flag -lfreetype //#flag -lfreetype

View File

@ -33,6 +33,14 @@ pub const (
#flag freebsd -DSOKOL_NO_ENTRY #flag freebsd -DSOKOL_NO_ENTRY
#flag solaris -DSOKOL_NO_ENTRY #flag solaris -DSOKOL_NO_ENTRY
// TODO end // TODO end
$if gcboehm ? {
#define SOKOL_MALLOC GC_MALLOC
#define SOKOL_CALLOC(n,m) GC_MALLOC((n)*(m))
#define SOKOL_REALLOC GC_REALLOC
#define SOKOL_FREE GC_FREE
}
#include "sokol_v.h" #include "sokol_v.h"
#include "sokol_app.h" #include "sokol_app.h"
#define SOKOL_IMPL #define SOKOL_IMPL