From efcea3a80da7c4479d5fe168435ecc9fd06bdc72 Mon Sep 17 00:00:00 2001 From: John Turner Date: Sun, 14 Sep 2025 00:16:10 -0400 Subject: Squashed 'subprojects/boost-sqlite/' content from commit 3378e35 git-subtree-dir: subprojects/boost-sqlite git-subtree-split: 3378e353705271e569cf4ba15c467b840a39798c --- include/boost/sqlite/memory.hpp | 105 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 include/boost/sqlite/memory.hpp (limited to 'include/boost/sqlite/memory.hpp') diff --git a/include/boost/sqlite/memory.hpp b/include/boost/sqlite/memory.hpp new file mode 100644 index 0000000..e136a0e --- /dev/null +++ b/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 + +#include + +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 +void delete_(T * t) +{ + struct scoped_free + { + void * p; + ~scoped_free() + { + sqlite3_free(p); + } + }; + scoped_free _{t}; + t->~T(); +} + +namespace detail +{ + +template +struct deleter +{ + void operator()(T* t) + { + delete_(t); + } +}; + +template +struct deleter +{ + static_assert(std::is_trivially_destructible::value, "T[] needs to be trivially destructible"); + void operator()(T* t) + { + sqlite3_free(t); + } +}; + +template<> +struct deleter +{ + void operator()(void* t) + { + sqlite3_free(t); + } +}; +} + +template +using unique_ptr = std::unique_ptr>; + +template +inline std::size_t msize(const unique_ptr & ptr) +{ + return sqlite3_msize(ptr.get()); +} + +template +unique_ptr make_unique(Args && ... args) +{ + unique_ptr up{sqlite3_malloc64(sizeof(T))}; + unique_ptr res{new (up.get()) T(std::forward(args)...)}; + up.release(); + return res; +} + +BOOST_SQLITE_END_NAMESPACE + +#endif //BOOST_SQLITE_MEMORY_HPP -- cgit v1.2.3