loaders: revise code from cce4b443b3

use copy variable instead of additional buffer pointer.
This commit is contained in:
Hermet Park 2021-06-21 20:18:08 +09:00 committed by Hermet Park
parent cce4b443b3
commit 0df8c00519
7 changed files with 48 additions and 43 deletions

View file

@ -989,7 +989,7 @@ public:
* *
* @param[in] data A pointer to a memory location where the content of the picture file is stored. * @param[in] data A pointer to a memory location where the content of the picture file is stored.
* @param[in] size The size in bytes of the memory occupied by the @p data. * @param[in] size The size in bytes of the memory occupied by the @p data.
* @param[in] copy Decides whether the data should be copied into the local buffer. * @param[in] copy Decides whether the data should be copied into the engine local buffer.
* *
* @retval Result::Success When succeed. * @retval Result::Success When succeed.
* @retval Result::InvalidArguments In case no data are provided or the @p size is zero or less. * @retval Result::InvalidArguments In case no data are provided or the @p size is zero or less.
@ -997,6 +997,8 @@ public:
* @retval Result::Unknown If an error occurs at a later stage. * @retval Result::Unknown If an error occurs at a later stage.
* *
* @note: This api supports only SVG format * @note: This api supports only SVG format
*
* @warning: you have responsibility to release the @p data memory if the @p copy is true
*/ */
Result load(const char* data, uint32_t size, bool copy = false) noexcept; Result load(const char* data, uint32_t size, bool copy = false) noexcept;

View file

@ -53,7 +53,7 @@ Result Picture::load(const std::string& path) noexcept
} }
Result Picture::load(const char* data, uint32_t size, bool copy /*=false*/) noexcept Result Picture::load(const char* data, uint32_t size, bool copy) noexcept
{ {
if (!data || size <= 0) return Result::InvalidArguments; if (!data || size <= 0) return Result::InvalidArguments;

View file

@ -2563,12 +2563,12 @@ static bool _svgLoaderParserForValidCheck(void* data, SimpleXMLType type, const
} }
void SvgLoader::clearBuffer() void SvgLoader::clear()
{ {
free(buffer); if (copy) free((char*)content);
size = 0; size = 0;
buffer = nullptr;
content = nullptr; content = nullptr;
copy = false;
} }
@ -2644,17 +2644,16 @@ bool SvgLoader::header()
bool SvgLoader::open(const char* data, uint32_t size, bool copy) bool SvgLoader::open(const char* data, uint32_t size, bool copy)
{ {
clearBuffer(); clear();
if (copy) { if (copy) {
buffer = (char*)malloc(size); content = (char*)malloc(size);
if (!buffer) return false; if (!content) return false;
memcpy(buffer, data, size); memcpy((char*)content, data, size);
this->content = buffer; } else content = data;
} else {
this->content = data;
}
this->size = size; this->size = size;
this->copy = copy;
return header(); return header();
} }
@ -2662,8 +2661,7 @@ bool SvgLoader::open(const char* data, uint32_t size, bool copy)
bool SvgLoader::open(const string& path) bool SvgLoader::open(const string& path)
{ {
//TODO: verify memory leak if open() is called multiple times. clear();
clearBuffer();
ifstream f; ifstream f;
f.open(path); f.open(path);
@ -2711,7 +2709,7 @@ bool SvgLoader::close()
loaderData.doc = nullptr; loaderData.doc = nullptr;
loaderData.stack.reset(); loaderData.stack.reset();
clearBuffer(); clear();
return true; return true;
} }

View file

@ -29,13 +29,14 @@ class SvgLoader : public Loader, public Task
{ {
public: public:
string filePath; string filePath;
char* buffer = nullptr;
const char* content = nullptr; const char* content = nullptr;
uint32_t size = 0; uint32_t size = 0;
SvgLoaderData loaderData; SvgLoaderData loaderData;
unique_ptr<Scene> root; unique_ptr<Scene> root;
bool copy = false;
SvgLoader(); SvgLoader();
~SvgLoader(); ~SvgLoader();
@ -51,7 +52,7 @@ public:
unique_ptr<Scene> scene() override; unique_ptr<Scene> scene() override;
private: private:
void clearBuffer(); void clear();
}; };

View file

@ -542,4 +542,4 @@ unique_ptr<Scene> tvgLoadData(const char *ptr, uint32_t size)
} }
return move(scene); return move(scene);
} }

