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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#include <cstdlib>
#include <cstring>
#include "DataBuffer.h"
using namespace AEThreading;
namespace AsuraEngine
{
namespace FileSystem
{
DataBuffer::DataBuffer(DataBuffer& src)
: m_Size(0)
, m_Capacity(0)
, m_Bytes(nullptr)
{
// ʼ
lock(m_Mutex)
{
m_Capacity = src.m_Size;
m_Bytes = new byte[m_Capacity];
Clear();
Load(src);
}
}
DataBuffer::DataBuffer(std::size_t capacity)
: m_Size(0)
, m_Capacity(0)
, m_Bytes(nullptr)
{
lock(m_Mutex)
{
m_Capacity = capacity;
m_Bytes = new byte[m_Capacity];
Clear();
}
}
DataBuffer::DataBuffer(const void* data, std::size_t size)
: m_Capacity(0)
, m_Size(0)
, m_Bytes(nullptr)
{
lock(m_Mutex)
{
m_Capacity = size;
m_Bytes = new byte[m_Capacity];
Clear();
Load(data, size);
}
}
DataBuffer::~DataBuffer()
{
lock(m_Mutex)
{
delete[] m_Bytes;
}
}
void DataBuffer::Refactor(size_t capacity)
{
lock(m_Mutex)
{
if (!m_Bytes || m_Capacity != capacity)
{
if(m_Bytes)
delete[] m_Bytes;
m_Capacity = capacity;
m_Bytes = new byte[m_Capacity];
m_Size = 0;
}
Clear();
}
}
void DataBuffer::Load(DataBuffer& db)
{
lock(m_Mutex)
{
Load(db.GetData(), db.GetSize());
}
}
void DataBuffer::Load(const void* data, std::size_t size)
{
lock(m_Mutex)
{
ASSERT(m_Capacity >= size);
memcpy(m_Bytes, data, size);
m_Size = size;
}
}
void DataBuffer::Move(void* bytes, std::size_t size)
{
lock(m_Mutex)
{
if (m_Bytes == bytes)
{
// sizeֵڶļʱ
m_Size = size;
}
else
{
if (m_Bytes)
delete[] m_Bytes;
m_Bytes = (byte*)bytes;
m_Size = size;
m_Capacity = size;
}
}
}
byte* DataBuffer::GetData()
{
return m_Bytes;
}
void DataBuffer::Clear()
{
lock(m_Mutex)
{
if (m_Bytes)
{
memset(m_Bytes, 0, m_Size);
m_Size = 0;
}
}
}
std::size_t DataBuffer::GetSize()
{
return m_Size;
}
std::size_t DataBuffer::GetCapacity()
{
return m_Capacity;
}
void DataBuffer::Lock()
{
m_Mutex.Lock();
}
void DataBuffer::Unlock()
{
m_Mutex.Unlock();
}
}
}
|