My favorites
▼
|
Sign in
code4cs
Code Repository for Changshu Liu
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
Tools
/
MemPerfor
/
CppAlloc.cpp
r138
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <memory.h>
#include <iostream>
#include <limits>
#include <vector>
class A
{
public:
A()
{
std::cout << "A's constructor" << std::endl;
};
~A()
{
std::cout << "A's destructor" << std::endl;
}
void* operator new(size_t allocSize)
{
std::cout << "A's simple operator new" << std::endl;
return ::operator new(allocSize);
}
void* operator new(size_t allocSize, unsigned char initValue)
{
std::cout << "A's init value placement operator new" << std::endl;
void * returnPointer = ::operator new(allocSize);
memset(returnPointer, initValue, allocSize);
return returnPointer;
}
void* operator new(size_t allocSize, void* ptr)
{
std::cout << "A's raw pointer placement operator new" << std::endl;
return ptr;
}
void operator delete(void* ptr)
{
std::cout << "A's simple delete operator" << std::endl;
::operator delete(ptr);
return;
}
void operator delete(void* ptr, unsigned char initValue)
{
std::cout << "A's init value placement delete operator" << std::endl;
::operator delete(ptr);
return;
}
void operator delete(void* ptr, void* ptr2)
{
std::cout << "A's raw pointer placement delete operator" << std::endl;
return;
}
};
class B
{
public:
B()
{
std::cout << "B's constructor." << std::endl;
}
B(const B&)
{
std::cout << "B's copy constructor." << std::endl;
}
~B()
{
std::cout << "B's destructor." << std::endl;
}
};
void* operator new(size_t allocSize)
{
std::cout << "Customized global new operator." << std::endl;
void* returnPointer = malloc(allocSize + sizeof(__int32));
__int32* sizePointer = (__int32*)returnPointer;
*sizePointer = (__int32)allocSize;
return sizePointer + 1;
}
void operator delete(void* ptr)
{
std::cout << "Customized global delete operator." << std::endl;
__int32* orignalPointer = (__int32*)ptr - 1;
free(orignalPointer);
return;
}
template<typename T> class YaMemAlloc
{
public:
// Type definitions.
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
// Convert an allocator<T> to allocator<U>.
template<typename U>
struct rebind
{
typedef YaMemAlloc<U> other;
};
// Ctor/Dtor.
YaMemAlloc() throw() {};
YaMemAlloc(const YaMemAlloc&) throw() {};
template <class U> YaMemAlloc(const YaMemAlloc<U>&) throw() {};
~YaMemAlloc() throw() {};
// Address.
inline pointer address(reference r) { return &r; }
inline const_pointer address(const_reference r) { return &r; }
// Memory management.
pointer allocate(size_type cnt, std::allocator<void>::const_pointer hint = 0)
{
return reinterpret_cast<pointer>(::operator new(cnt * sizeof (T)));
};
void deallocate(pointer p, size_type)
{
::operator delete(p);
};
size_type max_size() const throw()
{
return std::numeric_limits<size_type>::max() / sizeof(T);
};
// Placement new/delete.
void construct(pointer p, const T& val) { new(p) T(val); }
void destroy(pointer p) { p->~T(); }
// Compare.
template <typename U>
bool operator==(YaMemAlloc<U> const&) { return true; };
template <typename U>
bool operator!=(YaMemAlloc<U> const& a) { return !operator==(a); };
};
//
//template <class T1, class T2>
//bool operator==(const allocator<T1>& a1, const allocator<T2>& a2) throw()
//{
// return true;
//}
//
//template <class T1, class T2>
//bool operator!=(const allocator<T1>& a1, const allocator<T2>& a2) throw()
//{
// return false;
//}
// Specilized for void
template<> class YaMemAlloc<void>
{
typedef void value_type;
typedef void* pointer;
typedef const void* const_pointer;
template <typename U>
struct rebind { typedef YaMemAlloc<U> other; };
};
int main(int argc, char** argv)
{
A* pa1 = new A();
delete pa1;
A* pa2 = new(1) A();
delete pa2;
char* pc = new char[sizeof(A)];
A* pa3 = new(pc) A();
pa3->~A();
delete pc;
B* pb = new B();
delete pb;
std::vector<B, YaMemAlloc<B>> vecB;
vecB.push_back(B());
vecB.push_back(B());
vecB.push_back(B());
vecB.push_back(B());
return 0;
}
Show details
Hide details
Change log
r89
by changshuliu on Jul 9, 2011
Diff
add memory performance analyzer into tools folder
Go to:
/trunk/Tools/MemPerfor
...nk/Tools/MemPerfor/CachePerf.cpp
/trunk/Tools/MemPerfor/CppAlloc.cpp
/trunk/Tools/MemPerfor/ParaSum.cxx
/trunk/Tools/MemPerfor/WinHeap.cxx
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 4783 bytes, 194 lines
View raw file
Powered by
Google Project Hosting