View file

@ -31,12 +31,13 @@
/* Internal Class Implementation */ /* Internal Class Implementation */
/************************************************************************/ /************************************************************************/
void TvgLoader::clearBuffer() void TvgLoader::clear()
{ {
free(buffer); if (copy) free((char*)data);
size = 0; data = nullptr;
buffer = nullptr;
pointer = nullptr; pointer = nullptr;
size = 0;
copy = false;
} }
@ -52,7 +53,7 @@ TvgLoader::~TvgLoader()
bool TvgLoader::open(const string &path) bool TvgLoader::open(const string &path)
{ {
//TODO: verify memory leak if open() is called multiple times. clear();
ifstream f; ifstream f;
f.open(path, ifstream::in | ifstream::binary | ifstream::ate); f.open(path, ifstream::in | ifstream::binary | ifstream::ate);
@ -62,40 +63,41 @@ bool TvgLoader::open(const string &path)
size = f.tellg(); size = f.tellg();
f.seekg(0, ifstream::beg); f.seekg(0, ifstream::beg);
buffer = (char*)malloc(size); copy = true;
if (!buffer) { data = (char*)malloc(size);
size = 0; if (!data) {
clear();
f.close(); f.close();
return false; return false;
} }
if (!f.read(buffer, size)) if (!f.read((char*)data, size))
{ {
clearBuffer(); clear();
f.close(); f.close();
return false; return false;
} }
f.close(); f.close();
pointer = buffer; pointer = data;
return tvgValidateData(pointer, size); return tvgValidateData(pointer, size);
} }
bool TvgLoader::open(const char *data, uint32_t size, bool copy) bool TvgLoader::open(const char *data, uint32_t size, bool copy)
{ {
if (copy) { clear();
if (buffer) clearBuffer();
buffer = (char*)malloc(size);
if (!buffer) return false;
memcpy(buffer, data, size);
this->pointer = buffer;
} else {
this->pointer = data;
}
if (copy) {
this->data = (char*)malloc(size);
if (!this->data) return false;
memcpy((char*)this->data, data, size);
} else this->data = data;
this->pointer = this->data;
this->size = size; this->size = size;
this->copy = copy;
return tvgValidateData(pointer, size); return tvgValidateData(pointer, size);
} }
@ -112,7 +114,7 @@ bool TvgLoader::read()
bool TvgLoader::close() bool TvgLoader::close()
{ {
this->done(); this->done();
clearBuffer(); clear();
return true; return true;
} }
@ -120,7 +122,7 @@ void TvgLoader::run(unsigned tid)
{ {
if (root) root.reset(); if (root) root.reset();
root = tvgLoadData(pointer, size); root = tvgLoadData(pointer, size);
if (!root) clearBuffer(); if (!root) clear();
} }
unique_ptr<Scene> TvgLoader::scene() unique_ptr<Scene> TvgLoader::scene()

View file

@ -28,12 +28,14 @@
class TvgLoader : public Loader, public Task class TvgLoader : public Loader, public Task
{ {
public: public:
char* buffer = nullptr; const char* data = nullptr;
const char* pointer = nullptr; const char* pointer = nullptr;
uint32_t size = 0; uint32_t size = 0;
unique_ptr<Scene> root = nullptr; unique_ptr<Scene> root = nullptr;
bool copy = false;
~TvgLoader(); ~TvgLoader();
using Loader::open; using Loader::open;
@ -46,7 +48,7 @@ public:
unique_ptr<Scene> scene() override; unique_ptr<Scene> scene() override;
private: private:
void clearBuffer(); void clear();
}; };
#endif //_TVG_TVG_LOADER_H_ #endif //_TVG_TVG_LOADER_H_