28#ifndef CRITERION_ALLOC_H_
29#define CRITERION_ALLOC_H_
96# include <type_traits>
118template <
typename T,
typename... Params>
119T *new_obj(Params... params)
121 T *obj =
static_cast<T *
>(
cr_malloc(
sizeof (T)));
123 throw std::bad_alloc();
126 new (obj) T(params...);
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();
149 void *ptr =
cr_malloc(
sizeof (
size_t) +
sizeof (T) * len);
151 throw std::bad_alloc();
154 *(
reinterpret_cast<size_t *
>(ptr)) = len;
155 T *arr =
reinterpret_cast<T *
>(
reinterpret_cast<size_t *
>(ptr) + 1);
173T *new_arr(
size_t len)
175 if (len > (
SIZE_MAX -
sizeof (
size_t)) /
sizeof (T)) {
176 throw std::bad_alloc();
178 void *ptr =
cr_malloc(
sizeof (
size_t) +
sizeof (T) * len);
180 throw std::bad_alloc();
183 *(
reinterpret_cast<size_t *
>(ptr)) = len;
185 T *arr =
reinterpret_cast<T *
>(
reinterpret_cast<size_t *
>(ptr) + 1);
186 for (
size_t i = 0; i < len; ++i)
200void delete_obj(T *ptr)
215void delete_arr(
typename std::enable_if<std::is_fundamental<T>::value>::type *ptr)
229void delete_arr(T *ptr)
231 size_t *ptr_ =
reinterpret_cast<size_t *
>(ptr);
232 size_t len = *(ptr_ - 1);
233 T *arr =
reinterpret_cast<T *
>(ptr_);
235 for (
size_t i = 0; i < len; ++i)
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;
255 typedef std::ptrdiff_t difference_type;
257 template <
typename U>
259 typedef allocator<U> other;
262 inline explicit allocator() {}
263 inline ~allocator() {}
264 inline explicit allocator(allocator
const &) {}
265 template <
typename U>
266 inline allocator(allocator<U>
const &) {}
268 inline pointer address(reference r) {
return &r; }
269 inline const_pointer address(const_reference r) {
return &r; }
271 inline pointer allocate(size_type cnt,
const std::allocator<void>::value_type * = 0)
273 return reinterpret_cast<pointer
>(
cr_malloc(cnt *
sizeof (T)));
276 inline void deallocate(pointer p, size_type) {
cr_free(p); }
278 inline size_type max_size()
const
280 return size_type(-1) /
sizeof (T);
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(); }
287 inline bool operator==(allocator
const &) {
return true; }
291template<
typename T,
typename U>
292bool operator==(allocator<T>
const& lhs, allocator<U>
const& rhs)
297template<
typename T,
typename U>
298bool operator!=(allocator<T>
const& lhs, allocator<U>
const& rhs)
300 return !(lhs == rhs);
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)
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