memory std::allocator源代码及注释.

//D:/Program Files/Microsoft Visual Studio 8/VC/include/xmemory

// xmemory internal header (from )
#pragma once
#ifndef _XMEMORY_
#define _XMEMORY_
#ifndef RC_INVOKED
#include
#include
#include
#include

#ifdef _MSC_VER
#pragma pack(push,_CRT_PACKING)
#pragma warning(push,3)

#pragma warning(disable: 4100)
#endif /* _MSC_VER */

#ifndef _FARQ /* specify standard memory model */
#define _FARQ
#define _PDFT ptrdiff_t
#define _SIZT size_t
#endif /* _FARQ */

#define _CPOINTER_X(T, A) /
typename A::template rebind::other::const_pointer
#define _CREFERENCE_X(T, A) /
typename A::template rebind::other::const_reference
#define _POINTER_X(T, A) /
typename A::template rebind::other::pointer
#define _REFERENCE_X(T, A) /
typename A::template rebind::other::reference

_STD_BEGIN
// TEMPLATE FUNCTION _Allocate
template inline
_Ty _FARQ *_Allocate(_SIZT _Count, _Ty _FARQ *)
{ // check for integer overflow
if (_Count <= 0)
_Count = 0;
else if (((_SIZT)(-1) / _Count) < sizeof (_Ty))
_THROW_NCEE(std::bad_alloc, NULL);

// allocate storage for _Count elements of type _Ty
return ((_Ty _FARQ *)::operator new(_Count * sizeof (_Ty)));
}

// TEMPLATE FUNCTION _Construct
templateclass _T2> inline
void _Construct(_T1 _FARQ *_Ptr, const _T2& _Val)
{ // construct object at _Ptr with value _Val
void _FARQ *_Vptr = _Ptr;
::new (_Vptr) _T1(_Val);// [::] new [placement] new-type-name [new-initializer]
}

// TEMPLATE FUNCTION _Destroy
template inline
void _Destroy(_Ty _FARQ *_Ptr)
{ // destroy object at _Ptr
_DESTRUCTOR(_Ty, _Ptr);
}

//特化??

template<> inline
void _Destroy(char _FARQ *)
{ // destroy a char (do nothing)
}

template<> inline
void _Destroy(wchar_t _FARQ *)
{ // destroy a wchar_t (do nothing)
}

// TEMPLATE CLASS _Allocator_base
template
struct _Allocator_base
{ // base class for generic allocators
typedef _Ty value_type;
};

// TEMPLATE CLASS _Allocator_base
template
struct _Allocator_base
{ // base class for generic allocators for const _Ty
typedef _Ty value_type;
};

// TEMPLATE CLASS allocator
template
class allocator
: public _Allocator_base<_Ty>
{ // generic allocator for objects of class _Ty
public:
typedef _Allocator_base<_Ty> _Mybase;
typedef typename _Mybase::value_type value_type;
typedef value_type _FARQ *pointer;
typedef value_type _FARQ& reference;
typedef const value_type _FARQ *const_pointer;
typedef const value_type _FARQ& const_reference;

typedef _SIZT size_type;
typedef _PDFT difference_type;

template
struct rebind
{ // convert an allocator<_Ty> to an allocator <_Other>
typedef allocator<_Other> other;
};

pointer address(reference _Val) const
{ // return address of mutable _Val
return (&_Val);
}

const_pointer address(const_reference _Val) const
{ // return address of nonmutable _Val
return (&_Val);
}

allocator() _THROW0()
{ // construct default allocator (do nothing)
}

allocator(const allocator<_Ty>&) _THROW0()
{ // construct by copying (do nothing)
}

template
allocator(const allocator<_Other>&) _THROW0()
{ // construct from a related allocator (do nothing)
}

template
allocator<_Ty>& operator=(const allocator<_Other>&)
{ // assign from a related allocator (do nothing)
return (*this);
}

void deallocate(pointer _Ptr, size_type)
{ // deallocate object at _Ptr, ignore size
::operator delete(_Ptr);
}

pointer allocate(size_type _Count)
{ // allocate array of _Count elements
return (_Allocate(_Count, (pointer)0));
}

pointer allocate(size_type _Count, const void _FARQ *)
{ // allocate array of _Count elements, ignore hint
return (allocate(_Count));
}

void construct(pointer _Ptr, const _Ty& _Val)
{ // construct object at _Ptr with value _Val
_Construct(_Ptr, _Val);
}

void destroy(pointer _Ptr)
{ // destroy object at _Ptr
_Destroy(_Ptr);
}

_SIZT max_size() const _THROW0()
{ // estimate maximum array size
_SIZT _Count = (_SIZT)(-1) / sizeof (_Ty);
return (0 < _Count ? _Count : 1);
}
};

// allocator TEMPLATE OPERATORS
templateclass _Other> inline
bool operator==(const allocator<_Ty>&, const allocator<_Other>&) _THROW0()
{ // test for allocator equality (always true)
return (true);
}

templateclass _Other> inline
bool operator!=(const allocator<_Ty>&, const allocator<_Other>&) _THROW0()
{ // test for allocator inequality (always false)
return (false);
}

// CLASS allocator
template<> class _CRTIMP2_PURE allocator
{ // generic allocator for type void
public:
typedef void _Ty;
typedef _Ty _FARQ *pointer;
typedef const _Ty _FARQ *const_pointer;
typedef _Ty value_type;

template
struct rebind
{ // convert an allocator to an allocator <_Other>
typedef allocator<_Other> other;
};

allocator() _THROW0()
{ // construct default allocator (do nothing)
}

allocator(const allocator<_Ty>&) _THROW0()
{ // construct by copying (do nothing)
}

template
allocator(const allocator<_Other>&) _THROW0()
{ // construct from related allocator (do nothing)
}

template
allocator<_Ty>& operator=(const allocator<_Other>&)
{ // assign from a related allocator (do nothing)
return (*this);
}
};

// TEMPLATE FUNCTION _Destroy_range
templateclass _Alloc> inline
void _Destroy_range(_Ty *_First, _Ty *_Last, _Alloc& _Al)
{ // destroy [_First, _Last)
_Destroy_range(_First, _Last, _Al, _Ptr_cat(_First, _Last));
}

templateclass _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);
}

templateclass _Alloc> inline
void _Destroy_range(_Ty *_First, _Ty *_Last, _Alloc& _Al,
_Scalar_ptr_iterator_tag)
{ // destroy [_First, _Last), scalar type (do nothing)
}
_STD_END

#ifdef _MSC_VER
#pragma warning(default: 4100)

#pragma warning(pop)
#pragma pack(pop)
#endif /* _MSC_VER */

#endif /* RC_INVOKED */
#endif /* _XMEMORY_ */

/*
* Copyright (c) 1992-2005 by P.J. Plauger. ALL RIGHTS RESERVED.
* Consult your license regarding permissions and restrictions.
*/

/*
* This file is derived from software bearing the following
* restrictions:
*
* 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.
V4.05:0009 */

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值