-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryBlock.cpp
executable file
·71 lines (59 loc) · 1.25 KB
/
MemoryBlock.cpp
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
#include <new>
#include <stdio.h>
#include <string.h>
#include "MemoryBlock.h"
MemoryBlock::MemoryBlock(size_t capacity) :
m_capacity(capacity), m_size(0)
{
m_buffer = new byte[m_capacity];
memset(m_buffer, 0, sizeof(m_buffer));
}
MemoryBlock::MemoryBlock(byte * buffer, size_t length) :
m_capacity(length)
{
m_buffer = new byte[m_capacity];
memset(m_buffer, 0, sizeof(m_buffer));
memcpy(m_buffer, buffer, length);
}
MemoryBlock::~MemoryBlock()
{
delete [] m_buffer;
m_capacity = 0;
m_size = 0;
}
size_t MemoryBlock::getSize(void) const
{
return m_size;
}
void MemoryBlock::setSize(size_t sz)
{
m_size = sz;
}
size_t MemoryBlock::capacity() const
{
return m_capacity;
}
void MemoryBlock::copy(const MemoryBlock & b)
{
// TODO: sanity checks.
memcpy(m_buffer, b.m_buffer, m_capacity);
m_size = b.m_size;
}
void MemoryBlock::get(byte * buffer, size_t offset, size_t nbytes)
{
copy(buffer, m_buffer + offset, nbytes);
};
size_t MemoryBlock::put(byte * buffer, size_t offset, size_t nbytes)
{
copy(m_buffer + offset, buffer, nbytes);
return offset + nbytes;
}
void MemoryBlock::copy(byte * dst, byte * src, size_t nbytes)
{
memcpy(dst, src, nbytes);
}
void MemoryBlock::clear()
{
m_size = 0;
memset(m_buffer, 0, m_capacity);
}