DelegateMQ
Loading...
Searching...
No Matches
stl_allocator.h
Go to the documentation of this file.
1#ifndef _STL_ALLOCATOR_H
2#define _STL_ALLOCATOR_H
3
4// @see https://github.com/endurodave/stl_allocator
5// David Lafreniere
6
7#include "xallocator.h"
8#include <memory> // For std::allocator and std::allocator_traits
9
10// Forward declaration for stl_allocator<void>
11template <typename T>
12class stl_allocator;
13
14// Specialization for `void`, but we no longer need to define `pointer` and `const_pointer`
15template <>
16class stl_allocator<void>
17{
18public:
19 typedef void value_type;
20
21 template <class U>
22 struct rebind { typedef stl_allocator<U> other; };
23};
24
25// Define the custom stl_allocator inheriting from std::allocator
26template <typename T>
27class stl_allocator : public std::allocator<T>
28{
29public:
30 typedef size_t size_type;
31 typedef ptrdiff_t difference_type;
32 typedef T* pointer;
33 typedef const T* const_pointer;
34 typedef T& reference;
35 typedef const T& const_reference;
36 typedef T value_type;
37
38 // Default constructor
40
41 // Copy constructor
42 template <class U>
44
45 // Rebind struct
46 template <class U>
47 struct rebind { typedef stl_allocator<U> other; };
48
49 // Override allocate method to use custom allocation function
50 pointer allocate(size_type n, typename std::allocator_traits<stl_allocator<void>>::const_pointer hint = 0)
51 {
52 return static_cast<pointer>(xmalloc(n * sizeof(T)));
53 }
54
55 // Override deallocate method to use custom deallocation function
57 {
58 xfree(p);
59 }
60
61 // You can inherit other methods like construct and destroy from std::allocator
62};
63
64// Comparison operators for compatibility
65template <typename T, typename U>
66inline bool operator==(const stl_allocator<T>&, const stl_allocator<U>) { return true; }
67
68template <typename T, typename U>
69inline bool operator!=(const stl_allocator<T>&, const stl_allocator<U>) { return false; }
70
71#endif
void value_type
Definition stl_allocator.h:19
Definition stl_allocator.h:28
stl_allocator(const stl_allocator< U > &)
Definition stl_allocator.h:43
const T & const_reference
Definition stl_allocator.h:35
pointer allocate(size_type n, typename std::allocator_traits< stl_allocator< void > >::const_pointer hint=0)
Definition stl_allocator.h:50
void deallocate(pointer p, size_type n)
Definition stl_allocator.h:56
size_t size_type
Definition stl_allocator.h:30
const T * const_pointer
Definition stl_allocator.h:33
T value_type
Definition stl_allocator.h:36
T * pointer
Definition stl_allocator.h:32
stl_allocator()
Definition stl_allocator.h:39
ptrdiff_t difference_type
Definition stl_allocator.h:31
T & reference
Definition stl_allocator.h:34
bool operator!=(const stl_allocator< T > &, const stl_allocator< U >)
Definition stl_allocator.h:69
bool operator==(const stl_allocator< T > &, const stl_allocator< U >)
Definition stl_allocator.h:66
Definition stl_allocator.h:47
stl_allocator< U > other
Definition stl_allocator.h:47
Definition stl_allocator.h:22
stl_allocator< U > other
Definition stl_allocator.h:22
void xfree(void *ptr)
Definition xallocator.cpp:302
void * xmalloc(size_t size)
Definition xallocator.cpp:284