Line data Source code
1 : //
2 : // Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3 : // Use of this source code is governed by a BSD-style license that can be
4 : // found in the LICENSE file.
5 : //
6 :
7 : #include "common/MemoryBuffer.h"
8 :
9 : #include <algorithm>
10 : #include <cstdlib>
11 :
12 : #include "common/debug.h"
13 :
14 : namespace rx
15 : {
16 :
17 0 : MemoryBuffer::MemoryBuffer()
18 : : mSize(0),
19 0 : mData(NULL)
20 : {
21 0 : }
22 :
23 0 : MemoryBuffer::~MemoryBuffer()
24 : {
25 0 : free(mData);
26 0 : mData = NULL;
27 0 : }
28 :
29 0 : bool MemoryBuffer::resize(size_t size)
30 : {
31 0 : if (size == 0)
32 : {
33 0 : free(mData);
34 0 : mData = NULL;
35 0 : mSize = 0;
36 0 : return true;
37 : }
38 :
39 0 : if (size == mSize)
40 : {
41 0 : return true;
42 : }
43 :
44 : // Only reallocate if the size has changed.
45 0 : uint8_t *newMemory = reinterpret_cast<uint8_t*>(malloc(sizeof(uint8_t) * size));
46 0 : if (newMemory == NULL)
47 : {
48 0 : return false;
49 : }
50 :
51 0 : if (mData)
52 : {
53 : // Copy the intersection of the old data and the new data
54 0 : std::copy(mData, mData + std::min(mSize, size), newMemory);
55 0 : free(mData);
56 : }
57 :
58 0 : mData = newMemory;
59 0 : mSize = size;
60 :
61 0 : return true;
62 : }
63 :
64 0 : size_t MemoryBuffer::size() const
65 : {
66 0 : return mSize;
67 : }
68 :
69 0 : const uint8_t *MemoryBuffer::data() const
70 : {
71 0 : return mData;
72 : }
73 :
74 0 : uint8_t *MemoryBuffer::data()
75 : {
76 0 : ASSERT(mData);
77 0 : return mData;
78 : }
79 :
80 : }
|