summaryrefslogtreecommitdiff
path: root/subprojects/boost-sqlite/include/boost/sqlite/allocator.hpp
blob: f449475c3193b38be34b29334afd8bcbbbdd84cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// Copyright (c) 2022 Klemens Morgenstern (klemens.morgenstern@gmx.net)
//
// 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_ALLOCATOR_HPP
#define BOOST_SQLITE_ALLOCATOR_HPP

#include <boost/sqlite/detail/config.hpp>

#include <cstddef>
#include <cstdint>

BOOST_SQLITE_BEGIN_NAMESPACE

template<typename T>
struct allocator
{
  constexpr allocator() noexcept {}
  constexpr allocator( const allocator& other ) noexcept {}
  template< class U >
  constexpr allocator( const allocator<U>& other ) noexcept {}

#if defined(SQLITE_4_BYTE_ALIGNED_MALLOC)
  constexpr static std::size_t alignment = 4u;
#else
  constexpr static std::size_t alignment = 8u;
#endif

  static_assert(alignof(T) <= alignment, "T alignment can't be fulfilled by sqlite");
  [[nodiscard]] T* allocate( std::size_t n )
  {
    auto p = static_cast<T*>(sqlite3_malloc64(n * sizeof(T)));
    if (p == nullptr)
      boost::throw_exception(std::bad_alloc());
    return p;
  }
  void deallocate( T* p, std::size_t)
  {
    return sqlite3_free(p);
  }
};

BOOST_SQLITE_END_NAMESPACE

#endif //BOOST_SQLITE_ALLOCATOR_HPP