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 "libGLESv2/renderer/d3d/MemoryBuffer.h"
8#include "common/debug.h"
9
10#include <algorithm>
11#include <cstdlib>
12
13namespace rx
14{
15
16MemoryBuffer::MemoryBuffer()
17    : mSize(0),
18      mData(NULL)
19{
20}
21
22MemoryBuffer::~MemoryBuffer()
23{
24    free(mData);
25    mData = NULL;
26}
27
28bool MemoryBuffer::resize(size_t size)
29{
30    if (size == 0)
31    {
32        free(mData);
33        mData = NULL;
34        mSize = 0;
35    }
36    else
37    {
38        uint8_t *newMemory = reinterpret_cast<uint8_t*>(malloc(sizeof(uint8_t) * size));
39        if (newMemory == NULL)
40        {
41            return false;
42        }
43
44        if (mData)
45        {
46            // Copy the intersection of the old data and the new data
47            std::copy(mData, mData + std::min(mSize, size), newMemory);
48            free(mData);
49        }
50
51        mData = newMemory;
52        mSize = size;
53    }
54
55    return true;
56}
57
58size_t MemoryBuffer::size() const
59{
60    return mSize;
61}
62
63const uint8_t *MemoryBuffer::data() const
64{
65    return mData;
66}
67
68uint8_t *MemoryBuffer::data()
69{
70    ASSERT(mData);
71    return mData;
72}
73
74}
75