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] 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::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.
*
* @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;

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;

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

View file

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

View file

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

View file

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

View file

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