PALISADE Lattice Crypto Library  1.11.9
A lattice crypto library for software engineers by software engineers.
stl_allocator.h
1 // @file TODO
2 //
3 // @copyright Copyright (c) TODO
4 
5 #ifndef _STL_ALLOCATOR_H
6 #define _STL_ALLOCATOR_H
7 
8 // See
9 // http://www.codeproject.com/Articles/1089905/A-Custom-STL-std-allocator-Replacement-Improves-Performance-
10 
11 #include "xallocator.h"
12 
13 template <typename T>
15 template <>
16 class stl_allocator<void> {
17  public:
18  typedef void* pointer;
19  typedef const void* const_pointer;
20  // reference to void members are impossible.
21  typedef void value_type;
22 
23  template <class U>
24  struct rebind {
25  typedef stl_allocator<U> other;
26  };
27 };
28 
34 template <typename T>
35 class stl_allocator {
36  public:
37  typedef size_t size_type;
38  typedef ptrdiff_t difference_type;
39  typedef T* pointer;
40  typedef const T* const_pointer;
41  typedef T& reference;
42  typedef const T& const_reference;
43  typedef T value_type;
44 
47 
50 
52  template <class U>
54 
55  template <class U>
56  struct rebind {
57  typedef stl_allocator<U> other;
58  };
59 
62  pointer address(reference x) const { return &x; }
63 
66  const_pointer address(const_reference x) const { return &x; }
67 
70  size_type max_size() const throw() { return size_t(-1) / sizeof(value_type); }
71 
76  pointer allocate(size_type n,
77  stl_allocator<void>::const_pointer hint = nullptr) {
78  return static_cast<pointer>(xmalloc(n * sizeof(T)));
79  }
80 
84  void deallocate(pointer p, size_type n) { xfree(p); }
85 
90  void construct(pointer p, const T& val) {
91  new (static_cast<void*>(p)) T(val);
92  }
93 
97  void construct(pointer p) { new (static_cast<void*>(p)) T(); }
98 
102  void destroy(pointer p) { p->~T(); }
103 };
104 
105 template <typename T, typename U>
106 inline bool operator==(const stl_allocator<T>&, const stl_allocator<U>) {
107  return true;
108 }
109 
110 template <typename T, typename U>
111 inline bool operator!=(const stl_allocator<T>&, const stl_allocator<U>) {
112  return false;
113 }
114 
115 // For VC6/STLPort 4-5-3 see /stl/_alloc.h, line 464
116 // "If custom allocators are being used without member template classes support
117 // : user (on purpose) is forced to define rebind/get operations !!!"
118 #ifdef _WIN32
119 #define STD_ALLOC_CDECL __cdecl
120 #else
121 #define STD_ALLOC_CDECL
122 #endif
123 
124 namespace std {
125 template <class _Tp1, class _Tp2>
126 inline stl_allocator<_Tp2>& STD_ALLOC_CDECL
127 __stl_alloc_rebind(stl_allocator<_Tp1>& __a, const _Tp2*) {
128  return (stl_allocator<_Tp2>&)(__a);
129 }
130 
131 template <class _Tp1, class _Tp2>
132 inline stl_allocator<_Tp2> STD_ALLOC_CDECL
133 __stl_alloc_create(const stl_allocator<_Tp1>&, const _Tp2*) {
134  return stl_allocator<_Tp2>();
135 }
136 } // namespace std
137 
138 #endif
Definition: stl_allocator.h:56
stl_allocator(const stl_allocator< U > &)
Copy constructor.
Definition: stl_allocator.h:53
void deallocate(pointer p, size_type n)
Definition: stl_allocator.h:84
stl_allocator is STL-compatible allocator used to provide fixed block allocations.
Definition: stl_allocator.h:14
pointer allocate(size_type n, stl_allocator< void >::const_pointer hint=nullptr)
Definition: stl_allocator.h:76
void construct(pointer p, const T &val)
Definition: stl_allocator.h:90
Definition: stl_allocator.h:124
size_type max_size() const
Definition: stl_allocator.h:70
~stl_allocator()
Destructor.
Definition: stl_allocator.h:49
pointer address(reference x) const
Definition: stl_allocator.h:62
const_pointer address(const_reference x) const
Definition: stl_allocator.h:66
stl_allocator()
Constructor.
Definition: stl_allocator.h:46
void destroy(pointer p)
Definition: stl_allocator.h:102
void construct(pointer p)
Definition: stl_allocator.h:97