xref: /aosp_15_r20/external/angle/src/common/MemoryBuffer.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 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 #ifndef COMMON_MEMORYBUFFER_H_
8 #define COMMON_MEMORYBUFFER_H_
9 
10 #include "common/Optional.h"
11 #include "common/angleutils.h"
12 #include "common/debug.h"
13 
14 #include <stdint.h>
15 #include <cstddef>
16 
17 namespace angle
18 {
19 
20 class MemoryBuffer final : NonCopyable
21 {
22   public:
23     MemoryBuffer() = default;
24     ~MemoryBuffer();
25 
26     MemoryBuffer(MemoryBuffer &&other);
27     MemoryBuffer &operator=(MemoryBuffer &&other);
28 
29     // On success, size will be equal to capacity.
30     [[nodiscard]] bool resize(size_t size);
31     // Sets size bound by capacity.
setSize(size_t size)32     void setSize(size_t size)
33     {
34         ASSERT(size <= mCapacity);
35         mSize = size;
36     }
setSizeToCapacity()37     void setSizeToCapacity() { mSize = mCapacity; }
clear()38     void clear() { (void)resize(0); }
size()39     size_t size() const { return mSize; }
capacity()40     size_t capacity() const { return mCapacity; }
empty()41     bool empty() const { return mSize == 0; }
42 
data()43     const uint8_t *data() const { return mData; }
data()44     uint8_t *data()
45     {
46         ASSERT(mData);
47         return mData;
48     }
49 
50     uint8_t &operator[](size_t pos)
51     {
52         ASSERT(pos < mSize);
53         return mData[pos];
54     }
55     const uint8_t &operator[](size_t pos) const
56     {
57         ASSERT(pos < mSize);
58         return mData[pos];
59     }
60 
61     void fill(uint8_t datum);
62 
63   private:
64     size_t mSize     = 0;
65     size_t mCapacity = 0;
66     uint8_t *mData   = nullptr;
67 };
68 
69 class ScratchBuffer final : NonCopyable
70 {
71   public:
72     ScratchBuffer();
73     // If we request a scratch buffer requesting a smaller size this many times, release and
74     // recreate the scratch buffer. This ensures we don't have a degenerate case where we are stuck
75     // hogging memory.
76     ScratchBuffer(uint32_t lifetime);
77     ~ScratchBuffer();
78 
79     ScratchBuffer(ScratchBuffer &&other);
80     ScratchBuffer &operator=(ScratchBuffer &&other);
81 
82     // Returns true with a memory buffer of the requested size, or false on failure.
83     bool get(size_t requestedSize, MemoryBuffer **memoryBufferOut);
84 
85     // Same as get, but ensures new values are initialized to a fixed constant.
86     bool getInitialized(size_t requestedSize, MemoryBuffer **memoryBufferOut, uint8_t initValue);
87 
88     // Ticks the release counter for the scratch buffer. Also done implicitly in get().
89     void tick();
90 
91     void clear();
92 
93   private:
94     bool getImpl(size_t requestedSize, MemoryBuffer **memoryBufferOut, Optional<uint8_t> initValue);
95 
96     uint32_t mLifetime;
97     uint32_t mResetCounter;
98     MemoryBuffer mScratchMemory;
99 };
100 
101 }  // namespace angle
102 
103 #endif  // COMMON_MEMORYBUFFER_H_
104