Line data Source code
1 : /** 2 : Copyright (c) 2019-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_SPMEMUSERDATA_H_ 25 : #define STAPPLER_CORE_MEMORY_SPMEMUSERDATA_H_ 26 : 27 : #include "SPMemPoolApi.h" 28 : #include "SPStringView.h" 29 : #include "SPCommon.h" 30 : 31 : namespace STAPPLER_VERSIONIZED stappler::memory::pool { 32 : 33 : void store(pool_t *, void *ptr, const StringView &key, memory::function<void()> && = nullptr); 34 : 35 : template <typename T = void> 36 89634 : inline T *get(pool_t *pool, const StringView &key) { 37 : struct Handle : AllocPool { 38 : void *pointer; 39 : memory::function<void()> callback; 40 : }; 41 : 42 89634 : void *ptr = nullptr; 43 89634 : if (pool::userdata_get(&ptr, key.data(), key.size(), pool) == SUCCESS) { 44 89640 : if (ptr) { 45 77348 : return (T *)((Handle *)ptr)->pointer; 46 : } 47 : } 48 12290 : return nullptr; 49 : } 50 : 51 6954 : inline void store(void *ptr, const StringView &key, memory::function<void()> &&cb = nullptr) { 52 6954 : store(acquire(), ptr, key, move(cb)); 53 6954 : } 54 : 55 : template <typename T = void> 56 125 : inline T *get(const StringView &key) { 57 125 : return get<T>(acquire(), key); 58 : } 59 : 60 : } 61 : 62 : #endif /* STAPPLER_CORE_MEMORY_SPMEMUSERDATA_H_ */