blob: c22d38502df083646589f4b5d4e9d7e471fd2126 (
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
|
#include "drape/cpu_buffer.hpp"
#include "base/math.hpp"
#include "base/shared_buffer_manager.hpp"
#include "base/assert.hpp"
#include "std/cstring.hpp"
namespace dp
{
CPUBuffer::CPUBuffer(uint8_t elementSize, uint16_t capacity)
: base_t(elementSize, capacity)
{
uint32_t memorySize = my::NextPowOf2(GetCapacity() * GetElementSize());
m_memory = SharedBufferManager::instance().reserveSharedBuffer(memorySize);
m_memoryCursor = NonConstData();
}
CPUBuffer::~CPUBuffer()
{
m_memoryCursor = NULL;
SharedBufferManager::instance().freeSharedBuffer(m_memory->size(), m_memory);
}
void CPUBuffer::UploadData(void const * data, uint16_t elementCount)
{
uint32_t byteCountToCopy = GetElementSize() * elementCount;
#ifdef DEBUG
// Memory check
ASSERT(GetCursor() + byteCountToCopy <= Data() + m_memory->size(), ());
#endif
memcpy(GetCursor(), data, byteCountToCopy);
base_t::UploadData(elementCount);
}
void CPUBuffer::Seek(uint16_t elementNumber)
{
uint32_t offsetFromBegin = GetElementSize() * elementNumber;
ASSERT(Data() + offsetFromBegin <= Data() + m_memory->size(), ());
base_t::Seek(elementNumber);
m_memoryCursor = NonConstData() + offsetFromBegin;
}
uint16_t CPUBuffer::GetCurrentElementNumber() const
{
uint16_t pointerDiff = GetCursor() - Data();
ASSERT(pointerDiff % GetElementSize() == 0, ());
return pointerDiff / GetElementSize();
}
unsigned char const * CPUBuffer::Data() const
{
return &((*m_memory)[0]);
}
unsigned char * CPUBuffer::NonConstData()
{
return &((*m_memory)[0]);
}
unsigned char * CPUBuffer::GetCursor() const
{
return m_memoryCursor;
}
} // namespace dp
|