3 /******************************************************************************
5 * Copyright (c) 2019 AT&T Intellectual Property.
6 * Copyright (c) 2018-2019 Nokia.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 ******************************************************************************/
22 // Standard Includes: ANSI C/C++, MSA, and Third-Party Libraries
26 // Local Includes: Application specific classes, functions, and libraries
33 allocator() = default;
35 allocator(void* data, size_t size) { reset(data, size); }
37 template <typename T, std::size_t SZ>
38 explicit allocator(T (&buf)[SZ]) : allocator(buf, SZ * sizeof(T)) { }
40 void reset(void* data, size_t size)
42 std::size_t space = size;
43 m_begin = static_cast<uint8_t*>(std::align(alignment, size, data, space));
44 m_end = m_begin ? m_begin + space : nullptr;
49 uint8_t* alloc_bytes(std::size_t size)
51 std::size_t const esize = (size + alignment - 1) & -alignment;
52 if (m_current + esize > m_end) { return nullptr; } //throw std::bad_alloc();
54 uint8_t* const rval = m_current;
59 size_t add_ref() { return ++m_ref_counter; }
62 if (m_ref_counter) --m_ref_counter;
63 if (m_ref_counter == 0) reset();
66 size_t ref_counter() const { return m_ref_counter; }
68 //called by last release() when m_ref_counter becomes 0
77 alignment = alignof(std::size_t)
80 uint8_t* m_begin { nullptr }; //set in reset()
81 uint8_t* m_current { nullptr }; //changed by alloc_bytes
82 uint8_t* m_end { nullptr };
84 size_t m_ref_counter {0};