Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2021 Project CHIP Authors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #pragma once
19 :
20 : #include <cstddef>
21 : #include <cstdint>
22 :
23 : namespace chip {
24 : /**
25 : * Memory allocator that uses a fixed-size buffer.
26 : *
27 : * This class allocates subsequent memory regions out of a fixed-size buffer.
28 : * Deallocation of specific regions is unsupported and it is assumed that the entire
29 : * buffer will be released at once.
30 : */
31 : class FixedBufferAllocator
32 : {
33 : public:
34 : FixedBufferAllocator() = default;
35 : FixedBufferAllocator(uint8_t * buffer, size_t capacity) { Init(buffer, capacity); }
36 :
37 : template <size_t N>
38 2 : explicit FixedBufferAllocator(uint8_t (&buffer)[N])
39 2 : {
40 2 : Init(buffer);
41 2 : }
42 :
43 2 : void Init(uint8_t * buffer, size_t capacity)
44 : {
45 2 : mBegin = buffer;
46 2 : mEnd = buffer + capacity;
47 2 : mAnyAllocFailed = false;
48 2 : }
49 :
50 : template <size_t N>
51 2 : void Init(uint8_t (&buffer)[N])
52 : {
53 2 : Init(buffer, N);
54 2 : }
55 :
56 : /**
57 : * Allocate a specified number of bytes.
58 : *
59 : * @param count Number of bytes to allocate.
60 : * @return Pointer to the allocated memory region or nullptr on failure.
61 : */
62 : uint8_t * Alloc(size_t count);
63 :
64 : /**
65 : * Allocate memory for the specified data and copy the data into the allocated region.
66 : *
67 : * @param data Pointer to the data to be copied into the allocated memory region.
68 : * @param dataLen Size of the data to be copied into the allocated memory region.
69 : * @return Pointer to the allocated memory region or nullptr on failure.
70 : */
71 : uint8_t * Clone(const void * data, size_t dataLen);
72 :
73 : /**
74 : * Allocate memory for the specified string and copy the string, including
75 : * the null-character, into the allocated region.
76 : *
77 : * @param str Pointer to the string to be copied into the allocated memory region.
78 : * @return Pointer to the allocated memory region or nullptr on failure.
79 : */
80 : char * Clone(const char * str);
81 :
82 : /**
83 : * Returns whether any allocation has failed so far.
84 : */
85 2 : bool AnyAllocFailed() const { return mAnyAllocFailed; }
86 :
87 : private:
88 : FixedBufferAllocator(const FixedBufferAllocator &) = delete;
89 : void operator=(const FixedBufferAllocator &) = delete;
90 :
91 : uint8_t * mBegin = nullptr;
92 : uint8_t * mEnd = nullptr;
93 : bool mAnyAllocFailed = false;
94 : };
95 :
96 : } // namespace chip
|