-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathEZHeapAlloc.h
79 lines (64 loc) · 1.6 KB
/
EZHeapAlloc.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
// std lib alloc replacement to use our own global heap
// Based Pete Isensee's articles
//
#pragma once
#include <memory>
template <typename _Ty> class EZHeapAlloc
{
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef _Ty* pointer;
typedef const _Ty* const_pointer;
typedef _Ty& reference;
typedef const _Ty& const_reference;
typedef _Ty value_type;
// convert an allocator<_Ty> to an allocator <_Other>
template <typename _Other> struct rebind
{
typedef EZHeapAlloc<_Other> other;
};
// return address of mutable _Val
pointer address(reference _Val) const
{
return(&_Val);
}
// return address of non-mutable _Val
const_pointer address(const_reference _Val) const
{
return(&_Val);
}
// Constructors
EZHeapAlloc() throw() {}
EZHeapAlloc(const EZHeapAlloc&) throw() {}
template <typename _Other> EZHeapAlloc(const EZHeapAlloc<_Other> &) throw() {}
EZHeapAlloc &operator=(const EZHeapAlloc &ha)
{
return(*this);
}
// Destructor
~EZHeapAlloc() throw() { }
pointer allocate(size_type _Count)
{
return(pointer(Heap().Alloc(_Count * sizeof(_Ty))));
}
void deallocate(pointer _Ptr, size_type)
{
Heap().Free(_Ptr);
}
// construct object at _Ptr with value _Val
void construct(pointer _Ptr, const _Ty& _Val)
{
std::_Construct(_Ptr, _Val);
}
// destroy object at _Ptr
void destroy(pointer _Ptr)
{
std::_Destroy(_Ptr);
}
size_t max_size() const
{
size_t _Count = ((size_t)(-1) / sizeof (_Ty));
return(((0 < _Count) ? _Count : 1));
}
};