Line data Source code
1 : /** 2 : Copyright (c) 2017-2022 Roman Katuntsev <sbkarr@stappler.org> 3 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev> 4 : 5 : Permission is hereby granted, free of charge, to any person obtaining a copy 6 : of this software and associated documentation files (the "Software"), to deal 7 : in the Software without restriction, including without limitation the rights 8 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 : copies of the Software, and to permit persons to whom the Software is 10 : furnished to do so, subject to the following conditions: 11 : 12 : The above copyright notice and this permission notice shall be included in 13 : all copies or substantial portions of the Software. 14 : 15 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 : THE SOFTWARE. 22 : **/ 23 : 24 : #ifndef STAPPLER_CORE_MEMORY_SPMEMPOOLAPI_H_ 25 : #define STAPPLER_CORE_MEMORY_SPMEMPOOLAPI_H_ 26 : 27 : #include "SPMemPoolInterface.h" 28 : 29 : namespace STAPPLER_VERSIONIZED stappler::memory { 30 : 31 : using namespace mempool::base; 32 : 33 : static constexpr int SUCCESS = 0; 34 : 35 : namespace pool { 36 : 37 : using namespace mempool::base::pool; 38 : 39 : template<typename _Pool = pool_t *> 40 : class context { 41 : public: 42 : using pool_type = _Pool; 43 : 44 535825 : explicit context(pool_type &__m) : _pool(std::addressof(__m)), _owns(false) { push(); } 45 : context(pool_type &__m, uint32_t tag) : _pool(std::addressof(__m)), _owns(false) { push(tag); } 46 535786 : ~context() { if (_owns) { pop(); } } 47 : 48 : context(const context &) = delete; 49 : context& operator=(const context &) = delete; 50 : 51 : context(context && u) noexcept 52 : : _pool(u._pool), _owns(u._owns) { 53 : u._pool = 0; 54 : u._owns = false; 55 : } 56 : 57 : context & operator=(context && u) noexcept { 58 : if (_owns) { 59 : pop(); 60 : } 61 : 62 : context(std::move(u)).swap(*this); 63 : 64 : u._pool = 0; 65 : u._owns = false; 66 : return *this; 67 : } 68 : 69 535790 : void push() { 70 535790 : if (_pool && !_owns) { 71 535843 : pool::push(*_pool); 72 535787 : _owns = true; 73 : } 74 535734 : } 75 : 76 : void push(uint32_t tag) { 77 : if (_pool && !_owns) { 78 : pool::push(*_pool, tag); 79 : _owns = true; 80 : } 81 : } 82 : 83 535836 : void pop() { 84 535836 : if (_owns) { 85 535866 : pool::pop(); 86 535728 : _owns = false; 87 : } 88 535698 : } 89 : 90 : void swap(context &u) noexcept { 91 : std::swap(_pool, u._pool); 92 : std::swap(_owns, u._owns); 93 : } 94 : 95 : bool owns() const noexcept { return _owns; } 96 : explicit operator bool() const noexcept { return owns(); } 97 : pool_type* pool() const noexcept { return _pool; } 98 : 99 : private: 100 : pool_type *_pool; 101 : bool _owns; 102 : }; 103 : 104 : } 105 : 106 : } 107 : 108 : #endif /* STAPPLER_CORE_MEMORY_SPMEMPOOLAPI_H_ */