From b77f3ca0242bb81f38519d44f232c7a9c4ed7db5 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Tue, 18 Feb 2025 15:22:19 +0900 Subject: [PATCH] common: introduced designated memory allocators Support the bindings to be more integrable with a system's coherent memory management. Pleaes note that thorvg now only allow the desinated memory allocators here: malloc -> tvg::malloc calloc -> tvg::calloc realloc -> tvg::realloc free -> tvg::free issue: https://github.com/thorvg/thorvg/issues/2652 --- src/common/tvgArray.h | 12 +- src/common/tvgCompressor.cpp | 8 +- src/common/tvgStr.cpp | 7 +- src/loaders/external_jpg/tvgJpgLoader.cpp | 7 +- src/loaders/external_png/tvgPngLoader.cpp | 10 +- src/loaders/external_webp/tvgWebpLoader.cpp | 6 +- src/loaders/jpg/tvgJpgLoader.cpp | 7 +- src/loaders/jpg/tvgJpgd.cpp | 9 +- src/loaders/lottie/rapidjson/rapidjson.h | 9 +- src/loaders/lottie/tvgLottieBuilder.cpp | 11 +- src/loaders/lottie/tvgLottieBuilder.h | 2 +- src/loaders/lottie/tvgLottieExpressions.cpp | 12 +- src/loaders/lottie/tvgLottieLoader.cpp | 10 +- src/loaders/lottie/tvgLottieModel.cpp | 10 +- src/loaders/lottie/tvgLottieModel.h | 16 +- src/loaders/lottie/tvgLottieParser.cpp | 14 +- src/loaders/lottie/tvgLottieProperty.h | 42 ++--- src/loaders/png/tvgLodePng.cpp | 76 ++++----- src/loaders/png/tvgPngLoader.cpp | 8 +- src/loaders/raw/tvgRawLoader.cpp | 5 +- src/loaders/svg/tvgSvgCssStyle.cpp | 12 +- src/loaders/svg/tvgSvgLoader.cpp | 174 ++++++++++---------- src/loaders/svg/tvgSvgLoaderCommon.h | 10 +- src/loaders/svg/tvgSvgSceneBuilder.cpp | 13 +- src/loaders/svg/tvgSvgUtil.cpp | 3 +- src/loaders/svg/tvgXmlParser.cpp | 17 +- src/loaders/ttf/tvgTtfLoader.cpp | 14 +- src/loaders/ttf/tvgTtfReader.cpp | 11 +- src/loaders/webp/dec/alpha.cpp | 6 +- src/loaders/webp/dec/buffer.cpp | 6 +- src/loaders/webp/dec/frame.cpp | 6 +- src/loaders/webp/dec/io.cpp | 6 +- src/loaders/webp/dec/vp8.cpp | 8 +- src/loaders/webp/dec/vp8l.cpp | 42 ++--- src/loaders/webp/tvgWebpLoader.cpp | 8 +- src/loaders/webp/utils/color_cache.cpp | 7 +- src/loaders/webp/utils/huffman.cpp | 19 +-- src/loaders/webp/utils/quant_levels_dec.cpp | 8 +- src/renderer/gl_engine/tvgGlCommon.h | 2 +- src/renderer/gl_engine/tvgGlProgram.cpp | 4 +- src/renderer/gl_engine/tvgGlShader.cpp | 4 +- src/renderer/gl_engine/tvgGlTessellator.cpp | 4 +- src/renderer/sw_engine/tvgSwFill.cpp | 9 +- src/renderer/sw_engine/tvgSwMemPool.cpp | 16 +- src/renderer/sw_engine/tvgSwPostEffect.cpp | 4 +- src/renderer/sw_engine/tvgSwRasterTexmap.h | 8 +- src/renderer/sw_engine/tvgSwRenderer.cpp | 7 +- src/renderer/sw_engine/tvgSwRle.cpp | 21 ++- src/renderer/sw_engine/tvgSwShape.cpp | 15 +- src/renderer/sw_engine/tvgSwStroke.cpp | 16 +- src/renderer/tvgCommon.h | 85 ++++++---- src/renderer/tvgFill.h | 10 +- src/renderer/tvgInitializer.cpp | 14 +- src/renderer/tvgLoadModule.h | 3 +- src/renderer/tvgPaint.h | 9 +- src/renderer/tvgRender.h | 6 +- src/renderer/tvgShape.h | 8 +- src/renderer/tvgText.h | 6 +- src/renderer/wg_engine/tvgWgGeometry.h | 28 ++-- src/savers/gif/tvgGifEncoder.cpp | 13 +- src/savers/gif/tvgGifEncoder.h | 4 +- src/savers/gif/tvgGifSaver.cpp | 6 +- 62 files changed, 466 insertions(+), 487 deletions(-) diff --git a/src/common/tvgArray.h b/src/common/tvgArray.h index 5c2755ab..7fdac10b 100644 --- a/src/common/tvgArray.h +++ b/src/common/tvgArray.h @@ -23,9 +23,7 @@ #ifndef _TVG_ARRAY_H_ #define _TVG_ARRAY_H_ -#include -#include -#include +#include "tvgCommon.h" #define ARRAY_FOREACH(A, B) \ for (auto A = (B).begin(); A < (B).end(); ++A) @@ -60,7 +58,7 @@ struct Array { if (count + 1 > reserved) { reserved = count + (count + 2) / 2; - data = static_cast(realloc(data, sizeof(T) * reserved)); + data = tvg::realloc(data, sizeof(T) * reserved); } data[count++] = element; } @@ -77,7 +75,7 @@ struct Array { if (size > reserved) { reserved = size; - data = static_cast(realloc(data, sizeof(T) * reserved)); + data = tvg::realloc(data, sizeof(T) * reserved); } return true; } @@ -144,7 +142,7 @@ struct Array void reset() { - free(data); + tvg::free(data); data = nullptr; count = reserved = 0; } @@ -174,7 +172,7 @@ struct Array ~Array() { - free(data); + tvg::free(data); } private: diff --git a/src/common/tvgCompressor.cpp b/src/common/tvgCompressor.cpp index 830f0181..8f8f7940 100644 --- a/src/common/tvgCompressor.cpp +++ b/src/common/tvgCompressor.cpp @@ -54,9 +54,7 @@ * http://marknelson.us/1989/10/01/lzw-data-compression/ */ #include "config.h" - -#include -#include +#include "tvgCommon.h" #include "tvgCompressor.h" namespace tvg { @@ -209,7 +207,7 @@ uint8_t* lzwDecode(const uint8_t* compressed, uint32_t compressedSizeBytes, uint int firstByte = 0; int bytesDecoded = 0; int codeBitsWidth = StartBits; - auto uncompressed = (uint8_t*) malloc(sizeof(uint8_t) * uncompressedSizeBytes); + auto uncompressed = tvg::malloc(sizeof(uint8_t) * uncompressedSizeBytes); auto ptr = uncompressed; /* We'll reconstruct the dictionary based on the bit stream codes. @@ -263,7 +261,7 @@ size_t b64Decode(const char* encoded, const size_t len, char** decoded) if (!decoded || !encoded || len == 0) return 0; auto reserved = 3 * (1 + (len >> 2)) + 1; - auto output = static_cast(malloc(reserved * sizeof(char))); + auto output = tvg::malloc(reserved * sizeof(char)); if (!output) return 0; output[reserved - 1] = '\0'; diff --git a/src/common/tvgStr.cpp b/src/common/tvgStr.cpp index 49a59017..ea04a959 100644 --- a/src/common/tvgStr.cpp +++ b/src/common/tvgStr.cpp @@ -22,8 +22,7 @@ #include "config.h" #include -#include -#include +#include "tvgCommon.h" #include "tvgMath.h" #include "tvgStr.h" @@ -212,7 +211,7 @@ char* strDuplicate(const char *str, size_t n) auto len = strlen(str); if (len < n) n = len; - auto ret = (char *) malloc(n + 1); + auto ret = tvg::malloc(n + 1); if (!ret) return nullptr; ret[n] = '\0'; @@ -223,7 +222,7 @@ char* strAppend(char* lhs, const char* rhs, size_t n) { if (!rhs) return lhs; if (!lhs) return strDuplicate(rhs, n); - lhs = (char*)realloc(lhs, strlen(lhs) + n + 1); + lhs = tvg::realloc(lhs, strlen(lhs) + n + 1); return strncat(lhs, rhs, n); } diff --git a/src/loaders/external_jpg/tvgJpgLoader.cpp b/src/loaders/external_jpg/tvgJpgLoader.cpp index e0a8d2a2..94cce88f 100644 --- a/src/loaders/external_jpg/tvgJpgLoader.cpp +++ b/src/loaders/external_jpg/tvgJpgLoader.cpp @@ -20,7 +20,6 @@ * SOFTWARE. */ -#include #include #include "tvgJpgLoader.h" @@ -30,7 +29,7 @@ void JpgLoader::clear() { - if (freeData) free(data); + if (freeData) tvg::free(data); data = nullptr; size = 0; freeData = false; @@ -69,7 +68,7 @@ bool JpgLoader::open(const char* path) if (((size = ftell(jpegFile)) < 1)) goto finalize; if (fseek(jpegFile, 0, SEEK_SET)) goto finalize; - data = (unsigned char *) malloc(size); + data = tvg::malloc(size); if (!data) goto finalize; freeData = true; @@ -106,7 +105,7 @@ bool JpgLoader::open(const char* data, uint32_t size, TVG_UNUSED const char* rpa if (tjDecompressHeader3(jpegDecompressor, (unsigned char *) data, size, &width, &height, &subSample, &colorSpace) < 0) return false; if (copy) { - this->data = (unsigned char *) malloc(size); + this->data = tvg::malloc(size); if (!this->data) return false; memcpy((unsigned char *)this->data, data, size); freeData = true; diff --git a/src/loaders/external_png/tvgPngLoader.cpp b/src/loaders/external_png/tvgPngLoader.cpp index b45f8f4b..4ae844e4 100644 --- a/src/loaders/external_png/tvgPngLoader.cpp +++ b/src/loaders/external_png/tvgPngLoader.cpp @@ -29,7 +29,7 @@ void PngLoader::clear() { png_image_free(image); - free(image); + tvg::free(image); image = nullptr; } @@ -39,7 +39,7 @@ void PngLoader::clear() PngLoader::PngLoader() : ImageLoader(FileType::Png) { - image = static_cast(calloc(1, sizeof(png_image))); + image = tvg::calloc(1, sizeof(png_image)); image->version = PNG_IMAGE_VERSION; image->opaque = NULL; } @@ -47,7 +47,7 @@ PngLoader::PngLoader() : ImageLoader(FileType::Png) PngLoader::~PngLoader() { clear(); - free((void*)surface.buf32); + tvg::free(surface.buf32); } @@ -95,9 +95,9 @@ bool PngLoader::read() surface.cs = ColorSpace::ABGR8888S; } - auto buffer = static_cast(malloc(PNG_IMAGE_SIZE((*image)))); + auto buffer = tvg::malloc(PNG_IMAGE_SIZE((*image))); if (!png_image_finish_read(image, NULL, buffer, 0, NULL)) { - free(buffer); + tvg::free(buffer); return false; } diff --git a/src/loaders/external_webp/tvgWebpLoader.cpp b/src/loaders/external_webp/tvgWebpLoader.cpp index 6f690bf7..bb57d515 100644 --- a/src/loaders/external_webp/tvgWebpLoader.cpp +++ b/src/loaders/external_webp/tvgWebpLoader.cpp @@ -57,7 +57,7 @@ WebpLoader::~WebpLoader() { this->done(); - if (freeData) free(data); + if (freeData) tvg::free(data); data = nullptr; size = 0; freeData = false; @@ -78,7 +78,7 @@ bool WebpLoader::open(const char* path) if (((size = ftell(webpFile)) < 1)) goto finalize; if (fseek(webpFile, 0, SEEK_SET)) goto finalize; - data = (unsigned char *) malloc(size); + data = tvg::malloc(size); if (!data) goto finalize; freeData = true; @@ -105,7 +105,7 @@ finalize: bool WebpLoader::open(const char* data, uint32_t size, TVG_UNUSED const char* rpath, bool copy) { if (copy) { - this->data = (unsigned char *) malloc(size); + this->data = tvg::malloc(size); if (!this->data) return false; memcpy((unsigned char *)this->data, data, size); freeData = true; diff --git a/src/loaders/jpg/tvgJpgLoader.cpp b/src/loaders/jpg/tvgJpgLoader.cpp index 303d775f..f1c2c4d0 100644 --- a/src/loaders/jpg/tvgJpgLoader.cpp +++ b/src/loaders/jpg/tvgJpgLoader.cpp @@ -20,7 +20,6 @@ * SOFTWARE. */ -#include #include "tvgJpgLoader.h" /************************************************************************/ @@ -30,7 +29,7 @@ void JpgLoader::clear() { jpgdDelete(decoder); - if (freeData) free(data); + if (freeData) tvg::free(data); decoder = nullptr; data = nullptr; freeData = false; @@ -64,7 +63,7 @@ JpgLoader::JpgLoader() : ImageLoader(FileType::Jpg) JpgLoader::~JpgLoader() { clear(); - free(surface.buf8); + tvg::free(surface.buf8); } @@ -88,7 +87,7 @@ bool JpgLoader::open(const char* path) bool JpgLoader::open(const char* data, uint32_t size, TVG_UNUSED const char* rpath, bool copy) { if (copy) { - this->data = (char *) malloc(size); + this->data = tvg::malloc(size); if (!this->data) return false; memcpy((char *)this->data, data, size); freeData = true; diff --git a/src/loaders/jpg/tvgJpgd.cpp b/src/loaders/jpg/tvgJpgd.cpp index 0af13240..cabb6dd0 100644 --- a/src/loaders/jpg/tvgJpgd.cpp +++ b/src/loaders/jpg/tvgJpgd.cpp @@ -31,7 +31,6 @@ // Chroma upsampling reference: "Fast Scheme for Image Size Change in the Compressed Domain" // http://vision.ai.uiuc.edu/~dugad/research/dct/index.html -#include #include #include #include @@ -1087,7 +1086,7 @@ void jpeg_decoder::free_all_blocks() for (mem_block *b = m_pMem_blocks; b; ) { mem_block *n = b->m_pNext; - free(b); + tvg::free(b); b = n; } m_pMem_blocks = nullptr; @@ -1117,7 +1116,7 @@ void *jpeg_decoder::alloc(size_t nSize, bool zero) } if (!rv) { int capacity = JPGD_MAX(32768 - 256, (nSize + 2047) & ~2047); - mem_block *b = (mem_block*)malloc(sizeof(mem_block) + capacity); + mem_block *b = tvg::malloc(sizeof(mem_block) + capacity); if (!b) stop_decoding(JPGD_NOTENOUGHMEM); b->m_pNext = m_pMem_blocks; m_pMem_blocks = b; b->m_used_count = nSize; @@ -2967,14 +2966,14 @@ unsigned char* jpgdDecompress(jpeg_decoder* decoder) if (decoder->begin_decoding() != JPGD_SUCCESS) return nullptr; const int dst_bpl = image_width * req_comps; - uint8_t *pImage_data = (uint8_t*)malloc(dst_bpl * image_height); + uint8_t *pImage_data = tvg::malloc(dst_bpl * image_height); if (!pImage_data) return nullptr; for (int y = 0; y < image_height; y++) { const uint8_t* pScan_line; uint32_t scan_line_len; if (decoder->decode((const void**)&pScan_line, &scan_line_len) != JPGD_SUCCESS) { - free(pImage_data); + tvg::free(pImage_data); return nullptr; } diff --git a/src/loaders/lottie/rapidjson/rapidjson.h b/src/loaders/lottie/rapidjson/rapidjson.h index 00bdc3f6..60d8b10d 100644 --- a/src/loaders/lottie/rapidjson/rapidjson.h +++ b/src/loaders/lottie/rapidjson/rapidjson.h @@ -36,8 +36,7 @@ different translation units of a single application. */ -#include // malloc(), realloc(), free(), size_t -#include // memset(), memcpy(), memmove(), memcmp() +#include "tvgCommon.h" /////////////////////////////////////////////////////////////////////////////// // RAPIDJSON_VERSION_STRING @@ -694,15 +693,15 @@ RAPIDJSON_NAMESPACE_END #ifndef RAPIDJSON_MALLOC ///! customization point for global \c malloc -#define RAPIDJSON_MALLOC(size) std::malloc(size) +#define RAPIDJSON_MALLOC(size) tvg::malloc(size) #endif #ifndef RAPIDJSON_REALLOC ///! customization point for global \c realloc -#define RAPIDJSON_REALLOC(ptr, new_size) std::realloc(ptr, new_size) +#define RAPIDJSON_REALLOC(ptr, new_size) tvg::realloc(ptr, new_size) #endif #ifndef RAPIDJSON_FREE ///! customization point for global \c free -#define RAPIDJSON_FREE(ptr) std::free(ptr) +#define RAPIDJSON_FREE(ptr) tvg::free(ptr) #endif /////////////////////////////////////////////////////////////////////////////// diff --git a/src/loaders/lottie/tvgLottieBuilder.cpp b/src/loaders/lottie/tvgLottieBuilder.cpp index 0d3f14ae..519eb923 100644 --- a/src/loaders/lottie/tvgLottieBuilder.cpp +++ b/src/loaders/lottie/tvgLottieBuilder.cpp @@ -20,16 +20,7 @@ * SOFTWARE. */ -#include #include -#ifdef _WIN32 - #include -#elif defined(__linux__) || defined(__ZEPHYR__) - #include -#else - #include -#endif - #include "tvgCommon.h" #include "tvgMath.h" #include "tvgLottieModel.h" @@ -177,7 +168,7 @@ void LottieBuilder::updateTransform(LottieGroup* parent, LottieObject** child, f uint8_t opacity; if (parent->mergeable()) { - if (!ctx->transform) ctx->transform = (Matrix*)malloc(sizeof(Matrix)); + if (!ctx->transform) ctx->transform = tvg::malloc(sizeof(Matrix)); _updateTransform(transform, frameNo, *ctx->transform, opacity, false, tween, exps); return; } diff --git a/src/loaders/lottie/tvgLottieBuilder.h b/src/loaders/lottie/tvgLottieBuilder.h index 27c3911f..03a5caae 100644 --- a/src/loaders/lottie/tvgLottieBuilder.h +++ b/src/loaders/lottie/tvgLottieBuilder.h @@ -71,7 +71,7 @@ struct RenderContext ~RenderContext() { propagator->unref(false); - free(transform); + tvg::free(transform); delete(roundness); delete(offset); } diff --git a/src/loaders/lottie/tvgLottieExpressions.cpp b/src/loaders/lottie/tvgLottieExpressions.cpp index bf47c468..c83961c1 100644 --- a/src/loaders/lottie/tvgLottieExpressions.cpp +++ b/src/loaders/lottie/tvgLottieExpressions.cpp @@ -60,7 +60,7 @@ static LottieExpressions* exps = nullptr; //singleton instance engine static ExpContent* _expcontent(LottieExpression* exp, float frameNo, LottieObject* obj) { - auto data = (ExpContent*)malloc(sizeof(ExpContent)); + auto data = tvg::malloc(sizeof(ExpContent)); data->exp = exp; data->frameNo = frameNo; data->obj = obj; @@ -70,7 +70,7 @@ static ExpContent* _expcontent(LottieExpression* exp, float frameNo, LottieObjec static void contentFree(void *native_p, struct jerry_object_native_info_t *info_p) { - free(native_p); + tvg::free(native_p); } static jerry_object_native_info_t freeCb {contentFree, 0, 0}; @@ -81,7 +81,7 @@ static char* _name(jerry_value_t args) { auto arg0 = jerry_value_to_string(args); auto len = jerry_string_length(arg0); - auto name = (jerry_char_t*)malloc(len * sizeof(jerry_char_t) + 1); + auto name = tvg::malloc(len * sizeof(jerry_char_t) + 1); jerry_string_to_buffer(arg0, JERRY_ENCODING_UTF8, name, len); name[len] = '\0'; jerry_value_free(arg0); @@ -93,7 +93,7 @@ static unsigned long _idByName(jerry_value_t args) { auto name = _name(args); auto id = djb2Encode(name); - free(name); + tvg::free(name); return id; } @@ -831,7 +831,7 @@ static bool _loopOutCommon(LottieExpression* exp, const jerry_value_t args[], co else if (!strcmp(name, EXP_PINGPONG)) exp->loop.mode = LottieExpression::LoopMode::OutPingPong; else if (!strcmp(name, EXP_OFFSET)) exp->loop.mode = LottieExpression::LoopMode::OutOffset; else if (!strcmp(name, EXP_CONTINUE)) exp->loop.mode = LottieExpression::LoopMode::OutContinue; - free(name); + tvg::free(name); } if (exp->loop.mode != LottieExpression::LoopMode::OutCycle && exp->loop.mode != LottieExpression::LoopMode::OutPingPong) { @@ -883,7 +883,7 @@ static bool _loopInCommon(LottieExpression* exp, const jerry_value_t args[], con else if (!strcmp(name, EXP_PINGPONG)) exp->loop.mode = LottieExpression::LoopMode::InPingPong; else if (!strcmp(name, EXP_OFFSET)) exp->loop.mode = LottieExpression::LoopMode::InOffset; else if (!strcmp(name, EXP_CONTINUE)) exp->loop.mode = LottieExpression::LoopMode::InContinue; - free(name); + tvg::free(name); } if (exp->loop.mode != LottieExpression::LoopMode::InCycle && exp->loop.mode != LottieExpression::LoopMode::InPingPong) { diff --git a/src/loaders/lottie/tvgLottieLoader.cpp b/src/loaders/lottie/tvgLottieLoader.cpp index 56f8bf96..01a878b5 100644 --- a/src/loaders/lottie/tvgLottieLoader.cpp +++ b/src/loaders/lottie/tvgLottieLoader.cpp @@ -58,7 +58,7 @@ void LottieLoader::run(unsigned tid) void LottieLoader::release() { if (copy) { - free((char*)content); + tvg::free((char*)content); content = nullptr; } } @@ -84,7 +84,7 @@ LottieLoader::~LottieLoader() delete(comp); delete(builder); - free(dirName); + tvg::free(dirName); } @@ -200,7 +200,7 @@ bool LottieLoader::header() bool LottieLoader::open(const char* data, uint32_t size, const char* rpath, bool copy) { if (copy) { - content = (char*)malloc(size + 1); + content = tvg::malloc(size + 1); if (!content) return false; memcpy((char*)content, data, size); const_cast(content)[size] = '\0'; @@ -230,7 +230,7 @@ bool LottieLoader::open(const char* path) return false; } - auto content = (char*)(malloc(sizeof(char) * size + 1)); + auto content = tvg::malloc(sizeof(char) * size + 1); fseek(f, 0, SEEK_SET); size = fread(content, sizeof(char), size, f); content[size] = '\0'; @@ -313,7 +313,7 @@ bool LottieLoader::override(const char* slots, bool byDefault) if (!applied) parser.skip(); ++idx; } - free((char*)temp); + tvg::free((char*)temp); rebuild = succeed; overridden |= succeed; return rebuild; diff --git a/src/loaders/lottie/tvgLottieModel.cpp b/src/loaders/lottie/tvgLottieModel.cpp index 1bb50c8b..f4e7c666 100644 --- a/src/loaders/lottie/tvgLottieModel.cpp +++ b/src/loaders/lottie/tvgLottieModel.cpp @@ -493,7 +493,7 @@ LottieLayer::~LottieLayer() ARRAY_FOREACH(p, effects) delete(*p); delete(transform); - free(name); + tvg::free(name); } @@ -543,12 +543,12 @@ LottieComposition::~LottieComposition() if (!initiated && root) delete(root->scene); delete(root); - free(version); - free(name); + tvg::free(version); + tvg::free(name); ARRAY_FOREACH(p, interpolators) { - free((*p)->key); - free(*p); + tvg::free((*p)->key); + tvg::free(*p); } ARRAY_FOREACH(p, assets) delete(*p); diff --git a/src/loaders/lottie/tvgLottieModel.h b/src/loaders/lottie/tvgLottieModel.h index f716f8e8..e8837aac 100644 --- a/src/loaders/lottie/tvgLottieModel.h +++ b/src/loaders/lottie/tvgLottieModel.h @@ -245,7 +245,7 @@ struct LottieGlyph ~LottieGlyph() { ARRAY_FOREACH(p, children) delete(*p); - free(code); + tvg::free(code); } }; @@ -258,7 +258,7 @@ struct LottieTextRange ~LottieTextRange() { - free(interpolator); + tvg::free(interpolator); } struct { @@ -300,10 +300,10 @@ struct LottieFont ~LottieFont() { ARRAY_FOREACH(p, chars) delete(*p); - free(style); - free(family); - free(name); - free(data.b64src); + tvg::free(style); + tvg::free(family); + tvg::free(name); + tvg::free(data.b64src); } struct { @@ -330,7 +330,7 @@ struct LottieMarker ~LottieMarker() { - free(name); + tvg::free(name); } }; @@ -925,7 +925,7 @@ struct LottieSlot ~LottieSlot() { - free(sid); + tvg::free(sid); if (!overridden) return; ARRAY_FOREACH(pair, pairs) delete(pair->prop); } diff --git a/src/loaders/lottie/tvgLottieParser.cpp b/src/loaders/lottie/tvgLottieParser.cpp index bcd4c355..6e5f5380 100644 --- a/src/loaders/lottie/tvgLottieParser.cpp +++ b/src/loaders/lottie/tvgLottieParser.cpp @@ -383,7 +383,7 @@ LottieInterpolator* LottieParser::getInterpolator(const char* key, Point& in, Po //new interpolator if (!interpolator) { - interpolator = static_cast(malloc(sizeof(LottieInterpolator))); + interpolator = tvg::malloc(sizeof(LottieInterpolator)); interpolator->set(key, in, out); comp->interpolators.push(interpolator); } @@ -901,7 +901,7 @@ void LottieParser::parseImage(LottieImage* image, const char* data, const char* //external image resource } else { auto len = strlen(dirName) + strlen(subPath) + strlen(data) + 2; - image->data.path = static_cast(malloc(len)); + image->data.path = tvg::malloc(len); snprintf(image->data.path, len, "%s/%s%s", dirName, subPath, data); } @@ -1135,7 +1135,7 @@ void LottieParser::parseTextRange(LottieText* text) if (KEY_AS("t")) selector->expressible = (bool) getInt(); else if (KEY_AS("xe")) { parseProperty(selector->maxEase); - selector->interpolator = static_cast(malloc(sizeof(LottieInterpolator))); + selector->interpolator = tvg::malloc(sizeof(LottieInterpolator)); } else if (KEY_AS("ne")) parseProperty(selector->minEase); else if (KEY_AS("a")) parseProperty(selector->maxAmount); @@ -1467,8 +1467,8 @@ void LottieParser::postProcess(Array& glyphs) auto& font = comp->fonts[i]; if (!strcmp(font->family, glyph->family) && !strcmp(font->style, glyph->style)) { font->chars.push(glyph); - free(glyph->family); - free(glyph->style); + tvg::free(glyph->family); + tvg::free(glyph->style); break; } } @@ -1570,7 +1570,7 @@ bool LottieParser::apply(LottieSlot* slot, bool byDefault) void LottieParser::captureSlots(const char* key) { - free(slots); + tvg::free(slots); // TODO: Replace with immediate parsing, once the slot spec is confirmed by the LAC @@ -1600,7 +1600,7 @@ void LottieParser::captureSlots(const char* key) //composite '{' + slots + '}' auto len = (end - begin + 2); - slots = (char*)malloc(sizeof(char) * len + 1); + slots = tvg::malloc(sizeof(char) * len + 1); slots[0] = '{'; memcpy(slots + 1, begin, len); slots[len] = '\0'; diff --git a/src/loaders/lottie/tvgLottieProperty.h b/src/loaders/lottie/tvgLottieProperty.h index 4f1a98c2..1c49ff7b 100644 --- a/src/loaders/lottie/tvgLottieProperty.h +++ b/src/loaders/lottie/tvgLottieProperty.h @@ -158,7 +158,7 @@ struct LottieExpression ~LottieExpression() { - free(code); + tvg::free(code); } }; @@ -415,17 +415,17 @@ struct LottiePathSet : LottieProperty exp = nullptr; } - free(value.cmds); - free(value.pts); + tvg::free(value.cmds); + tvg::free(value.pts); if (!frames) return; ARRAY_FOREACH(p, *frames) { - free((*p).value.cmds); - free((*p).value.pts); + tvg::free((*p).value.cmds); + tvg::free((*p).value.pts); } - free(frames->data); - free(frames); + tvg::free(frames->data); + tvg::free(frames); } uint32_t nearest(float frameNo) override @@ -446,7 +446,7 @@ struct LottiePathSet : LottieProperty LottieScalarFrame& newFrame() { if (!frames) { - frames = static_cast>*>(calloc(1, sizeof(Array>))); + frames = tvg::calloc>*>(1, sizeof(Array>)); } if (frames->count + 1 >= frames->reserved) { auto old = frames->reserved; @@ -500,7 +500,7 @@ struct LottiePathSet : LottieProperty //interpolate 2 frames auto s = frame->value.pts; auto e = (frame + 1)->value.pts; - auto interpPts = (Point*)malloc(frame->value.ptsCnt * sizeof(Point)); + auto interpPts = tvg::malloc(frame->value.ptsCnt * sizeof(Point)); auto p = interpPts; for (auto i = 0; i < frame->value.ptsCnt; ++i, ++s, ++e, ++p) { @@ -510,7 +510,7 @@ struct LottiePathSet : LottieProperty if (modifier) modifier->modifyPath(frame->value.cmds, frame->value.cmdsCnt, interpPts, frame->value.ptsCnt, nullptr, out); - free(interpPts); + tvg::free(interpPts); return true; } @@ -610,17 +610,17 @@ struct LottieColorStop : LottieProperty } if (value.data) { - free(value.data); + tvg::free(value.data); value.data = nullptr; } if (!frames) return; ARRAY_FOREACH(p, *frames) { - free((*p).value.data); + tvg::free((*p).value.data); } - free(frames->data); - free(frames); + tvg::free(frames->data); + tvg::free(frames); frames = nullptr; } @@ -642,7 +642,7 @@ struct LottieColorStop : LottieProperty LottieScalarFrame& newFrame() { if (!frames) { - frames = static_cast>*>(calloc(1, sizeof(Array>))); + frames = tvg::calloc>*>(1, sizeof(Array>)); } if (frames->count + 1 >= frames->reserved) { auto old = frames->reserved; @@ -789,19 +789,19 @@ struct LottieTextDoc : LottieProperty } if (value.text) { - free(value.text); + tvg::free(value.text); value.text = nullptr; } if (value.name) { - free(value.name); + tvg::free(value.name); value.name = nullptr; } if (!frames) return; ARRAY_FOREACH(p, *frames) { - free((*p).value.text); - free((*p).value.name); + tvg::free((*p).value.text); + tvg::free((*p).value.name); } delete(frames); frames = nullptr; @@ -900,8 +900,8 @@ struct LottieBitmap : LottieProperty void release() { - free(b64Data); - free(mimeType); + tvg::free(b64Data); + tvg::free(mimeType); b64Data = nullptr; mimeType = nullptr; diff --git a/src/loaders/png/tvgLodePng.cpp b/src/loaders/png/tvgLodePng.cpp index 2946fddc..4203b858 100644 --- a/src/loaders/png/tvgLodePng.cpp +++ b/src/loaders/png/tvgLodePng.cpp @@ -44,7 +44,7 @@ 3. This notice may not be removed or altered from any sourcedistribution. */ -#include +#include "tvgCommon.h" #include "tvgLodePng.h" @@ -184,7 +184,7 @@ static unsigned ucvector_resize(ucvector* p, size_t size) { if (size > p->allocsize) { size_t newsize = size + (p->allocsize >> 1u); - void* data = realloc(p->data, newsize); + auto data = tvg::realloc(p->data, newsize); if(data) { p->allocsize = newsize; p->data = (unsigned char*)data; @@ -456,10 +456,10 @@ static void HuffmanTree_init(HuffmanTree* tree) static void HuffmanTree_cleanup(HuffmanTree* tree) { - free(tree->codes); - free(tree->lengths); - free(tree->table_len); - free(tree->table_value); + tvg::free(tree->codes); + tvg::free(tree->lengths); + tvg::free(tree->table_len); + tvg::free(tree->table_value); } @@ -477,7 +477,7 @@ static unsigned HuffmanTree_makeTable(HuffmanTree* tree) static const unsigned headsize = 1u << FIRSTBITS; /*size of the first table*/ static const unsigned mask = (1u << FIRSTBITS) /*headsize*/ - 1u; size_t i, numpresent, pointer, size; /*total table size*/ - unsigned* maxlens = (unsigned*)malloc(headsize * sizeof(unsigned)); + unsigned* maxlens = tvg::malloc(headsize * sizeof(unsigned)); if (!maxlens) return 83; /*alloc fail*/ /* compute maxlens: max total bit length of symbols sharing prefix in the first table*/ @@ -497,10 +497,10 @@ static unsigned HuffmanTree_makeTable(HuffmanTree* tree) unsigned l = maxlens[i]; if (l > FIRSTBITS) size += (1u << (l - FIRSTBITS)); } - tree->table_len = (unsigned char*)malloc(size * sizeof(*tree->table_len)); - tree->table_value = (unsigned short*)malloc(size * sizeof(*tree->table_value)); + tree->table_len = tvg::malloc(size * sizeof(*tree->table_len)); + tree->table_value = tvg::malloc(size * sizeof(*tree->table_value)); if (!tree->table_len || !tree->table_value) { - free(maxlens); + tvg::free(maxlens); /* freeing tree->table values is done at a higher scope */ return 83; /*alloc fail*/ } @@ -516,7 +516,7 @@ static unsigned HuffmanTree_makeTable(HuffmanTree* tree) tree->table_value[i] = pointer; pointer += (1u << (l - FIRSTBITS)); } - free(maxlens); + tvg::free(maxlens); /*fill in the first table for short symbols, or secondary table for long symbols*/ numpresent = 0; @@ -600,9 +600,9 @@ static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree) unsigned error = 0; unsigned bits, n; - tree->codes = (unsigned*)malloc(tree->numcodes * sizeof(unsigned)); - blcount = (unsigned*)malloc((tree->maxbitlen + 1) * sizeof(unsigned)); - nextcode = (unsigned*)malloc((tree->maxbitlen + 1) * sizeof(unsigned)); + tree->codes = tvg::malloc(tree->numcodes * sizeof(unsigned)); + blcount = tvg::malloc((tree->maxbitlen + 1) * sizeof(unsigned)); + nextcode = tvg::malloc((tree->maxbitlen + 1) * sizeof(unsigned)); if (!tree->codes || !blcount || !nextcode) error = 83; /*alloc fail*/ if (!error) { @@ -623,8 +623,8 @@ static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree) } } - free(blcount); - free(nextcode); + tvg::free(blcount); + tvg::free(nextcode); if (!error) error = HuffmanTree_makeTable(tree); return error; @@ -639,7 +639,7 @@ static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree) static unsigned HuffmanTree_makeFromLengths(HuffmanTree* tree, const unsigned* bitlen, size_t numcodes, unsigned maxbitlen) { unsigned i; - tree->lengths = (unsigned*)malloc(numcodes * sizeof(unsigned)); + tree->lengths = tvg::malloc(numcodes * sizeof(unsigned)); if (!tree->lengths) return 83; /*alloc fail*/ for (i = 0; i != numcodes; ++i) tree->lengths[i] = bitlen[i]; tree->numcodes = (unsigned)numcodes; /*number of symbols*/ @@ -652,7 +652,7 @@ static unsigned HuffmanTree_makeFromLengths(HuffmanTree* tree, const unsigned* b static unsigned generateFixedLitLenTree(HuffmanTree* tree) { unsigned i, error = 0; - unsigned* bitlen = (unsigned*)malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned)); + unsigned* bitlen = tvg::malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned)); if (!bitlen) return 83; /*alloc fail*/ /*288 possible codes: 0-255=literals, 256=endcode, 257-285=lengthcodes, 286-287=unused*/ @@ -663,7 +663,7 @@ static unsigned generateFixedLitLenTree(HuffmanTree* tree) error = HuffmanTree_makeFromLengths(tree, bitlen, NUM_DEFLATE_CODE_SYMBOLS, 15); - free(bitlen); + tvg::free(bitlen); return error; } @@ -672,14 +672,14 @@ static unsigned generateFixedLitLenTree(HuffmanTree* tree) static unsigned generateFixedDistanceTree(HuffmanTree* tree) { unsigned i, error = 0; - unsigned* bitlen = (unsigned*)malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned)); + unsigned* bitlen = tvg::malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned)); if (!bitlen) return 83; /*alloc fail*/ /*there are 32 distance codes, but 30-31 are unused*/ for (i = 0; i != NUM_DISTANCE_SYMBOLS; ++i) bitlen[i] = 5; error = HuffmanTree_makeFromLengths(tree, bitlen, NUM_DISTANCE_SYMBOLS, 15); - free(bitlen); + tvg::free(bitlen); return error; } @@ -742,7 +742,7 @@ static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d, /*number of code length codes. Unlike the spec, the value 4 is added to it here already*/ HCLEN = readBits(reader, 4) + 4; - bitlen_cl = (unsigned*)malloc(NUM_CODE_LENGTH_CODES * sizeof(unsigned)); + bitlen_cl = tvg::malloc(NUM_CODE_LENGTH_CODES * sizeof(unsigned)); if(!bitlen_cl) return 83 /*alloc fail*/; HuffmanTree_init(&tree_cl); @@ -764,8 +764,8 @@ static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d, if(error) break; /*now we can use this tree to read the lengths for the tree that this function will return*/ - bitlen_ll = (unsigned*)malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned)); - bitlen_d = (unsigned*)malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned)); + bitlen_ll = tvg::malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned)); + bitlen_d = tvg::malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned)); if (!bitlen_ll || !bitlen_d) ERROR_BREAK(83 /*alloc fail*/); lodepng_memset(bitlen_ll, 0, NUM_DEFLATE_CODE_SYMBOLS * sizeof(*bitlen_ll)); lodepng_memset(bitlen_d, 0, NUM_DISTANCE_SYMBOLS * sizeof(*bitlen_d)); @@ -844,9 +844,9 @@ static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d, break; /*end of error-while*/ } - free(bitlen_cl); - free(bitlen_ll); - free(bitlen_d); + tvg::free(bitlen_cl); + tvg::free(bitlen_ll); + tvg::free(bitlen_d); HuffmanTree_cleanup(&tree_cl); return error; @@ -1366,7 +1366,7 @@ static void lodepng_color_mode_alloc_palette(LodePNGColorMode* info) size_t i; /*if the palette is already allocated, it will have size 1024 so no reallocation needed in that case*/ /*the palette must have room for up to 256 colors with 4 bytes each.*/ - if (!info->palette) info->palette = (unsigned char*)malloc(1024); + if (!info->palette) info->palette = tvg::malloc(1024); if (!info->palette) return; /*alloc fail*/ for (i = 0; i != 256; ++i) { /*Initialize all unused colors with black, the value used for invalid palette indices. @@ -1381,7 +1381,7 @@ static void lodepng_color_mode_alloc_palette(LodePNGColorMode* info) static void lodepng_palette_clear(LodePNGColorMode* info) { - if (info->palette) free(info->palette); + if (info->palette) tvg::free(info->palette); info->palette = 0; info->palettesize = 0; } @@ -1399,7 +1399,7 @@ static unsigned lodepng_color_mode_copy(LodePNGColorMode* dest, const LodePNGCol lodepng_color_mode_cleanup(dest); lodepng_memcpy(dest, source, sizeof(LodePNGColorMode)); if (source->palette) { - dest->palette = (unsigned char*)malloc(1024); + dest->palette = tvg::malloc(1024); if (!dest->palette && source->palettesize) return 83; /*alloc fail*/ lodepng_memcpy(dest->palette, source->palette, source->palettesize * 4); } @@ -1531,7 +1531,7 @@ static void color_tree_cleanup(ColorTree* tree) for (i = 0; i != 16; ++i) { if(tree->children[i]) { color_tree_cleanup(tree->children[i]); - free(tree->children[i]); + tvg::free(tree->children[i]); } } } @@ -1559,7 +1559,7 @@ static unsigned color_tree_add(ColorTree* tree, unsigned char r, unsigned char g for (bit = 0; bit < 8; ++bit) { int i = 8 * ((r >> bit) & 1) + 4 * ((g >> bit) & 1) + 2 * ((b >> bit) & 1) + 1 * ((a >> bit) & 1); if (!tree->children[i]) { - tree->children[i] = (ColorTree*)malloc(sizeof(ColorTree)); + tree->children[i] = tvg::malloc(sizeof(ColorTree)); if (!tree->children[i]) return 83; /*alloc fail*/ color_tree_init(tree->children[i]); } @@ -2386,7 +2386,7 @@ static void decodeGeneric(unsigned char** out, unsigned* w, unsigned* h, LodePNG } /*the input filesize is a safe upper bound for the sum of idat chunks size*/ - idat = (unsigned char*)malloc(insize); + idat = tvg::malloc(insize); if (!idat) CERROR_RETURN(state->error, 83); /*alloc fail*/ chunk = &in[33]; /*first byte of the first chunk after the header*/ @@ -2482,18 +2482,18 @@ static void decodeGeneric(unsigned char** out, unsigned* w, unsigned* h, LodePNG } if (!state->error && scanlines_size != expected_size) state->error = 91; /*decompressed size doesn't match prediction*/ - free(idat); + tvg::free(idat); if (!state->error) { outsize = lodepng_get_raw_size(*w, *h, &state->info_png.color); - *out = (unsigned char*)malloc(outsize); + *out = tvg::malloc(outsize); if (!*out) state->error = 83; /*alloc fail*/ } if (!state->error) { lodepng_memset(*out, 0, outsize); state->error = postProcessScanlines(*out, scanlines, *w, *h, &state->info_png); } - free(scanlines); + tvg::free(scanlines); } @@ -2601,12 +2601,12 @@ unsigned lodepng_decode(unsigned char** out, unsigned* w, unsigned* h, LodePNGSt } outsize = lodepng_get_raw_size(*w, *h, &state->info_raw); - *out = (unsigned char*)malloc(outsize); + *out = tvg::malloc(outsize); if (!(*out)) { state->error = 83; /*alloc fail*/ } else state->error = lodepng_convert(*out, data, &state->info_raw, &state->info_png.color, *w, *h); - free(data); + tvg::free(data); } return state->error; } diff --git a/src/loaders/png/tvgPngLoader.cpp b/src/loaders/png/tvgPngLoader.cpp index f0a8af38..45b04b93 100644 --- a/src/loaders/png/tvgPngLoader.cpp +++ b/src/loaders/png/tvgPngLoader.cpp @@ -61,8 +61,8 @@ PngLoader::PngLoader() : ImageLoader(FileType::Png) PngLoader::~PngLoader() { - if (freeData) free(data); - free(surface.buf8); + if (freeData) tvg::free(data); + tvg::free(surface.buf8); lodepng_state_cleanup(&state); } @@ -80,7 +80,7 @@ bool PngLoader::open(const char* path) if (((size = ftell(pngFile)) < 1)) goto finalize; if (fseek(pngFile, 0, SEEK_SET)) goto finalize; - data = (unsigned char *) malloc(size); + data = tvg::malloc(size); if (!data) goto finalize; freeData = true; @@ -114,7 +114,7 @@ bool PngLoader::open(const char* data, uint32_t size, TVG_UNUSED const char* rpa if (lodepng_inspect(&width, &height, &state, (unsigned char*)(data), size) > 0) return false; if (copy) { - this->data = (unsigned char *) malloc(size); + this->data = tvg::malloc(size); if (!this->data) return false; memcpy((unsigned char *)this->data, data, size); freeData = true; diff --git a/src/loaders/raw/tvgRawLoader.cpp b/src/loaders/raw/tvgRawLoader.cpp index 73bc0d2c..6eef8efe 100644 --- a/src/loaders/raw/tvgRawLoader.cpp +++ b/src/loaders/raw/tvgRawLoader.cpp @@ -21,7 +21,6 @@ */ #include -#include #include "tvgLoader.h" #include "tvgRawLoader.h" @@ -33,7 +32,7 @@ RawLoader::RawLoader() : ImageLoader(FileType::Raw) RawLoader::~RawLoader() { - if (copy) free(surface.buf32); + if (copy) tvg::free(surface.buf32); } @@ -48,7 +47,7 @@ bool RawLoader::open(const uint32_t* data, uint32_t w, uint32_t h, ColorSpace cs this->copy = copy; if (copy) { - surface.buf32 = (uint32_t*)malloc(sizeof(uint32_t) * w * h); + surface.buf32 = tvg::malloc(sizeof(uint32_t) * w * h); if (!surface.buf32) return false; memcpy((void*)surface.buf32, data, sizeof(uint32_t) * w * h); } diff --git a/src/loaders/svg/tvgSvgCssStyle.cpp b/src/loaders/svg/tvgSvgCssStyle.cpp index be75fa22..590fcd68 100644 --- a/src/loaders/svg/tvgSvgCssStyle.cpp +++ b/src/loaders/svg/tvgSvgCssStyle.cpp @@ -22,8 +22,6 @@ #include "tvgSvgCssStyle.h" -#include - /************************************************************************/ /* Internal Class Implementation */ /************************************************************************/ @@ -72,7 +70,7 @@ static void _copyStyle(SvgStyleProperty* to, const SvgStyleProperty* from) to->fill.paint.none = from->fill.paint.none; to->fill.paint.curColor = from->fill.paint.curColor; if (from->fill.paint.url) { - if (to->fill.paint.url) free(to->fill.paint.url); + if (to->fill.paint.url) tvg::free(to->fill.paint.url); to->fill.paint.url = strdup(from->fill.paint.url); } to->fill.flags = (to->fill.flags | SvgFillFlags::Paint); @@ -106,7 +104,7 @@ static void _copyStyle(SvgStyleProperty* to, const SvgStyleProperty* from) to->stroke.paint.none = from->stroke.paint.none; to->stroke.paint.curColor = from->stroke.paint.curColor; if (from->stroke.paint.url) { - if (to->stroke.paint.url) free(to->stroke.paint.url); + if (to->stroke.paint.url) tvg::free(to->stroke.paint.url); to->stroke.paint.url = strdup(from->stroke.paint.url); } to->stroke.flags = (to->stroke.flags | SvgStrokeFlags::Paint); @@ -187,7 +185,7 @@ void cssCopyStyleAttr(SvgNode* to, const SvgNode* from) { //Copy matrix attribute if (from->transform && !(to->style->flags & SvgStyleFlags::Transform)) { - to->transform = (Matrix*)malloc(sizeof(Matrix)); + to->transform = tvg::malloc(sizeof(Matrix)); if (to->transform) { *to->transform = *from->transform; to->style->flags = (to->style->flags | SvgStyleFlags::Transform); @@ -197,11 +195,11 @@ void cssCopyStyleAttr(SvgNode* to, const SvgNode* from) _copyStyle(to->style, from->style); if (from->style->clipPath.url) { - if (to->style->clipPath.url) free(to->style->clipPath.url); + if (to->style->clipPath.url) tvg::free(to->style->clipPath.url); to->style->clipPath.url = strdup(from->style->clipPath.url); } if (from->style->mask.url) { - if (to->style->mask.url) free(to->style->mask.url); + if (to->style->mask.url) tvg::free(to->style->mask.url); to->style->mask.url = strdup(from->style->mask.url); } } diff --git a/src/loaders/svg/tvgSvgLoader.cpp b/src/loaders/svg/tvgSvgLoader.cpp index 5548425b..42f11885 100644 --- a/src/loaders/svg/tvgSvgLoader.cpp +++ b/src/loaders/svg/tvgSvgLoader.cpp @@ -683,7 +683,7 @@ static bool _toColor(const char* str, uint8_t* r, uint8_t* g, uint8_t* b, char** } return true; } else if (ref && len >= 3 && !strncmp(str, "url", 3)) { - free(*ref); + tvg::free(*ref); *ref = _idFromUrl((const char*)(str + 3)); return true; } else if (len >= 10 && (str[0] == 'h' || str[0] == 'H') && (str[1] == 's' || str[1] == 'S') && (str[2] == 'l' || str[2] == 'L') && str[3] == '(' && str[len - 1] == ')') { @@ -783,7 +783,7 @@ static Matrix* _parseTransformationMatrix(const char* value) { const int POINT_CNT = 8; - auto matrix = (Matrix*)malloc(sizeof(Matrix)); + auto matrix = tvg::malloc(sizeof(Matrix)); identity(matrix); float points[POINT_CNT]; @@ -866,7 +866,7 @@ static Matrix* _parseTransformationMatrix(const char* value) } return matrix; error: - free(matrix); + tvg::free(matrix); return nullptr; } @@ -1073,7 +1073,7 @@ static void _handleClipPathAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, SvgStyleProperty* style = node->style; int len = strlen(value); if (len >= 3 && !strncmp(value, "url", 3)) { - free(style->clipPath.url); + tvg::free(style->clipPath.url); style->clipPath.url = _idFromUrl((const char*)(value + 3)); } } @@ -1084,7 +1084,7 @@ static void _handleMaskAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, con SvgStyleProperty* style = node->style; int len = strlen(value); if (len >= 3 && !strncmp(value, "url", 3)) { - free(style->mask.url); + tvg::free(style->mask.url); style->mask.url = _idFromUrl((const char*)(value + 3)); } } @@ -1095,7 +1095,7 @@ static void _handleFilterAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, c SvgStyleProperty* style = node->style; int len = strlen(value); if (len >= 3 && !strncmp(value, "url", 3)) { - if (style->filter.url) free(style->filter.url); + if (style->filter.url) tvg::free(style->filter.url); style->filter.url = _idFromUrl((const char*)(value + 3)); } } @@ -1130,7 +1130,7 @@ static void _handleCssClassAttr(SvgLoaderData* loader, SvgNode* node, const char { auto cssClass = &node->style->cssClass; - if (value) free(*cssClass); + if (value) tvg::free(*cssClass); *cssClass = _copyId(value); bool cssClassFound = false; @@ -1217,7 +1217,7 @@ static bool _parseStyleAttr(void* data, const char* key, const char* value, bool } if (importance) { node->style->flagsImportance = (node->style->flags | styleTags[i].flag); - free(const_cast(value)); + tvg::free(const_cast(value)); } return true; } @@ -1246,7 +1246,7 @@ static bool _attrParseGNode(void* data, const char* key, const char* value) } else if (STR_AS(key, "transform")) { node->transform = _parseTransformationMatrix(value); } else if (STR_AS(key, "id")) { - if (value) free(node->id); + if (value) tvg::free(node->id); node->id = _copyId(value); } else if (STR_AS(key, "class")) { _handleCssClassAttr(loader, node, value); @@ -1277,7 +1277,7 @@ static bool _attrParseClipPathNode(void* data, const char* key, const char* valu } else if (STR_AS(key, "transform")) { node->transform = _parseTransformationMatrix(value); } else if (STR_AS(key, "id")) { - if (value) free(node->id); + if (value) tvg::free(node->id); node->id = _copyId(value); } else if (STR_AS(key, "class")) { _handleCssClassAttr(loader, node, value); @@ -1301,7 +1301,7 @@ static bool _attrParseMaskNode(void* data, const char* key, const char* value) } else if (STR_AS(key, "transform")) { node->transform = _parseTransformationMatrix(value); } else if (STR_AS(key, "id")) { - if (value) free(node->id); + if (value) tvg::free(node->id); node->id = _copyId(value); } else if (STR_AS(key, "class")) { _handleCssClassAttr(loader, node, value); @@ -1322,7 +1322,7 @@ static bool _attrParseCssStyleNode(void* data, const char* key, const char* valu SvgNode* node = loader->svgParse->node; if (STR_AS(key, "id")) { - if (value) free(node->id); + if (value) tvg::free(node->id); node->id = _copyId(value); } else { return _parseStyleAttr(loader, key, value, false); @@ -1407,7 +1407,7 @@ static bool _attrParseFilterNode(void* data, const char* key, const char* value) _parseBox(key, value, &filter->box, filter->isPercentage); if (STR_AS(key, "id")) { - if (node->id && value) free(node->id); + if (node->id && value) tvg::free(node->id); node->id = _copyId(value); } else if (STR_AS(key, "primitiveUnits")) { if (STR_AS(value, "objectBoundingBox")) filter->primitiveUserSpace = false; @@ -1448,7 +1448,7 @@ static bool _attrParseGaussianBlurNode(void* data, const char* key, const char* if (_parseBox(key, value, &gaussianBlur->box, gaussianBlur->isPercentage)) gaussianBlur->hasBox = true; if (STR_AS(key, "id")) { - if (node->id && value) free(node->id); + if (node->id && value) tvg::free(node->id); node->id = _copyId(value); } else if (STR_AS(key, "stdDeviation")) { _parseGaussianBlurStdDeviation(&value, &gaussianBlur->stdDevX, &gaussianBlur->stdDevY); @@ -1463,15 +1463,15 @@ static bool _attrParseGaussianBlurNode(void* data, const char* key, const char* static SvgNode* _createNode(SvgNode* parent, SvgNodeType type) { - SvgNode* node = (SvgNode*)calloc(1, sizeof(SvgNode)); + SvgNode* node = tvg::calloc(1, sizeof(SvgNode)); if (!node) return nullptr; //Default fill property - node->style = (SvgStyleProperty*)calloc(1, sizeof(SvgStyleProperty)); + node->style = tvg::calloc(1, sizeof(SvgStyleProperty)); if (!node->style) { - free(node); + tvg::free(node); return nullptr; } @@ -1640,7 +1640,7 @@ static bool _attrParsePathNode(void* data, const char* key, const char* value) SvgPathNode* path = &(node->node.path); if (STR_AS(key, "d")) { - free(path->path); + tvg::free(path->path); //Temporary: need to copy path->path = _copyId(value); } else if (STR_AS(key, "style")) { @@ -1652,7 +1652,7 @@ static bool _attrParsePathNode(void* data, const char* key, const char* value) } else if (STR_AS(key, "filter")) { _handleFilterAttr(loader, node, value); } else if (STR_AS(key, "id")) { - if (value) free(node->id); + if (value) tvg::free(node->id); node->id = _copyId(value); } else if (STR_AS(key, "class")) { _handleCssClassAttr(loader, node, value); @@ -1716,7 +1716,7 @@ static bool _attrParseCircleNode(void* data, const char* key, const char* value) } else if (STR_AS(key, "filter")) { _handleFilterAttr(loader, node, value); } else if (STR_AS(key, "id")) { - if (value) free(node->id); + if (value) tvg::free(node->id); node->id = _copyId(value); } else if (STR_AS(key, "class")) { _handleCssClassAttr(loader, node, value); @@ -1772,7 +1772,7 @@ static bool _attrParseEllipseNode(void* data, const char* key, const char* value } if (STR_AS(key, "id")) { - if (value) free(node->id); + if (value) tvg::free(node->id); node->id = _copyId(value); } else if (STR_AS(key, "class")) { _handleCssClassAttr(loader, node, value); @@ -1836,7 +1836,7 @@ static bool _attrParsePolygonNode(void* data, const char* key, const char* value } else if (STR_AS(key, "filter")) { _handleFilterAttr(loader, node, value); } else if (STR_AS(key, "id")) { - if (value) free(node->id); + if (value) tvg::free(node->id); node->id = _copyId(value); } else if (STR_AS(key, "class")) { _handleCssClassAttr(loader, node, value); @@ -1912,7 +1912,7 @@ static bool _attrParseRectNode(void* data, const char* key, const char* value) } if (STR_AS(key, "id")) { - if (value) free(node->id); + if (value) tvg::free(node->id); node->id = _copyId(value); } else if (STR_AS(key, "class")) { _handleCssClassAttr(loader, node, value); @@ -1977,7 +1977,7 @@ static bool _attrParseLineNode(void* data, const char* key, const char* value) } if (STR_AS(key, "id")) { - if (value) free(node->id); + if (value) tvg::free(node->id); node->id = _copyId(value); } else if (STR_AS(key, "class")) { _handleCssClassAttr(loader, node, value); @@ -2049,10 +2049,10 @@ static bool _attrParseImageNode(void* data, const char* key, const char* value) } if (STR_AS(key, "href") || STR_AS(key, "xlink:href")) { - if (value) free(image->href); + if (value) tvg::free(image->href); image->href = _idFromHref(value); } else if (STR_AS(key, "id")) { - if (value) free(node->id); + if (value) tvg::free(node->id); node->id = _copyId(value); } else if (STR_AS(key, "class")) { _handleCssClassAttr(loader, node, value); @@ -2175,7 +2175,7 @@ static bool _attrParseUseNode(void* data, const char* key, const char* value) } else { TVGLOG("SVG", "%s is ancestor element. This reference is invalid.", id); } - free(id); + tvg::free(id); } else { //some svg export software include element at the end of the file //if so the 'from' element won't be found now and we have to repeat finding @@ -2232,7 +2232,7 @@ static bool _attrParseTextNode(void* data, const char* key, const char* value) if (STR_AS(key, "font-family")) { if (value) { - free(text->fontFamily); + tvg::free(text->fontFamily); text->fontFamily = strdup(value); } } else if (STR_AS(key, "style")) { @@ -2244,7 +2244,7 @@ static bool _attrParseTextNode(void* data, const char* key, const char* value) } else if (STR_AS(key, "filter")) { _handleFilterAttr(loader, node, value); } else if (STR_AS(key, "id")) { - if (value) free(node->id); + if (value) tvg::free(node->id); node->id = _copyId(value); } else if (STR_AS(key, "class")) { _handleCssClassAttr(loader, node, value); @@ -2571,13 +2571,13 @@ static bool _attrParseRadialGradientNode(void* data, const char* key, const char } if (STR_AS(key, "id")) { - if (value) free(grad->id); + if (value) tvg::free(grad->id); grad->id = _copyId(value); } else if (STR_AS(key, "spreadMethod")) { grad->spread = _parseSpreadValue(value); grad->flags = (grad->flags | SvgGradientFlags::SpreadMethod); } else if (STR_AS(key, "href") || STR_AS(key, "xlink:href")) { - if (value) free(grad->ref); + if (value) tvg::free(grad->ref); grad->ref = _idFromHref(value); } else if (STR_AS(key, "gradientUnits")) { if (STR_AS(value, "userSpaceOnUse")) grad->userSpace = true; @@ -2594,15 +2594,15 @@ static bool _attrParseRadialGradientNode(void* data, const char* key, const char static SvgStyleGradient* _createRadialGradient(SvgLoaderData* loader, const char* buf, unsigned bufLength) { - auto grad = (SvgStyleGradient*)(calloc(1, sizeof(SvgStyleGradient))); + auto grad = tvg::calloc(1, sizeof(SvgStyleGradient)); loader->svgParse->styleGrad = grad; grad->flags = SvgGradientFlags::None; grad->type = SvgGradientType::Radial; - grad->radial = (SvgRadialGradient*)calloc(1, sizeof(SvgRadialGradient)); + grad->radial = tvg::calloc(1, sizeof(SvgRadialGradient)); if (!grad->radial) { grad->clear(); - free(grad); + tvg::free(grad); return nullptr; } /** @@ -2862,13 +2862,13 @@ static bool _attrParseLinearGradientNode(void* data, const char* key, const char } if (STR_AS(key, "id")) { - if (value) free(grad->id); + if (value) tvg::free(grad->id); grad->id = _copyId(value); } else if (STR_AS(key, "spreadMethod")) { grad->spread = _parseSpreadValue(value); grad->flags = (grad->flags | SvgGradientFlags::SpreadMethod); } else if (STR_AS(key, "href") || STR_AS(key, "xlink:href")) { - if (value) free(grad->ref); + if (value) tvg::free(grad->ref); grad->ref = _idFromHref(value); } else if (STR_AS(key, "gradientUnits")) { if (STR_AS(value, "userSpaceOnUse")) grad->userSpace = true; @@ -2885,15 +2885,15 @@ static bool _attrParseLinearGradientNode(void* data, const char* key, const char static SvgStyleGradient* _createLinearGradient(SvgLoaderData* loader, const char* buf, unsigned bufLength) { - auto grad = (SvgStyleGradient*)(calloc(1, sizeof(SvgStyleGradient))); + auto grad = tvg::calloc(1, sizeof(SvgStyleGradient)); loader->svgParse->styleGrad = grad; grad->flags = SvgGradientFlags::None; grad->type = SvgGradientType::Linear; - grad->linear = (SvgLinearGradient*)calloc(1, sizeof(SvgLinearGradient)); + grad->linear = tvg::calloc(1, sizeof(SvgLinearGradient)); if (!grad->linear) { grad->clear(); - free(grad); + tvg::free(grad); return nullptr; } /** @@ -2970,7 +2970,7 @@ static void _inheritGradient(SvgLoaderData* loader, SvgStyleGradient* to, SvgSty } if (!to->transform && from->transform) { - to->transform = (Matrix*)malloc(sizeof(Matrix)); + to->transform = tvg::malloc(sizeof(Matrix)); if (to->transform) memcpy(to->transform, from->transform, sizeof(Matrix)); } @@ -3024,7 +3024,7 @@ static SvgStyleGradient* _cloneGradient(SvgStyleGradient* from) { if (!from) return nullptr; - auto grad = (SvgStyleGradient*)(calloc(1, sizeof(SvgStyleGradient))); + auto grad = tvg::calloc(1, sizeof(SvgStyleGradient)); if (!grad) return nullptr; grad->type = from->type; @@ -3035,16 +3035,16 @@ static SvgStyleGradient* _cloneGradient(SvgStyleGradient* from) grad->flags = from->flags; if (from->transform) { - grad->transform = (Matrix*)calloc(1, sizeof(Matrix)); + grad->transform = tvg::calloc(1, sizeof(Matrix)); if (grad->transform) memcpy(grad->transform, from->transform, sizeof(Matrix)); } if (grad->type == SvgGradientType::Linear) { - grad->linear = (SvgLinearGradient*)calloc(1, sizeof(SvgLinearGradient)); + grad->linear = tvg::calloc(1, sizeof(SvgLinearGradient)); if (!grad->linear) goto error_grad_alloc; memcpy(grad->linear, from->linear, sizeof(SvgLinearGradient)); } else if (grad->type == SvgGradientType::Radial) { - grad->radial = (SvgRadialGradient*)calloc(1, sizeof(SvgRadialGradient)); + grad->radial = tvg::calloc(1, sizeof(SvgRadialGradient)); if (!grad->radial) goto error_grad_alloc; memcpy(grad->radial, from->radial, sizeof(SvgRadialGradient)); } @@ -3056,7 +3056,7 @@ static SvgStyleGradient* _cloneGradient(SvgStyleGradient* from) error_grad_alloc: if (grad) { grad->clear(); - free(grad); + tvg::free(grad); } return nullptr; } @@ -3079,7 +3079,7 @@ static void _styleInherit(SvgStyleProperty* child, const SvgStyleProperty* paren child->fill.paint.none = parent->fill.paint.none; child->fill.paint.curColor = parent->fill.paint.curColor; if (parent->fill.paint.url) { - free(child->fill.paint.url); + tvg::free(child->fill.paint.url); child->fill.paint.url = _copyId(parent->fill.paint.url); } } @@ -3095,7 +3095,7 @@ static void _styleInherit(SvgStyleProperty* child, const SvgStyleProperty* paren child->stroke.paint.none = parent->stroke.paint.none; child->stroke.paint.curColor = parent->stroke.paint.curColor; if (parent->stroke.paint.url) { - free(child->stroke.paint.url); + tvg::free(child->stroke.paint.url); child->stroke.paint.url = _copyId(parent->stroke.paint.url); } } @@ -3153,7 +3153,7 @@ static void _styleCopy(SvgStyleProperty* to, const SvgStyleProperty* from) to->fill.paint.none = from->fill.paint.none; to->fill.paint.curColor = from->fill.paint.curColor; if (from->fill.paint.url) { - free(to->fill.paint.url); + tvg::free(to->fill.paint.url); to->fill.paint.url = _copyId(from->fill.paint.url); } } @@ -3170,7 +3170,7 @@ static void _styleCopy(SvgStyleProperty* to, const SvgStyleProperty* from) to->stroke.paint.none = from->stroke.paint.none; to->stroke.paint.curColor = from->stroke.paint.curColor; if (from->stroke.paint.url) { - free(to->stroke.paint.url); + tvg::free(to->stroke.paint.url); to->stroke.paint.url = _copyId(from->stroke.paint.url); } } @@ -3208,22 +3208,22 @@ static void _copyAttr(SvgNode* to, const SvgNode* from) { //Copy matrix attribute if (from->transform) { - to->transform = (Matrix*)malloc(sizeof(Matrix)); + to->transform = tvg::malloc(sizeof(Matrix)); if (to->transform) *to->transform = *from->transform; } //Copy style attribute _styleCopy(to->style, from->style); to->style->flags = (to->style->flags | from->style->flags); if (from->style->clipPath.url) { - free(to->style->clipPath.url); + tvg::free(to->style->clipPath.url); to->style->clipPath.url = strdup(from->style->clipPath.url); } if (from->style->mask.url) { - free(to->style->mask.url); + tvg::free(to->style->mask.url); to->style->mask.url = strdup(from->style->mask.url); } if (from->style->filter.url) { - if (to->style->filter.url) free(to->style->filter.url); + if (to->style->filter.url) tvg::free(to->style->filter.url); to->style->filter.url = strdup(from->style->filter.url); } @@ -3247,7 +3247,7 @@ static void _copyAttr(SvgNode* to, const SvgNode* from) } case SvgNodeType::Path: { if (from->node.path.path) { - free(to->node.path.path); + tvg::free(to->node.path.path); to->node.path.path = strdup(from->node.path.path); } break; @@ -3270,7 +3270,7 @@ static void _copyAttr(SvgNode* to, const SvgNode* from) to->node.image.w = from->node.image.w; to->node.image.h = from->node.image.h; if (from->node.image.href) { - free(to->node.image.href); + tvg::free(to->node.image.href); to->node.image.href = strdup(from->node.image.href); } break; @@ -3290,11 +3290,11 @@ static void _copyAttr(SvgNode* to, const SvgNode* from) to->node.text.y = from->node.text.y; to->node.text.fontSize = from->node.text.fontSize; if (from->node.text.text) { - free(to->node.text.text); + tvg::free(to->node.text.text); to->node.text.text = strdup(from->node.text.text); } if (from->node.text.fontFamily) { - free(to->node.text.fontFamily); + tvg::free(to->node.text.fontFamily); to->node.text.fontFamily = strdup(from->node.text.fontFamily); } break; @@ -3345,7 +3345,7 @@ static void _clonePostponedNodes(Array* cloneNodes, SvgNode* doc) } else { TVGLOG("SVG", "%s is ancestor element. This reference is invalid.", nodeIdPair.id); } - free(nodeIdPair.id); + tvg::free(nodeIdPair.id); } } @@ -3524,8 +3524,8 @@ static void _svgLoaderParserXmlCssStyle(SvgLoaderData* loader, const char* conte length -= next - content; content = next; - free(tag); - free(name); + tvg::free(tag); + tvg::free(name); } loader->openedTag = OpenedTagType::Other; } @@ -3659,7 +3659,7 @@ static void _updateGradient(SvgLoaderData* loader, SvgNode* node, Arraystyle->fill.paint.gradient) { node->style->fill.paint.gradient->clear(); - free(node->style->fill.paint.gradient); + tvg::free(node->style->fill.paint.gradient); } node->style->fill.paint.gradient = newGrad; } @@ -3669,7 +3669,7 @@ static void _updateGradient(SvgLoaderData* loader, SvgNode* node, Arraystyle->stroke.paint.gradient) { node->style->stroke.paint.gradient->clear(); - free(node->style->stroke.paint.gradient); + tvg::free(node->style->stroke.paint.gradient); } node->style->stroke.paint.gradient = newGrad; } @@ -3716,23 +3716,23 @@ static void _freeNodeStyle(SvgStyleProperty* style) if (!style) return; //style->clipPath.node/mask.node/filter.node has only the addresses of node. Therefore, node is released from _freeNode. - free(style->clipPath.url); - free(style->mask.url); - free(style->filter.url); - free(style->cssClass); + tvg::free(style->clipPath.url); + tvg::free(style->mask.url); + tvg::free(style->filter.url); + tvg::free(style->cssClass); if (style->fill.paint.gradient) { style->fill.paint.gradient->clear(); - free(style->fill.paint.gradient); + tvg::free(style->fill.paint.gradient); } if (style->stroke.paint.gradient) { style->stroke.paint.gradient->clear(); - free(style->stroke.paint.gradient); + tvg::free(style->stroke.paint.gradient); } - free(style->fill.paint.url); - free(style->stroke.paint.url); + tvg::free(style->fill.paint.url); + tvg::free(style->stroke.paint.url); style->stroke.dash.array.reset(); - free(style); + tvg::free(style); } @@ -3743,20 +3743,20 @@ static void _freeNode(SvgNode* node) ARRAY_FOREACH(p, node->child) _freeNode(*p); node->child.reset(); - free(node->id); - free(node->transform); + tvg::free(node->id); + tvg::free(node->transform); _freeNodeStyle(node->style); switch (node->type) { case SvgNodeType::Path: { - free(node->node.path.path); + tvg::free(node->node.path.path); break; } case SvgNodeType::Polygon: { - free(node->node.polygon.pts.data); + tvg::free(node->node.polygon.pts.data); break; } case SvgNodeType::Polyline: { - free(node->node.polyline.pts.data); + tvg::free(node->node.polyline.pts.data); break; } case SvgNodeType::Doc: { @@ -3767,25 +3767,25 @@ static void _freeNode(SvgNode* node) case SvgNodeType::Defs: { ARRAY_FOREACH(p, node->node.defs.gradients) { (*p)->clear(); - free(*p); + tvg::free(*p); } node->node.defs.gradients.reset(); break; } case SvgNodeType::Image: { - free(node->node.image.href); + tvg::free(node->node.image.href); break; } case SvgNodeType::Text: { - free(node->node.text.text); - free(node->node.text.fontFamily); + tvg::free(node->node.text.text); + tvg::free(node->node.text.fontFamily); break; } default: { break; } } - free(node); + tvg::free(node); } @@ -3852,12 +3852,12 @@ static bool _svgLoaderParserForValidCheck(void* data, SimpleXMLType type, const void SvgLoader::clear(bool all) { //flush out the intermediate data - free(loaderData.svgParse); + tvg::free(loaderData.svgParse); loaderData.svgParse = nullptr; ARRAY_FOREACH(p, loaderData.gradients) { (*p)->clear(); - free(*p); + tvg::free(*p); } loaderData.gradients.reset(); @@ -3867,10 +3867,10 @@ void SvgLoader::clear(bool all) if (!all) return; - ARRAY_FOREACH(p, loaderData.images) free(*p); + ARRAY_FOREACH(p, loaderData.images) tvg::free(*p); loaderData.images.reset(); - if (copy) free((char*)content); + if (copy) tvg::free((char*)content); delete(root); root = nullptr; @@ -3948,7 +3948,7 @@ bool SvgLoader::header() //For valid check, only tag is parsed first. //If the tag is found, the loaded file is valid and stores viewbox information. //After that, the remaining content data is parsed in order with async. - loaderData.svgParse = (SvgParser*)malloc(sizeof(SvgParser)); + loaderData.svgParse = tvg::malloc(sizeof(SvgParser)); if (!loaderData.svgParse) return false; loaderData.svgParse->flags = SvgStopStyleFlags::StopDefault; @@ -4021,7 +4021,7 @@ bool SvgLoader::open(const char* data, uint32_t size, TVG_UNUSED const char* rpa clear(); if (copy) { - content = (char*)malloc(size + 1); + content = tvg::malloc(size + 1); if (!content) return false; memcpy((char*)content, data, size); content[size] = '\0'; diff --git a/src/loaders/svg/tvgSvgLoaderCommon.h b/src/loaders/svg/tvgSvgLoaderCommon.h index 51e44a3d..1e3cd57a 100644 --- a/src/loaders/svg/tvgSvgLoaderCommon.h +++ b/src/loaders/svg/tvgSvgLoaderCommon.h @@ -463,11 +463,11 @@ struct SvgStyleGradient void clear() { stops.reset(); - free(transform); - free(radial); - free(linear); - free(ref); - free(id); + tvg::free(transform); + tvg::free(radial); + tvg::free(linear); + tvg::free(ref); + tvg::free(id); } }; diff --git a/src/loaders/svg/tvgSvgSceneBuilder.cpp b/src/loaders/svg/tvgSvgSceneBuilder.cpp index c1c3d34b..b1880024 100644 --- a/src/loaders/svg/tvgSvgSceneBuilder.cpp +++ b/src/loaders/svg/tvgSvgSceneBuilder.cpp @@ -21,7 +21,6 @@ */ #include "tvgMath.h" /* to include math.h before cstring */ -#include #include "tvgShape.h" #include "tvgCompressor.h" #include "tvgFill.h" @@ -109,7 +108,7 @@ static LinearGradient* _applyLinearGradientProperty(SvgStyleGradient* g, const B //Update the stops if (g->stops.count == 0) return fillGrad; - stops = (Fill::ColorStop*)malloc(g->stops.count * sizeof(Fill::ColorStop)); + stops = tvg::malloc(g->stops.count * sizeof(Fill::ColorStop)); auto prevOffset = 0.0f; for (uint32_t i = 0; i < g->stops.count; ++i) { auto colorStop = &g->stops[i]; @@ -125,7 +124,7 @@ static LinearGradient* _applyLinearGradientProperty(SvgStyleGradient* g, const B prevOffset = stops[i].offset; } fillGrad->colorStops(stops, g->stops.count); - free(stops); + tvg::free(stops); return fillGrad; } @@ -159,7 +158,7 @@ static RadialGradient* _applyRadialGradientProperty(SvgStyleGradient* g, const B //Update the stops if (g->stops.count == 0) return fillGrad; - stops = (Fill::ColorStop*)malloc(g->stops.count * sizeof(Fill::ColorStop)); + stops = tvg::malloc(g->stops.count * sizeof(Fill::ColorStop)); auto prevOffset = 0.0f; for (uint32_t i = 0; i < g->stops.count; ++i) { auto colorStop = &g->stops[i]; @@ -175,7 +174,7 @@ static RadialGradient* _applyRadialGradientProperty(SvgStyleGradient* g, const B prevOffset = stops[i].offset; } fillGrad->colorStops(stops, g->stops.count); - free(stops); + tvg::free(stops); return fillGrad; } @@ -659,14 +658,14 @@ static Paint* _imageBuildHelper(SvgLoaderData& loaderData, SvgNode* node, const if (encoding == imageMimeTypeEncoding::base64) { auto size = b64Decode(href, strlen(href), &decoded); if (picture->load(decoded, size, mimetype) != Result::Success) { - free(decoded); + tvg::free(decoded); TaskScheduler::async(true); return nullptr; } } else { auto size = svgUtilURLDecode(href, &decoded); if (picture->load(decoded, size, mimetype) != Result::Success) { - free(decoded); + tvg::free(decoded); TaskScheduler::async(true); return nullptr; } diff --git a/src/loaders/svg/tvgSvgUtil.cpp b/src/loaders/svg/tvgSvgUtil.cpp index 5aabbfca..92dc7367 100644 --- a/src/loaders/svg/tvgSvgUtil.cpp +++ b/src/loaders/svg/tvgSvgUtil.cpp @@ -20,7 +20,6 @@ * SOFTWARE. */ -#include #include "tvgSvgUtil.h" /************************************************************************/ @@ -46,7 +45,7 @@ size_t svgUtilURLDecode(const char *src, char** dst) auto length = strlen(src); if (length == 0) return 0; - char* decoded = (char*)malloc(sizeof(char) * length + 1); + char* decoded = tvg::malloc(sizeof(char) * length + 1); char a, b; int idx =0; diff --git a/src/loaders/svg/tvgXmlParser.cpp b/src/loaders/svg/tvgXmlParser.cpp index a3467812..df895bc0 100644 --- a/src/loaders/svg/tvgXmlParser.cpp +++ b/src/loaders/svg/tvgXmlParser.cpp @@ -20,18 +20,7 @@ * SOFTWARE. */ -#include #include -#include - -#ifdef _WIN32 - #include -#elif defined(__linux__) || defined(__ZEPHYR__) - #include -#else - #include -#endif - #include "tvgXmlParser.h" #include "tvgStr.h" @@ -298,7 +287,7 @@ bool isIgnoreUnsupportedLogElements(TVG_UNUSED const char* tagName) bool simpleXmlParseAttributes(const char* buf, unsigned bufLength, simpleXMLAttributeCb func, const void* data) { const char *itr = buf, *itrEnd = buf + bufLength; - char* tmpBuf = (char*)malloc(bufLength + 1); + char* tmpBuf = tvg::malloc(bufLength + 1); if (!buf || !func || !tmpBuf) goto error; @@ -363,11 +352,11 @@ bool simpleXmlParseAttributes(const char* buf, unsigned bufLength, simpleXMLAttr } success: - free(tmpBuf); + tvg::free(tmpBuf); return true; error: - free(tmpBuf); + tvg::free(tmpBuf); return false; } diff --git a/src/loaders/ttf/tvgTtfLoader.cpp b/src/loaders/ttf/tvgTtfLoader.cpp index 8976e3fc..11d957e8 100644 --- a/src/loaders/ttf/tvgTtfLoader.cpp +++ b/src/loaders/ttf/tvgTtfLoader.cpp @@ -133,7 +133,7 @@ static bool _map(TtfLoader* loader, const char* path) return false; } - reader.data = (uint8_t*)malloc(reader.size); + reader.data = tvg::malloc(reader.size); fseek(f, 0, SEEK_SET); auto ret = fread(reader.data, sizeof(char), reader.size, f); @@ -151,7 +151,7 @@ static bool _map(TtfLoader* loader, const char* path) static void _unmap(TtfLoader* loader) { auto& reader = loader->reader; - free(reader.data); + tvg::free(reader.data); reader.data = nullptr; reader.size = 0; } @@ -167,7 +167,7 @@ static uint32_t* _codepoints(const char* text, size_t n) auto utf8 = text; //preserve approximate enough space. - auto utf32 = (uint32_t*) malloc(sizeof(uint32_t) * (n + 1)); + auto utf32 = tvg::malloc(sizeof(uint32_t) * (n + 1)); while(*utf8) { if (!(*utf8 & 0x80U)) { @@ -186,7 +186,7 @@ static uint32_t* _codepoints(const char* text, size_t n) c += (*utf8++ & 0x3fU); utf32[i++] = c; } else { - free(utf32); + tvg::free(utf32); return nullptr; } } @@ -198,7 +198,7 @@ static uint32_t* _codepoints(const char* text, size_t n) void TtfLoader::clear() { if (nomap) { - if (freeData) free(reader.data); + if (freeData) tvg::free(reader.data); reader.data = nullptr; reader.size = 0; freeData = false; @@ -260,7 +260,7 @@ bool TtfLoader::open(const char* data, uint32_t size, TVG_UNUSED const char* rpa nomap = true; if (copy) { - reader.data = (uint8_t*)malloc(size); + reader.data = tvg::malloc(size); if (!reader.data) return false; memcpy((char*)reader.data, data, reader.size); freeData = true; @@ -313,7 +313,7 @@ bool TtfLoader::read() ++idx; } - free(code); + tvg::free(code); return true; } \ No newline at end of file diff --git a/src/loaders/ttf/tvgTtfReader.cpp b/src/loaders/ttf/tvgTtfReader.cpp index 3cfd7b43..19dcd6e0 100644 --- a/src/loaders/ttf/tvgTtfReader.cpp +++ b/src/loaders/ttf/tvgTtfReader.cpp @@ -20,18 +20,9 @@ * SOFTWARE. */ - -#ifdef _WIN32 - #include -#elif defined(__linux__) || defined(__ZEPHYR__) - #include -#else - #include -#endif - +#include "tvgTtfReader.h" #include "tvgMath.h" #include "tvgShape.h" -#include "tvgTtfReader.h" /************************************************************************/ /* Internal Class Implementation */ diff --git a/src/loaders/webp/dec/alpha.cpp b/src/loaders/webp/dec/alpha.cpp index 3d3df148..0e55d497 100644 --- a/src/loaders/webp/dec/alpha.cpp +++ b/src/loaders/webp/dec/alpha.cpp @@ -11,7 +11,7 @@ // // Author: Skal (pascal.massimino@gmail.com) -#include +#include "tvgCommon.h" #include "./alphai.h" #include "./vp8i.h" #include "./vp8li.h" @@ -24,7 +24,7 @@ // ALPHDecoder object. ALPHDecoder* ALPHNew(void) { - ALPHDecoder* const dec = (ALPHDecoder*)calloc(1ULL, sizeof(*dec)); + ALPHDecoder* const dec = tvg::calloc(1ULL, sizeof(*dec)); return dec; } @@ -32,7 +32,7 @@ void ALPHDelete(ALPHDecoder* const dec) { if (dec != NULL) { VP8LDelete(dec->vp8l_dec_); dec->vp8l_dec_ = NULL; - free(dec); + tvg::free(dec); } } diff --git a/src/loaders/webp/dec/buffer.cpp b/src/loaders/webp/dec/buffer.cpp index bdddcc93..f5b04817 100644 --- a/src/loaders/webp/dec/buffer.cpp +++ b/src/loaders/webp/dec/buffer.cpp @@ -11,7 +11,7 @@ // // Author: Skal (pascal.massimino@gmail.com) -#include +#include "tvgCommon.h" #include "./vp8i.h" #include "./webpi.h" @@ -104,7 +104,7 @@ static VP8StatusCode AllocateBuffer(WebPDecBuffer* const buffer) { total_size = size + 2 * uv_size + a_size; // Security/sanity checks - output = (uint8_t*)malloc(total_size * sizeof(*output)); + output = tvg::malloc(total_size * sizeof(*output)); if (output == NULL) { return VP8_STATUS_OUT_OF_MEMORY; } @@ -217,7 +217,7 @@ int WebPInitDecBufferInternal(WebPDecBuffer* buffer, int version) { void WebPFreeDecBuffer(WebPDecBuffer* buffer) { if (buffer != NULL) { if (!buffer->is_external_memory) { - free(buffer->private_memory); + tvg::free(buffer->private_memory); } buffer->private_memory = NULL; } diff --git a/src/loaders/webp/dec/frame.cpp b/src/loaders/webp/dec/frame.cpp index 1f388a38..b8a9386c 100644 --- a/src/loaders/webp/dec/frame.cpp +++ b/src/loaders/webp/dec/frame.cpp @@ -11,7 +11,7 @@ // // Author: Skal (pascal.massimino@gmail.com) -#include +#include "tvgCommon.h" #include "./vp8i.h" #include "../utils/utils.h" @@ -626,9 +626,9 @@ static int AllocateMemory(VP8Decoder* const dec) { if (needed != (size_t)needed) return 0; // check for overflow if (needed > dec->mem_size_) { - free(dec->mem_); + tvg::free(dec->mem_); dec->mem_size_ = 0; - dec->mem_ = malloc(needed * sizeof(uint8_t)); + dec->mem_ = tvg::malloc(needed * sizeof(uint8_t)); if (dec->mem_ == NULL) { return VP8SetError(dec, VP8_STATUS_OUT_OF_MEMORY, "no memory during frame initialization."); diff --git a/src/loaders/webp/dec/io.cpp b/src/loaders/webp/dec/io.cpp index d0d05496..6ad6baff 100644 --- a/src/loaders/webp/dec/io.cpp +++ b/src/loaders/webp/dec/io.cpp @@ -12,7 +12,7 @@ // Author: Skal (pascal.massimino@gmail.com) #include -#include +#include "tvgCommon.h" #include "../dec/vp8i.h" #include "./webpi.h" #include "../dsp/dsp.h" @@ -261,7 +261,7 @@ static int InitRGBRescaler(const VP8Io* const io, WebPDecParams* const p) { tmp_size2 += out_width; } total_size = tmp_size1 * sizeof(*work) + tmp_size2 * sizeof(*tmp); - p->memory = calloc(1ULL, total_size); + p->memory = tvg::calloc(1ULL, total_size); if (p->memory == NULL) { return 0; // memory error } @@ -375,7 +375,7 @@ static int CustomPut(const VP8Io* io) { static void CustomTeardown(const VP8Io* io) { WebPDecParams* const p = (WebPDecParams*)io->opaque; - free(p->memory); + tvg::free(p->memory); p->memory = NULL; } diff --git a/src/loaders/webp/dec/vp8.cpp b/src/loaders/webp/dec/vp8.cpp index cdb0e8a3..9d878096 100644 --- a/src/loaders/webp/dec/vp8.cpp +++ b/src/loaders/webp/dec/vp8.cpp @@ -11,7 +11,7 @@ // // Author: Skal (pascal.massimino@gmail.com) -#include +#include "tvgCommon.h" #include "./alphai.h" #include "./vp8i.h" @@ -45,7 +45,7 @@ int VP8InitIoInternal(VP8Io* const io, int version) { } VP8Decoder* VP8New(void) { - VP8Decoder* const dec = (VP8Decoder*)calloc(1ULL, sizeof(*dec)); + VP8Decoder* const dec = tvg::calloc(1ULL, sizeof(*dec)); if (dec != NULL) { SetOk(dec); dec->ready_ = 0; @@ -68,7 +68,7 @@ const char* VP8StatusMessage(VP8Decoder* const dec) { void VP8Delete(VP8Decoder* const dec) { if (dec != NULL) { VP8Clear(dec); - free(dec); + tvg::free(dec); } } @@ -670,7 +670,7 @@ void VP8Clear(VP8Decoder* const dec) { } ALPHDelete(dec->alph_dec_); dec->alph_dec_ = NULL; - free(dec->mem_); + tvg::free(dec->mem_); dec->mem_ = NULL; dec->mem_size_ = 0; memset(&dec->br_, 0, sizeof(dec->br_)); diff --git a/src/loaders/webp/dec/vp8l.cpp b/src/loaders/webp/dec/vp8l.cpp index 8ddeb7ef..aa933cdb 100644 --- a/src/loaders/webp/dec/vp8l.cpp +++ b/src/loaders/webp/dec/vp8l.cpp @@ -12,7 +12,7 @@ // Authors: Vikas Arora (vikaas.arora@gmail.com) // Jyrki Alakuijala (jyrki@google.com) -#include +#include "tvgCommon.h" #include "./alphai.h" #include "./vp8li.h" @@ -352,9 +352,9 @@ static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize, } } - huffman_tables = (HuffmanCode*)malloc(num_htree_groups * table_size * sizeof(*huffman_tables)); + huffman_tables = tvg::malloc(num_htree_groups * table_size * sizeof(*huffman_tables)); htree_groups = VP8LHtreeGroupsNew(num_htree_groups); - code_lengths = (int*)calloc((uint64_t)max_alphabet_size, sizeof(*code_lengths)); + code_lengths = tvg::calloc((uint64_t)max_alphabet_size, sizeof(*code_lengths)); if (htree_groups == NULL || code_lengths == NULL || huffman_tables == NULL) { dec->status_ = VP8_STATUS_OUT_OF_MEMORY; @@ -391,7 +391,7 @@ static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize, ((uint32_t)alpha << 24) | (red << 16) | blue; } } - free(code_lengths); + tvg::free(code_lengths); // All OK. Finalize pointers and return. hdr->huffman_image_ = huffman_image; @@ -401,9 +401,9 @@ static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize, return 1; Error: - free(code_lengths); - free(huffman_image); - free(huffman_tables); + tvg::free(code_lengths); + tvg::free(huffman_image); + tvg::free(huffman_tables); VP8LHtreeGroupsFree(htree_groups); return 0; } @@ -424,7 +424,7 @@ static int AllocateAndInitRescaler(VP8LDecoder* const dec, VP8Io* const io) { const uint64_t memory_size = sizeof(*dec->rescaler) + work_size * sizeof(*work) + scaled_data_size * sizeof(*scaled_data); - uint8_t* memory = (uint8_t*)calloc(memory_size, sizeof(*memory)); + uint8_t* memory = tvg::calloc(memory_size, sizeof(*memory)); if (memory == NULL) { dec->status_ = VP8_STATUS_OUT_OF_MEMORY; return 0; @@ -1119,7 +1119,7 @@ static int DecodeImageData(VP8LDecoder* const dec, uint32_t* const data, // VP8LTransform static void ClearTransform(VP8LTransform* const transform) { - free(transform->data_); + tvg::free(transform->data_); transform->data_ = NULL; } @@ -1128,7 +1128,7 @@ static void ClearTransform(VP8LTransform* const transform) { static int ExpandColorMap(int num_colors, VP8LTransform* const transform) { int i; const int final_num_colors = 1 << (8 >> transform->bits_); - uint32_t* const new_color_map = (uint32_t*)malloc((uint64_t)final_num_colors * sizeof(*new_color_map)); + uint32_t* const new_color_map = tvg::malloc((uint64_t)final_num_colors * sizeof(*new_color_map)); if (new_color_map == NULL) { return 0; } else { @@ -1141,7 +1141,7 @@ static int ExpandColorMap(int num_colors, VP8LTransform* const transform) { } for (; i < 4 * final_num_colors; ++i) new_data[i] = 0; // black tail. - free(transform->data_); + tvg::free(transform->data_); transform->data_ = new_color_map; } return 1; @@ -1211,8 +1211,8 @@ static void InitMetadata(VP8LMetadata* const hdr) { static void ClearMetadata(VP8LMetadata* const hdr) { assert(hdr != NULL); - free(hdr->huffman_image_); - free(hdr->huffman_tables_); + tvg::free(hdr->huffman_image_); + tvg::free(hdr->huffman_tables_); VP8LHtreeGroupsFree(hdr->htree_groups_); VP8LColorCacheClear(&hdr->color_cache_); VP8LColorCacheClear(&hdr->saved_color_cache_); @@ -1223,7 +1223,7 @@ static void ClearMetadata(VP8LMetadata* const hdr) { // VP8LDecoder VP8LDecoder* VP8LNew(void) { - VP8LDecoder* const dec = (VP8LDecoder*)calloc(1ULL, sizeof(*dec)); + VP8LDecoder* const dec = tvg::calloc(1ULL, sizeof(*dec)); if (dec == NULL) return NULL; dec->status_ = VP8_STATUS_OK; dec->state_ = READ_DIM; @@ -1238,7 +1238,7 @@ void VP8LClear(VP8LDecoder* const dec) { if (dec == NULL) return; ClearMetadata(&dec->hdr_); - free(dec->pixels_); + tvg::free(dec->pixels_); dec->pixels_ = NULL; for (i = 0; i < dec->next_transform_; ++i) { ClearTransform(&dec->transforms_[i]); @@ -1246,7 +1246,7 @@ void VP8LClear(VP8LDecoder* const dec) { dec->next_transform_ = 0; dec->transforms_seen_ = 0; - free(dec->rescaler_memory); + tvg::free(dec->rescaler_memory); dec->rescaler_memory = NULL; dec->output_ = NULL; // leave no trace behind @@ -1255,7 +1255,7 @@ void VP8LClear(VP8LDecoder* const dec) { void VP8LDelete(VP8LDecoder* const dec) { if (dec != NULL) { VP8LClear(dec); - free(dec); + tvg::free(dec); } } @@ -1326,7 +1326,7 @@ static int DecodeImageStream(int xsize, int ysize, { const uint64_t total_size = (uint64_t)transform_xsize * transform_ysize; - data = (uint32_t*)malloc(total_size * sizeof(*data)); + data = tvg::malloc(total_size * sizeof(*data)); if (data == NULL) { dec->status_ = VP8_STATUS_OUT_OF_MEMORY; ok = 0; @@ -1341,7 +1341,7 @@ static int DecodeImageStream(int xsize, int ysize, End: if (!ok) { - free(data); + tvg::free(data); ClearMetadata(hdr); } else { if (decoded_data != NULL) { @@ -1371,7 +1371,7 @@ static int AllocateInternalBuffers32b(VP8LDecoder* const dec, int final_width) { num_pixels + cache_top_pixels + cache_pixels; assert(dec->width_ <= final_width); - dec->pixels_ = (uint32_t*)malloc(total_num_pixels * sizeof(uint32_t)); + dec->pixels_ = tvg::malloc(total_num_pixels * sizeof(uint32_t)); if (dec->pixels_ == NULL) { dec->argb_cache_ = NULL; // for sanity check dec->status_ = VP8_STATUS_OUT_OF_MEMORY; @@ -1384,7 +1384,7 @@ static int AllocateInternalBuffers32b(VP8LDecoder* const dec, int final_width) { static int AllocateInternalBuffers8b(VP8LDecoder* const dec) { const uint64_t total_num_pixels = (uint64_t)dec->width_ * dec->height_; dec->argb_cache_ = NULL; // for sanity check - dec->pixels_ = (uint32_t*)malloc(total_num_pixels * sizeof(uint8_t)); + dec->pixels_ = tvg::malloc(total_num_pixels * sizeof(uint8_t)); if (dec->pixels_ == NULL) { dec->status_ = VP8_STATUS_OUT_OF_MEMORY; return 0; diff --git a/src/loaders/webp/tvgWebpLoader.cpp b/src/loaders/webp/tvgWebpLoader.cpp index 5d1e5e4d..59eba5b4 100644 --- a/src/loaders/webp/tvgWebpLoader.cpp +++ b/src/loaders/webp/tvgWebpLoader.cpp @@ -27,7 +27,7 @@ void WebpLoader::clear() { - if (freeData) free(data); + if (freeData) tvg::free(data); data = nullptr; freeData = false; } @@ -65,7 +65,7 @@ WebpLoader::WebpLoader() : ImageLoader(FileType::Webp) WebpLoader::~WebpLoader() { clear(); - free(surface.buf8); + tvg::free(surface.buf8); } @@ -83,7 +83,7 @@ bool WebpLoader::open(const char* path) return false; } - data = (uint8_t*)malloc(size); + data = tvg::malloc(size); fseek(f, 0, SEEK_SET); auto ret = fread(data, sizeof(char), size, f); @@ -111,7 +111,7 @@ bool WebpLoader::open(const char* path) bool WebpLoader::open(const char* data, uint32_t size, TVG_UNUSED const char* rpath, bool copy) { if (copy) { - this->data = (uint8_t*) malloc(size); + this->data = tvg::malloc(size); if (!this->data) return false; memcpy((uint8_t*)this->data, data, size); freeData = true; diff --git a/src/loaders/webp/utils/color_cache.cpp b/src/loaders/webp/utils/color_cache.cpp index bedaa72a..d92ffb9d 100644 --- a/src/loaders/webp/utils/color_cache.cpp +++ b/src/loaders/webp/utils/color_cache.cpp @@ -12,8 +12,7 @@ // Author: Jyrki Alakuijala (jyrki@google.com) #include -#include -#include +#include "tvgCommon.h" #include "./color_cache.h" #include "../utils/utils.h" @@ -24,7 +23,7 @@ int VP8LColorCacheInit(VP8LColorCache* const cc, int hash_bits) { const int hash_size = 1 << hash_bits; assert(cc != NULL); assert(hash_bits > 0); - cc->colors_ = (uint32_t*)calloc((uint64_t)hash_size, sizeof(*cc->colors_)); + cc->colors_ = tvg::calloc((uint64_t)hash_size, sizeof(*cc->colors_)); if (cc->colors_ == NULL) return 0; cc->hash_shift_ = 32 - hash_bits; cc->hash_bits_ = hash_bits; @@ -33,7 +32,7 @@ int VP8LColorCacheInit(VP8LColorCache* const cc, int hash_bits) { void VP8LColorCacheClear(VP8LColorCache* const cc) { if (cc != NULL) { - free(cc->colors_); + tvg::free(cc->colors_); cc->colors_ = NULL; } } diff --git a/src/loaders/webp/utils/huffman.cpp b/src/loaders/webp/utils/huffman.cpp index d7af79dd..0144997a 100644 --- a/src/loaders/webp/utils/huffman.cpp +++ b/src/loaders/webp/utils/huffman.cpp @@ -12,8 +12,7 @@ // Author: Urvang Joshi (urvang@google.com) #include -#include -#include +#include "tvgCommon.h" #include "./huffman.h" #include "../utils/utils.h" #include "../webp/format_constants.h" @@ -23,7 +22,7 @@ #define MAX_HTREE_GROUPS 0x10000 HTreeGroup* VP8LHtreeGroupsNew(int num_htree_groups) { - HTreeGroup* const htree_groups = (HTreeGroup*)malloc(num_htree_groups * sizeof(*htree_groups)); + HTreeGroup* const htree_groups = tvg::malloc(num_htree_groups * sizeof(*htree_groups)); if (htree_groups == NULL) { return NULL; } @@ -33,7 +32,7 @@ HTreeGroup* VP8LHtreeGroupsNew(int num_htree_groups) { void VP8LHtreeGroupsFree(HTreeGroup* const htree_groups) { if (htree_groups != NULL) { - free(htree_groups); + tvg::free(htree_groups); } } @@ -113,7 +112,7 @@ int VP8LBuildHuffmanTable(HuffmanCode* const root_table, int root_bits, offset[len + 1] = offset[len] + count[len]; } - sorted = (int*)malloc(code_lengths_size * sizeof(*sorted)); + sorted = tvg::malloc(code_lengths_size * sizeof(*sorted)); if (sorted == NULL) { return 0; } @@ -132,7 +131,7 @@ int VP8LBuildHuffmanTable(HuffmanCode* const root_table, int root_bits, code.bits = 0; code.value = (uint16_t)sorted[0]; ReplicateValue(table, 1, total_size, code); - free(sorted); + tvg::free(sorted); return total_size; } @@ -152,7 +151,7 @@ int VP8LBuildHuffmanTable(HuffmanCode* const root_table, int root_bits, num_nodes += num_open; num_open -= count[len]; if (num_open < 0) { - free(sorted); + tvg::free(sorted); return 0; } for (; count[len] > 0; --count[len]) { @@ -171,7 +170,7 @@ int VP8LBuildHuffmanTable(HuffmanCode* const root_table, int root_bits, num_nodes += num_open; num_open -= count[len]; if (num_open < 0) { - free(sorted); + tvg::free(sorted); return 0; } for (; count[len] > 0; --count[len]) { @@ -194,11 +193,11 @@ int VP8LBuildHuffmanTable(HuffmanCode* const root_table, int root_bits, // Check if tree is full. if (num_nodes != 2 * offset[MAX_ALLOWED_CODE_LENGTH] - 1) { - free(sorted); + tvg::free(sorted); return 0; } } - free(sorted); + tvg::free(sorted); return total_size; } diff --git a/src/loaders/webp/utils/quant_levels_dec.cpp b/src/loaders/webp/utils/quant_levels_dec.cpp index 5250c477..9c0550b2 100644 --- a/src/loaders/webp/utils/quant_levels_dec.cpp +++ b/src/loaders/webp/utils/quant_levels_dec.cpp @@ -14,12 +14,10 @@ // // Author: Skal (pascal.massimino@gmail.com) +#include "tvgCommon.h" #include "./quant_levels_dec.h" #include "./utils.h" -#include -#include // for memset - // #define USE_DITHERING // uncomment to enable ordered dithering (not vital) #define FIX 16 // fix-point precision for averaging @@ -216,7 +214,7 @@ static int InitParams(uint8_t* const data, int width, int height, const size_t size_m = width * sizeof(*p->average_); const size_t size_lut = (1 + 2 * LUT_SIZE) * sizeof(*p->correction_); const size_t total_size = size_scratch_m + size_m + size_lut; - uint8_t* mem = (uint8_t*)malloc(1U * total_size); + uint8_t* mem = tvg::malloc(1U * total_size); if (mem == NULL) return 0; p->mem_ = (void*)mem; @@ -250,7 +248,7 @@ static int InitParams(uint8_t* const data, int width, int height, } static void CleanupParams(SmoothParams* const p) { - free(p->mem_); + tvg::free(p->mem_); } int WebPDequantizeLevels(uint8_t* const data, int width, int height, diff --git a/src/renderer/gl_engine/tvgGlCommon.h b/src/renderer/gl_engine/tvgGlCommon.h index 83ebfece..ebf27508 100644 --- a/src/renderer/gl_engine/tvgGlCommon.h +++ b/src/renderer/gl_engine/tvgGlCommon.h @@ -24,7 +24,7 @@ #define _TVG_GL_COMMON_H_ #include -#include + #if defined (THORVG_GL_TARGET_GLES) #include #define TVG_REQUIRE_GL_MAJOR_VER 3 diff --git a/src/renderer/gl_engine/tvgGlProgram.cpp b/src/renderer/gl_engine/tvgGlProgram.cpp index 13a3905f..e3ac313d 100644 --- a/src/renderer/gl_engine/tvgGlProgram.cpp +++ b/src/renderer/gl_engine/tvgGlProgram.cpp @@ -56,10 +56,10 @@ GlProgram::GlProgram(const char* vertSrc, const char* fragSrc) glGetProgramiv(progObj, GL_INFO_LOG_LENGTH, &infoLen); if (infoLen > 0) { - auto infoLog = static_cast(malloc(sizeof(char) * infoLen)); + auto infoLog = tvg::malloc(sizeof(char) * infoLen); glGetProgramInfoLog(progObj, infoLen, NULL, infoLog); TVGERR("GL_ENGINE", "Error linking shader: %s", infoLog); - free(infoLog); + tvg::free(infoLog); } glDeleteProgram(progObj); diff --git a/src/renderer/gl_engine/tvgGlShader.cpp b/src/renderer/gl_engine/tvgGlShader.cpp index 10a20761..ef69ddbd 100644 --- a/src/renderer/gl_engine/tvgGlShader.cpp +++ b/src/renderer/gl_engine/tvgGlShader.cpp @@ -66,10 +66,10 @@ uint32_t GlShader::compileShader(uint32_t type, char* shaderSrc) if (infoLen > 0) { - auto infoLog = static_cast(malloc(sizeof(char)*infoLen)); + auto infoLog = tvg::malloc(sizeof(char)*infoLen); glGetShaderInfoLog(shader, infoLen, NULL, infoLog); TVGERR("GL_ENGINE", "Error compiling shader: %s", infoLog); - free(infoLog); + tvg::free(infoLog); } glDeleteShader(shader); } diff --git a/src/renderer/gl_engine/tvgGlTessellator.cpp b/src/renderer/gl_engine/tvgGlTessellator.cpp index eb794f6a..dea60414 100644 --- a/src/renderer/gl_engine/tvgGlTessellator.cpp +++ b/src/renderer/gl_engine/tvgGlTessellator.cpp @@ -1570,8 +1570,8 @@ void Stroker::stroke(const RenderShape *rshape) if (dashCnt == 0) doStroke(cmds, cmdCnt, pts, ptsCnt); else doDashStroke(cmds, cmdCnt, pts, ptsCnt, dashCnt, dash_pattern, dash_offset); - free(trimmedCmds); - free(trimmedPts); + tvg::free(trimmedCmds); + tvg::free(trimmedPts); } diff --git a/src/renderer/sw_engine/tvgSwFill.cpp b/src/renderer/sw_engine/tvgSwFill.cpp index 972b0ccf..01814566 100644 --- a/src/renderer/sw_engine/tvgSwFill.cpp +++ b/src/renderer/sw_engine/tvgSwFill.cpp @@ -20,7 +20,6 @@ * SOFTWARE. */ -#include "tvgMath.h" #include "tvgSwCommon.h" #include "tvgFill.h" @@ -130,7 +129,7 @@ static bool _updateColorTable(SwFill* fill, const Fill* fdata, const SwSurface* if (fill->solid) return true; if (!fill->ctable) { - fill->ctable = static_cast(malloc(GRADIENT_STOP_SIZE * sizeof(uint32_t))); + fill->ctable = tvg::malloc(GRADIENT_STOP_SIZE * sizeof(uint32_t)); } const Fill::ColorStop* colors; @@ -830,7 +829,7 @@ const Fill::ColorStop* fillFetchSolid(const SwFill* fill, const Fill* fdata) void fillReset(SwFill* fill) { if (fill->ctable) { - free(fill->ctable); + tvg::free(fill->ctable); fill->ctable = nullptr; } fill->translucent = false; @@ -842,7 +841,7 @@ void fillFree(SwFill* fill) { if (!fill) return; - if (fill->ctable) free(fill->ctable); + if (fill->ctable) tvg::free(fill->ctable); - free(fill); + tvg::free(fill); } diff --git a/src/renderer/sw_engine/tvgSwMemPool.cpp b/src/renderer/sw_engine/tvgSwMemPool.cpp index b340f40e..403b30de 100644 --- a/src/renderer/sw_engine/tvgSwMemPool.cpp +++ b/src/renderer/sw_engine/tvgSwMemPool.cpp @@ -81,10 +81,10 @@ SwMpool* mpoolInit(uint32_t threads) { auto allocSize = threads + 1; - auto mpool = static_cast(calloc(1, sizeof(SwMpool))); - mpool->outline = static_cast(calloc(1, sizeof(SwOutline) * allocSize)); - mpool->strokeOutline = static_cast(calloc(1, sizeof(SwOutline) * allocSize)); - mpool->dashOutline = static_cast(calloc(1, sizeof(SwOutline) * allocSize)); + auto mpool = tvg::calloc(1, sizeof(SwMpool)); + mpool->outline = tvg::calloc(1, sizeof(SwOutline) * allocSize); + mpool->strokeOutline = tvg::calloc(1, sizeof(SwOutline) * allocSize); + mpool->dashOutline = tvg::calloc(1, sizeof(SwOutline) * allocSize); mpool->allocSize = allocSize; return mpool; @@ -120,10 +120,10 @@ bool mpoolTerm(SwMpool* mpool) mpoolClear(mpool); - free(mpool->outline); - free(mpool->strokeOutline); - free(mpool->dashOutline); - free(mpool); + tvg::free(mpool->outline); + tvg::free(mpool->strokeOutline); + tvg::free(mpool->dashOutline); + tvg::free(mpool); return true; } diff --git a/src/renderer/sw_engine/tvgSwPostEffect.cpp b/src/renderer/sw_engine/tvgSwPostEffect.cpp index 441662e2..2bdab334 100644 --- a/src/renderer/sw_engine/tvgSwPostEffect.cpp +++ b/src/renderer/sw_engine/tvgSwPostEffect.cpp @@ -155,7 +155,7 @@ bool effectGaussianBlurRegion(RenderEffectGaussianBlur* params) void effectGaussianBlurUpdate(RenderEffectGaussianBlur* params, const Matrix& transform) { - if (!params->rd) params->rd = (SwGaussianBlur*)malloc(sizeof(SwGaussianBlur)); + if (!params->rd) params->rd = tvg::malloc(sizeof(SwGaussianBlur)); auto rd = static_cast(params->rd); //compute box kernel sizes @@ -315,7 +315,7 @@ bool effectDropShadowRegion(RenderEffectDropShadow* params) void effectDropShadowUpdate(RenderEffectDropShadow* params, const Matrix& transform) { - if (!params->rd) params->rd = (SwDropShadow*)malloc(sizeof(SwDropShadow)); + if (!params->rd) params->rd = tvg::malloc(sizeof(SwDropShadow)); auto rd = static_cast(params->rd); //compute box kernel sizes diff --git a/src/renderer/sw_engine/tvgSwRasterTexmap.h b/src/renderer/sw_engine/tvgSwRasterTexmap.h index ab5dfde1..138020ee 100644 --- a/src/renderer/sw_engine/tvgSwRasterTexmap.h +++ b/src/renderer/sw_engine/tvgSwRasterTexmap.h @@ -583,14 +583,14 @@ static AASpans* _AASpans(float ymin, float ymax, const SwImage* image, const SwB if (!_arrange(image, region, yStart, yEnd)) return nullptr; - auto aaSpans = static_cast(malloc(sizeof(AASpans))); + auto aaSpans = tvg::malloc(sizeof(AASpans)); aaSpans->yStart = yStart; aaSpans->yEnd = yEnd; //Initialize X range auto height = yEnd - yStart; - aaSpans->lines = static_cast(malloc(height * sizeof(AALine))); + aaSpans->lines = tvg::malloc(height * sizeof(AALine)); for (int32_t i = 0; i < height; i++) { aaSpans->lines[i].x[0] = INT32_MAX; @@ -846,8 +846,8 @@ static bool _apply(SwSurface* surface, AASpans* aaSpans) y++; } - free(aaSpans->lines); - free(aaSpans); + tvg::free(aaSpans->lines); + tvg::free(aaSpans); return true; } diff --git a/src/renderer/sw_engine/tvgSwRenderer.cpp b/src/renderer/sw_engine/tvgSwRenderer.cpp index d025cc91..b1a8cac8 100644 --- a/src/renderer/sw_engine/tvgSwRenderer.cpp +++ b/src/renderer/sw_engine/tvgSwRenderer.cpp @@ -25,7 +25,6 @@ #endif #include #include -#include "tvgMath.h" #include "tvgSwCommon.h" #include "tvgTaskScheduler.h" #include "tvgSwRenderer.h" @@ -384,7 +383,7 @@ void SwRenderer::clearCompositors() { //Free Composite Caches ARRAY_FOREACH(p, compositors) { - free((*p)->compositor->image.data); + tvg::free((*p)->compositor->image.data); delete((*p)->compositor); delete(*p); } @@ -555,7 +554,7 @@ SwSurface* SwRenderer::request(int channelSize, bool square) //Inherits attributes from main surface cmp = new SwSurface(surface); cmp->compositor = new SwCompositor; - cmp->compositor->image.data = (pixel_t*)malloc(channelSize * w * h); + cmp->compositor->image.data = tvg::malloc(channelSize * w * h); cmp->w = cmp->compositor->image.w = w; cmp->h = cmp->compositor->image.h = h; cmp->stride = cmp->compositor->image.stride = w; @@ -700,7 +699,7 @@ bool SwRenderer::render(RenderCompositor* cmp, const RenderEffect* effect, bool void SwRenderer::dispose(RenderEffect* effect) { - free(effect->rd); + tvg::free(effect->rd); effect->rd = nullptr; } diff --git a/src/renderer/sw_engine/tvgSwRle.cpp b/src/renderer/sw_engine/tvgSwRle.cpp index 5815d10c..20fcef00 100644 --- a/src/renderer/sw_engine/tvgSwRle.cpp +++ b/src/renderer/sw_engine/tvgSwRle.cpp @@ -189,7 +189,6 @@ */ #include -#include #include "tvgSwCommon.h" /************************************************************************/ @@ -342,7 +341,7 @@ static void _horizLine(RleWorker& rw, SwCoord x, SwCoord y, SwCoord area, SwCoor auto newSize = (rle->size > 0) ? (rle->size * 2) : 256; if (rle->alloc < newSize) { rle->alloc = newSize; - rle->spans = static_cast(realloc(rle->spans, rle->alloc * sizeof(SwSpan))); + rle->spans = tvg::realloc(rle->spans, rle->alloc * sizeof(SwSpan)); } } @@ -831,7 +830,7 @@ static SwSpan* _intersectSpansRect(const SwBBox *bbox, const SwRle *targetRle, S void _replaceClipSpan(SwRle *rle, SwSpan* clippedSpans, uint32_t size) { - free(rle->spans); + tvg::free(rle->spans); rle->spans = clippedSpans; rle->size = rle->alloc = size; } @@ -869,7 +868,7 @@ SwRle* rleRender(SwRle* rle, const SwOutline* outline, const SwBBox& renderRegio rw.bandShoot = 0; rw.antiAlias = antiAlias; - if (!rle) rw.rle = reinterpret_cast(calloc(1, sizeof(SwRle))); + if (!rle) rw.rle = tvg::calloc(1, sizeof(SwRle)); else rw.rle = rle; //Generate RLE @@ -951,7 +950,7 @@ SwRle* rleRender(SwRle* rle, const SwOutline* outline, const SwBBox& renderRegio return rw.rle; error: - free(rw.rle); + tvg::free(rw.rle); return nullptr; } @@ -961,8 +960,8 @@ SwRle* rleRender(const SwBBox* bbox) auto width = static_cast(bbox->max.x - bbox->min.x); auto height = static_cast(bbox->max.y - bbox->min.y); - auto rle = static_cast(malloc(sizeof(SwRle))); - rle->spans = static_cast(malloc(sizeof(SwSpan) * height)); + auto rle = tvg::malloc(sizeof(SwRle)); + rle->spans = tvg::malloc(sizeof(SwSpan) * height); rle->size = height; rle->alloc = height; @@ -988,8 +987,8 @@ void rleReset(SwRle* rle) void rleFree(SwRle* rle) { if (!rle) return; - if (rle->spans) free(rle->spans); - free(rle); + if (rle->spans) tvg::free(rle->spans); + tvg::free(rle); } @@ -997,7 +996,7 @@ bool rleClip(SwRle *rle, const SwRle *clip) { if (rle->size == 0 || clip->size == 0) return false; auto spanCnt = rle->size > clip->size ? rle->size : clip->size; - auto spans = static_cast(malloc(sizeof(SwSpan) * (spanCnt))); + auto spans = tvg::malloc(sizeof(SwSpan) * (spanCnt)); auto spansEnd = _intersectSpansRegion(clip, rle, spans, spanCnt); _replaceClipSpan(rle, spans, spansEnd - spans); @@ -1011,7 +1010,7 @@ bool rleClip(SwRle *rle, const SwRle *clip) bool rleClip(SwRle *rle, const SwBBox* clip) { if (rle->size == 0) return false; - auto spans = static_cast(malloc(sizeof(SwSpan) * (rle->size))); + auto spans = tvg::malloc(sizeof(SwSpan) * (rle->size)); auto spansEnd = _intersectSpansRect(clip, rle, spans, rle->size); _replaceClipSpan(rle, spans, spansEnd - spans); diff --git a/src/renderer/sw_engine/tvgSwShape.cpp b/src/renderer/sw_engine/tvgSwShape.cpp index 0dea226d..e57e75ec 100644 --- a/src/renderer/sw_engine/tvgSwShape.cpp +++ b/src/renderer/sw_engine/tvgSwShape.cpp @@ -21,7 +21,6 @@ */ #include "tvgSwCommon.h" -#include "tvgMath.h" /************************************************************************/ /* Internal Class Implementation */ @@ -319,8 +318,8 @@ static SwOutline* _genDashOutline(const RenderShape* rshape, const Matrix& trans ++cmds; } - free(trimmedCmds); - free(trimmedPts); + tvg::free(trimmedCmds); + tvg::free(trimmedPts); _outlineEnd(*dash.outline); @@ -410,8 +409,8 @@ static SwOutline* _genOutline(SwShape* shape, const RenderShape* rshape, const M outline->fillRule = rshape->rule; - free(trimmedCmds); - free(trimmedPts); + tvg::free(trimmedCmds); + tvg::free(trimmedPts); if (!trimmed) shape->fastTrack = (!hasComposite && _axisAlignedRect(outline)); return outline; @@ -494,7 +493,7 @@ void shapeDelStroke(SwShape* shape) void shapeResetStroke(SwShape* shape, const RenderShape* rshape, const Matrix& transform) { - if (!shape->stroke) shape->stroke = static_cast(calloc(1, sizeof(SwStroke))); + if (!shape->stroke) shape->stroke = tvg::calloc(1, sizeof(SwStroke)); auto stroke = shape->stroke; if (!stroke) return; @@ -567,7 +566,7 @@ bool shapeGenStrokeFillColors(SwShape* shape, const Fill* fill, const Matrix& tr void shapeResetFill(SwShape* shape) { if (!shape->fill) { - shape->fill = static_cast(calloc(1, sizeof(SwFill))); + shape->fill = tvg::calloc(1, sizeof(SwFill)); if (!shape->fill) return; } fillReset(shape->fill); @@ -577,7 +576,7 @@ void shapeResetFill(SwShape* shape) void shapeResetStrokeFill(SwShape* shape) { if (!shape->stroke->fill) { - shape->stroke->fill = static_cast(calloc(1, sizeof(SwFill))); + shape->stroke->fill = tvg::calloc(1, sizeof(SwFill)); if (!shape->stroke->fill) return; } fillReset(shape->stroke->fill); diff --git a/src/renderer/sw_engine/tvgSwStroke.cpp b/src/renderer/sw_engine/tvgSwStroke.cpp index 1f0c303b..1cc58484 100644 --- a/src/renderer/sw_engine/tvgSwStroke.cpp +++ b/src/renderer/sw_engine/tvgSwStroke.cpp @@ -20,8 +20,6 @@ * SOFTWARE. */ -#include -#include #include "tvgSwCommon.h" /************************************************************************/ @@ -58,8 +56,8 @@ static void _growBorder(SwStrokeBorder* border, uint32_t newPts) while (maxCur < maxNew) maxCur += (maxCur >> 1) + 16; //OPTIMIZE: use mempool! - border->pts = static_cast(realloc(border->pts, maxCur * sizeof(SwPoint))); - border->tags = static_cast(realloc(border->tags, maxCur * sizeof(uint8_t))); + border->pts = tvg::realloc(border->pts, maxCur * sizeof(SwPoint)); + border->tags = tvg::realloc(border->tags, maxCur * sizeof(uint8_t)); border->maxPts = maxCur; } @@ -806,15 +804,15 @@ void strokeFree(SwStroke* stroke) if (!stroke) return; //free borders - if (stroke->borders[0].pts) free(stroke->borders[0].pts); - if (stroke->borders[0].tags) free(stroke->borders[0].tags); - if (stroke->borders[1].pts) free(stroke->borders[1].pts); - if (stroke->borders[1].tags) free(stroke->borders[1].tags); + if (stroke->borders[0].pts) tvg::free(stroke->borders[0].pts); + if (stroke->borders[0].tags) tvg::free(stroke->borders[0].tags); + if (stroke->borders[1].pts) tvg::free(stroke->borders[1].pts); + if (stroke->borders[1].tags) tvg::free(stroke->borders[1].tags); fillFree(stroke->fill); stroke->fill = nullptr; - free(stroke); + tvg::free(stroke); } diff --git a/src/renderer/tvgCommon.h b/src/renderer/tvgCommon.h index 0d744998..baff8e50 100644 --- a/src/renderer/tvgCommon.h +++ b/src/renderer/tvgCommon.h @@ -23,7 +23,17 @@ #ifndef _TVG_COMMON_H_ #define _TVG_COMMON_H_ +#ifdef _WIN32 + #include +#elif defined(__linux__) || defined(__ZEPHYR__) + #include +#else + #include +#endif #include +#include +#include +#include #include "config.h" #include "thorvg.h" @@ -55,40 +65,59 @@ using namespace tvg; #define strdup _strdup #endif -enum class FileType { Png = 0, Jpg, Webp, Svg, Lottie, Ttf, Raw, Gif, Unknown }; +void* operator new(std::size_t size); +void operator delete(void* ptr) noexcept; -using Size = Point; +namespace tvg { -#ifdef THORVG_LOG_ENABLED - constexpr auto ErrorColor = "\033[31m"; //red - constexpr auto ErrorBgColor = "\033[41m";//bg red - constexpr auto LogColor = "\033[32m"; //green - constexpr auto LogBgColor = "\033[42m"; //bg green - constexpr auto GreyColor = "\033[90m"; //grey - constexpr auto ResetColors = "\033[0m"; //default - #define TVGERR(tag, fmt, ...) fprintf(stderr, "%s[E]%s %s" tag "%s (%s %d): %s" fmt "\n", ErrorBgColor, ResetColors, ErrorColor, GreyColor, __FILE__, __LINE__, ResetColors, ##__VA_ARGS__) - #define TVGLOG(tag, fmt, ...) fprintf(stdout, "%s[L]%s %s" tag "%s (%s %d): %s" fmt "\n", LogBgColor, ResetColors, LogColor, GreyColor, __FILE__, __LINE__, ResetColors, ##__VA_ARGS__) -#else - #define TVGERR(...) do {} while(0) - #define TVGLOG(...) do {} while(0) -#endif + enum class FileType { Png = 0, Jpg, Webp, Svg, Lottie, Ttf, Raw, Gif, Unknown }; -uint16_t THORVG_VERSION_NUMBER(); + using Size = Point; -#define PIMPL(INST, CLASS) ((CLASS::Impl*)INST->pImpl) //Access to pimpl + #ifdef THORVG_LOG_ENABLED + constexpr auto ErrorColor = "\033[31m"; //red + constexpr auto ErrorBgColor = "\033[41m";//bg red + constexpr auto LogColor = "\033[32m"; //green + constexpr auto LogBgColor = "\033[42m"; //bg green + constexpr auto GreyColor = "\033[90m"; //grey + constexpr auto ResetColors = "\033[0m"; //default + #define TVGERR(tag, fmt, ...) fprintf(stderr, "%s[E]%s %s" tag "%s (%s %d): %s" fmt "\n", ErrorBgColor, ResetColors, ErrorColor, GreyColor, __FILE__, __LINE__, ResetColors, ##__VA_ARGS__) + #define TVGLOG(tag, fmt, ...) fprintf(stdout, "%s[L]%s %s" tag "%s (%s %d): %s" fmt "\n", LogBgColor, ResetColors, LogColor, GreyColor, __FILE__, __LINE__, ResetColors, ##__VA_ARGS__) + #else + #define TVGERR(...) do {} while(0) + #define TVGLOG(...) do {} while(0) + #endif -#define TVG_DELETE(PAINT) \ - if (PAINT->refCnt() == 0) delete(PAINT) + uint16_t THORVG_VERSION_NUMBER(); -//for debugging -#if 0 -#include -static inline double THORVG_TIMESTAMP() -{ - struct timeval tv; - gettimeofday(&tv, NULL); - return (tv.tv_sec + tv.tv_usec / 1000000.0); + #define PIMPL(INST, CLASS) ((CLASS::Impl*)INST->pImpl) //Access to pimpl + + #define TVG_DELETE(PAINT) \ + if (PAINT->refCnt() == 0) delete(PAINT) + + //custom memory allocators + template + static inline T malloc(size_t size) + { + return static_cast(std::malloc(size)); + } + + template + static inline T calloc(size_t nmem, size_t size) + { + return static_cast(std::calloc(nmem, size)); + } + + template + static inline T realloc(void* ptr, size_t size) + { + return static_cast(std::realloc(ptr, size)); + } + + static inline void free(void* ptr) + { + std::free(ptr); + } } -#endif #endif //_TVG_COMMON_H_ diff --git a/src/renderer/tvgFill.h b/src/renderer/tvgFill.h index 8dde1852..4bbf86c4 100644 --- a/src/renderer/tvgFill.h +++ b/src/renderer/tvgFill.h @@ -23,8 +23,6 @@ #ifndef _TVG_FILL_H_ #define _TVG_FILL_H_ -#include -#include #include "tvgCommon.h" #define LINEAR(A) PIMPL(A, LinearGradient) @@ -39,14 +37,14 @@ struct Fill::Impl virtual ~Impl() { - free(colorStops); + tvg::free(colorStops); } void copy(Fill::Impl* dup) { cnt = dup->cnt; spread = dup->spread; - colorStops = static_cast(malloc(sizeof(ColorStop) * dup->cnt)); + colorStops = tvg::malloc(sizeof(ColorStop) * dup->cnt); if (dup->cnt > 0) memcpy(colorStops, dup->colorStops, sizeof(ColorStop) * dup->cnt); transform = dup->transform; } @@ -57,7 +55,7 @@ struct Fill::Impl if (cnt == 0) { if (this->colorStops) { - free(this->colorStops); + tvg::free(this->colorStops); this->colorStops = nullptr; this->cnt = 0; } @@ -65,7 +63,7 @@ struct Fill::Impl } if (cnt != this->cnt) { - this->colorStops = static_cast(realloc(this->colorStops, cnt * sizeof(ColorStop))); + this->colorStops = tvg::realloc(this->colorStops, cnt * sizeof(ColorStop)); } this->cnt = cnt; diff --git a/src/renderer/tvgInitializer.cpp b/src/renderer/tvgInitializer.cpp index fb1dad59..ef8e8bed 100644 --- a/src/renderer/tvgInitializer.cpp +++ b/src/renderer/tvgInitializer.cpp @@ -24,10 +24,6 @@ #include "tvgTaskScheduler.h" #include "tvgLoader.h" -#ifdef _WIN32 - #include -#endif - #ifdef THORVG_SW_RASTER_SUPPORT #include "tvgSwRenderer.h" #endif @@ -174,4 +170,14 @@ const char* Initializer::version(uint32_t* major, uint32_t* minor, uint32_t* mic uint16_t THORVG_VERSION_NUMBER() { return _version; +} + + +void* operator new(std::size_t size) { + return tvg::malloc(size); +} + + +void operator delete(void* ptr) noexcept { + tvg::free(ptr); } \ No newline at end of file diff --git a/src/renderer/tvgLoadModule.h b/src/renderer/tvgLoadModule.h index 57472c0d..0352f4bb 100644 --- a/src/renderer/tvgLoadModule.h +++ b/src/renderer/tvgLoadModule.h @@ -23,6 +23,7 @@ #ifndef _TVG_LOAD_MODULE_H_ #define _TVG_LOAD_MODULE_H_ +#include "tvgCommon.h" #include "tvgRender.h" #include "tvgInlist.h" @@ -45,7 +46,7 @@ struct LoadModule LoadModule(FileType type) : type(type) {} virtual ~LoadModule() { - if (pathcache) free(hashpath); + if (pathcache) tvg::free(hashpath); } virtual bool open(const char* path) { return false; } diff --git a/src/renderer/tvgPaint.h b/src/renderer/tvgPaint.h index d61f6650..e52580e2 100644 --- a/src/renderer/tvgPaint.h +++ b/src/renderer/tvgPaint.h @@ -23,6 +23,7 @@ #ifndef _TVG_PAINT_H_ #define _TVG_PAINT_H_ +#include "tvgCommon.h" #include "tvgRender.h" #include "tvgMath.h" @@ -91,7 +92,7 @@ namespace tvg { if (maskData) { maskData->target->unref(); - free(maskData); + tvg::free(maskData); } if (clipper) clipper->unref(); @@ -163,13 +164,13 @@ namespace tvg maskData->target->unref(maskData->target != target); //Reset scenario if (!target && method == MaskMethod::None) { - free(maskData); + tvg::free(maskData); maskData = nullptr; return true; } } else { if (!target && method == MaskMethod::None) return true; - maskData = static_cast(malloc(sizeof(Mask))); + maskData = tvg::malloc(sizeof(Mask)); } target->ref(); maskData->target = target; @@ -198,7 +199,7 @@ namespace tvg if (maskData) { maskData->target->unref(); - free(maskData); + tvg::free(maskData); maskData = nullptr; } diff --git a/src/renderer/tvgRender.h b/src/renderer/tvgRender.h index d5a1cdb0..c826b3c9 100644 --- a/src/renderer/tvgRender.h +++ b/src/renderer/tvgRender.h @@ -117,9 +117,9 @@ struct RenderStroke if (rhs.fill) fill = rhs.fill->duplicate(); else fill = nullptr; - free(dashPattern); + tvg::free(dashPattern); if (rhs.dashCnt > 0) { - dashPattern = static_cast(malloc(sizeof(float) * rhs.dashCnt)); + dashPattern = tvg::malloc(sizeof(float) * rhs.dashCnt); memcpy(dashPattern, rhs.dashPattern, sizeof(float) * rhs.dashCnt); } else { dashPattern = nullptr; @@ -135,7 +135,7 @@ struct RenderStroke ~RenderStroke() { - free(dashPattern); + tvg::free(dashPattern); delete(fill); } }; diff --git a/src/renderer/tvgShape.h b/src/renderer/tvgShape.h index 6740cb33..b581f82b 100644 --- a/src/renderer/tvgShape.h +++ b/src/renderer/tvgShape.h @@ -23,7 +23,7 @@ #ifndef _TVG_SHAPE_H_ #define _TVG_SHAPE_H_ -#include +#include "tvgCommon.h" #include "tvgMath.h" #include "tvgPaint.h" @@ -307,16 +307,16 @@ struct Shape::Impl : Paint::Impl //Reset dash if (!pattern && cnt == 0) { - free(rs.stroke->dashPattern); + tvg::free(rs.stroke->dashPattern); rs.stroke->dashPattern = nullptr; } else { if (!rs.stroke) rs.stroke = new RenderStroke(); if (rs.stroke->dashCnt != cnt) { - free(rs.stroke->dashPattern); + tvg::free(rs.stroke->dashPattern); rs.stroke->dashPattern = nullptr; } if (!rs.stroke->dashPattern) { - rs.stroke->dashPattern = static_cast(malloc(sizeof(float) * cnt)); + rs.stroke->dashPattern = tvg::malloc(sizeof(float) * cnt); if (!rs.stroke->dashPattern) return Result::FailedAllocation; } for (uint32_t i = 0; i < cnt; ++i) { diff --git a/src/renderer/tvgText.h b/src/renderer/tvgText.h index 6430977a..0a026466 100644 --- a/src/renderer/tvgText.h +++ b/src/renderer/tvgText.h @@ -23,7 +23,7 @@ #ifndef _TVG_TEXT_H #define _TVG_TEXT_H -#include +#include "tvgCommon.h" #include "tvgShape.h" #include "tvgFill.h" #include "tvgLoader.h" @@ -45,14 +45,14 @@ struct Text::Impl : Paint::Impl ~Impl() { - free(utf8); + tvg::free(utf8); LoaderMgr::retrieve(loader); delete(shape); } Result text(const char* utf8) { - free(this->utf8); + tvg::free(this->utf8); if (utf8) this->utf8 = strdup(utf8); else this->utf8 = nullptr; changed = true; diff --git a/src/renderer/wg_engine/tvgWgGeometry.h b/src/renderer/wg_engine/tvgWgGeometry.h index caa889fc..79b8fd7b 100755 --- a/src/renderer/wg_engine/tvgWgGeometry.h +++ b/src/renderer/wg_engine/tvgWgGeometry.h @@ -70,14 +70,14 @@ struct WgVertexBuffer WgVertexBuffer(float scale = 1.0f) : scale(scale) { - data = (Point*)malloc(sizeof(Point) * reserved); - dist = (Distance*)malloc(sizeof(Distance) * reserved); + data = tvg::malloc(sizeof(Point) * reserved); + dist = tvg::malloc(sizeof(Distance) * reserved); } ~WgVertexBuffer() { - free(data); - free(dist); + tvg::free(data); + tvg::free(dist); } // reset buffer @@ -159,8 +159,8 @@ struct WgVertexBuffer { if (count >= reserved) { reserved *= 2; - data = (Point*) realloc(data, reserved * sizeof(Point)); - dist = (Distance*) realloc(dist, reserved * sizeof(Distance)); + data = tvg::realloc(data, reserved * sizeof(Point)); + dist = tvg::realloc(dist, reserved * sizeof(Distance)); } data[count++] = p; } @@ -262,8 +262,8 @@ struct WgVertexBuffer } } - free(trimmedCmds); - free(trimmedPts); + tvg::free(trimmedCmds); + tvg::free(trimmedPts); // after path decoding we need to update distances and total length if (update_dist) updateDistances(); @@ -286,16 +286,16 @@ struct WgIndexedVertexBuffer WgIndexedVertexBuffer(WgGeometryBufferPool* pool, float scale = 1.0f) : pool(pool), scale(scale) { - vbuff = (Point*)malloc(sizeof(Point) * vreserved); - ibuff = (uint32_t*)malloc(sizeof(uint32_t) * ireserved); + vbuff = tvg::malloc(sizeof(Point) * vreserved); + ibuff = tvg::malloc(sizeof(uint32_t) * ireserved); dashed = pool->reqVertexBuffer(); } ~WgIndexedVertexBuffer() { pool->retVertexBuffer(dashed); - free(vbuff); - free(ibuff); + tvg::free(vbuff); + tvg::free(ibuff); } // reset buffer @@ -309,7 +309,7 @@ struct WgIndexedVertexBuffer { if (icount + grow >= ireserved) { ireserved *= 2; - ibuff = (uint32_t*) realloc(ibuff, ireserved * sizeof(uint32_t)); + ibuff = tvg::realloc(ibuff, ireserved * sizeof(uint32_t)); } } @@ -317,7 +317,7 @@ struct WgIndexedVertexBuffer { if (vcount + grow >= vreserved) { vreserved *= 2; - vbuff = (Point*) realloc(vbuff, vreserved * sizeof(Point)); + vbuff = tvg::realloc(vbuff, vreserved * sizeof(Point)); } } diff --git a/src/savers/gif/tvgGifEncoder.cpp b/src/savers/gif/tvgGifEncoder.cpp index a85db5b1..d50dda68 100644 --- a/src/savers/gif/tvgGifEncoder.cpp +++ b/src/savers/gif/tvgGifEncoder.cpp @@ -45,7 +45,6 @@ // Finally, call GifEnd() to close the file handle and free memory. // -#include #include "tvgMath.h" #include "tvgGifEncoder.h" @@ -465,7 +464,7 @@ static void _writeLzwImage(GifWriter* writer, uint32_t width, uint32_t height, u fputc(minCodeSize, f); // min code size 8 bits - GifLzwNode* codetree = (GifLzwNode*)malloc(sizeof(GifLzwNode)*4096); + GifLzwNode* codetree = tvg::malloc(sizeof(GifLzwNode)*4096); memset(codetree, 0, sizeof(GifLzwNode)*4096); int32_t curCode = -1; @@ -531,7 +530,7 @@ static void _writeLzwImage(GifWriter* writer, uint32_t width, uint32_t height, u fputc(0, f); // image block terminator - free(codetree); + tvg::free(codetree); } @@ -553,8 +552,8 @@ bool gifBegin(GifWriter* writer, const char* filename, uint32_t width, uint32_t writer->firstFrame = true; // allocate - writer->oldImage = (uint8_t*)malloc(width*height*4); - writer->tmpImage = (uint8_t*)malloc(width*height*4); + writer->oldImage = tvg::malloc(width*height*4); + writer->tmpImage = tvg::malloc(width*height*4); fputs("GIF89a", writer->f); @@ -618,8 +617,8 @@ bool gifEnd(GifWriter* writer) fputc(0x3b, writer->f); // end of file fclose(writer->f); - free(writer->oldImage); - free(writer->tmpImage); + tvg::free(writer->oldImage); + tvg::free(writer->tmpImage); writer->f = NULL; writer->oldImage = NULL; diff --git a/src/savers/gif/tvgGifEncoder.h b/src/savers/gif/tvgGifEncoder.h index 1714d37f..3dca235a 100644 --- a/src/savers/gif/tvgGifEncoder.h +++ b/src/savers/gif/tvgGifEncoder.h @@ -24,9 +24,7 @@ #ifndef TVG_GIF_ENCODER_H #define TVG_GIF_ENCODER_H -#include -#include - +#include "tvgCommon.h" typedef struct { diff --git a/src/savers/gif/tvgGifSaver.cpp b/src/savers/gif/tvgGifSaver.cpp index a0669562..2f62e2ca 100644 --- a/src/savers/gif/tvgGifSaver.cpp +++ b/src/savers/gif/tvgGifSaver.cpp @@ -37,7 +37,7 @@ void GifSaver::run(unsigned tid) auto w = static_cast(vsize[0]); auto h = static_cast(vsize[1]); - buffer = (uint32_t*)realloc(buffer, sizeof(uint32_t) * w * h); + buffer = tvg::realloc(buffer, sizeof(uint32_t) * w * h); canvas->target(buffer, w, w, h, ColorSpace::ABGR8888S); canvas->push(bg); bg = nullptr; @@ -99,10 +99,10 @@ bool GifSaver::close() if (animation && animation->picture()->refCnt() <= 1) delete(animation); animation = nullptr; - free(path); + tvg::free(path); path = nullptr; - free(buffer); + tvg::free(buffer); buffer = nullptr; return true;