ThinSQLite++
A thin, safe and convenient modern C++ wrapper for the SQLite API.
Loading...
Searching...
No Matches
memory_iface.hpp
1/*
2 Copyright 2019 Eugene Gershnik
3
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file or at
6 https://github.com/gershnik/thinsqlitepp/blob/master/LICENSE
7*/
8
9#ifndef HEADER_SQLITEPP_MEMORY_IFACE_INCLUDED
10#define HEADER_SQLITEPP_MEMORY_IFACE_INCLUDED
11
12#include "config.hpp"
13
14#include <memory>
15#include <limits>
16
17namespace thinsqlitepp
18{
23
29 SQLITEPP_EXPORTED
30 template<class T>
32 {
33 public:
34 void operator()(T * mem) const noexcept
35 { sqlite3_free(const_cast<std::remove_const_t<T> *>(mem)); }
36 };
37
39 SQLITEPP_EXPORTED
40 template<class T>
41 using deleter [[deprecated]] = sqlite_deleter<T>;
43
49 SQLITEPP_EXPORTED
51
57 SQLITEPP_EXPORTED
59
60
62 inline void * sqlite_allocate_nothrow(std::size_t size) noexcept
63 {
64 if (size == 0)
65 return sqlite3_malloc(1);
66
67 #if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 8, 7)
68 if constexpr (sizeof(size_t) > sizeof(sqlite3_uint64))
69 {
71 return nullptr;
72 }
73 return sqlite3_malloc64(size);
74 #else
76 return nullptr;
77 return sqlite3_malloc(int(size));
78 #endif
79 }
80
81 SQLITEPP_EXPORTED
82 inline void * sqlite_allocate(std::size_t size)
83 {
84 if (auto ret = sqlite_allocate_nothrow(size))
85 return ret;
86 throw std::bad_alloc();
87 }
88
89
90
92
104 SQLITEPP_EXPORTED
106 {
107 void * operator new(std::size_t size, const std::nothrow_t &) noexcept
108 { return sqlite_allocate_nothrow(size); }
109
110 void* operator new[](std::size_t size, const std::nothrow_t &) noexcept
111 { return sqlite_allocate_nothrow(size); }
112
113 void * operator new(size_t size)
114 { return sqlite_allocate(size); }
115
116 void * operator new[](size_t size)
117 { return sqlite_allocate(size); }
118
119
120 void operator delete (void * ptr) noexcept
121 { sqlite3_free(ptr); }
122 void operator delete[](void * ptr) noexcept
123 { sqlite3_free(ptr); }
124 };
125
126
131 SQLITEPP_EXPORTED
132 template<class T>
133 struct sqlite_allocator
134 {
135 using value_type = T;
136
137 sqlite_allocator() = default;
138
139 template<class U>
140 constexpr sqlite_allocator(const sqlite_allocator <U>&) noexcept {}
141
142 T * allocate(std::size_t n)
143 {
144 if (std::numeric_limits<std::size_t>::max() / sizeof(T) < n)
146 return (T *)sqlite_allocate(n * sizeof(T));
147 }
148
149 void deallocate(T * ptr, std::size_t /*n*/) noexcept
150 { sqlite3_free(ptr); }
151
152 template<class U>
153 friend bool operator==(const sqlite_allocator<T> &, const sqlite_allocator<U> &) noexcept { return true; }
154
155 template<class U>
156 friend bool operator!=(const sqlite_allocator<T> &, const sqlite_allocator<U> &) noexcept { return false; }
157 };
158
160}
161
162#endif
163
164
165
Memory deleter that uses sqlite3_free.
Definition memory_iface.hpp:32
sqlite3_free
std::unique_ptr< char, sqlite_deleter< char > > allocated_string
A string allocated by SQLite.
Definition memory_iface.hpp:50
std::unique_ptr< std::byte, sqlite_deleter< std::byte > > allocated_bytes
A byte buffer allocated by SQLite.
Definition memory_iface.hpp:58
T max(T... args)
ThinSQLite++ namespace.
Definition backup_iface.hpp:17
Base class that makes derived classes be allocated using SQLite.
Definition memory_iface.hpp:106