7
/* Compiler dependent. Certain compilers may inline operator new, in that
8
case this will not replace default allocator */
9
void* operator new(size_t bytes) throw(std::bad_alloc) {
10
std::cout << "GlobalAlloc(new): " << bytes << std::endl;
14
//template <typename T> class DSAllocator;
17
class DSAllocator : public std::allocator<T> {
19
typedef size_t size_type;
22
template<typename U> struct rebind { typedef DSAllocator<U> other; };
25
DSAllocator() : std::allocator<T>() {
26
std::cout << "Constructor " << typeid(T).name() << "()" << std::endl;
30
std::cout << "Destructor " << typeid(T).name() << "()" << std::endl;
33
DSAllocator(const DSAllocator &a) : std::allocator<T>(a) {
34
std::cout << "Constructor " << typeid(T).name() << "(a)" << std::endl;
38
DSAllocator(const DSAllocator<U> &a) : std::allocator<T>(a) {
39
std::cout << "Constructor " << typeid(T).name() << "(U a)" << std::endl;
42
pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer hint = 0) {
43
std::cout << "Allocating: " << cnt << '*' << typeid(T).name() << "(" << sizeof(T) << ")" << std::endl;
44
return std::allocator<T>::allocate(cnt, hint);
47
void deallocate(pointer p, size_type cnt) {
48
std::cout << "Deallocating: " << cnt << '*' << typeid(T).name() << "(" << sizeof(T) << ")" << std::endl;
49
return std::allocator<T>::deallocate(p, cnt);
52
void construct(pointer p, const T& val) {
53
std::cout << "Init" << std::endl;
54
std::allocator<T>::construct(p, val);
57
void destroy(pointer p) {
58
std::cout << "Destroy" << std::endl;
59
std::allocator<T>::destroy(p);
64
//template <typename T> class DSAllocator2;
69
typedef size_t size_type;
70
typedef ptrdiff_t difference_type;
72
typedef const T* const_pointer;
74
typedef const T& const_reference;
77
std::allocator<T> *alloc;
79
template<typename U> struct rebind { typedef DSAllocator2<U> other; };
83
std::cout << "Constructor " << typeid(T).name() << "()" << ", " << this << std::endl;
84
alloc = new std::allocator<T>();
88
std::cout << "Destructor " << typeid(T).name() << "()" << ", " << this << std::endl;
92
/* if this function doesn't exist, everything looks fine, but segmentations
93
occurs due to the fact, what instead of this constructor simply copy
94
is running and 'alloc' is freed twice */
95
DSAllocator2(const DSAllocator2 &a) {
96
std::cout << "Constructor " << typeid(T).name() << "(a)" << std::endl;
97
alloc = new std::allocator<T>(*a.alloc);
100
template <typename U> DSAllocator2(const std::allocator<U> &a) {
101
std::cout << "Constructor " << typeid(T).name() << "(stdal)" << std::endl;
102
alloc = new std::allocator<T>(a);
105
pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer hint = 0) {
106
std::cout << "Allocating: " << cnt << '*' << typeid(T).name() << "(" << sizeof(T) << ")" << std::endl;
107
return alloc->allocate(cnt, hint);
110
void deallocate(pointer p, size_type cnt) {
111
std::cout << "Deallocating: " << cnt << '*' << typeid(T).name() << "(" << sizeof(T) << ")" << std::endl;
112
return alloc->deallocate(p, cnt);
115
void construct(pointer p, const T& val) {
116
std::cout << "Construct: " << typeid(T).name() << "(" << sizeof(T) << ")" << std::endl;
117
alloc->construct(p, val);
120
void destroy(pointer p) {
121
std::cout << "Destroy: " << typeid(T).name() << "(" << sizeof(T) << ")" << std::endl;
125
size_type max_size() const throw() {
126
return alloc->max_size();
129
pointer address(reference r) const {
133
const pointer address(const_reference r) const {
139
template<typename _T1, typename _T2>
140
inline bool operator==(const DSAllocator2<_T1>& a, const DSAllocator2<_T2>& b) {
141
std::cout << &a << " == " << &b << std::endl;
144
template<typename _T1, typename _T2>
145
inline bool operator!=(const DSAllocator2<_T1>& a, const DSAllocator2<_T2>& b) {
146
std::cout << &a << " != " << &b << std::endl;
154
typedef basic_string<char, char_traits<char>, DSAllocator<char> > ds_string1;
155
typedef vector <ds_string1, DSAllocator<ds_string1> > ds_vector1;
157
typedef basic_string<char, char_traits<char>, DSAllocator2<char> > ds_string;
158
typedef vector <ds_string, DSAllocator<ds_string> > ds_vector;
161
cout << "Inherited allocator works in vector, but not in string:" << endl;
162
ds_vector1 *vec1 = new ds_vector1();
164
char *c = (char*)malloc(50);
170
vec1->push_back(str);
176
cout << "New allocator works well:" << endl;
179
vec.push_back("xxxxxxxxxx");