Zth (libzth)
allocator.h
Go to the documentation of this file.
1 #ifndef ZTH_ALLOCATOR_H
2 #define ZTH_ALLOCATOR_H
3 /*
4  * Zth (libzth), a cooperative userspace multitasking library.
5  * Copyright (C) 2019-2022 Jochem Rutgers
6  *
7  * This Source Code Form is subject to the terms of the Mozilla Public
8  * License, v. 2.0. If a copy of the MPL was not distributed with this
9  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
10  */
11 
12 #ifdef __cplusplus
13 
14 # include <libzth/macros.h>
15 # include <libzth/config.h>
16 # include <libzth/util.h>
17 
18 # include <list>
19 # include <map>
20 # include <string>
21 # include <vector>
22 
23 namespace zth {
24 
29 template <typename T>
30 __attribute__((warn_unused_result, returns_nonnull)) static inline T* allocate(size_t n = 1)
31 {
32  typename Config::Allocator<T>::type allocator;
33  return allocator.allocate(n);
34 }
35 
44 template <typename T>
45 __attribute__((warn_unused_result)) static inline T* allocate_noexcept(size_t n = 1) noexcept
46 {
47  try {
48  return allocate<T>(n);
49  } catch(...) {
50  }
51  return nullptr;
52 }
53 
54 template <typename T>
55 __attribute__((warn_unused_result)) static inline T* new_alloc()
56 {
57  T* o = allocate<T>(1);
58  new(o) T;
59  return o;
60 }
61 
62 template <typename T, typename Arg>
63 __attribute__((warn_unused_result)) static inline T* new_alloc(Arg const& arg)
64 {
65  T* o = allocate<T>(1);
66  new(o) T(arg);
67  return o;
68 }
69 
70 # if __cplusplus >= 201103L
71 template <typename T, typename... Arg>
72 __attribute__((warn_unused_result)) static inline T* new_alloc(Arg&&... arg)
73 {
74  T* o = allocate<T>(1);
75  new(o) T(std::forward<Arg>(arg)...);
76  return o;
77 }
78 # endif
79 
84 template <typename T>
85 static inline void deallocate(T* p, size_t n = 1) noexcept
86 {
87  if(!p)
88  return;
89 
90  typename Config::Allocator<T>::type allocator;
91  allocator.deallocate(p, n);
92 }
93 
94 template <typename T>
95 static inline void delete_alloc(T* p) noexcept
96 {
97  if(unlikely(!p))
98  return;
99 
100  p->~T();
101  deallocate(p, 1);
102 }
103 
114 # define ZTH_CLASS_NEW_DELETE(T) \
115  public: \
116  void* operator new(std::size_t UNUSED_PAR(n)) \
117  { \
118  zth_assert(n == sizeof(T)); \
119  return ::zth::allocate<T>(); \
120  } \
121  void operator delete(void* ptr) \
122  { \
123  ::zth::deallocate<T>(static_cast<T*>(ptr)); \
124  } \
125  \
126  private:
127 
132 template <typename T>
133 struct vector_type {
134  typedef std::vector<T, typename Config::Allocator<T>::type> type;
135 };
136 
141 template <typename Key, typename T, typename Compare = std::less<Key> >
142 struct map_type {
143  typedef std::map<
144  Key, T, Compare, typename Config::Allocator<std::pair<const Key, T> >::type>
146 };
147 
152 template <typename T>
153 struct list_type {
154  typedef std::list<T, typename Config::Allocator<T>::type> type;
155 };
156 
157 template <typename Traits, typename Allocator>
158 string to_zth_string(std::basic_string<char, Traits, Allocator> const& s)
159 {
160  return string(s.data(), s.size());
161 }
162 
163 inline std::string to_std_string(string const& s)
164 {
165  return std::string(s.data(), s.size());
166 }
167 
168 } // namespace zth
169 #endif // __cplusplus
170 #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:335
Definition: allocator.h:23
std::string to_std_string(string const &s)
Definition: allocator.h:163
string to_zth_string(std::basic_string< char, Traits, Allocator > const &s)
Definition: allocator.h:158
std::list type using Config::Allocator::type.
Definition: allocator.h:153
std::list< T, typename Config::Allocator< T >::type > type
Definition: allocator.h:154
std::map type using Config::Allocator::type.
Definition: allocator.h:142
std::map< Key, T, Compare, typename Config::Allocator< std::pair< const Key, T > >::type > type
Definition: allocator.h:145
std::vector type using Config::Allocator::type.
Definition: allocator.h:133
std::vector< T, typename Config::Allocator< T >::type > type
Definition: allocator.h:134
#define unlikely(expr)
Marks the given expression to likely be evaluated to true.
Definition: util.h:56