/project/jni/stlport/stlport/stl/_alloc.h

https://github.com/aichunyu/FFPlayer · C Header · 675 lines · 463 code · 85 blank · 127 comment · 60 complexity · fccee764962926905bdf0560f64d5058 MD5 · raw file

  1. /*
  2. *
  3. * Copyright (c) 1996,1997
  4. * Silicon Graphics Computer Systems, Inc.
  5. *
  6. * Copyright (c) 1997
  7. * Moscow Center for SPARC Technology
  8. *
  9. * Copyright (c) 1999
  10. * Boris Fomitchev
  11. *
  12. * This material is provided "as is", with absolutely no warranty expressed
  13. * or implied. Any use is at your own risk.
  14. *
  15. * Permission to use or copy this software for any purpose is hereby granted
  16. * without fee, provided the above notices are retained on all copies.
  17. * Permission to modify the code and to distribute modified code is granted,
  18. * provided the above notices are retained, and a notice that the code was
  19. * modified is included with the above copyright notice.
  20. *
  21. */
  22. /* NOTE: This is an internal header file, included by other STL headers.
  23. * You should not attempt to use it directly.
  24. */
  25. #ifndef _STLP_INTERNAL_ALLOC_H
  26. #define _STLP_INTERNAL_ALLOC_H
  27. #ifndef _STLP_INTERNAL_CSTDDEF
  28. # include <stl/_cstddef.h>
  29. #endif
  30. #if !defined (_STLP_DEBUG_H) && (defined(_STLP_DEBUG) || defined(_STLP_ASSERTIONS) || defined(_STLP_DEBUG_ALLOC))
  31. # include <stl/debug/_debug.h>
  32. #endif
  33. #ifndef _STLP_INTERNAL_CSTDLIB
  34. # include <stl/_cstdlib.h>
  35. #endif
  36. #ifndef _STLP_INTERNAL_CSTRING
  37. # include <stl/_cstring.h>
  38. #endif
  39. #ifndef _STLP_INTERNAL_ALGOBASE_H
  40. # include <stl/_algobase.h>
  41. #endif
  42. #ifndef __THROW_BAD_ALLOC
  43. # if !defined(_STLP_USE_EXCEPTIONS)
  44. # ifndef _STLP_INTERNAL_CSTDIO
  45. # include <stl/_cstdio.h>
  46. # endif
  47. # define __THROW_BAD_ALLOC puts("out of memory\n"); exit(1)
  48. # else
  49. # define __THROW_BAD_ALLOC throw _STLP_STD::bad_alloc()
  50. # endif
  51. #endif
  52. #ifndef _STLP_INTERNAL_NEW_HEADER
  53. # include <stl/_new.h>
  54. #endif
  55. #ifndef _STLP_INTERNAL_CONSTRUCT_H
  56. # include <stl/_construct.h>
  57. #endif
  58. #if !defined (__ALLOC)
  59. # define __ALLOC __sgi_alloc
  60. #endif
  61. _STLP_BEGIN_NAMESPACE
  62. #if defined (_STLP_USE_RAW_SGI_ALLOCATORS)
  63. template <class _Tp, class _Alloc> struct __allocator;
  64. #endif
  65. // Malloc-based allocator. Typically slower than default alloc below.
  66. // Typically thread-safe and more storage efficient.
  67. #if !defined (_STLP_USE_NO_IOSTREAMS)
  68. typedef void (* __oom_handler_type)();
  69. #endif
  70. class _STLP_CLASS_DECLSPEC __malloc_alloc {
  71. public:
  72. // this one is needed for proper simple_alloc wrapping
  73. typedef char value_type;
  74. #if defined (_STLP_MEMBER_TEMPLATE_CLASSES) && defined (_STLP_USE_RAW_SGI_ALLOCATORS)
  75. template <class _Tp1> struct rebind {
  76. typedef __allocator<_Tp1, __malloc_alloc> other;
  77. };
  78. #endif
  79. static void* _STLP_CALL allocate(size_t& __n)
  80. #if !defined (_STLP_USE_NO_IOSTREAMS)
  81. ;
  82. #else
  83. {
  84. void *__result = malloc(__n);
  85. # if defined (_STLP_MALLOC_USABLE_SIZE)
  86. if (__result != 0) {
  87. __n = _STLP_MALLOC_USABLE_SIZE(__result);
  88. }
  89. # endif
  90. if (__result == 0) {
  91. __THROW_BAD_ALLOC;
  92. }
  93. return __result;
  94. }
  95. #endif
  96. static void _STLP_CALL deallocate(void* __p, size_t /* __n */) { free((char*)__p); }
  97. #if !defined (_STLP_USE_NO_IOSTREAMS)
  98. static __oom_handler_type _STLP_CALL set_malloc_handler(__oom_handler_type __f);
  99. #endif
  100. };
  101. // New-based allocator. Typically slower than default alloc below.
  102. // Typically thread-safe and more storage efficient.
  103. class _STLP_CLASS_DECLSPEC __new_alloc {
  104. public:
  105. // this one is needed for proper simple_alloc wrapping
  106. typedef char value_type;
  107. #if defined (_STLP_MEMBER_TEMPLATE_CLASSES) && defined (_STLP_USE_RAW_SGI_ALLOCATORS)
  108. template <class _Tp1> struct rebind {
  109. typedef __allocator<_Tp1, __new_alloc > other;
  110. };
  111. #endif
  112. static void* _STLP_CALL allocate(size_t __n) { return __stl_new(__n); }
  113. static void _STLP_CALL deallocate(void* __p, size_t) { __stl_delete(__p); }
  114. };
  115. // Allocator adaptor to check size arguments for debugging.
  116. // Reports errors using assert. Checking can be disabled with
  117. // NDEBUG, but it's far better to just use the underlying allocator
  118. // instead when no checking is desired.
  119. // There is some evidence that this can confuse Purify.
  120. // This adaptor can only be applied to raw allocators
  121. template <class _Alloc>
  122. class __debug_alloc : public _Alloc {
  123. public:
  124. typedef _Alloc __allocator_type;
  125. typedef typename _Alloc::value_type value_type;
  126. private:
  127. struct __alloc_header {
  128. size_t __magic: 16;
  129. size_t __type_size:16;
  130. _STLP_UINT32_T _M_size;
  131. }; // that is 8 bytes for sure
  132. // Sunpro CC has bug on enums, so extra_before/after set explicitly
  133. enum { __pad = 8, __magic = 0xdeba, __deleted_magic = 0xdebd,
  134. __shred_byte = _STLP_SHRED_BYTE };
  135. enum { __extra_before = 16, __extra_after = 8 };
  136. // Size of space used to store size. Note
  137. // that this must be large enough to preserve
  138. // alignment.
  139. static size_t _STLP_CALL __extra_before_chunk() {
  140. return (long)__extra_before / sizeof(value_type) +
  141. (size_t)((long)__extra_before % sizeof(value_type) > 0);
  142. }
  143. static size_t _STLP_CALL __extra_after_chunk() {
  144. return (long)__extra_after / sizeof(value_type) +
  145. (size_t)((long)__extra_after % sizeof(value_type) > 0);
  146. }
  147. public:
  148. #if defined (_STLP_MEMBER_TEMPLATE_CLASSES) && defined (_STLP_USE_RAW_SGI_ALLOCATORS)
  149. template <class _Tp1> struct rebind {
  150. typedef __allocator< _Tp1, __debug_alloc<_Alloc> > other;
  151. };
  152. #endif
  153. __debug_alloc() {}
  154. ~__debug_alloc() {}
  155. static void* _STLP_CALL allocate(size_t);
  156. static void _STLP_CALL deallocate(void *, size_t);
  157. };
  158. # if defined (__OS400__) || defined (_WIN64)
  159. enum {_ALIGN = 16, _ALIGN_SHIFT = 4, _MAX_BYTES = 256};
  160. # else
  161. enum {_ALIGN = 8, _ALIGN_SHIFT = 3, _MAX_BYTES = 128};
  162. # endif /* __OS400__ */
  163. #if !defined (_STLP_USE_NO_IOSTREAMS)
  164. # if !defined (_STLP_USE_SIMPLE_NODE_ALLOC)
  165. // Default node allocator.
  166. // With a reasonable compiler, this should be roughly as fast as the
  167. // original STL class-specific allocators, but with less fragmentation.
  168. // Define _STLP_USE_SIMPLE_NODE_ALLOC to use __new_alloc instead, which
  169. // can be implemented without out-of-line functions.
  170. class _STLP_CLASS_DECLSPEC __node_alloc {
  171. static void * _STLP_CALL _M_allocate(size_t& __n);
  172. /* __p may not be 0 */
  173. static void _STLP_CALL _M_deallocate(void *__p, size_t __n);
  174. public:
  175. // this one is needed for proper simple_alloc wrapping
  176. typedef char value_type;
  177. # if defined (_STLP_MEMBER_TEMPLATE_CLASSES) && defined (_STLP_USE_RAW_SGI_ALLOCATORS)
  178. template <class _Tp1> struct rebind {
  179. typedef __allocator<_Tp1, __node_alloc> other;
  180. };
  181. # endif
  182. /* __n must be > 0 */
  183. static void* _STLP_CALL allocate(size_t& __n)
  184. { return (__n > (size_t)_MAX_BYTES) ? __stl_new(__n) : _M_allocate(__n); }
  185. /* __p may not be 0 */
  186. static void _STLP_CALL deallocate(void *__p, size_t __n)
  187. { if (__n > (size_t)_MAX_BYTES) __stl_delete(__p); else _M_deallocate(__p, __n); }
  188. };
  189. # if defined (_STLP_USE_TEMPLATE_EXPORT)
  190. _STLP_EXPORT_TEMPLATE_CLASS __debug_alloc<__node_alloc>;
  191. # endif
  192. # else /* _STLP_USE_SIMPLE_NODE_ALLOC */
  193. // Use __new_alloc instead of __node_alloc. This prevents the need for
  194. // out-of-line _M_allocate and _M_dealloacte functions.
  195. typedef __new_alloc __node_alloc;
  196. # endif /* _STLP_USE_SIMPLE_NODE_ALLOC */
  197. #endif /* _STLP_USE_NO_IOSTREAMS */
  198. #if defined (_STLP_USE_TEMPLATE_EXPORT)
  199. _STLP_EXPORT_TEMPLATE_CLASS __debug_alloc<__new_alloc>;
  200. _STLP_EXPORT_TEMPLATE_CLASS __debug_alloc<__malloc_alloc>;
  201. #endif
  202. /* macro to convert the allocator for initialization
  203. * not using MEMBER_TEMPLATE_CLASSES as it should work given template constructor */
  204. #if defined (_STLP_MEMBER_TEMPLATES) || ! defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
  205. /* if _STLP_NO_TEMPLATE_CONVERSIONS is set, the member template constructor is
  206. * not used implicitly to convert allocator parameter, so let us do it explicitly */
  207. # if defined (_STLP_MEMBER_TEMPLATE_CLASSES) && defined (_STLP_NO_TEMPLATE_CONVERSIONS)
  208. # define _STLP_CONVERT_ALLOCATOR(__a, _Tp) __stl_alloc_create(__a,(_Tp*)0)
  209. # else
  210. # define _STLP_CONVERT_ALLOCATOR(__a, _Tp) __a
  211. # endif
  212. /* else convert, but only if partial specialization works, since else
  213. * Container::allocator_type won't be different */
  214. #else
  215. # define _STLP_CONVERT_ALLOCATOR(__a, _Tp) __stl_alloc_create(__a,(_Tp*)0)
  216. #endif /* _STLP_MEMBER_TEMPLATES || !_STLP_CLASS_PARTIAL_SPECIALIZATION */
  217. // Another allocator adaptor: _Alloc_traits. This serves two
  218. // purposes. First, make it possible to write containers that can use
  219. // either SGI-style allocators or standard-conforming allocator.
  220. // The fully general version.
  221. template <class _Tp, class _Allocator>
  222. struct _Alloc_traits {
  223. typedef _Allocator _Orig;
  224. #if !defined (_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE)
  225. typedef typename _Allocator::_STLP_TEMPLATE rebind<_Tp> _Rebind_type;
  226. typedef typename _Rebind_type::other allocator_type;
  227. static allocator_type create_allocator(const _Orig& __a)
  228. { return allocator_type(_STLP_CONVERT_ALLOCATOR(__a, _Tp)); }
  229. #else
  230. // this is not actually true, used only to pass this type through
  231. // to dynamic overload selection in _STLP_alloc_proxy methods
  232. typedef _Allocator allocator_type;
  233. #endif /* !_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE */
  234. };
  235. #if defined (_STLP_USE_PERTHREAD_ALLOC)
  236. _STLP_END_NAMESPACE
  237. // include additional header here
  238. # include <stl/_pthread_alloc.h>
  239. _STLP_BEGIN_NAMESPACE
  240. # if defined (_STLP_DEBUG_ALLOC)
  241. typedef __debug_alloc<__pthread_alloc> __sgi_alloc;
  242. # else
  243. typedef __pthread_alloc __sgi_alloc;
  244. # endif /* _STLP_DEBUG_ALLOC */
  245. typedef __pthread_alloc __single_client_alloc;
  246. typedef __pthread_alloc __multithreaded_alloc;
  247. #else /* _STLP_USE_PERTHREAD_ALLOC */
  248. # if defined (_STLP_USE_NEWALLOC)
  249. # if defined (_STLP_DEBUG_ALLOC)
  250. typedef __debug_alloc<__new_alloc> __sgi_alloc;
  251. # else
  252. typedef __new_alloc __sgi_alloc;
  253. # endif /* _STLP_DEBUG_ALLOC */
  254. typedef __new_alloc __single_client_alloc;
  255. typedef __new_alloc __multithreaded_alloc;
  256. # elif defined (_STLP_USE_MALLOC)
  257. # if defined (_STLP_DEBUG_ALLOC)
  258. typedef __debug_alloc<__malloc_alloc> __sgi_alloc;
  259. # else
  260. typedef __malloc_alloc __sgi_alloc;
  261. # endif /* _STLP_DEBUG_ALLOC */
  262. typedef __malloc_alloc __single_client_alloc;
  263. typedef __malloc_alloc __multithreaded_alloc;
  264. # else
  265. # if defined (_STLP_DEBUG_ALLOC)
  266. typedef __debug_alloc<__node_alloc> __sgi_alloc;
  267. # else
  268. typedef __node_alloc __sgi_alloc;
  269. # endif
  270. typedef __node_alloc __single_client_alloc;
  271. typedef __node_alloc __multithreaded_alloc;
  272. # endif /* _STLP_USE_NEWALLOC */
  273. #endif /* _STLP_USE_PERTHREAD_ALLOC */
  274. // This implements allocators as specified in the C++ standard.
  275. //
  276. // Note that standard-conforming allocators use many language features
  277. // that are not yet widely implemented. In particular, they rely on
  278. // member templates, partial specialization, partial ordering of function
  279. // templates, the typename keyword, and the use of the template keyword
  280. // to refer to a template member of a dependent type.
  281. /*
  282. template <class _Tp>
  283. struct _AllocatorAux {
  284. typedef _Tp* pointer;
  285. typedef const _Tp* const_pointer;
  286. typedef _Tp& reference;
  287. typedef const _Tp& const_reference;
  288. pointer address(reference __x) const {return &__x;}
  289. const_pointer address(const_reference __x) const { return &__x; }
  290. };
  291. template <class _Tp>
  292. struct _AllocatorAux<const _Tp> {
  293. typedef _Tp* pointer;
  294. typedef const _Tp* const_pointer;
  295. typedef _Tp& reference;
  296. typedef const _Tp& const_reference;
  297. const_pointer address(const_reference __x) const { return &__x; }
  298. };
  299. */
  300. template <class _Tp>
  301. class allocator //: public _AllocatorAux<_Tp>
  302. /* A small helper struct to recognize STLport allocator implementation
  303. * from any user specialization one.
  304. */
  305. : public __stlport_class<allocator<_Tp> > {
  306. public:
  307. typedef _Tp value_type;
  308. typedef _Tp* pointer;
  309. typedef const _Tp* const_pointer;
  310. typedef _Tp& reference;
  311. typedef const _Tp& const_reference;
  312. typedef size_t size_type;
  313. typedef ptrdiff_t difference_type;
  314. #if defined (_STLP_MEMBER_TEMPLATE_CLASSES)
  315. template <class _Tp1> struct rebind {
  316. typedef allocator<_Tp1> other;
  317. };
  318. #endif
  319. allocator() _STLP_NOTHROW {}
  320. #if defined (_STLP_MEMBER_TEMPLATES)
  321. template <class _Tp1> allocator(const allocator<_Tp1>&) _STLP_NOTHROW {}
  322. #endif
  323. allocator(const allocator<_Tp>&) _STLP_NOTHROW {}
  324. allocator(__move_source<allocator<_Tp> > src) _STLP_NOTHROW {}
  325. ~allocator() _STLP_NOTHROW {}
  326. pointer address(reference __x) const {return &__x;}
  327. const_pointer address(const_reference __x) const { return &__x; }
  328. // __n is permitted to be 0. The C++ standard says nothing about what the return value is when __n == 0.
  329. _Tp* allocate(size_type __n, const void* = 0) {
  330. if (__n > max_size()) {
  331. __THROW_BAD_ALLOC;
  332. }
  333. if (__n != 0) {
  334. size_type __buf_size = __n * sizeof(value_type);
  335. _Tp* __ret = __REINTERPRET_CAST(_Tp*, __sgi_alloc::allocate(__buf_size));
  336. #if defined (_STLP_DEBUG_UNINITIALIZED) && !defined (_STLP_DEBUG_ALLOC)
  337. if (__ret != 0) {
  338. memset((char*)__ret, _STLP_SHRED_BYTE, __buf_size);
  339. }
  340. #endif
  341. return __ret;
  342. }
  343. else
  344. return 0;
  345. }
  346. // __p is permitted to be a null pointer, only if n==0.
  347. void deallocate(pointer __p, size_type __n) {
  348. _STLP_ASSERT( (__p == 0) == (__n == 0) )
  349. if (__p != 0) {
  350. #if defined (_STLP_DEBUG_UNINITIALIZED) && !defined (_STLP_DEBUG_ALLOC)
  351. memset((char*)__p, _STLP_SHRED_BYTE, __n * sizeof(value_type));
  352. #endif
  353. __sgi_alloc::deallocate((void*)__p, __n * sizeof(value_type));
  354. }
  355. }
  356. // backwards compatibility
  357. void deallocate(pointer __p) const { if (__p != 0) __sgi_alloc::deallocate((void*)__p, sizeof(value_type)); }
  358. size_type max_size() const _STLP_NOTHROW { return size_t(-1) / sizeof(value_type); }
  359. void construct(pointer __p, const_reference __val) { _STLP_STD::_Copy_Construct(__p, __val); }
  360. void destroy(pointer __p) { _STLP_STD::_Destroy(__p); }
  361. #if defined(__MRC__)||(defined(__SC__) && !defined(__DMC__))
  362. template <class _T2> bool operator==(const allocator<_T2>&) const _STLP_NOTHROW { return true; }
  363. template <class _T2> bool operator!=(const allocator<_T2>&) const _STLP_NOTHROW { return false; }
  364. #endif
  365. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  366. //This is just to make swap workaround for compiler without template function partial
  367. //happy.
  368. void swap(allocator<_Tp>&) {}
  369. #endif
  370. #if defined (_STLP_NO_EXTENSIONS)
  371. /* STLport extension giving rounded size of an allocated memory buffer
  372. * This method do not have to be part of a user defined allocator implementation
  373. * and won't even be called if such a function was granted.
  374. */
  375. protected:
  376. #endif
  377. _Tp* allocate(size_type __n, size_type& __allocated_n) {
  378. if (__n > max_size()) {
  379. __THROW_BAD_ALLOC;
  380. }
  381. if (__n != 0) {
  382. size_type __buf_size = __n * sizeof(value_type);
  383. _Tp* __ret = __REINTERPRET_CAST(_Tp*, __sgi_alloc::allocate(__buf_size));
  384. #if defined (_STLP_DEBUG_UNINITIALIZED) && !defined (_STLP_DEBUG_ALLOC)
  385. if (__ret != 0) {
  386. memset((char*)__ret, _STLP_SHRED_BYTE, __buf_size);
  387. }
  388. #endif
  389. __allocated_n = __buf_size / sizeof(value_type);
  390. return __ret;
  391. }
  392. else
  393. return 0;
  394. }
  395. };
  396. _STLP_TEMPLATE_NULL
  397. class _STLP_CLASS_DECLSPEC allocator<void> {
  398. public:
  399. typedef size_t size_type;
  400. typedef ptrdiff_t difference_type;
  401. typedef void* pointer;
  402. typedef const void* const_pointer;
  403. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
  404. typedef void value_type;
  405. #endif
  406. #if defined (_STLP_MEMBER_TEMPLATE_CLASSES)
  407. template <class _Tp1> struct rebind {
  408. typedef allocator<_Tp1> other;
  409. };
  410. #endif
  411. #if defined(__MRC__)||(defined(__SC__)&&!defined(__DMC__)) //*ty 03/24/2001 - MPW compilers get confused on these operator definitions
  412. template <class _T2> bool operator==(const allocator<_T2>&) const _STLP_NOTHROW { return true; }
  413. template <class _T2> bool operator!=(const allocator<_T2>&) const _STLP_NOTHROW { return false; }
  414. #endif
  415. };
  416. #if !(defined(__MRC__)||(defined(__SC__)&&!defined(__DMC__))) //*ty 03/24/2001 - MPW compilers get confused on these operator definitions
  417. template <class _T1, class _T2> inline bool _STLP_CALL operator==(const allocator<_T1>&, const allocator<_T2>&) _STLP_NOTHROW { return true; }
  418. template <class _T1, class _T2> inline bool _STLP_CALL operator!=(const allocator<_T1>&, const allocator<_T2>&) _STLP_NOTHROW { return false; }
  419. #endif
  420. #if defined (_STLP_USE_TEMPLATE_EXPORT)
  421. _STLP_EXPORT_TEMPLATE_CLASS allocator<char>;
  422. # if defined (_STLP_HAS_WCHAR_T)
  423. _STLP_EXPORT_TEMPLATE_CLASS allocator<wchar_t>;
  424. # endif
  425. # if defined (_STLP_USE_PTR_SPECIALIZATIONS)
  426. _STLP_EXPORT_TEMPLATE_CLASS allocator<void*>;
  427. # endif
  428. #endif
  429. _STLP_MOVE_TO_PRIV_NAMESPACE
  430. template <class _Tp>
  431. struct __alloc_type_traits {
  432. #if !defined (__BORLANDC__)
  433. typedef typename _IsSTLportClass<allocator<_Tp> >::_Ret _STLportAlloc;
  434. #else
  435. enum { _Is = _IsSTLportClass<allocator<_Tp> >::_Is };
  436. typedef typename __bool2type<_Is>::_Ret _STLportAlloc;
  437. #endif
  438. //The default allocator implementation which is recognize thanks to the
  439. //__stlport_class inheritance is a stateless object so:
  440. typedef _STLportAlloc has_trivial_default_constructor;
  441. typedef _STLportAlloc has_trivial_copy_constructor;
  442. typedef _STLportAlloc has_trivial_assignment_operator;
  443. typedef _STLportAlloc has_trivial_destructor;
  444. typedef _STLportAlloc is_POD_type;
  445. };
  446. _STLP_MOVE_TO_STD_NAMESPACE
  447. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
  448. template <class _Tp>
  449. struct __type_traits<allocator<_Tp> > : _STLP_PRIV __alloc_type_traits<_Tp> {};
  450. #else
  451. _STLP_TEMPLATE_NULL
  452. struct __type_traits<allocator<char> > : _STLP_PRIV __alloc_type_traits<char> {};
  453. # if defined (_STLP_HAS_WCHAR_T)
  454. _STLP_TEMPLATE_NULL
  455. struct __type_traits<allocator<wchar_t> > : _STLP_PRIV __alloc_type_traits<wchar_t> {};
  456. # endif
  457. # if defined (_STLP_USE_PTR_SPECIALIZATIONS)
  458. _STLP_TEMPLATE_NULL
  459. struct __type_traits<allocator<void*> > : _STLP_PRIV __alloc_type_traits<void*> {};
  460. # endif
  461. #endif
  462. #if !defined (_STLP_FORCE_ALLOCATORS)
  463. # define _STLP_FORCE_ALLOCATORS(a,y)
  464. #endif
  465. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) && !defined (_STLP_MEMBER_TEMPLATE_CLASSES)
  466. // The version for the default allocator, for rare occasion when we have partial spec w/o member template classes
  467. template <class _Tp, class _Tp1>
  468. struct _Alloc_traits<_Tp, allocator<_Tp1> > {
  469. typedef allocator<_Tp1> _Orig;
  470. typedef allocator<_Tp> allocator_type;
  471. static allocator_type create_allocator(const allocator<_Tp1 >& __a)
  472. { return allocator_type(_STLP_CONVERT_ALLOCATOR(__a, _Tp)); }
  473. };
  474. #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
  475. #if !defined (_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE) && defined (_STLP_MEMBER_TEMPLATES)
  476. template <class _Tp, class _Alloc>
  477. inline _STLP_TYPENAME_ON_RETURN_TYPE _Alloc_traits<_Tp, _Alloc>::allocator_type _STLP_CALL
  478. __stl_alloc_create(const _Alloc& __a, const _Tp*) {
  479. typedef typename _Alloc::_STLP_TEMPLATE rebind<_Tp>::other _Rebound_type;
  480. return _Rebound_type(__a);
  481. }
  482. #else
  483. // If custom allocators are being used without member template classes support :
  484. // user (on purpose) is forced to define rebind/get operations !!!
  485. template <class _Tp1, class _Tp2>
  486. inline allocator<_Tp2>& _STLP_CALL
  487. __stl_alloc_rebind(allocator<_Tp1>& __a, const _Tp2*) { return (allocator<_Tp2>&)(__a); }
  488. template <class _Tp1, class _Tp2>
  489. inline allocator<_Tp2> _STLP_CALL
  490. __stl_alloc_create(const allocator<_Tp1>&, const _Tp2*) { return allocator<_Tp2>(); }
  491. #endif /* _STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE */
  492. #if defined (_STLP_USE_RAW_SGI_ALLOCATORS)
  493. // move obsolete stuff out of the way
  494. # include <stl/_alloc_old.h>
  495. #endif
  496. _STLP_MOVE_TO_PRIV_NAMESPACE
  497. // inheritance is being used for EBO optimization
  498. template <class _Value, class _Tp, class _MaybeReboundAlloc>
  499. class _STLP_alloc_proxy : public _MaybeReboundAlloc {
  500. private:
  501. typedef _MaybeReboundAlloc _Base;
  502. typedef typename _Base::size_type size_type;
  503. typedef _STLP_alloc_proxy<_Value, _Tp, _MaybeReboundAlloc> _Self;
  504. public:
  505. _Value _M_data;
  506. _STLP_alloc_proxy (const _MaybeReboundAlloc& __a, _Value __p) :
  507. _MaybeReboundAlloc(__a), _M_data(__p) {}
  508. _STLP_alloc_proxy (__move_source<_Self> src) :
  509. _MaybeReboundAlloc(_STLP_PRIV _AsMoveSource<_Base>(src.get())),
  510. _M_data(_STLP_PRIV _AsMoveSource<_Value>(src.get()._M_data)) {}
  511. private:
  512. /* Following are helper methods to detect stateless allocators and avoid
  513. * swap in this case. For some compilers (VC6) it is a workaround for a
  514. * compiler bug in the Empty Base class Optimization feature, for others
  515. * it is a small optimization or nothing if no EBO. */
  516. void _M_swap_alloc(_Self&, const __true_type& /*_IsStateless*/)
  517. {}
  518. void _M_swap_alloc(_Self& __x, const __false_type& /*_IsStateless*/) {
  519. _MaybeReboundAlloc &__base_this = *this;
  520. _MaybeReboundAlloc &__base_x = __x;
  521. _STLP_STD::swap(__base_this, __base_x);
  522. }
  523. public:
  524. void _M_swap_alloc(_Self& __x) {
  525. #if !defined (__BORLANDC__)
  526. typedef typename _IsStateless<_MaybeReboundAlloc>::_Ret _StatelessAlloc;
  527. #else
  528. typedef typename __bool2type<_IsStateless<_MaybeReboundAlloc>::_Is>::_Ret _StatelessAlloc;
  529. #endif
  530. _M_swap_alloc(__x, _StatelessAlloc());
  531. }
  532. /* We need to define the following swap implementation for allocator with state
  533. * as those allocators might have implement a special swap function to correctly
  534. * move datas from an instance to the oher, _STLP_alloc_proxy should not break
  535. * this mecanism. */
  536. void swap(_Self& __x) {
  537. _M_swap_alloc(__x);
  538. _STLP_STD::swap(_M_data, __x._M_data);
  539. }
  540. _Tp* allocate(size_type __n, size_type& __allocated_n) {
  541. #if !defined (__BORLANDC__)
  542. typedef typename _IsSTLportClass<_MaybeReboundAlloc>::_Ret _STLportAlloc;
  543. #else
  544. typedef typename __bool2type<_IsSTLportClass<_MaybeReboundAlloc>::_Is>::_Ret _STLportAlloc;
  545. #endif
  546. return allocate(__n, __allocated_n, _STLportAlloc());
  547. }
  548. // Unified interface to perform allocate()/deallocate() with limited
  549. // language support
  550. #if defined (_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE)
  551. // else it is rebound already, and allocate() member is accessible
  552. _Tp* allocate(size_type __n)
  553. { return __stl_alloc_rebind(__STATIC_CAST(_Base&, *this), __STATIC_CAST(_Tp*, 0)).allocate(__n, 0); }
  554. void deallocate(_Tp* __p, size_type __n)
  555. { __stl_alloc_rebind(__STATIC_CAST(_Base&, *this), __STATIC_CAST(_Tp*, 0)).deallocate(__p, __n); }
  556. private:
  557. _Tp* allocate(size_type __n, size_type& __allocated_n, const __true_type& /*STLport allocator*/)
  558. { return __stl_alloc_rebind(__STATIC_CAST(_Base&, *this), __STATIC_CAST(_Tp*, 0)).allocate(__n, __allocated_n); }
  559. #else
  560. //Expose Standard allocate overload (using expression do not work for some compilers (Borland))
  561. _Tp* allocate(size_type __n)
  562. { return _Base::allocate(__n); }
  563. private:
  564. _Tp* allocate(size_type __n, size_type& __allocated_n, const __true_type& /*STLport allocator*/)
  565. { return _Base::allocate(__n, __allocated_n); }
  566. #endif
  567. _Tp* allocate(size_type __n, size_type& __allocated_n, const __false_type& /*STLport allocator*/)
  568. { __allocated_n = __n; return allocate(__n); }
  569. };
  570. #if defined (_STLP_USE_TEMPLATE_EXPORT)
  571. _STLP_EXPORT_TEMPLATE_CLASS _STLP_alloc_proxy<char*, char, allocator<char> >;
  572. # if defined (_STLP_HAS_WCHAR_T)
  573. _STLP_EXPORT_TEMPLATE_CLASS _STLP_alloc_proxy<wchar_t*, wchar_t, allocator<wchar_t> >;
  574. # endif
  575. # if defined (_STLP_USE_PTR_SPECIALIZATIONS)
  576. _STLP_EXPORT_TEMPLATE_CLASS _STLP_alloc_proxy<void**, void*, allocator<void*> >;
  577. # endif
  578. #endif
  579. _STLP_MOVE_TO_STD_NAMESPACE
  580. _STLP_END_NAMESPACE
  581. #if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
  582. # include <stl/_alloc.c>
  583. #endif
  584. #endif /* _STLP_INTERNAL_ALLOC_H */
  585. // Local Variables:
  586. // mode:C++
  587. // End: