Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions cocos/base/CCData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Data& Data::operator= (Data&& other)

void Data::move(Data& other)
{
clear();
if(_bytes != other._bytes) clear();

_bytes = other._bytes;
_size = other._size;
Expand All @@ -106,27 +106,35 @@ ssize_t Data::getSize() const
return _size;
}

void Data::copy(const unsigned char* bytes, const ssize_t size)
ssize_t Data::copy(const unsigned char* bytes, const ssize_t size)
{
clear();
CCASSERT(size >= 0, "copy size should be non-negative");
CCASSERT(bytes, "bytes should not be nullptr");

if (size <= 0) return 0;

if (size > 0)
if (bytes != _bytes)
{
_size = size;
clear();
_bytes = (unsigned char*)malloc(sizeof(unsigned char) * _size);
memcpy(_bytes, bytes, _size);
}

_size = size;
return _size;
}

void Data::fastSet(unsigned char* bytes, const ssize_t size)
{
CCASSERT(size >= 0, "fastSet size should be non-negative");
CCASSERT(bytes, "bytes should not be nullptr");
_bytes = bytes;
_size = size;
}

void Data::clear()
{
free(_bytes);
if(_bytes) free(_bytes);
_bytes = nullptr;
_size = 0;
}
Expand Down
3 changes: 2 additions & 1 deletion cocos/base/CCData.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ class CC_DLL Data
* @note This method will copy the whole buffer.
* Developer should free the pointer after invoking this method.
* @see Data::fastSet
* @return The size of bytes copied, return 0 if size <= 0
*/
void copy(const unsigned char* bytes, const ssize_t size);
ssize_t copy(const unsigned char* bytes, const ssize_t size);

/** Fast set the buffer pointer and its size. Please use it carefully.
* @param bytes The buffer pointer, note that it have to be allocated by 'malloc' or 'calloc',
Expand Down