summaryrefslogtreecommitdiff
path: root/Kernel/DataBuffer.cpp
blob: 9be5bce330903ffcbcbcbf10a3b2e43b9fe0c681 (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
#include "DataBuffer.h"
#include "StdLib.h"

#define SANITIZE_DATABUFFER

DataBuffer::DataBuffer(size_t length)
    : m_length(length)
    , m_owned(true)
{
    m_data = new BYTE[m_length];
#ifdef SANITIZE_DATABUFFER
    memset(m_data, 0x1a, length);
#endif
}

DataBuffer::DataBuffer(BYTE* data, size_t length, ConstructionMode mode)
    : m_length(length)
{
    switch (mode) {
    case Copy:
        m_data = new BYTE[m_length];
        memcpy(m_data, data, m_length);
        m_owned = true;
        break;
    case Adopt:
        m_data = data;
        m_owned = true;
        break;
    case Wrap:
        m_data = data;
        m_owned = false;
        break;
    }
}

DataBuffer::~DataBuffer()
{
    clear();
#ifdef SANITIZE_DATABUFFER
    m_data = (BYTE*)0x88888888;
#endif
}

void DataBuffer::clear()
{
    if (m_owned) {
#ifdef SANITIZE_DATABUFFER
        memset(m_data, 0x99, m_length);
#endif
        delete [] m_data;
    }
    m_owned = false;
    m_data = nullptr;
    m_length = 0;
}

RefPtr<DataBuffer> DataBuffer::createUninitialized(size_t length)
{
    return adoptRef(new DataBuffer(length));
}

RefPtr<DataBuffer> DataBuffer::copy(const BYTE* data, size_t length)
{
    return adoptRef(new DataBuffer(const_cast<BYTE*>(data), length, Copy));
}

RefPtr<DataBuffer> DataBuffer::wrap(BYTE* data, size_t length)
{
    return adoptRef(new DataBuffer(data, length, Wrap));
}

RefPtr<DataBuffer> DataBuffer::adopt(BYTE* data, size_t length)
{
    return adoptRef(new DataBuffer(data, length, Adopt));
}