Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
alloc.h
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright © 2015-2016 Franklin "Snaipe" Mathieu <http://snai.pe/>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24/**
25 * @file
26 * @brief Test intern memory managment
27 *****************************************************************************/
28#ifndef CRITERION_ALLOC_H_
29#define CRITERION_ALLOC_H_
30
31#ifdef __cplusplus
32# include <memory>
33# include <cstddef>
34using std::size_t;
35#else
36# include <stddef.h>
37#endif
38#include "internal/common.h"
39
41
42/**
43 * Allocates a block of memory usable by the test.
44 *
45 * It is undefined behaviour to access a pointer returned by malloc(3)
46 * inside a test or its setup and teardown functions; cr_malloc must
47 * be use in its place for this purpose.
48 *
49 * This function is semantically identical to malloc(3).
50 *
51 * @param[in] size The minimal size in bytes of the newly allocated memory.
52 * @returns The pointer to the start of the allocated memory.
53 */
54CR_API void *cr_malloc(size_t size);
55
56/**
57 * Allocates and zero-initialize a block of memory usable by the test.
58 *
59 * It is undefined behaviour to access a pointer returned by calloc(3)
60 * inside a test or its setup and teardown functions; cr_calloc must
61 * be use in its place for this purpose.
62 *
63 * This function is semantically identical to calloc(3).
64 *
65 * @param[in] nmemb The number of elements to allocate
66 * @param[in] size The minimal size of each element.
67 * @returns The pointer to the start of the allocated memory.
68 */
69CR_API void *cr_calloc(size_t nmemb, size_t size);
70
71/**
72 * Reallocates a block of memory usable by the test.
73 *
74 * It is undefined behaviour to access a pointer returned by realloc(3)
75 * inside a test or its setup and teardown functions; cr_realloc must
76 * be used in its place for this purpose.
77 *
78 * This function is semantically identical to realloc(3).
79 *
80 * @param[in] ptr A pointer to the memory that needs to be resized.
81 * @param[in] size The minimal size of the reallocated memory.
82 * @returns The pointer to the start of the reallocated memory.
83 */
84CR_API void *cr_realloc(void *ptr, size_t size);
85
86/**
87 * Free a block of memory allocated by cr_malloc, cr_free or cr_realloc.
88 *
89 * @param[in] ptr A pointer to the memory that needs to be freed.
90 */
91CR_API void cr_free(void *ptr);
92
94
95#ifdef __cplusplus
96# include <type_traits>
97
98namespace criterion
99{
100void *(*const malloc)(size_t) = cr_malloc;
101void(*const free)(void *) = cr_free;
102void *(*const calloc)(size_t, size_t) = cr_calloc;
103void *(*const realloc)(void *, size_t) = cr_realloc;
104
105/**
106 * Allocates and construct a new object.
107 *
108 * It is undefined behaviour to access a pointer returned by the new
109 * operator inside a test or its setup and teardown functions;
110 * new_obj must be used in its place for this purpose.
111 *
112 * This function is semantically identical to the new operator.
113 *
114 * @tparam T The type of the object to construct
115 * @param[in] params The constructor parameters of T.
116 * @returns The pointer to the newly constructed object.
117 */
118template <typename T, typename... Params>
119T *new_obj(Params... params)
120{
121 T *obj = static_cast<T *>(cr_malloc(sizeof (T)));
122 if (!obj) {
123 throw std::bad_alloc();
124 }
125
126 new (obj) T(params...);
127 return obj;
128}
129
130/**
131 * Allocates and construct a new array of primitive types
132 *
133 * It is undefined behaviour to access a pointer returned by the new[]
134 * operator inside a test or its setup and teardown functions;
135 * new_arr must be used in its place for this purpose.
136 *
137 * This function is semantically identical to the new[] operator.
138 *
139 * @tparam T The compound type of the array to construct
140 * @param[in] len The length of the array.
141 * @returns The pointer to the newly constructed array.
142 */
143template <typename T>
144typename std::enable_if<std::is_fundamental<T>::value>::type
145* new_arr(size_t len) {
146 if (len > (SIZE_MAX - sizeof (size_t)) / sizeof (T)) {
147 throw std::bad_alloc();
148 }
149 void *ptr = cr_malloc(sizeof (size_t) + sizeof (T) * len);
150 if (!ptr) {
151 throw std::bad_alloc();
152 }
153
154 *(reinterpret_cast<size_t *>(ptr)) = len;
155 T *arr = reinterpret_cast<T *>(reinterpret_cast<size_t *>(ptr) + 1);
156 return arr;
157}
158
159/**
160 * Allocates and construct a new array of object types
161 *
162 * It is undefined behaviour to access a pointer returned by the new[]
163 * operator inside a test or its setup and teardown functions;
164 * new_arr must be used in its place for this purpose.
165 *
166 * This function is semantically identical to the new[] operator.
167 *
168 * @tparam T The compound type of the array to construct
169 * @param[in] len The length of the array.
170 * @returns The pointer to the newly constructed array.
171 */
172template <typename T>
173T *new_arr(size_t len)
174{
175 if (len > (SIZE_MAX - sizeof (size_t)) / sizeof (T)) {
176 throw std::bad_alloc();
177 }
178 void *ptr = cr_malloc(sizeof (size_t) + sizeof (T) * len);
179 if (!ptr) {
180 throw std::bad_alloc();
181 }
182
183 *(reinterpret_cast<size_t *>(ptr)) = len;
184
185 T *arr = reinterpret_cast<T *>(reinterpret_cast<size_t *>(ptr) + 1);
186 for (size_t i = 0; i < len; ++i)
187 new (arr + i)T();
188 return arr;
189}
190
191/**
192 * Destroys and frees an object allocated by new_obj.
193 *
194 * This function is semantically identical to the delete operator.
195 *
196 * @tparam T The type of the object to construct
197 * @param[in] ptr The object to destroy.
198 */
199template <typename T>
200void delete_obj(T *ptr)
201{
202 ptr->~T();
203 cr_free(ptr);
204}
205
206/**
207 * Destroys and frees an array allocated by delete_arr.
208 *
209 * This function is semantically identical to the delete[] operator.
210 *
211 * @tparam T The type of the object to construct
212 * @param[in] ptr The object to destroy.
213 */
214template <typename T>
215void delete_arr(typename std::enable_if<std::is_fundamental<T>::value>::type *ptr)
216{
217 cr_free(ptr);
218}
219
220/**
221 * Destroys and frees an array allocated by delete_arr.
222 *
223 * This function is semantically identical to the delete[] operator.
224 *
225 * @tparam T The type of the object to construct
226 * @param[in] ptr The object to destroy.
227 */
228template <typename T>
229void delete_arr(T *ptr)
230{
231 size_t *ptr_ = reinterpret_cast<size_t *>(ptr);
232 size_t len = *(ptr_ - 1);
233 T *arr = reinterpret_cast<T *>(ptr_);
234
235 for (size_t i = 0; i < len; ++i)
236 arr[i].~T();
237 cr_free(ptr_ - 1);
238}
239
240/**
241 * Allocator for use in the STL.
242 *
243 * This internally uses calls to the cr_malloc function family, which
244 * means that STL collections can be safely used inside tests or
245 * setup/teardown functions if this allocator is used.
246 */
247template <typename T>
248struct allocator {
249 typedef T value_type;
250 typedef value_type *pointer;
251 typedef const value_type *const_pointer;
252 typedef value_type &reference;
253 typedef const value_type &const_reference;
254 typedef std::size_t size_type;
255 typedef std::ptrdiff_t difference_type;
256
257 template <typename U>
258 struct rebind {
259 typedef allocator<U> other;
260 };
261
262 inline explicit allocator() {}
263 inline ~allocator() {}
264 inline explicit allocator(allocator const &) {}
265 template <typename U>
266 inline allocator(allocator<U> const &) {}
267
268 inline pointer address(reference r) { return &r; }
269 inline const_pointer address(const_reference r) { return &r; }
270
271 inline pointer allocate(size_type cnt, const std::allocator<void>::value_type * = 0)
272 {
273 return reinterpret_cast<pointer>(cr_malloc(cnt * sizeof (T)));
274 }
275
276 inline void deallocate(pointer p, size_type) { cr_free(p); }
277
278 inline size_type max_size() const
279 {
280 return size_type(-1) / sizeof (T);
281 }
282
283 inline void construct(pointer p, const T &t) { new(p) T(t); }
284 inline void construct(pointer p, T &&t) { new (p) T(std::move(t)); }
285 inline void destroy(pointer p) { p->~T(); }
286
287 inline bool operator==(allocator const &) { return true; }
288 inline bool operator!=(allocator const &a) { return !operator==(a); }
289};
290
291template<typename T, typename U>
292bool operator==(allocator<T> const& lhs, allocator<U> const& rhs)
293{
294 return false;
295}
296
297template<typename T, typename U>
298bool operator!=(allocator<T> const& lhs, allocator<U> const& rhs)
299{
300 return !(lhs == rhs);
301}
302}
303#endif
304
305#endif /* !CRITERION_ALLOC_H_ */
CR_BEGIN_C_API CR_API void * cr_malloc(size_t size)
CR_API void cr_free(void *ptr)
CR_API void * cr_realloc(void *ptr, size_t size)
CR_API void * cr_calloc(size_t nmemb, size_t size)
#define realloc
Definition civetweb.c:1541
#define free
Definition civetweb.c:1542
#define calloc
Definition civetweb.c:1540
#define malloc
Definition civetweb.c:1539
#define CR_BEGIN_C_API
Definition common.h:54
#define CR_API
Definition common.h:128
#define CR_END_C_API
Definition common.h:55
DOCTEST_INTERFACE bool operator!=(const String &lhs, const String &rhs)
DOCTEST_INTERFACE bool operator==(const String &lhs, const String &rhs)
decltype(sizeof(void *)) size_t
Definition doctest.h:524