-
Notifications
You must be signed in to change notification settings - Fork 3
/
post_stl.h
405 lines (337 loc) · 10.5 KB
/
post_stl.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//-< POST_STL.H >----------------------------------------------------*--------*
// POST++ Version 1.0 (c) 1998 GARRET * ? *
// (Persistent Object Storage) * /\| *
// * / \ *
// Created: 31-May-99 K.A. Knizhnik * / [] \ *
// Last update: 31-May-99 K.A. Knizhnik * GARRET *
//-------------------------------------------------------------------*--------*
// Interface with STL library
//-------------------------------------------------------------------*--------*
#ifndef __POST_STL_H__
#define __POST_STL_H__
#include "object.h"
#ifdef _WIN32
// trucation of long debug names
#pragma warning(disable:4786)
#endif
#include <new>
#ifdef REDEFINE_DEFAULT_ALLOCATOR
#ifdef USE_MICROSOFT_STL
#if _MSC_VER >= 1300
#include <cstdlib>
#include <xutility>
#endif
#define post_alloc allocator
#define _XMEMORY_ // redefine Microsoft allocator
#ifndef _FARQ /* specify standard memory model */
#define _FARQ
#define _PDFT ptrdiff_t
#define _SIZT size_t
#define _POINTER_X(T, A) T*
#define _REFERENCE_X(T, A) T&
#define _CPOINTER_X(T, A) T const*
#define _CREFERENCE_X(T, A) T const&
_STD_BEGIN
template<class _Ty> inline
_Ty _FARQ *_Allocate(_PDFT _N, _Ty _FARQ *)
{if (_N < 0)
_N = 0;
return ((_Ty _FARQ *)operator new(
(_SIZT)_N * sizeof (_Ty))); }
// TEMPLATE FUNCTION _Construct
template<class _T1, class _T2> inline
void _Construct(_T1 _FARQ *_P, const _T2& _V)
{new ((void _FARQ *)_P) _T1(_V); }
// TEMPLATE FUNCTION _Destroy
template<class _Ty> inline
void _Destroy(_Ty _FARQ *_P)
{_DESTRUCTOR(_Ty, _P); }
template<class _Ty,
class _Alloc> inline
void _Destroy_range(_Ty *_First, _Ty *_Last, _Alloc& _Al)
{ // destroy [_First, _Last)
_Destroy_range(_First, _Last, _Al, _Ptr_cat(_First, _Last));
}
#if _MSC_VER >= 1300
template<class _Ty,
class _Alloc> inline
void _Destroy_range(_Ty *_First, _Ty *_Last, _Alloc& _Al,
_Nonscalar_ptr_iterator_tag)
{ // destroy [_First, _Last), arbitrary type
for (; _First != _Last; ++_First)
_Al.destroy(_First);
}
template<class _Ty,
class _Alloc> inline
void _Destroy_range(_Ty *_First, _Ty *_Last, _Alloc& _Al,
_Scalar_ptr_iterator_tag)
{ // destroy [_First, _Last), scalar type (do nothing)
}
#endif
inline void _Destroy(char _FARQ *_P)
{}
inline void _Destroy(wchar_t _FARQ *_P)
{}
_STD_END
#endif // ifndef _FARQ
#else // ifdef USE_MICROSOFT_STL
#ifdef USE_OSPACE_LIBRARY
#define OS_STL_ALLOCATE_H
#define post_alloc allocator
#else // ifdef USE_OSPACE_LIBRARY
#ifdef USE_STLPORT
#ifndef USE_STD_ALLOCATORS
#define _STLP_USE_RAW_SGI_ALLOCATORS 1
#endif
#include "stl/_config.h"
#undef _STLP_DEFAULT_ALLOCATOR
#undef _STLP_DEFAULT_ALLOCATOR_SELECT
#undef _STLP_DEFAULT_PAIR_ALLOCATOR
#undef _STLP_DEFAULT_PAIR_ALLOCATOR_SELECT
#ifdef USE_STD_ALLOCATORS
#define _STLP_DEFAULT_ALLOCATOR(_Tp) post_alloc<_Tp>
#define _STLP_DEFAULT_ALLOCATOR_SELECT( _Tp ) __DFL_TMPL_PARAM(_Alloc, post_alloc< _Tp >)
#define _STLP_DEFAULT_PAIR_ALLOCATOR(_Key, _Tp) post_alloc< pair < _Key, _Tp > >
#define _STLP_DEFAULT_PAIR_ALLOCATOR_SELECT(_Key, _Tp ) \
class _Alloc = post_alloc< pair < _Key, _Tp > >
#else // ifdef USE_STD_ALLOCATORS
#define _STLP_DEFAULT_ALLOCATOR( _Tp )post_alloc
#define _STLP_DEFAULT_ALLOCATOR_SELECT( _Tp ) __DFL_TYPE_PARAM(_Alloc,post_alloc)
#define _STLP_DEFAULT_PAIR_ALLOCATOR( _Key, _Tp ) post_alloc
#define _STLP_DEFAULT_PAIR_ALLOCATOR_SELECT(_Key, _Tp ) __DFL_TYPE_PARAM(_Alloc,post_alloc)
#endif // USE_STD_ALLOCATORS
#else // ifdef USE_STLPORT
#if defined(USE_STD_ALLOCATORS)
#define __STL_DEFAULT_ALLOCATOR(T) post_alloc<T>
#else
#define __STL_DEFAULT_ALLOCATOR(T) post_alloc
#endif
#endif // USE_STLPORT
#endif // USE_OSPACE_LIBRARY
#endif // USE_MICROSOFT_STL
#endif // ifdef REDEFINE_DEFAULT_ALLOCATOR
#if defined(USE_STD_ALLOCATORS) || defined(USE_MICROSOFT_STL)
#ifndef NO_NAMESPACES
namespace std {
#endif
template<class _Tp> inline
void _DestroyPtr(_Tp *_P) { _P->~_Tp(); }
inline void _DestroyPtr(char *) {}
inline void _DestroyPtr(wchar_t *) {}
class basic_post_alloc {
public:
};
template<class _Tp>
class POST_DLL_ENTRY post_alloc : public basic_post_alloc {
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef _Tp *pointer;
typedef const _Tp *const_pointer;
typedef _Tp& reference;
typedef const _Tp& const_reference;
typedef _Tp value_type;
pointer address(reference _X) const {
return (&_X);
}
#if 0 // this code is not compilable if _Tp is already const type
const_pointer address(const_reference _X) const {
return (&_X);
}
#endif
post_alloc(basic_post_alloc const&) {}
post_alloc() {}
template <class _Tp1> struct rebind {
typedef post_alloc<_Tp1> other;
};
void construct(pointer _P, const _Tp& _V) {
new (_P) _Tp(_V);
}
void destroy(pointer _P) {
_DestroyPtr(_P);
}
size_t max_size() const {
size_t _N = (size_t)(-1) / sizeof (_Tp);
return (0 < _N ? _N : 1);
}
_Tp* allocate(size_t __n, void const*) {
return allocate(__n);
}
_Tp* raw_allocate(size_type each_object_size = 1,
size_type n_objects = 1,
const void* hint = 0)
{
return allocate(each_object_size*n_objects, hint);
}
char* _Charalloc(size_t __n) {
return (char*)allocate(__n);
}
_Tp* allocate(size_t __n = 1) {
if (__n == 0) {
return 0;
}
POST_NAMESPACE storage* store = POST_NAMESPACE storage::find_storage((POST_NAMESPACE object*)this);
if (store == NULL) {
#ifdef REDEFINE_DEFAULT_ALLOCATOR
return (_Tp*)malloc(__n*sizeof(_Tp));
#else
store = POST_NAMESPACE storage::get_current_storage();
#endif
}
POST_NAMESPACE post_raw_object* a = (POST_NAMESPACE post_raw_object*)store->get_static_data();
if (a == NULL) {
a = POST_NAMESPACE post_raw_object::create(*store, __n*sizeof(_Tp));
store->set_static_data(a);
}
return (_Tp*)a;
}
void deallocate(void* __p, size_t __n = 1) {
POST_NAMESPACE storage* store = POST_NAMESPACE storage::find_storage((POST_NAMESPACE object*)__p);
if (store != NULL) {
#if defined(USE_MICROSOFT_STL)
if (!store->is_static_data(__p))
#endif
store->free((POST_NAMESPACE object*)__p);
} else {
free(__p);
}
}
void raw_deallocate( void* p ) {
deallocate(p);
}
};
template<class _Tp, class _Tq> inline
bool operator==(const post_alloc<_Tp>&, const post_alloc<_Tq>&) {
return (false);
}
template<class _Tp, class _Tq> inline
bool operator!=(const post_alloc<_Tp>&, const post_alloc<_Tq>&) {
return (true);
}
#ifdef USE_STLPORT
template <class _Tp1, class _Tp2>
inline post_alloc<_Tp2>& _STLP_CALL
__stl_alloc_rebind(post_alloc<_Tp1>& __a, const _Tp2*) { return (post_alloc<_Tp2>&)(__a); }
template <class _Tp1, class _Tp2>
inline post_alloc<_Tp2> _STLP_CALL
__stl_alloc_create(const post_alloc<_Tp1>&, const _Tp2*) { return post_alloc<_Tp2>(); }
#endif
#define __STL_USE_STD_ALLOCATORS
#ifdef REDEFINE_STRING
class POST_DLL_ENTRY static_post_alloc {
public:
template <class _Tp1> struct rebind {
typedef post_alloc<_Tp1> other;
};
static void* allocate(size_t __n) {
POST_NAMESPACE storage* store = POST_NAMESPACE storage::get_current_storage();
assert(store != NULL);
return POST_NAMESPACE post_raw_object::create(*store, __n);
}
static void deallocate(void* __p, size_t /* __n */) {
POST_NAMESPACE storage* store = POST_NAMESPACE storage::find_storage((POST_NAMESPACE object*)__p);
assert(store != NULL);
store->free((POST_NAMESPACE object*)__p);
}
};
#if defined(__GNUC__) && __GNUC__ >= 3
#include <bits/straits.h>
#include <bits/bastring.h>
#else
#include <std/straits.h>
#include <std/bastring.h>
#endif
typedef basic_string<char, string_char_traits<char>, static_post_alloc > string;
#define __STRING__
#endif // REDEFINE_STRING
#ifndef NO_NAMESPACES
}
#endif
#else // defined(USE_STD_ALLOCATORS) || defined(USE_MICROSOFT_STL)
class POST_DLL_ENTRY post_alloc {
public:
static void* allocate(size_t __n) {
POST_NAMESPACE storage* store = POST_NAMESPACE storage::get_current_storage();
assert(store != NULL);
return POST_NAMESPACE post_raw_object::create(*store, __n);
}
static void deallocate(void* __p, size_t /* __n */) {
POST_NAMESPACE storage* store = POST_NAMESPACE storage::find_storage((POST_NAMESPACE object*)__p);
assert(store != NULL);
store->free((POST_NAMESPACE object*)__p);
}
};
#if defined(REPL_RW_ALLOCATORS)
template <class T>
class POST_DLL_ENTRY static_post_alloc {
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;
static_post_alloc(){};
~static_post_alloc(){};
void* allocate(size_t __n, void* = 0) {
POST_NAMESPACE storage* store = POST_NAMESPACE storage::get_current_storage();
assert(store != NULL);
return POST_NAMESPACE post_raw_object::create(*store, __n);
}
void deallocate(void* __p, size_t /* __n */) {
if(__p)
{
POST_NAMESPACE storage* store = POST_NAMESPACE storage::find_storage((POST_NAMESPACE object*)__p);
assert(store != NULL);
store->free((POST_NAMESPACE object*)__p);
}
}
size_t max_size(size_type size) const {
size_t _N = (size_t)(-1) / size;
return (0 < _N ? _N : 1);
}
};
#ifdef REDEFINE_STRING
#include <string>
#ifndef NO_NAMESPACES
using namespace std;
#endif
typedef basic_string<char, char_traits<char>, static_post_alloc<char> > Pstring;
#endif
#endif
#endif
#if defined(__NO_NEW_DEFINED__)
inline void* operator new(size_t, void* addr)
#ifndef USE_MICROSOFT_STL
throw (std::bad_alloc)
#endif
{
return addr;
}
#endif
inline void* operator new(size_t size, POST_NAMESPACE storage& store) {
POST_NAMESPACE post_raw_object* a = (POST_NAMESPACE post_raw_object*)store.get_static_data();
if (a == NULL) {
a = POST_NAMESPACE post_raw_object::create(store, size);
store.set_static_data(a);
}
return a;
}
inline void* operator new(size_t size)
#if !defined(_STLPORT_VERSION) && !defined(USE_MICROSOFT_STL)
throw (std::bad_alloc)
#endif
{
return malloc(size);
}
inline void operator delete(void* ptr, POST_NAMESPACE storage&) throw()
{
POST_NAMESPACE storage* store = POST_NAMESPACE storage::find_storage((POST_NAMESPACE object*)ptr);
if (store != NULL) {
store->free((POST_NAMESPACE object*)ptr);
} else {
free(ptr);
}
}
#endif