Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
callocator.hpp
Go to the documentation of this file.
1 /*
2  * Author: Sven Gothel <sgothel@jausoft.com>
3  * Copyright (c) 2021 Gothel Software e.K.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef C_ALLOCATOR_HPP
26 #define C_ALLOCATOR_HPP
27 
28 #include <cinttypes>
29 #include <memory>
30 
31 #include <jau/basic_types.hpp>
32 
33 namespace jau {
34 
35 /**
36  * A simple allocator using POSIX C functions: <code>::malloc()</code>, <code>::free()</code> and <code>::realloc()</code>.<br>
37  * It is the missing <code>::realloc()</code> in <code>std::allocator</code>, motivating this class.<br>
38  * Since <code>realloc()</code> requires the passed pointer to originate from <code>malloc()</code> or <code>calloc</code>,
39  * we have to use it for <code>allocate()</code> as well.
40  * <p>
41  * Added method is <code>reallocate()</code> using the native <code>realloc()</code>.
42  * </p>
43  * <p>
44  * This class shall be compliant with <i>C++ named requirements for Allocator</i>.
45  * </p>
46  * <p>
47  * Not implementing deprecated (C++17) and removed (C++20)
48  * methods: address(), max_size(), construct() and destroy().
49  * </p>
50  */
51 template <class T>
52 struct callocator
53 {
54  public:
55  template <class U> struct rebind {typedef callocator<U> other;};
56 
57  // typedefs' for C++ named requirements: Allocator
58  typedef T value_type;
59 
60  public:
61  callocator() noexcept
62  { } // C++11
63 
64 #if __cplusplus > 201703L
65  constexpr callocator(const callocator& other) noexcept
66  {} // C++20
67 #else
68  callocator(const callocator& other) noexcept
69  { (void)other; }
70 #endif
71 
72 #if __cplusplus > 201703L
73  template <typename U>
74  constexpr callocator(const callocator<U>& other) noexcept
75  { (void)other; } // C++20
76 #else
77  template <typename U>
78  callocator(const callocator<U>& other) noexcept
79  { (void)other; }
80 #endif
81 
82 #if __cplusplus > 201703L
83  constexpr ~callocator() {} // C++20
84 #else
86 #endif
87 
88 #if __cplusplus <= 201703L
89  value_type* allocate(std::size_t n, const void * hint) { // C++17 deprecated; C++20 removed
90  (void)hint;
91  return reinterpret_cast<value_type*>( malloc( n * sizeof(value_type) ) );
92  }
93 #endif
94 
95 #if __cplusplus > 201703L
96  [[nodiscard]] constexpr value_type* allocate(std::size_t n) { // C++20
97  return reinterpret_cast<value_type*>( malloc( n * sizeof(value_type) ) );
98  }
99 #else
100  value_type* allocate(std::size_t n) { // C++17
101  return reinterpret_cast<value_type*>( malloc( n * sizeof(value_type) ) );
102  }
103 #endif
104 
105  [[nodiscard]] constexpr value_type* reallocate(value_type* p, std::size_t old_size, std::size_t new_size) {
106  (void)old_size;
107  return reinterpret_cast<value_type*>(
108  realloc( reinterpret_cast<void*>(p), new_size * sizeof(value_type) ) );
109  }
110 
111 #if __cplusplus > 201703L
112  constexpr void deallocate(value_type* p, std::size_t n ) {
113  (void)n;
114  free( reinterpret_cast<void*>( p ) );
115  }
116 #else
117  void deallocate(value_type* p, std::size_t n ) {
118  (void)n;
119  free( reinterpret_cast<void*>( p ) );
120  }
121 #endif
122 
123 };
124 
125 
126 #if __cplusplus > 201703L
127 template <class T1, class T2>
128  constexpr bool operator==(const callocator<T1>& lhs, const callocator<T2>& rhs) noexcept {
129 #if 0
130  if( &lhs == &rhs ) {
131  return true;
132  }
133  return lhs.memory_usage == rhs.memory_usage;
134 #else
135  (void)lhs;
136  (void)rhs;
137  return true;
138 #endif
139  }
140 #else
141  template <class T1, class T2>
142  bool operator==(const callocator<T1>& lhs, const callocator<T2>& rhs) noexcept {
143 #if 0
144  if( &lhs == &rhs ) {
145  return true;
146  }
147  return lhs.memory_usage == rhs.memory_usage;
148 #else
149  (void)lhs;
150  (void)rhs;
151  return true;
152 #endif
153  }
154  template <class T1, class T2>
155  bool operator!=(const callocator<T1>& lhs, const callocator<T2>& rhs) noexcept {
156  return !(lhs==rhs);
157  }
158 #endif
159 
160 } /* namespace jau */
161 
162 #endif // TEST_ALLOCATOR_HPP
163 
jau::callocator::rebind
Definition: callocator.hpp:55
jau::callocator::callocator
callocator(const callocator< U > &other) noexcept
Definition: callocator.hpp:78
jau::callocator::~callocator
~callocator()
Definition: callocator.hpp:85
jau::callocator::callocator
callocator() noexcept
Definition: callocator.hpp:61
jau::callocator::callocator
callocator(const callocator &other) noexcept
Definition: callocator.hpp:68
jau
Definition: basic_algos.hpp:34
jau::callocator::value_type
T value_type
Definition: callocator.hpp:58
jau::callocator::reallocate
constexpr value_type * reallocate(value_type *p, std::size_t old_size, std::size_t new_size)
Definition: callocator.hpp:105
jau::callocator::deallocate
void deallocate(value_type *p, std::size_t n)
Definition: callocator.hpp:117
jau::callocator::allocate
value_type * allocate(std::size_t n)
Definition: callocator.hpp:100
jau::callocator::rebind::other
callocator< U > other
Definition: callocator.hpp:55
jau::callocator
A simple allocator using POSIX C functions: ::malloc(), ::free() and ::realloc().
Definition: callocator.hpp:53
jau::operator!=
bool operator!=(const callocator< T1 > &lhs, const callocator< T2 > &rhs) noexcept
Definition: callocator.hpp:155
basic_types.hpp
jau::callocator::allocate
value_type * allocate(std::size_t n, const void *hint)
Definition: callocator.hpp:89
jau::operator==
bool operator==(const callocator< T1 > &lhs, const callocator< T2 > &rhs) noexcept
Definition: callocator.hpp:142