Skip to content

Commit

Permalink
feat(nanovg): make NanoVG/SVG use the LWJGL memory allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
Spasi committed May 20, 2018
1 parent d6d0e98 commit 88544d3
Show file tree
Hide file tree
Showing 17 changed files with 133 additions and 104 deletions.
1 change: 1 addition & 0 deletions doc/notes/3.1.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ This build includes the following changes:
- Generator: Made StructBuffer subclass generation optional.
- Loader: Now prints the path of shared libraries loaded from system paths in debug mode, when possible.
- docs(core): Improved `PointerBuffer` javadoc.
- NanoVG/NanoSVG: The LWJGL memory allocator is now used for internal allocations.

#### Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
* MACHINE GENERATED FILE, DO NOT EDIT
*/
#include "common_tools.h"
#include "lwjgl_malloc.h"
#define NVG_MALLOC(sz) org_lwjgl_malloc(sz)
#define NVG_REALLOC(p,sz) org_lwjgl_realloc(p,sz)
#define NVG_FREE(p) org_lwjgl_free(p)
DISABLE_WARNINGS()
#define NANOSVG_IMPLEMENTATION
#include "nanosvg.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
* MACHINE GENERATED FILE, DO NOT EDIT
*/
#include "common_tools.h"
DISABLE_WARNINGS()
#include <stdlib.h>
#include <string.h>
#include "lwjgl_malloc.h"
#define NVG_MALLOC(sz) org_lwjgl_malloc(sz)
#define NVG_REALLOC(p,sz) org_lwjgl_realloc(p,sz)
#define NVG_FREE(p) org_lwjgl_free(p)
DISABLE_WARNINGS()
#define STBI_MALLOC(sz) org_lwjgl_malloc(sz)
#define STBI_REALLOC(p,sz) org_lwjgl_realloc(p,sz)
#define STBI_FREE(p) org_lwjgl_free(p)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
* MACHINE GENERATED FILE, DO NOT EDIT
*/
#include "common_tools.h"
#include "lwjgl_malloc.h"
#define NVG_MALLOC(sz) org_lwjgl_malloc(sz)
#define NVG_REALLOC(p,sz) org_lwjgl_realloc(p,sz)
#define NVG_FREE(p) org_lwjgl_free(p)
DISABLE_WARNINGS()
#define NANOVG_GL2_IMPLEMENTATION
#include "nanovg.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
* MACHINE GENERATED FILE, DO NOT EDIT
*/
#include "common_tools.h"
#include "lwjgl_malloc.h"
#define NVG_MALLOC(sz) org_lwjgl_malloc(sz)
#define NVG_REALLOC(p,sz) org_lwjgl_realloc(p,sz)
#define NVG_FREE(p) org_lwjgl_free(p)
DISABLE_WARNINGS()
#define NANOVG_GL3_IMPLEMENTATION
#include "nanovg.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
* MACHINE GENERATED FILE, DO NOT EDIT
*/
#include "common_tools.h"
#include "lwjgl_malloc.h"
#define NVG_MALLOC(sz) org_lwjgl_malloc(sz)
#define NVG_REALLOC(p,sz) org_lwjgl_realloc(p,sz)
#define NVG_FREE(p) org_lwjgl_free(p)
DISABLE_WARNINGS()
#define NANOVG_GLES2_IMPLEMENTATION
#include "nanovg.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
* MACHINE GENERATED FILE, DO NOT EDIT
*/
#include "common_tools.h"
#include "lwjgl_malloc.h"
#define NVG_MALLOC(sz) org_lwjgl_malloc(sz)
#define NVG_REALLOC(p,sz) org_lwjgl_realloc(p,sz)
#define NVG_FREE(p) org_lwjgl_free(p)
DISABLE_WARNINGS()
#define NANOVG_GLES3_IMPLEMENTATION
#include "nanovg.h"
Expand Down
50 changes: 25 additions & 25 deletions modules/lwjgl/nanovg/src/main/c/fontstash.h
Original file line number Diff line number Diff line change
Expand Up @@ -502,24 +502,24 @@ static unsigned int fons__decutf8(unsigned int* state, unsigned int* codep, unsi
static void fons__deleteAtlas(FONSatlas* atlas)
{
if (atlas == NULL) return;
if (atlas->nodes != NULL) free(atlas->nodes);
free(atlas);
if (atlas->nodes != NULL) NVG_FREE(atlas->nodes);
NVG_FREE(atlas);
}

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

// Allocate memory for the font stash.
atlas = (FONSatlas*)malloc(sizeof(FONSatlas));
atlas = (FONSatlas*)NVG_MALLOC(sizeof(FONSatlas));
if (atlas == NULL) goto error;
memset(atlas, 0, sizeof(FONSatlas));

atlas->width = w;
atlas->height = h;

// Allocate space for skyline nodes
atlas->nodes = (FONSatlasNode*)malloc(sizeof(FONSatlasNode) * nnodes);
atlas->nodes = (FONSatlasNode*)NVG_MALLOC(sizeof(FONSatlasNode) * nnodes);
if (atlas->nodes == NULL) goto error;
memset(atlas->nodes, 0, sizeof(FONSatlasNode) * nnodes);
atlas->nnodes = 0;
Expand All @@ -544,7 +544,7 @@ static int fons__atlasInsertNode(FONSatlas* atlas, int idx, int x, int y, int w)
// Insert node
if (atlas->nnodes+1 > atlas->cnodes) {
atlas->cnodes = atlas->cnodes == 0 ? 8 : atlas->cnodes * 2;
atlas->nodes = (FONSatlasNode*)realloc(atlas->nodes, sizeof(FONSatlasNode) * atlas->cnodes);
atlas->nodes = (FONSatlasNode*)NVG_REALLOC(atlas->nodes, sizeof(FONSatlasNode) * atlas->cnodes);
if (atlas->nodes == NULL)
return 0;
}
Expand Down Expand Up @@ -705,14 +705,14 @@ FONScontext* fonsCreateInternal(FONSparams* params)
FONScontext* stash = NULL;

// Allocate memory for the font stash.
stash = (FONScontext*)malloc(sizeof(FONScontext));
stash = (FONScontext*)NVG_MALLOC(sizeof(FONScontext));
if (stash == NULL) goto error;
memset(stash, 0, sizeof(FONScontext));

stash->params = *params;

// Allocate scratch buffer.
stash->scratch = (unsigned char*)malloc(FONS_SCRATCH_BUF_SIZE);
stash->scratch = (unsigned char*)NVG_MALLOC(FONS_SCRATCH_BUF_SIZE);
if (stash->scratch == NULL) goto error;

// Initialize implementation library
Expand All @@ -727,7 +727,7 @@ FONScontext* fonsCreateInternal(FONSparams* params)
if (stash->atlas == NULL) goto error;

// Allocate space for fonts.
stash->fonts = (FONSfont**)malloc(sizeof(FONSfont*) * FONS_INIT_FONTS);
stash->fonts = (FONSfont**)NVG_MALLOC(sizeof(FONSfont*) * FONS_INIT_FONTS);
if (stash->fonts == NULL) goto error;
memset(stash->fonts, 0, sizeof(FONSfont*) * FONS_INIT_FONTS);
stash->cfonts = FONS_INIT_FONTS;
Expand All @@ -736,7 +736,7 @@ FONScontext* fonsCreateInternal(FONSparams* params)
// Create texture for the cache.
stash->itw = 1.0f/stash->params.width;
stash->ith = 1.0f/stash->params.height;
stash->texData = (unsigned char*)malloc(stash->params.width * stash->params.height);
stash->texData = (unsigned char*)NVG_MALLOC(stash->params.width * stash->params.height);
if (stash->texData == NULL) goto error;
memset(stash->texData, 0, stash->params.width * stash->params.height);

Expand Down Expand Up @@ -839,25 +839,25 @@ void fonsClearState(FONScontext* stash)
static void fons__freeFont(FONSfont* font)
{
if (font == NULL) return;
if (font->glyphs) free(font->glyphs);
if (font->freeData && font->data) free(font->data);
free(font);
if (font->glyphs) NVG_FREE(font->glyphs);
if (font->freeData && font->data) NVG_FREE(font->data);
NVG_FREE(font);
}

static int fons__allocFont(FONScontext* stash)
{
FONSfont* font = NULL;
if (stash->nfonts+1 > stash->cfonts) {
stash->cfonts = stash->cfonts == 0 ? 8 : stash->cfonts * 2;
stash->fonts = (FONSfont**)realloc(stash->fonts, sizeof(FONSfont*) * stash->cfonts);
stash->fonts = (FONSfont**)NVG_REALLOC(stash->fonts, sizeof(FONSfont*) * stash->cfonts);
if (stash->fonts == NULL)
return -1;
}
font = (FONSfont*)malloc(sizeof(FONSfont));
font = (FONSfont*)NVG_MALLOC(sizeof(FONSfont));
if (font == NULL) goto error;
memset(font, 0, sizeof(FONSfont));

font->glyphs = (FONSglyph*)malloc(sizeof(FONSglyph) * FONS_INIT_GLYPHS);
font->glyphs = (FONSglyph*)NVG_MALLOC(sizeof(FONSglyph) * FONS_INIT_GLYPHS);
if (font->glyphs == NULL) goto error;
font->cglyphs = FONS_INIT_GLYPHS;
font->nglyphs = 0;
Expand All @@ -884,7 +884,7 @@ int fonsAddFont(FONScontext* stash, const char* name, const char* path)
fseek(fp,0,SEEK_END);
dataSize = (int)ftell(fp);
fseek(fp,0,SEEK_SET);
data = (unsigned char*)malloc(dataSize);
data = (unsigned char*)NVG_MALLOC(dataSize);
if (data == NULL) goto error;
readed = fread(data, 1, dataSize, fp);
fclose(fp);
Expand All @@ -894,7 +894,7 @@ int fonsAddFont(FONScontext* stash, const char* name, const char* path)
return fonsAddFontMem(stash, name, data, dataSize, 1);

error:
if (data) free(data);
if (data) NVG_FREE(data);
if (fp) fclose(fp);
return FONS_INVALID;
}
Expand Down Expand Up @@ -957,7 +957,7 @@ static FONSglyph* fons__allocGlyph(FONSfont* font)
{
if (font->nglyphs+1 > font->cglyphs) {
font->cglyphs = font->cglyphs == 0 ? 8 : font->cglyphs * 2;
font->glyphs = (FONSglyph*)realloc(font->glyphs, sizeof(FONSglyph) * font->cglyphs);
font->glyphs = (FONSglyph*)NVG_REALLOC(font->glyphs, sizeof(FONSglyph) * font->cglyphs);
if (font->glyphs == NULL) return NULL;
}
font->nglyphs++;
Expand Down Expand Up @@ -1621,10 +1621,10 @@ void fonsDeleteInternal(FONScontext* stash)
fons__freeFont(stash->fonts[i]);

if (stash->atlas) fons__deleteAtlas(stash->atlas);
if (stash->fonts) free(stash->fonts);
if (stash->texData) free(stash->texData);
if (stash->scratch) free(stash->scratch);
free(stash);
if (stash->fonts) NVG_FREE(stash->fonts);
if (stash->texData) NVG_FREE(stash->texData);
if (stash->scratch) NVG_FREE(stash->scratch);
NVG_FREE(stash);
}

void fonsSetErrorCallback(FONScontext* stash, void (*callback)(void* uptr, int error, int val), void* uptr)
Expand Down Expand Up @@ -1662,7 +1662,7 @@ int fonsExpandAtlas(FONScontext* stash, int width, int height)
return 0;
}
// Copy old texture data over.
data = (unsigned char*)malloc(width * height);
data = (unsigned char*)NVG_MALLOC(width * height);
if (data == NULL)
return 0;
for (i = 0; i < stash->params.height; i++) {
Expand All @@ -1675,7 +1675,7 @@ int fonsExpandAtlas(FONScontext* stash, int width, int height)
if (height > stash->params.height)
memset(&data[stash->params.height * width], 0, (height - stash->params.height) * width);

free(stash->texData);
NVG_FREE(stash->texData);
stash->texData = data;

// Increase atlas size
Expand Down Expand Up @@ -1715,7 +1715,7 @@ int fonsResetAtlas(FONScontext* stash, int width, int height)
fons__atlasReset(stash->atlas, width, height);

// Clear texture data.
stash->texData = (unsigned char*)realloc(stash->texData, width * height);
stash->texData = (unsigned char*)NVG_REALLOC(stash->texData, width * height);
if (stash->texData == NULL) return 0;
memset(stash->texData, 0, width * height);

Expand Down
Loading

0 comments on commit 88544d3

Please sign in to comment.