-
Notifications
You must be signed in to change notification settings - Fork 1
/
STLAllocator.h
140 lines (113 loc) · 3.38 KB
/
STLAllocator.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
@file STLAllocator.h
@author huangwei
@param Email: [email protected]
@param Copyright (c) 2004-2013 杭州网易雷电工作室
@date 2013/4/28
@brief
*/
#pragma once
#ifndef __STLALLOCATOR_H__
#define __STLALLOCATOR_H__
#include <string>
#include <xmemory>
/**
* TAllocatorCounter
*
*/
template <typename C>
struct TAllocatorCounter {
typedef size_t size_type;
static size_type get_mem_size() {
return mem_total;
}
static size_type get_class_size() {
return class_total;
}
static size_type mem_total;
static size_type class_total;
};
/**
* TAllocator
*
* allocator 模板
* T参数参考stl默认配置
* C参数表示容器类型
* 只用作内存统计
*/
template <typename T, typename C>
struct TAllocator {
typedef size_t size_type, difference_type;
typedef T value_type;
typedef T *pointer, &reference;
typedef const T *const_pointer, &const_reference;
template <typename U>
struct rebind { typedef TAllocator <U, C> other; };
TAllocator() {
++ TAllocatorCounter<C>::class_total;
}
TAllocator(const TAllocator <T, C> &) {
++ TAllocatorCounter<C>::class_total;
}
template<class U>
TAllocator(const TAllocator<U, C>&) {
++ TAllocatorCounter<C>::class_total;
}
~TAllocator() {
-- TAllocatorCounter<C>::class_total;
}
pointer address(reference r) const { return &r; }
const_pointer address(const_reference r) const { return &r; }
size_type max_size() const { // estimate maximum array size
size_type _Count = (size_type)(-1) / sizeof (T);
return (0 < _Count ? _Count : 1);
}
void construct(pointer p, const T &t) { // construct object at _Ptr with value _Val
::new ((void *)p) T(t);
}
void destroy(pointer p) { // destroy object at _Ptr
(void)(p);
p->~T();
}
pointer allocate(size_type n) { // allocate array of _Count elements
TAllocatorCounter<C>::mem_total += n * sizeof(value_type);
return (pointer)::operator new(n * sizeof(value_type));
}
pointer allocate(size_type n, const void *) { // allocate array of _Count elements, ignore hint
return (allocate(n));
}
void deallocate(pointer p, size_type n) { // deallocate object at _Ptr, ignore size
TAllocatorCounter<C>::mem_total -= n * sizeof(value_type);
::operator delete((void *)p);
}
};
//////////////////////////////////////////////////////////////////////////
#define StringAllocator TAllocator<char, std::string>
#define StringAllocatorCounter TAllocatorCounter<std::string>
#define SetAllocator(T) TAllocator<T, std::set<T>>
#define SetAllocatorCounter(T) TAllocatorCounter<std::set<T>>
#define MapAllocator(KT, T) TAllocator<std::pair<const KT, T>, std::map<KT, T>>
#define MapAllocatorCounter(KT, T) TAllocatorCounter<std::map<KT, T>>
class managed_string : public std::basic_string<char, std::char_traits<char>, StringAllocator>
{
public:
typedef StringAllocatorCounter counter;
managed_string() {
}
managed_string(const std::string& right) {
this->assign(right.c_str(), right.length());
}
managed_string& operator= (const std::string& right) {
this->assign(right.c_str(), right.length());
return *this;
}
operator std::string() const {
return c_str();
}
};
//////////////////////////////////////////////////////////////////////////
template <typename C>
typename TAllocatorCounter<C>::size_type TAllocatorCounter<C>::class_total = 0;
template <typename C>
typename TAllocatorCounter<C>::size_type TAllocatorCounter<C>::mem_total = 0;
#endif // __STLALLOCATOR_H__