Zth (libzth)
Loading...
Searching...
No Matches
allocator.h
Go to the documentation of this file.
1#ifndef ZTH_ALLOCATOR_H
2#define ZTH_ALLOCATOR_H
3/*
4 * SPDX-FileCopyrightText: 2019-2026 Jochem Rutgers
5 *
6 * SPDX-License-Identifier: MPL-2.0
7 */
8
9#include <libzth/macros.h>
10
11#ifdef __cplusplus
12
13# include <libzth/config.h>
14# include <libzth/util.h>
15
16# include <exception>
17# include <list>
18# include <map>
19# include <string>
20# include <vector>
21
22namespace zth {
23
28template <typename T>
29__attribute__((warn_unused_result, returns_nonnull)) static inline T* allocate(size_t n = 1)
30{
31 typename Config::Allocator<T>::type allocator;
32 return allocator.allocate(n);
33}
34
43template <typename T>
44__attribute__((warn_unused_result)) static inline T* allocate_noexcept(size_t n = 1) noexcept
45{
46 try {
47 return allocate<T>(n);
48 } catch(std::bad_alloc const&) {
49 return nullptr;
50 } catch(...) {
51 std::terminate();
52 }
53
54 // Should not get here.
55 return nullptr;
56}
57
58template <typename T>
59__attribute__((warn_unused_result)) static inline T* new_alloc()
60{
61 T* o = allocate<T>();
62 try {
63 new(o) T;
64 return o;
65 } catch(...) {
66 deallocate(o);
67 zth_throw();
68 }
69
70 // Should not get here.
71 return nullptr;
72}
73
74template <typename T, typename Arg>
75__attribute__((warn_unused_result)) static inline T* new_alloc(Arg const& arg)
76{
77 T* o = allocate<T>();
78 try {
79 new(o) T(arg);
80 return o;
81 } catch(...) {
82 deallocate(o);
83 zth_throw();
84 }
85
86 // Should not get here.
87 return nullptr;
88}
89
90# if __cplusplus >= 201103L
91template <typename T, typename... Arg>
92__attribute__((warn_unused_result)) static inline T* new_alloc(Arg&&... arg)
93{
94 T* o = allocate<T>();
95 try {
96 new(o) T{std::forward<Arg>(arg)...};
97 return o;
98 } catch(...) {
99 deallocate(o);
100 zth_throw();
101 }
102
103 // Should not get here.
104 return nullptr;
105}
106# endif
107
112template <typename T>
113static inline void deallocate(T* p, size_t n = 1) noexcept
114{
115 if(!p)
116 return;
117
118 typename Config::Allocator<T>::type allocator;
119 allocator.deallocate(p, n);
120}
121
122template <typename T>
123static inline void delete_alloc(T* p) noexcept
124{
125 if(unlikely(!p))
126 return;
127
128 p->~T();
129 deallocate(p);
130}
131
142// cppcheck-suppress-macro duplInheritedMember
143# define ZTH_CLASS_NEW_DELETE(T) \
144 public: \
145 static void* operator new(std::size_t UNUSED_PAR(n)) \
146 { \
147 zth_assert(n == sizeof(T)); \
148 return ::zth::allocate<T>(); \
149 } \
150 static void operator delete(void* ptr) \
151 { \
152 ::zth::deallocate<T>(static_cast<T*>(ptr)); \
153 } \
154 static void* operator new[](std::size_t sz) \
155 { \
156 zth_assert(sz % sizeof(T) == 0); \
157 return ::zth::allocate<T>(sz / sizeof(T)); \
158 } \
159 static void operator delete[](void* ptr, size_t sz) \
160 { \
161 zth_assert(sz % sizeof(T) == 0); \
162 ::zth::deallocate<T>(static_cast<T*>(ptr), sz / sizeof(T)); \
163 } \
164 typedef typename zth::Config::Allocator<T>::type allocator_type; \
165 \
166 private:
167
172template <typename T>
174 typedef std::vector<T, typename Config::Allocator<T>::type> type;
175};
176
181template <typename Key, typename T, typename Compare = std::less<Key> >
182struct map_type {
183 typedef std::map<
184 Key, T, Compare, typename Config::Allocator<std::pair<const Key, T> >::type>
186};
187
192template <typename T>
193struct list_type {
194 typedef std::list<T, typename Config::Allocator<T>::type> type;
195};
196
197template <typename Traits, typename Allocator>
198string to_zth_string(std::basic_string<char, Traits, Allocator> const& s)
199{
200 return string(s.data(), s.size());
201}
202
203inline std::string to_std_string(string const& s)
204{
205 return std::string(s.data(), s.size());
206}
207
208} // namespace zth
209#endif // __cplusplus
210#endif // ZTH_ALLOCATOR_H
std::basic_string< char, std::char_traits< char >, Config::Allocator< char >::type > string
std::string type using Config::Allocator::type.
Definition util.h:310
#define zth_throw(...)
Definition macros.h:229
std::string to_std_string(string const &s)
Definition allocator.h:203
string to_zth_string(std::basic_string< char, Traits, Allocator > const &s)
Definition allocator.h:198
std::list type using Config::Allocator::type.
Definition allocator.h:193
std::list< T, typename Config::Allocator< T >::type > type
Definition allocator.h:194
std::map type using Config::Allocator::type.
Definition allocator.h:182
std::map< Key, T, Compare, typename Config::Allocator< std::pair< const Key, T > >::type > type
Definition allocator.h:185
std::vector type using Config::Allocator::type.
Definition allocator.h:173
std::vector< T, typename Config::Allocator< T >::type > type
Definition allocator.h:174
#define unlikely(expr)
Marks the given expression to likely be evaluated to true.
Definition util.h:55