📄 memory
字号:
// -*- C++ -*-
/***************************************************************************
*
* memory - declarations for the Standard Library memory implementation
*
* $Id: memory,v 1.3 2003/03/31 08:44:24 wmunns Exp $
*
***************************************************************************
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Hewlett-Packard Company makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
***************************************************************************
*
* Copyright (c) 1994-2001 Rogue Wave Software, Inc. All Rights Reserved.
*
* This computer software is owned by Rogue Wave Software, Inc. and is
* protected by U.S. copyright laws and other laws and by international
* treaties. This computer software is furnished by Rogue Wave Software,
* Inc. pursuant to a written license agreement and may be used, copied,
* transmitted, and stored only in accordance with the terms of such
* license and with the inclusion of the above copyright notice. This
* computer software or any other copies thereof may not be provided or
* otherwise made available to any other person.
*
* U.S. Government Restricted Rights. This computer software is provided
* with Restricted Rights. Use, duplication, or disclosure by the
* Government is subject to restrictions as set forth in subparagraph (c)
* (1) (ii) of The Rights in Technical Data and Computer Software clause
* at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the
* Commercial Computer Software--Restricted Rights at 48 CFR 52.227-19,
* as applicable. Manufacturer is Rogue Wave Software, Inc., 5500
* Flatiron Parkway, Boulder, Colorado 80301 USA.
*
**************************************************************************/
#ifndef _RWSTD_MEMORY_INCLUDED
#define _RWSTD_MEMORY_INCLUDED
#include <new>
#include <utility>
#include <rw/_iterbase.h>
#include <rw/_mutex.h>
#include <rw/_defs.h>
_RWSTD_NAMESPACE_BEGIN (__rw)
// [de]allocate storage (in bytes)
_RWSTD_EXPORT void* __rw_allocate (_RWSTD_C::size_t, int = 0);
_RWSTD_EXPORT void __rw_deallocate (void*, _RWSTD_C::size_t, int = 0);
// this function returns a suggested new capacity for a container needing
// more room; see stddefs.h for an explanation of these macro parameters;
// the _Container template parameter allows more specialized overloads
// for customization
template <class _Container>
inline _RWSTD_C::size_t __rw_new_capacity (_RWSTD_C::size_t __size, const _Container*)
{
_RWSTD_C::size_t __cap = _RWSTD_STATIC_CAST (_RWSTD_C::size_t,
_RWSTD_INCREASE_CAPACITY(__size)
/*__size * _RWSTD_NEW_CAPACITY_RATIO*/);
return (__size += _RWSTD_MINIMUM_NEW_CAPACITY) > __cap ? __size : __cap;
}
template <class _TypeT>
inline void __rw_destroy (_TypeT &__ref)
{
__ref.~_TypeT ();
}
template <class _TypeT, class _TypeU>
inline void __rw_construct (_TypeT* __p, const _TypeU& __val)
{
new (__p) _TypeT (__val);
}
template <class _ForwardIterator>
void __rw_destroy (_ForwardIterator __first, _ForwardIterator __last)
{
for (; __first != __last; ++__first)
__rw_destroy (*__first);
}
#ifndef _RWSTD_NO_PTR_VALUE_TEMPLATE_OVERLOAD
// for compilers that don't optimize "empty" loops
template <class _TypeT>
inline void __rw_destroy (_TypeT**, _TypeT**)
{ }
#endif // _RWSTD_NO_PTR_VALUE_TEMPLATE_OVERLOAD
_RWSTD_NAMESPACE_END // __rw
_RWSTD_NAMESPACE_BEGIN (std)
template <class _TypeT> class
allocator;
_RWSTD_SPECIALIZED_CLASS
class allocator<void>
{
public:
typedef void* pointer;
typedef const void* const_pointer;
typedef void value_type;
#ifdef _RWSTD_ALLOCATOR
template <class _TypeU>
struct rebind {
typedef allocator<_TypeU> other;
};
#endif /* _RWSTD_ALLOCATOR */
};
template <class _TypeT>
class allocator
{
public:
typedef _RWSTD_C::size_t size_type;
typedef ptrdiff_t difference_type;
typedef _TypeT value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
allocator () _THROWS (()) { }
allocator (const allocator &__rhs) _THROWS (()) {
// working around an HP aCC warning 431
_RWSTD_UNUSED (__rhs);
}
#ifdef _RWSTD_ALLOCATOR
template <class _TypeU>
struct rebind {
typedef allocator<_TypeU> other;
};
template <class _TypeU>
allocator (const allocator<_TypeU>&) _THROWS (()) { }
template <class _TypeU>
allocator&
operator= (const allocator<_TypeU>&) _THROWS (()) {
return *this;
}
#endif // _RWSTD_ALLOCATOR
pointer address (reference __x) const {
return &__x;
}
const_pointer address (const_reference __x) const {
return &__x;
}
pointer allocate (size_type __n, allocator<void>::const_pointer = 0) {
#ifdef _RWSTD_ALLOCATOR
return _RWSTD_STATIC_CAST (pointer,
_RW::__rw_allocate (__n * sizeof (value_type)));
#else
return _RWSTD_STATIC_CAST (pointer, _RW::__rw_allocate (__n));
#endif // _RWSTD_ALLOCATOR
}
#ifdef _RWSTD_ALLOCATOR
void deallocate (pointer __p, size_type __n)
#else
void deallocate (void* __p, size_type __n)
#endif // _RWSTD_ALLOCATOR
{
_RW::__rw_deallocate (__p, __n);
}
// 20.4.1.1, p11 - the largest N for which allocate (N) might succeed
size_type max_size () const _THROWS (()) {
return size_type (~0) / sizeof (value_type) ?
size_type (size_type (~0) / sizeof (value_type)) : size_type (1);
}
void construct (pointer __p, const_reference __val) {
_RW::__rw_construct (__p, __val);
}
void destroy (pointer __p) {
_RWSTD_ASSERT (0 != __p);
_RW::__rw_destroy (*__p);
}
};
#if !defined (_RWSTD_NO_CLASS_PARTIAL_SPEC) \
&& !defined (_RWSTD_NO_EXT_CONST_ALLOCATOR)
// extension: allocates/constructs/destroys const elements
template <class _TypeT>
class allocator<const _TypeT>
{
public:
typedef _RWSTD_C::size_t size_type;
typedef ptrdiff_t difference_type;
typedef const _TypeT value_type;
typedef const value_type* pointer;
typedef const value_type* const_pointer;
typedef const value_type& reference;
typedef const value_type& const_reference;
allocator () _THROWS (()) { }
allocator (const allocator &__rhs) _THROWS (()) {
// working around an HP aCC warning 431
_RWSTD_UNUSED (__rhs);
}
#ifdef _RWSTD_ALLOCATOR
template <class _TypeU>
struct rebind {
typedef allocator<_TypeU> other;
};
template <class _TypeU>
allocator (const allocator<_TypeU>&) _THROWS (()) { }
template <class _TypeU>
allocator& operator= (const allocator<_TypeU>&) _THROWS (()) {
return *this;
}
#endif // _RWSTD_ALLOCATOR
const_pointer address (const_reference __x) const {
return &__x;
}
const_pointer
allocate (size_type __n, allocator<void>::const_pointer = 0) {
#ifdef _RWSTD_ALLOCATOR
return _RWSTD_STATIC_CAST (const_pointer,
_RW::__rw_allocate (__n * sizeof (value_type)));
#else
return _RWSTD_STATIC_CAST (const_pointer, _RW::__rw_allocate (__n));
#endif // _RWSTD_ALLOCATOR
}
#ifdef _RWSTD_ALLOCATOR
void deallocate (const_pointer __p, size_type __n /* elements */)
#else
void deallocate (const void* __p, size_type __n /* bytes */)
#endif // _RWSTD_ALLOCATOR
{
_RW::__rw_deallocate (_RWSTD_CONST_CAST (_TypeT*, __p), __n);
}
// 20.4.1.1, p11 - the largest N for which allocate (N) might succeed
size_type max_size () const _THROWS (()) {
return ~size_type (0) / sizeof (value_type) ?
size_type (size_type (~0) / sizeof (value_type)) : size_type (1);
}
void construct (const_pointer __p, const_reference __val) {
_RW::__rw_construct (_RWSTD_CONST_CAST (_TypeT*, __p), __val);
}
void destroy (const_pointer __p) {
_RWSTD_ASSERT (0 != __p);
_RW::__rw_destroy (_RWSTD_CONST_CAST (_TypeT&, *__p));
}
};
#endif // !_RWSTD_NO_CLASS_PARTIAL_SPEC && !_RWSTD_NO_EXT_CONST_ALLOCATOR)
// allocator_interface provides all types and typed functions. Memory
// allocated as raw bytes using the class provided by the Allocator
// template parameter. allocator_interface casts appropriately.
//
// Multiple allocator_interface objects can attach to a single
// allocator, thus allowing one allocator to allocate all storage
// for a container, regardless of how many types are involved.
//
// The only real restriction is that pointer and reference are
// hard coded as _TypeT* and _TypeT&. Partial specialization would
// get around this.
//
#ifndef _RWSTD_ALLOCATOR
template <class _Allocator, class _TypeT>
class allocator_interface
{
public:
typedef _Allocator allocator_type;
typedef _TypeT value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef _TYPENAME allocator_type::size_type size_type;
typedef _TYPENAME allocator_type::difference_type difference_type;
protected:
allocator_type _C_alloc;
public:
allocator_interface() _THROWS (()) { }
allocator_interface (const allocator_type &__alloc) _THROWS (())
: _C_alloc (__alloc) { }
operator allocator_type& () {
return _C_alloc;
}
pointer address (reference __x) {
return &__x;
}
size_type max_size () const {
return _C_alloc.max_size () / sizeof (value_type);
}
pointer allocate (size_type __n, const void* __p = 0) {
//using c-style cast to perform reinterpret-cast & const-cast in 1 step
return (pointer)_C_alloc.allocate (__n * sizeof (value_type),
(pointer) __p );
}
void deallocate (pointer __p, size_type __n) {
_C_alloc.deallocate (__p, __n);
}
void construct (pointer __p, const_reference __val) const {
_RW::__rw_construct(__p, __val);
}
void destroy (pointer __p) const {
_RWSTD_ASSERT (0 != __p);
_RW::__rw_destroy (*__p);
}
};
_RWSTD_SPECIALIZED_CLASS
class allocator_interface<allocator<void>, void>
{
public:
typedef allocator<void> allocator_type;
typedef void* pointer;
typedef const void* const_pointer;
typedef void value_type;
protected:
allocator_type _C_alloc;
public:
allocator_interface () _THROWS (()) { }
allocator_interface (const allocator<void>& __rhs) _THROWS (())
: _C_alloc (__rhs) { }
};
template <class _TypeT, class _TypeU, class _TypeV, class _TypeW>
inline bool
operator== (const allocator_interface<_TypeT, _TypeU>&,
const allocator_interface<_TypeV, _TypeW>&) _THROWS (())
{
return true;
}
#endif // _RWSTD_ALLOCATOR
template <class _TypeT, class _TypeU>
inline bool
operator== (const allocator<_TypeT>&, const allocator<_TypeU>&) _THROWS (())
{
return true;
}
#ifndef _RWSTD_NO_NAMESPACE
template <class _TypeT, class _TypeU>
inline bool
operator!= (const allocator<_TypeT>& __x,
const allocator<_TypeU>& __y) _THROWS (())
{
return !(__x == __y);
}
#endif // _RWSTD_NO_NAMESPACE
// 20.4.2
template <class _OutputIterator, class _TypeT>
class raw_storage_iterator
: public iterator<output_iterator_tag, void, void, void, void>
{
_OutputIterator _C_iter;
public:
// for completeness and genericity
typedef _OutputIterator iterator_type;
// 20.4.2, p2
_EXPLICIT raw_storage_iterator (iterator_type __x) : _C_iter (__x) { }
// 20.4.2, p3
raw_storage_iterator& operator* () {
return *this;
}
// 20.4.2, p4
raw_storage_iterator& operator= (const _TypeT& __rhs) {
::new (&(*_C_iter)) _TypeT (__rhs);
return *this;
}
// 20.4.2, p6
raw_storage_iterator& operator++ () {
++_C_iter;
return *this;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -