From 02ca24ad75c402661018ebe641cfd3d3948af7f8 Mon Sep 17 00:00:00 2001 From: Mikko Mononen Date: Sat, 3 May 2014 12:17:27 +0300 Subject: [PATCH] Fix to update alloc count after successful alloc --- src/nanovg.c | 20 ++++++++++++-------- src/nanovg_gl.h | 25 +++++++++++++++---------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/nanovg.c b/src/nanovg.c index 3c56c6a..aec8ef9 100644 --- a/src/nanovg.c +++ b/src/nanovg.c @@ -858,10 +858,11 @@ static void nvg__appendCommands(struct NVGcontext* ctx, float* vals, int nvals) if (ctx->ncommands+nvals > ctx->ccommands) { float* commands; - ctx->ccommands = ctx->ncommands+nvals + ctx->ccommands/2; - commands = (float*)realloc(ctx->commands, ctx->ccommands*sizeof(float)); + int ccommands = ctx->ncommands+nvals + ctx->ccommands/2; + commands = (float*)realloc(ctx->commands, sizeof(float)*ccommands); if (commands == NULL) return; ctx->commands = commands; + ctx->ccommands = ccommands; } // transform commands @@ -923,10 +924,11 @@ static void nvg__addPath(struct NVGcontext* ctx) struct NVGpath* path; if (ctx->cache->npaths+1 > ctx->cache->cpaths) { struct NVGpath* paths; - ctx->cache->cpaths = ctx->cache->npaths+1 + ctx->cache->cpaths/2; - paths = (struct NVGpath*)realloc(ctx->cache->paths, sizeof(struct NVGpath)*ctx->cache->cpaths); + int cpaths = ctx->cache->npaths+1 + ctx->cache->cpaths/2; + paths = (struct NVGpath*)realloc(ctx->cache->paths, sizeof(struct NVGpath)*cpaths); if (paths == NULL) return; ctx->cache->paths = paths; + ctx->cache->cpaths = cpaths; } path = &ctx->cache->paths[ctx->cache->npaths]; memset(path, 0, sizeof(*path)); @@ -959,10 +961,11 @@ static void nvg__addPoint(struct NVGcontext* ctx, float x, float y, int flags) if (ctx->cache->npoints+1 > ctx->cache->cpoints) { struct NVGpoint* points; - ctx->cache->cpoints = ctx->cache->npoints+1 + ctx->cache->cpoints/2; - points = (struct NVGpoint*)realloc(ctx->cache->points, sizeof(struct NVGpoint)*ctx->cache->cpoints); + int cpoints = ctx->cache->npoints+1 + ctx->cache->cpoints/2; + points = (struct NVGpoint*)realloc(ctx->cache->points, sizeof(struct NVGpoint)*cpoints); if (points == NULL) return; ctx->cache->points = points; + ctx->cache->cpoints = cpoints; } pt = &ctx->cache->points[ctx->cache->npoints]; @@ -1000,10 +1003,11 @@ static struct NVGvertex* nvg__allocTempVerts(struct NVGcontext* ctx, int nverts) { if (nverts > ctx->cache->cverts) { struct NVGvertex* verts; - ctx->cache->cverts = (nverts + 0xff) & ~0xff; // Round up to prevent allocations when things change just slightly. - verts = (struct NVGvertex*)realloc(ctx->cache->verts, sizeof(struct NVGvertex)*ctx->cache->cverts); + int cverts = (nverts + 0xff) & ~0xff; // Round up to prevent allocations when things change just slightly. + verts = (struct NVGvertex*)realloc(ctx->cache->verts, sizeof(struct NVGvertex)*cverts); if (verts == NULL) return NULL; ctx->cache->verts = verts; + ctx->cache->cverts = cverts; } return ctx->cache->verts; diff --git a/src/nanovg_gl.h b/src/nanovg_gl.h index c72e64d..df3f8dc 100644 --- a/src/nanovg_gl.h +++ b/src/nanovg_gl.h @@ -216,10 +216,11 @@ static struct GLNVGtexture* glnvg__allocTexture(struct GLNVGcontext* gl) if (tex == NULL) { if (gl->ntextures+1 > gl->ctextures) { struct GLNVGtexture* textures; - gl->ctextures = glnvg__maxi(gl->ntextures+1, 4) + gl->ctextures/2; // 1.5x Overallocate - textures = (struct GLNVGtexture*)realloc(gl->textures, sizeof(struct GLNVGtexture)*gl->ctextures); + int ctextures = glnvg__maxi(gl->ntextures+1, 4) + gl->ctextures/2; // 1.5x Overallocate + textures = (struct GLNVGtexture*)realloc(gl->textures, sizeof(struct GLNVGtexture)*ctextures); if (textures == NULL) return NULL; gl->textures = textures; + gl->ctextures = ctextures; } tex = &gl->textures[gl->ntextures++]; } @@ -988,10 +989,11 @@ static struct GLNVGcall* glnvg__allocCall(struct GLNVGcontext* gl) struct GLNVGcall* ret = NULL; if (gl->ncalls+1 > gl->ccalls) { struct GLNVGcall* calls; - gl->ccalls = glnvg__maxi(gl->ncalls+1, 128) + gl->ccalls; // 1.5x Overallocate - calls = (struct GLNVGcall*)realloc(gl->calls, sizeof(struct GLNVGcall) * gl->ccalls); + int ccalls = glnvg__maxi(gl->ncalls+1, 128) + gl->ccalls; // 1.5x Overallocate + calls = (struct GLNVGcall*)realloc(gl->calls, sizeof(struct GLNVGcall) * ccalls); if (calls == NULL) return NULL; gl->calls = calls; + gl->ccalls = ccalls; } ret = &gl->calls[gl->ncalls++]; memset(ret, 0, sizeof(struct GLNVGcall)); @@ -1003,10 +1005,11 @@ static int glnvg__allocPaths(struct GLNVGcontext* gl, int n) int ret = 0; if (gl->npaths+n > gl->cpaths) { struct GLNVGpath* paths; - gl->cpaths = glnvg__maxi(gl->npaths + n, 128) + gl->cpaths; // 1.5x Overallocate - paths = (struct GLNVGpath*)realloc(gl->paths, sizeof(struct GLNVGpath) * gl->cpaths); + int cpaths = glnvg__maxi(gl->npaths + n, 128) + gl->cpaths; // 1.5x Overallocate + paths = (struct GLNVGpath*)realloc(gl->paths, sizeof(struct GLNVGpath) * cpaths); if (paths == NULL) return -1; gl->paths = paths; + gl->cpaths = cpaths; } ret = gl->npaths; gl->npaths += n; @@ -1018,10 +1021,11 @@ static int glnvg__allocVerts(struct GLNVGcontext* gl, int n) int ret = 0; if (gl->nverts+n > gl->cverts) { struct NVGvertex* verts; - gl->cverts = glnvg__maxi(gl->nverts + n, 4096) + gl->cverts/2; // 1.5x Overallocate - verts = (struct NVGvertex*)realloc(gl->verts, sizeof(struct NVGvertex) * gl->cverts); + int cverts = glnvg__maxi(gl->nverts + n, 4096) + gl->cverts/2; // 1.5x Overallocate + verts = (struct NVGvertex*)realloc(gl->verts, sizeof(struct NVGvertex) * cverts); if (verts == NULL) return -1; gl->verts = verts; + gl->cverts = cverts; } ret = gl->nverts; gl->nverts += n; @@ -1033,10 +1037,11 @@ static int glnvg__allocFragUniforms(struct GLNVGcontext* gl, int n) int ret = 0, structSize = gl->fragSize; if (gl->nuniforms+n > gl->cuniforms) { unsigned char* uniforms; - gl->cuniforms = glnvg__maxi(gl->nuniforms+n, 128) + gl->cuniforms/2; // 1.5x Overallocate - uniforms = (unsigned char*)realloc(gl->uniforms, gl->cuniforms * structSize); + int cuniforms = glnvg__maxi(gl->nuniforms+n, 128) + gl->cuniforms/2; // 1.5x Overallocate + uniforms = (unsigned char*)realloc(gl->uniforms, structSize * cuniforms); if (uniforms == NULL) return -1; gl->uniforms = uniforms; + gl->cuniforms = cuniforms; } ret = gl->nuniforms * structSize; gl->nuniforms += n;