summaryrefslogtreecommitdiff
path: root/source/libs/asura-lib-utils/io/data_buffer.cpp
blob: 000869d0bcf7b6f59c4ea1b2209f204a2990b6b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <cstdlib>
#include <cstring>
#include "data_buffer.h"

using namespace AEThreading;

namespace AsuraEngine
{
	namespace IO
	{

		DataBuffer::DataBuffer(DataBuffer& src)
		{
			Load(src);
		}

		DataBuffer::DataBuffer(std::size_t size)
			: mSize(size)
			, mBytes(nullptr)
		{
			lock(mMutex);
			mBytes = new byte[size];
			memset(mBytes, 0, size);
		}

		DataBuffer::DataBuffer(const void* data, std::size_t size)
			: mSize(size)
			, mBytes(nullptr)
		{
			Load(data, size);
		}

		DataBuffer::~DataBuffer()
		{
			delete[] mBytes;
		}

		void DataBuffer::Refactor(size_t size)
		{
			lock(mMutex);
			if (!mBytes || mSize != size)
			{
				delete[] mBytes;
				mBytes = new byte[size];
				mSize = size;
			}
			memset(mBytes, 0, size * sizeof(byte));
		}

		size_t DataBuffer::Load(DataBuffer& db)
		{
			return Load(db.GetData(), db.GetSize());
		}

		size_t DataBuffer::Load(const void* data, std::size_t size)
		{
			lock(mMutex);
			size_t len = mSize > size ? size : mSize;
			memcpy(mBytes, data, len);
			return len;
		}

		void DataBuffer::Move(void* bytes, std::size_t size)
		{
			lock(mMutex);
			if (!mBytes)
			{
				delete[] mBytes;
			}
			mBytes = (byte*)bytes;
			mSize = size;
		}

		byte* DataBuffer::GetData()
		{
			return mBytes;
		}

		void DataBuffer::Clear()
		{
			lock(mMutex);
			if (mBytes)
				memset(mBytes, 0, mSize);
		}
		
		std::size_t DataBuffer::GetSize()
		{
			return mSize;
		}

	}
}