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