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
24 #include <new> //std::bad_alloc
28 // Platform Includes: Platform specific classes, functions, and libraries
30 // Local Includes: Application specific classes, functions, and libraries
34 template <class ALLOC>
35 class chunked_allocator
38 using allocator_t = ALLOC;
42 put_chunk(new_chunk());
46 decltype(auto) emplace_back(TSEQ& v)
50 return v.emplace_back(m_allocator);
52 catch (std::bad_alloc const&)
54 put_chunk(new_chunk());
55 return v.emplace_back(m_allocator);
59 uint8_t* alloc_bytes(std::size_t sz)
61 //TODO: unify allocation: now it returns nullptr instead of throw!
62 auto* ret = m_allocator.alloc_bytes(sz);
65 put_chunk(new_chunk());
66 ret = m_allocator.alloc_bytes(sz);
72 static constexpr std::size_t CHUNK_SIZE = 32*1024;
73 using chunk_t = std::array<uint8_t, CHUNK_SIZE>;
74 using chunks_t = std::list<chunk_t>;
76 chunk_t& new_chunk() { return m_chunks.emplace_back(); }
77 void put_chunk(chunk_t& c) { m_allocator.reset(c.data(), c.size()); }
80 allocator_t m_allocator;
84 } //end: namespace asn