diff options
author | John Turner <jturner.usa@gmail.com> | 2025-09-14 00:16:10 -0400 |
---|---|---|
committer | John Turner <jturner.usa@gmail.com> | 2025-09-14 00:16:10 -0400 |
commit | 13e0821fd783a1d5083d825af53cf20e8dcbfd76 (patch) | |
tree | 1ea363b0f13b3e87d177100e6ae6b9f30a2de1b8 /subprojects/boost-sqlite/include/boost/sqlite/memory.hpp | |
parent | aa55cb93036a89c64c08e08f4e1de4fa1fd5a775 (diff) | |
parent | efcea3a80da7c4479d5fe168435ecc9fd06bdc72 (diff) | |
download | sqlite-kv-bench-13e0821fd783a1d5083d825af53cf20e8dcbfd76.tar.gz |
Merge commit 'efcea3a80da7c4479d5fe168435ecc9fd06bdc72' as 'subprojects/boost-sqlite'
Diffstat (limited to 'subprojects/boost-sqlite/include/boost/sqlite/memory.hpp')
-rw-r--r-- | subprojects/boost-sqlite/include/boost/sqlite/memory.hpp | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/subprojects/boost-sqlite/include/boost/sqlite/memory.hpp b/subprojects/boost-sqlite/include/boost/sqlite/memory.hpp new file mode 100644 index 0000000..e136a0e --- /dev/null +++ b/subprojects/boost-sqlite/include/boost/sqlite/memory.hpp @@ -0,0 +1,105 @@ +// Copyright (c) 2023 Klemens D. Morgenstern +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +#ifndef BOOST_SQLITE_MEMORY_HPP +#define BOOST_SQLITE_MEMORY_HPP + +#include <boost/sqlite/detail/config.hpp> + +#include <memory> + +BOOST_SQLITE_BEGIN_NAMESPACE +/// A tag to allow `operator new` +/// @ingroup reference +struct memory_tag {}; +BOOST_SQLITE_END_NAMESPACE + + +inline void *operator new ( std::size_t size, boost::sqlite::memory_tag) noexcept +{ + using namespace boost::sqlite; + return sqlite3_malloc64(size); +} +inline void *operator new[]( std::size_t size, boost::sqlite::memory_tag) noexcept +{ + using namespace boost::sqlite; + return sqlite3_malloc64(size); +} + +inline void operator delete ( void* ptr, boost::sqlite::memory_tag) noexcept +{ + using namespace boost::sqlite; + return sqlite3_free(ptr); +} + +BOOST_SQLITE_BEGIN_NAMESPACE + +template<typename T> +void delete_(T * t) +{ + struct scoped_free + { + void * p; + ~scoped_free() + { + sqlite3_free(p); + } + }; + scoped_free _{t}; + t->~T(); +} + +namespace detail +{ + +template<typename T> +struct deleter +{ + void operator()(T* t) + { + delete_(t); + } +}; + +template<typename T> +struct deleter<T[]> +{ + static_assert(std::is_trivially_destructible<T>::value, "T[] needs to be trivially destructible"); + void operator()(T* t) + { + sqlite3_free(t); + } +}; + +template<> +struct deleter<void> +{ + void operator()(void* t) + { + sqlite3_free(t); + } +}; +} + +template<typename T> +using unique_ptr = std::unique_ptr<T, detail::deleter<T>>; + +template<typename T> +inline std::size_t msize(const unique_ptr<T> & ptr) +{ + return sqlite3_msize(ptr.get()); +} + +template<typename T, typename ... Args> +unique_ptr<T> make_unique(Args && ... args) +{ + unique_ptr<void> up{sqlite3_malloc64(sizeof(T))}; + unique_ptr<T> res{new (up.get()) T(std::forward<Args>(args)...)}; + up.release(); + return res; +} + +BOOST_SQLITE_END_NAMESPACE + +#endif //BOOST_SQLITE_MEMORY_HPP |