Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020 Project CHIP Authors
4 : * All rights reserved.
5 : *
6 : * Licensed under the Apache License, Version 2.0 (the "License");
7 : * you may not use this file except in compliance with the License.
8 : * You may obtain a copy of the License at
9 : *
10 : * http://www.apache.org/licenses/LICENSE-2.0
11 : *
12 : * Unless required by applicable law or agreed to in writing, software
13 : * distributed under the License is distributed on an "AS IS" BASIS,
14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : */
18 :
19 : /**
20 : * @file
21 : * Defines scoped auto-free buffers for CHIP.
22 : *
23 : */
24 :
25 : #pragma once
26 :
27 : #include <lib/support/CHIPMem.h>
28 : #include <lib/support/CodeUtils.h>
29 : #include <lib/support/Span.h>
30 :
31 : #include <type_traits>
32 : #include <utility>
33 :
34 : namespace chip {
35 : namespace Platform {
36 :
37 : namespace Impl {
38 :
39 : /**
40 : * Represents a memory buffer that is auto-freed in the destructor.
41 : *
42 : * This class uses void* underneath on purpose (rather than a unique_ptr like
43 : * 'Type') and uses templated type on Ptr(). This is to avoid template explosion
44 : * when the buffers are used for different types - only one implementation of
45 : * the class will be stored in flash.
46 : */
47 : template <class Impl>
48 : class ScopedMemoryBufferBase
49 : {
50 : public:
51 24946 : ScopedMemoryBufferBase() {}
52 : ScopedMemoryBufferBase(const ScopedMemoryBufferBase &) = delete;
53 : ScopedMemoryBufferBase & operator=(const ScopedMemoryBufferBase & other) = delete;
54 :
55 : ScopedMemoryBufferBase(ScopedMemoryBufferBase && other) { *this = std::move(other); }
56 :
57 7929 : ScopedMemoryBufferBase & operator=(ScopedMemoryBufferBase && other)
58 : {
59 7929 : if (this != &other)
60 : {
61 7929 : mBuffer = other.mBuffer;
62 7929 : other.mBuffer = nullptr;
63 : }
64 7929 : return *this;
65 : }
66 :
67 24946 : ~ScopedMemoryBufferBase() { Free(); }
68 :
69 : /** Check if a buffer is valid */
70 1564 : explicit operator bool() const { return mBuffer != nullptr; }
71 8055 : bool operator!() const { return mBuffer == nullptr; }
72 2 : inline bool IsNull() const { return mBuffer == nullptr; }
73 :
74 : /** Release memory used */
75 55567 : void Free()
76 : {
77 55567 : if (mBuffer == nullptr)
78 : {
79 41119 : return;
80 : }
81 14448 : Impl::MemoryFree(mBuffer);
82 14448 : mBuffer = nullptr;
83 : }
84 :
85 : protected:
86 32801 : void * Ptr() { return mBuffer; }
87 10413 : const void * Ptr() const { return mBuffer; }
88 :
89 : /**
90 : * Releases the underlying buffer.
91 : *
92 : * The buffer stops being managed and will not be auto-freed.
93 : */
94 1 : CHECK_RETURN_VALUE void * Release()
95 : {
96 1 : void * buffer = mBuffer;
97 1 : mBuffer = nullptr;
98 1 : return buffer;
99 : }
100 :
101 9875 : void Alloc(size_t size)
102 : {
103 9875 : Free();
104 9875 : mBuffer = Impl::MemoryAlloc(size);
105 9875 : }
106 :
107 4574 : void Calloc(size_t elementCount, size_t elementSize)
108 : {
109 4574 : Free();
110 4574 : mBuffer = Impl::MemoryCalloc(elementCount, elementSize);
111 4574 : }
112 :
113 : private:
114 : void * mBuffer = nullptr;
115 : };
116 :
117 : /**
118 : * Helper class that forwards memory management tasks to Platform::Memory* calls.
119 : */
120 : class PlatformMemoryManagement
121 : {
122 : public:
123 14444 : static void MemoryFree(void * p) { chip::Platform::MemoryFree(p); }
124 9870 : static void * MemoryAlloc(size_t size) { return chip::Platform::MemoryAlloc(size); }
125 4573 : static void * MemoryCalloc(size_t num, size_t size) { return chip::Platform::MemoryCalloc(num, size); }
126 : };
127 :
128 : } // namespace Impl
129 :
130 : /**
131 : * Represents a memory buffer allocated using chip::Platform::Memory*Alloc
132 : * methods.
133 : *
134 : * Use for RAII to auto-free after use.
135 : *
136 : * For a single element RAII with dtor, use Platform::UniquePtr<>
137 : */
138 : template <typename T, class MemoryManagement = Impl::PlatformMemoryManagement>
139 : class ScopedMemoryBuffer : public Impl::ScopedMemoryBufferBase<MemoryManagement>
140 : {
141 : friend class Impl::ScopedMemoryBufferBase<MemoryManagement>;
142 :
143 : public:
144 : using Base = Impl::ScopedMemoryBufferBase<MemoryManagement>;
145 :
146 : static_assert(std::is_trivially_destructible<T>::value, "Destructors won't get run");
147 :
148 32801 : T * Get() { return static_cast<T *>(Base::Ptr()); }
149 712 : T & operator[](size_t index) { return Get()[index]; }
150 :
151 10413 : const T * Get() const { return static_cast<const T *>(Base::Ptr()); }
152 100 : const T & operator[](size_t index) const { return Get()[index]; }
153 :
154 : /**
155 : * Releases the underlying buffer.
156 : *
157 : * The buffer stops being managed and will not be auto-freed.
158 : */
159 1 : CHECK_RETURN_VALUE T * Release() { return static_cast<T *>(Base::Release()); }
160 :
161 4574 : ScopedMemoryBuffer & Calloc(size_t elementCount)
162 : {
163 4574 : Base::Calloc(elementCount, sizeof(T));
164 4574 : ExecuteConstructors(elementCount);
165 4574 : return *this;
166 : }
167 :
168 9875 : ScopedMemoryBuffer & Alloc(size_t elementCount)
169 : {
170 9875 : Base::Alloc(elementCount * sizeof(T));
171 9875 : ExecuteConstructors(elementCount);
172 9875 : return *this;
173 : }
174 :
175 : private:
176 : template <typename U = T, std::enable_if_t<std::is_trivial<U>::value, int> = 0>
177 14449 : void ExecuteConstructors(size_t elementCount)
178 : {
179 : // Do nothing if our type is trivial. In particular, if we are a buffer
180 : // of integers, we should not go zero-initializing them here: either
181 : // caller wants that and called Calloc(), or it doesn't and we shouldn't
182 : // do it.
183 14449 : }
184 :
185 : template <typename U = T, std::enable_if_t<!std::is_trivial<U>::value, int> = 0>
186 0 : void ExecuteConstructors(size_t elementCount)
187 : {
188 0 : T * elementPtr = Get();
189 0 : if (elementPtr == nullptr)
190 : {
191 : // Alloc failed, don't bother.
192 0 : return;
193 : }
194 0 : for (size_t i = 0; i < elementCount; ++i)
195 : {
196 0 : new (&elementPtr[i]) T();
197 : }
198 : }
199 : };
200 :
201 : /**
202 : * Represents a memory buffer with buffer size allocated using chip::Platform::Memory*Alloc
203 : * methods.
204 : *
205 : * Use for RAII to auto-free after use.
206 : */
207 : template <typename T>
208 : class ScopedMemoryBufferWithSize : public ScopedMemoryBuffer<T>
209 : {
210 : public:
211 9972 : ScopedMemoryBufferWithSize() {}
212 264 : ScopedMemoryBufferWithSize(ScopedMemoryBufferWithSize && other) { *this = std::move(other); }
213 :
214 3015 : ScopedMemoryBufferWithSize & operator=(ScopedMemoryBufferWithSize && other)
215 : {
216 3015 : if (this != &other)
217 : {
218 3015 : mCount = other.mCount;
219 3015 : other.mCount = 0;
220 : }
221 3015 : ScopedMemoryBuffer<T>::operator=(std::move(other));
222 3015 : return *this;
223 : }
224 :
225 : // return the size as count of elements
226 7036 : inline size_t AllocatedSize() const { return mCount; }
227 :
228 50 : chip::Span<T> Span() { return chip::Span<T>(this->Get(), AllocatedSize()); }
229 : chip::Span<const T> Span() const { return chip::Span<T>(this->Get(), AllocatedSize()); }
230 :
231 16095 : void Free()
232 : {
233 16095 : mCount = 0;
234 16095 : ScopedMemoryBuffer<T>::Free();
235 16095 : }
236 :
237 : /**
238 : * Releases the underlying buffer.
239 : *
240 : * The buffer stops being managed and will not be auto-freed.
241 : */
242 : CHECK_RETURN_VALUE T * Release()
243 : {
244 : T * buffer = ScopedMemoryBuffer<T>::Release();
245 : mCount = 0;
246 : return buffer;
247 : }
248 :
249 361 : ScopedMemoryBufferWithSize & Calloc(size_t elementCount)
250 : {
251 361 : ScopedMemoryBuffer<T>::Calloc(elementCount);
252 361 : if (this->Get() != nullptr)
253 : {
254 361 : mCount = elementCount;
255 : }
256 361 : return *this;
257 : }
258 :
259 3200 : ScopedMemoryBufferWithSize & Alloc(size_t elementCount)
260 : {
261 3200 : ScopedMemoryBuffer<T>::Alloc(elementCount);
262 3200 : if (this->Get() != nullptr)
263 : {
264 3200 : mCount = elementCount;
265 : }
266 3200 : return *this;
267 : }
268 :
269 : // Allow creating ScopedMemoryBufferWithSize from Span, so we
270 : // don't have to reinvent it in various places.
271 : template <class U, typename = std::enable_if_t<sizeof(U) == sizeof(T) && std::is_convertible_v<U *, T *>>>
272 51 : ScopedMemoryBufferWithSize & CopyFromSpan(const chip::Span<const U> & span)
273 : {
274 : static_assert(std::is_trivially_copyable_v<U>, "Span<const U> must be trivially copyable");
275 :
276 51 : if (span.size() == 0)
277 : {
278 0 : Free();
279 0 : return *this;
280 : }
281 :
282 51 : ScopedMemoryBufferWithSize<T>::Alloc(span.size());
283 :
284 51 : if (AllocatedSize() > 0)
285 : {
286 51 : memcpy(ScopedMemoryBuffer<T>::Get(), span.data(), AllocatedSize());
287 : }
288 :
289 51 : return *this;
290 : }
291 :
292 : private:
293 : size_t mCount = 0;
294 : };
295 :
296 : } // namespace Platform
297 : } // namespace chip
|