forked from huanchenz/index-microbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallocatortracker.h
86 lines (74 loc) · 2.68 KB
/
allocatortracker.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
#include <cstdio>
#include <typeinfo>
#include <vector>
#include <cstring>
#include <map>
#include <string>
#include <cstdlib>
#include <iostream>
template<typename ValueType>
/**
* Custom allocator for all the indexes except array index.
*/
class AllocatorTracker : public std::allocator<ValueType> {
public:
typedef typename std::allocator<ValueType> BaseAllocator;
typedef typename BaseAllocator::pointer pointer;
typedef typename BaseAllocator::size_type size_type;
// that's what we passed through copy constructor
// all the stl and other stuff only use copy constructor
int64_t *memory_size;
// This shouldn't be called. We should call the constructor with pointer to the memory_size.
AllocatorTracker() throw() : BaseAllocator() {}
AllocatorTracker(int64_t* m_ptr) throw() : BaseAllocator() {
memory_size = m_ptr;
}
AllocatorTracker(const AllocatorTracker& allocator) throw() : BaseAllocator(allocator) {
memory_size = allocator.memory_size;
}
template <class U> AllocatorTracker(const AllocatorTracker<U>& allocator) throw(): BaseAllocator(allocator) {
memory_size = allocator.memory_size;
}
~AllocatorTracker() {}
template<class U> struct rebind {
typedef AllocatorTracker<U> other;
};
pointer allocate(size_type size) {
pointer dataPtr = BaseAllocator::allocate(size);
*memory_size += size * sizeof(ValueType);
//VOLT_TRACE("allocate +++++++ %p %lu.\n", dataPtr, size * sizeof(ValueType));
//VOLT_TRACE("%s\n", typeid(ValueType).name());
return dataPtr;
}
pointer allocate(size_type size, void* ptr) {
pointer dataPtr = BaseAllocator::allocate(size, ptr);
*memory_size += size * sizeof(ValueType);
//VOLT_TRACE("allocate +++++++ %p %lu.\n", dataPtr, size * sizeof(ValueType));
//VOLT_TRACE("%s\n", typeid(ValueType).name());
return dataPtr;
}
pointer allocate(size_type size, pointer ptr) {
pointer dataPtr = BaseAllocator::allocate(size, ptr);
*memory_size += size * sizeof(ValueType);
//VOLT_TRACE("allocate +++++++ %p %lu.\n", dataPtr, size * sizeof(ValueType));
//VOLT_TRACE("%s\n", typeid(ValueType).name());
return dataPtr;
}
void deallocate(pointer ptr, size_type size) throw() {
BaseAllocator::deallocate(ptr, size);
*memory_size -= size * sizeof(ValueType);
}
/*
void construct(pointer __ptr, const ValueType& __val) {
new(__ptr) ValueType(__val);
//VOLT_TRACE("construct +++++++ %p %lu.\n", __ptr, sizeof(ValueType));
//VOLT_TRACE("%s\n", typeid(ValueType).name());
*memory_size += sizeof(ValueType);
}
void destroy(pointer __ptr) {
//VOLT_TRACE("-+-+-+- %08x.\n", __ptr);
__ptr->~ValueType();
*memory_size -= sizeof(ValueType);
}
*/
};