This repository has been archived by the owner on Dec 4, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlocked_allocator.h
99 lines (88 loc) · 2.25 KB
/
locked_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* SMARTCARDPP
*
* This software is released under either the GNU Library General Public
* License (see LICENSE.LGPL) or the BSD License (see LICENSE.BSD).
*
* Note that the only valid version of the LGPL license as far as this
* project is concerned is the original GNU Library General Public License
* Version 2.1, February 1999
*
*/
#include <cstddef>
#include <memory>
template <class T> class locked_allocator;
//void specialization
template <>class locked_allocator<void> {
public:
typedef void* pointer;
typedef const void* const_pointer;
// reference-to-void members are impossible
typedef void value_type;
template <class U>
struct rebind
{
typedef locked_allocator<U> other;
};
};
void * doAlloc(size_t num, void * hint);
void doFree(void * ptr,size_t num);
/// Allocator template that locks pages and cleans up free()'s
/// see http://www.ddj.com/cpp/184401646 for more complete version
template<class T>
class locked_allocator
{
public:
typedef T value_type;
typedef T *pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
pointer adress(reference x) const
{
return &x;
}
const_pointer adress(const_reference x) const
{
return &x;
}
void construct(pointer p, const T &val)
{
new ((void*)p) T(val);
}
void destroy(pointer p)
{
p->T::~T();
}
explicit locked_allocator() {}
locked_allocator(const locked_allocator<T> &) {}
template<class U> locked_allocator(const locked_allocator<U> &) {}
~locked_allocator() {}
pointer allocate(size_type n, void* hint= NULL)
{
void * pmem = doAlloc(n * sizeof (T),hint);
return static_cast<pointer>(pmem);
}
void deallocate(void *p, size_type n)
{
doFree(p,n);
return;
}
template <class U>
struct rebind {
typedef locked_allocator<U> other;
};
size_type max_size () const
{
std::allocator<T> al;
return al.max_size();
}
template <class U>
locked_allocator<T>& operator=(const locked_allocator<U> &) { return *this; }
};
template <class T>
bool operator==(const locked_allocator<T> &,const locked_allocator<T> &) { return true;}
template <class T>
bool operator!=(const locked_allocator<T> &,const locked_allocator<T> &) { return false;}