X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?p=sim%2Fe2-interface.git;a=blobdiff_plain;f=e2sim%2Fe2apv1sim%2Fricsim%2Fsrc%2FASN1%2Fasn%2Fchunked_allocator.hpp;fp=e2sim%2Fe2apv1sim%2Fricsim%2Fsrc%2FASN1%2Fasn%2Fchunked_allocator.hpp;h=008be98f4ca1edabefae2950f343f97780f1fc23;hp=0000000000000000000000000000000000000000;hb=3ebf932d23dcbec9ed19f4a51f9d00a0a54f5124;hpb=6896318f2b4ff01b4a88b16019c3dc93b0b693f5 diff --git a/e2sim/e2apv1sim/ricsim/src/ASN1/asn/chunked_allocator.hpp b/e2sim/e2apv1sim/ricsim/src/ASN1/asn/chunked_allocator.hpp new file mode 100755 index 0000000..008be98 --- /dev/null +++ b/e2sim/e2apv1sim/ricsim/src/ASN1/asn/chunked_allocator.hpp @@ -0,0 +1,84 @@ +#pragma once + +/******************************************************************************* +* +* Copyright (c) 2019 AT&T Intellectual Property. +* Copyright (c) 2018-2019 Nokia. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +*******************************************************************************/ + +// Standard Includes: ANSI C/C++, MSA, and Third-Party Libraries +#include +#include //std::bad_alloc +#include +#include + +// Platform Includes: Platform specific classes, functions, and libraries + +// Local Includes: Application specific classes, functions, and libraries + +namespace asn { + +template +class chunked_allocator +{ +public: + using allocator_t = ALLOC; + + chunked_allocator() + { + put_chunk(new_chunk()); + } + + template + decltype(auto) emplace_back(TSEQ& v) + { + try + { + return v.emplace_back(m_allocator); + } + catch (std::bad_alloc const&) + { + put_chunk(new_chunk()); + return v.emplace_back(m_allocator); + } + } + + uint8_t* alloc_bytes(std::size_t sz) + { + //TODO: unify allocation: now it returns nullptr instead of throw! + auto* ret = m_allocator.alloc_bytes(sz); + if (!ret) + { + put_chunk(new_chunk()); + ret = m_allocator.alloc_bytes(sz); + } + return ret; + } + +private: + static constexpr std::size_t CHUNK_SIZE = 32*1024; + using chunk_t = std::array; + using chunks_t = std::list; + + chunk_t& new_chunk() { return m_chunks.emplace_back(); } + void put_chunk(chunk_t& c) { m_allocator.reset(c.data(), c.size()); } + + + allocator_t m_allocator; + chunks_t m_chunks; +}; + +} //end: namespace asn