From 37a043147f16235ba5ed2dde6a0c3ad7d212caa9 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Wed, 23 Sep 2015 19:22:55 +0200 Subject: [PATCH] [Issue #54] Added custom allocator for cr_malloc/cr_free --- include/criterion/alloc.h | 50 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/include/criterion/alloc.h b/include/criterion/alloc.h index 4fd6ee0..eb68794 100644 --- a/include/criterion/alloc.h +++ b/include/criterion/alloc.h @@ -24,7 +24,13 @@ #ifndef CRITERION_ALLOC_H_ # define CRITERION_ALLOC_H_ -# include +# ifdef __cplusplus +# include +# include +using std::size_t; +# else +# include +# endif # include "common.h" CR_BEGIN_C_API @@ -94,6 +100,48 @@ namespace criterion { cr_free(ptr_ - 1); } + template + struct allocator { + typedef T value_type; + typedef value_type* pointer; + typedef const value_type* const_pointer; + typedef value_type& reference; + typedef const value_type& const_reference; + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + + template + struct rebind { + typedef allocator other; + }; + + inline explicit allocator() {} + inline ~allocator() {} + inline explicit allocator(allocator const&) {} + template + inline explicit allocator(allocator const&) {} + + inline pointer address(reference r) { return &r; } + inline const_pointer address(const_reference r) { return &r; } + + inline pointer allocate(size_type cnt, typename std::allocator::const_pointer = 0) { + return reinterpret_cast(cr_malloc(cnt * sizeof (T))); + } + + inline void deallocate(pointer p, size_type) { cr_free(p); } + + inline size_type max_size() const { + return size_type(-1) / sizeof(T); + } + + inline void construct(pointer p, const T& t) { new(p) T(t); } + inline void construct(pointer p, T&& t) { new (p) T(std::move(t)); } + inline void destroy(pointer p) { p->~T(); } + + inline bool operator==(allocator const&) { return true; } + inline bool operator!=(allocator const& a) { return !operator==(a); } + }; + } # endif