VS 2010 STL 内存分配器

本文深入探讨了C++中内存管理的关键概念,包括分配(Alloc)、构造(Construct)、析构(Destroy)和释放(DeAlloc)。通过分析内联模板函数的具体实现,如_Allocate、_Construct、_Destroy和分配器类_ALLOCATOR,揭示了这些操作背后的机制。特别关注了分配器如何替代new和delete操作符,以及其在不同类型的对象构造和销毁中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Alloc是分配内存之意,Construct是在内存里构造之意。就好C++里的new有分配内存,在分配的内存里构造我们的对象这两层作用。 其实这也就是分配器的作用。替换new与delete。

// TEMPLATE FUNCTION _Allocate
template<class _Ty> inline
	_Ty _FARQ *_Allocate(_SIZT _Count, _Ty _FARQ *)
	{	// allocate storage for _Count elements of type _Ty
	void *_Ptr = 0;

	if (_Count <= 0)
		_Count = 0;
	else if (((_SIZT)(-1) / sizeof (_Ty) < _Count)
		|| (_Ptr = ::operator new(_Count * sizeof (_Ty))) == 0)
		_THROW_NCEE(bad_alloc, 0);

	return ((_Ty _FARQ *)_Ptr);
	}
		// TEMPLATE FUNCTION _Construct
template<class _Ty1,
	class _Ty2> inline
	void _Construct(_Ty1 _FARQ *_Ptr, _Ty2&& _Val)
	{	// construct object at _Ptr with value _Val
	void _FARQ *_Vptr = _Ptr;
    //placement new
	::new (_Vptr) _Ty1(_STD forward<_Ty2>(_Val));
	}

template<class _Ty1> inline
	void _Construct(_Ty1 _FARQ *_Ptr)
	{	// construct object at _Ptr with default value
	void _FARQ *_Vptr = _Ptr;
    //placement new
	::new (_Vptr) _Ty1();
	}

Desotry是析构之意,可不是删除内存啊。删除内存对应的是DeAlloc。 

// TEMPLATE FUNCTION _Destroy
template<class _Ty> inline
	void _Destroy(_Ty _FARQ *_Ptr)
	{	// destroy object at _Ptr
	_Ptr->~_Ty();
	}

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

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

 #ifdef _NATIVE_WCHAR_T_DEFINED
//特化
template<> inline
	void _Destroy(unsigned short _FARQ *)
	{	// destroy a unsigned short (do nothing)
	}
 #endif /* _NATIVE_WCHAR_T_DEFINED */

 反正牛逼的代码总喜欢搞一个基类,搞基就对了。

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

//const _Ty为啥要单独分出来呢,欲意何为
		// TEMPLATE CLASS _Allocator_base<const _Ty>
template<class _Ty>
	struct _Allocator_base<const _Ty>
	{	// base class for generic allocators for const _Ty
	typedef _Ty value_type;
	};

正题来了: 

		// TEMPLATE CLASS _ALLOCATOR
template<class _Ty>
	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;

    //这个rebind真的是非常之重要。它是什么个意思呢。比如本来_ALLOCATOR<int>,那么这个分配器以    
    //int型来分配内存,结果现在
    //typename _ALLOCATOR<int>::template rebind<_Container_proxy>::other Al;
    //那么Al就是以_Container_proxy型来分配内存的了。
	template<class _Other>
		struct rebind
		{	// convert this type to _ALLOCATOR<_Other>
		typedef _ALLOCATOR<_Other> other;
		};

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

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

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

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

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

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

    //释放内存,这里调用的是::operator delete,delete的只释放内存的那部分功能
	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 construct(pointer _Ptr, _Ty&& _Val)
		{	// construct object at _Ptr with value _Val
		::new ((void _FARQ *)_Ptr) _Ty(_STD forward<_Ty>(_Val));
		}

	template<class _Other>
		void construct(pointer _Ptr, _Other&& _Val)
		{	// construct object at _Ptr with value _Val
		::new ((void _FARQ *)_Ptr) _Ty(_STD forward<_Other>(_Val));
		}

    //析构对象
	void destroy(pointer _Ptr)
		{	// destroy object at _Ptr
		_Destroy(_Ptr);
		}

    //(_SIZT)(-1)其实就是那个最大的unsigned int
	_SIZT max_size() const _THROW0()
		{	// estimate maximum array size
		_SIZT _Count = (_SIZT)(-1) / sizeof (_Ty);
		return (0 < _Count ? _Count : 1);
		}
	};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值