/docs/MyDocs

To get this branch, use:
bzr branch http://darksoft.org/webbzr/docs/MyDocs
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
#include <iostream>
#include <iterator>
#include <vector>
#include <memory>


  /* Compiler dependent. Certain compilers may inline operator new, in that
  case this will not replace default allocator */
void* operator new(size_t bytes) throw(std::bad_alloc) {
    std::cout << "GlobalAlloc(new): " << bytes << std::endl;
    return malloc(bytes);
}

//template <typename T> class DSAllocator;

template <typename T>
class DSAllocator : public std::allocator<T> {
 public:
    typedef size_t size_type;
    typedef T* pointer;

    template<typename U> struct rebind { typedef DSAllocator<U> other; };
 
 
    DSAllocator() : std::allocator<T>() { 
	std::cout << "Constructor " << typeid(T).name() << "()" << std::endl; 
    }

   ~DSAllocator() { 
	std::cout << "Destructor " << typeid(T).name() << "()" << std::endl; 
    }

    DSAllocator(const DSAllocator &a) : std::allocator<T>(a) { 
    	std::cout << "Constructor " << typeid(T).name() << "(a)" << std::endl; 
    }

    template <typename U>
    DSAllocator(const DSAllocator<U> &a) : std::allocator<T>(a) { 
    	std::cout << "Constructor " << typeid(T).name() << "(U a)" << std::endl; 
    }

    pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer hint = 0) {
	std::cout << "Allocating: " << cnt << '*' << typeid(T).name() << "(" << sizeof(T) << ")" << std::endl;
	return std::allocator<T>::allocate(cnt, hint);
    }
    
    void deallocate(pointer p, size_type cnt) {
	std::cout << "Deallocating: " << cnt << '*' << typeid(T).name() << "(" << sizeof(T) << ")" << std::endl;
	return std::allocator<T>::deallocate(p, cnt);
    }
    
    void construct(pointer p, const T& val) {
	std::cout << "Init" << std::endl;
	std::allocator<T>::construct(p, val);
    }
    
    void destroy(pointer p) {
	std::cout << "Destroy" << std::endl;
	std::allocator<T>::destroy(p);
    }

};

//template <typename T> class DSAllocator2;

template <typename T>
class DSAllocator2  {
 public:
    typedef size_t size_type;
    typedef ptrdiff_t difference_type;
    typedef T* pointer;
    typedef const T* const_pointer;
    typedef T& reference;
    typedef const T& const_reference;
    typedef T value_type;
    
    std::allocator<T> *alloc;

    template<typename U> struct rebind { typedef DSAllocator2<U> other; };

 
    DSAllocator2() { 
	std::cout << "Constructor " << typeid(T).name() << "()" << ", " << this << std::endl; 
	alloc = new std::allocator<T>();
    }

   ~DSAllocator2() { 
	std::cout << "Destructor " << typeid(T).name() << "()" << ", " << this << std::endl; 
	delete alloc;
    }

	/* if this function doesn't exist, everything looks fine, but segmentations
	occurs due to the fact, what instead of this constructor simply copy
	is running and 'alloc' is freed twice */
    DSAllocator2(const DSAllocator2 &a)  {
    	std::cout << "Constructor " << typeid(T).name() << "(a)" << std::endl; 
	alloc = new std::allocator<T>(*a.alloc);
    }

    template <typename U> DSAllocator2(const std::allocator<U> &a)  { 
    	std::cout << "Constructor " << typeid(T).name() << "(stdal)" << std::endl; 
	alloc = new std::allocator<T>(a);
    }

    pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer hint = 0) {
	std::cout << "Allocating: " << cnt << '*' << typeid(T).name() << "(" << sizeof(T) << ")" << std::endl;
	return alloc->allocate(cnt, hint);
    }
    
    void deallocate(pointer p, size_type cnt) {
	std::cout << "Deallocating: " << cnt << '*' << typeid(T).name() << "(" << sizeof(T) << ")" << std::endl;
	return alloc->deallocate(p, cnt);
    }
    
    void construct(pointer p, const T& val) {
	std::cout << "Construct: " << typeid(T).name() << "(" << sizeof(T) << ")" << std::endl;
	alloc->construct(p, val);
    }
    
    void destroy(pointer p) {
	std::cout << "Destroy: " << typeid(T).name() << "(" << sizeof(T) << ")" << std::endl;
	alloc->destroy(p);
    }
    
    size_type max_size() const throw() {
	return alloc->max_size();
    }
    
    pointer address(reference r) const {
	return &r;
    }
    
    const pointer address(const_reference r) const {
	return &r;
    }
    
};

template<typename _T1, typename _T2>
inline bool operator==(const DSAllocator2<_T1>& a, const DSAllocator2<_T2>& b) {
    std::cout << &a << " == " << &b << std::endl;
    return true; 
}
template<typename _T1, typename _T2>
inline bool operator!=(const DSAllocator2<_T1>& a, const DSAllocator2<_T2>& b) {
    std::cout << &a << " != " << &b << std::endl;
    return false; 
}


using namespace std;

main() {
    typedef basic_string<char, char_traits<char>, DSAllocator<char> > ds_string1;
    typedef vector <ds_string1, DSAllocator<ds_string1> > ds_vector1;

    typedef basic_string<char, char_traits<char>, DSAllocator2<char> > ds_string;
    typedef vector <ds_string, DSAllocator<ds_string> > ds_vector;


    cout << "Inherited allocator works in vector, but not in string:" << endl;
    ds_vector1 *vec1 = new ds_vector1();

    char *c = (char*)malloc(50);
    strcpy(c, "xxx");
    ds_string1 str(c);
    strcpy(c, "zzz");
    free(c);

    vec1->push_back(str);

    delete vec1;
    cout << endl;


    cout << "New allocator works well:" << endl;
    ds_vector vec;
    vec.push_back("1");
    vec.push_back("xxxxxxxxxx");
}