From eadb7fc4af0163b9f841efbe6f301e3495281e91 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Thu, 4 Nov 2021 18:53:53 +0900 Subject: [PATCH] binary size optimization. replaced new/delete with malloc/free saved 936 bytes --- src/lib/gl_engine/tvgGlProgram.cpp | 4 +-- src/lib/gl_engine/tvgGlShader.cpp | 2 +- src/lib/sw_engine/tvgSwCommon.h | 6 ++-- src/lib/sw_engine/tvgSwMemPool.cpp | 6 ++-- src/lib/tvgFill.cpp | 4 ++- src/lib/tvgFill.h | 6 ++-- src/lib/tvgShapeImpl.h | 49 ++++++++++++++++-------------- 7 files changed, 41 insertions(+), 36 deletions(-) diff --git a/src/lib/gl_engine/tvgGlProgram.cpp b/src/lib/gl_engine/tvgGlProgram.cpp index e7a71b52..a8e74859 100644 --- a/src/lib/gl_engine/tvgGlProgram.cpp +++ b/src/lib/gl_engine/tvgGlProgram.cpp @@ -163,10 +163,10 @@ void GlProgram::linkProgram(std::shared_ptr shader) glGetProgramiv(progObj, GL_INFO_LOG_LENGTH, &infoLen); if (infoLen > 0) { - char* infoLog = new char[infoLen]; + auto infoLog = static_cast(malloc(sizeof(char) * infoLen)); glGetProgramInfoLog(progObj, infoLen, NULL, infoLog); TVGERR("GL_ENGINE", "Error linking shader: %s", infoLog); - delete[] infoLog; + free(infoLog); } glDeleteProgram(progObj); diff --git a/src/lib/gl_engine/tvgGlShader.cpp b/src/lib/gl_engine/tvgGlShader.cpp index 89c635f3..1c3a2c65 100644 --- a/src/lib/gl_engine/tvgGlShader.cpp +++ b/src/lib/gl_engine/tvgGlShader.cpp @@ -84,7 +84,7 @@ uint32_t GlShader::complileShader(uint32_t type, char* shaderSrc) if (infoLen > 0) { - char* infoLog = new char[infoLen]; + auto infoLog = static_cast(malloc(sizeof(char)*infoLen)); glGetShaderInfoLog(shader, infoLen, NULL, infoLog); TVGERR("GL_ENGINE", "Error compiling shader: %s", infoLog); delete[] infoLog; diff --git a/src/lib/sw_engine/tvgSwCommon.h b/src/lib/sw_engine/tvgSwCommon.h index 61c6a682..a5272c55 100644 --- a/src/lib/sw_engine/tvgSwCommon.h +++ b/src/lib/sw_engine/tvgSwCommon.h @@ -251,9 +251,9 @@ struct SwCompositor : Compositor struct SwMpool { - SwOutline* outline = nullptr; - SwOutline* strokeOutline = nullptr; - unsigned allocSize = 0; + SwOutline* outline; + SwOutline* strokeOutline; + unsigned allocSize; }; static inline SwCoord TO_SWCOORD(float val) diff --git a/src/lib/sw_engine/tvgSwMemPool.cpp b/src/lib/sw_engine/tvgSwMemPool.cpp index 5bf15d7f..a44be85b 100644 --- a/src/lib/sw_engine/tvgSwMemPool.cpp +++ b/src/lib/sw_engine/tvgSwMemPool.cpp @@ -61,7 +61,7 @@ SwMpool* mpoolInit(unsigned threads) { if (threads == 0) threads = 1; - auto mpool = new SwMpool; + auto mpool = static_cast(calloc(sizeof(SwMpool), 1)); mpool->outline = static_cast(calloc(1, sizeof(SwOutline) * threads)); if (!mpool->outline) goto err; @@ -82,7 +82,7 @@ err: free(mpool->strokeOutline); mpool->strokeOutline = nullptr; } - delete(mpool); + free(mpool); return nullptr; } @@ -150,7 +150,7 @@ bool mpoolTerm(SwMpool* mpool) mpool->strokeOutline = nullptr; } - delete(mpool); + free(mpool); return true; } \ No newline at end of file diff --git a/src/lib/tvgFill.cpp b/src/lib/tvgFill.cpp index ab059c71..f26168aa 100644 --- a/src/lib/tvgFill.cpp +++ b/src/lib/tvgFill.cpp @@ -89,7 +89,9 @@ FillSpread Fill::spread() const noexcept Result Fill::transform(const Matrix& m) noexcept { - if (!pImpl->transform) pImpl->transform = new Matrix(); + if (!pImpl->transform) { + pImpl->transform = static_cast(malloc(sizeof(Matrix))); + } *pImpl->transform = m; return Result::Success; } diff --git a/src/lib/tvgFill.h b/src/lib/tvgFill.h index af33c061..42518499 100644 --- a/src/lib/tvgFill.h +++ b/src/lib/tvgFill.h @@ -58,8 +58,8 @@ struct Fill::Impl ~Impl() { if (dup) delete(dup); - if (colorStops) free(colorStops); - if (transform) delete(transform); + free(colorStops); + free(transform); } void method(DuplicateMethod* dup) @@ -77,7 +77,7 @@ struct Fill::Impl ret->pImpl->colorStops = static_cast(malloc(sizeof(ColorStop) * cnt)); memcpy(ret->pImpl->colorStops, colorStops, sizeof(ColorStop) * cnt); if (transform) { - ret->pImpl->transform = new Matrix; + ret->pImpl->transform = static_cast(malloc(sizeof(Matrix))); *ret->pImpl->transform = *transform; } return ret; diff --git a/src/lib/tvgShapeImpl.h b/src/lib/tvgShapeImpl.h index 84af1e6e..c887f049 100644 --- a/src/lib/tvgShapeImpl.h +++ b/src/lib/tvgShapeImpl.h @@ -31,22 +31,21 @@ struct ShapeStroke { - float width = 0; - uint8_t color[4] = {0, 0, 0, 0}; - Fill *fill = nullptr; - float* dashPattern = nullptr; - uint32_t dashCnt = 0; - StrokeCap cap = StrokeCap::Square; - StrokeJoin join = StrokeJoin::Bevel; + float width; + uint8_t color[4]; + Fill *fill; + float* dashPattern; + uint32_t dashCnt; + StrokeCap cap; + StrokeJoin join; - ShapeStroke() {} - - ShapeStroke(const ShapeStroke* src) - : width(src->width), - dashCnt(src->dashCnt), - cap(src->cap), - join(src->join) + void copy(const ShapeStroke* src) { + width = src->width; + dashCnt = src->dashCnt; + cap = src->cap; + join = src->join; + memcpy(color, src->color, sizeof(color)); if (dashCnt > 0) { dashPattern = static_cast(malloc(sizeof(float) * dashCnt)); @@ -55,7 +54,7 @@ struct ShapeStroke if (src->fill) fill = src->fill->duplicate(); } - ~ShapeStroke() + void clear() { if (dashPattern) free(dashPattern); if (fill) delete(fill); @@ -215,7 +214,10 @@ struct Shape::Impl ~Impl() { if (fill) delete(fill); - if (stroke) delete(stroke); + if (stroke) { + stroke->clear(); + free (stroke); + } } bool dispose(RenderMethod& renderer) @@ -260,7 +262,7 @@ struct Shape::Impl { //TODO: Size Exception? - if (!stroke) stroke = new ShapeStroke(); + if (!stroke) stroke = static_cast(calloc(sizeof(ShapeStroke), 1)); stroke->width = width; flag |= RenderUpdateFlag::Stroke; @@ -269,7 +271,7 @@ struct Shape::Impl bool strokeCap(StrokeCap cap) { - if (!stroke) stroke = new ShapeStroke(); + if (!stroke) stroke = static_cast(calloc(sizeof(ShapeStroke), 1)); stroke->cap = cap; flag |= RenderUpdateFlag::Stroke; @@ -278,7 +280,7 @@ struct Shape::Impl bool strokeJoin(StrokeJoin join) { - if (!stroke) stroke = new ShapeStroke(); + if (!stroke) stroke = static_cast(calloc(sizeof(ShapeStroke), 1)); stroke->join = join; flag |= RenderUpdateFlag::Stroke; @@ -287,7 +289,7 @@ struct Shape::Impl bool strokeColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { - if (!stroke) stroke = new ShapeStroke(); + if (!stroke) stroke = static_cast(calloc(sizeof(ShapeStroke), 1)); if (stroke->fill) { delete(stroke->fill); stroke->fill = nullptr; @@ -309,7 +311,7 @@ struct Shape::Impl auto p = f.release(); if (!p) return Result::MemoryCorruption; - if (!stroke) stroke = new ShapeStroke(); + if (!stroke) stroke = static_cast(calloc(sizeof(ShapeStroke), 1)); if (stroke->fill && stroke->fill != p) delete(stroke->fill); stroke->fill = p; @@ -326,7 +328,7 @@ struct Shape::Impl free(stroke->dashPattern); stroke->dashPattern = nullptr; } else { - if (!stroke) stroke = new ShapeStroke(); + if (!stroke) stroke = static_cast(calloc(sizeof(ShapeStroke), 1)); if (stroke->dashCnt != cnt) { free(stroke->dashPattern); stroke->dashPattern = nullptr; @@ -363,7 +365,8 @@ struct Shape::Impl //Stroke if (stroke) { - dup->stroke = new ShapeStroke(stroke); + dup->stroke = static_cast(calloc(sizeof(ShapeStroke), 1)); + dup->stroke->copy(stroke); dup->flag |= RenderUpdateFlag::Stroke; if (stroke->fill)