sample_allocator 源码

本文介绍了一个自定义的内存分配器实现,该分配器基于C++标准模板库中的allocator概念设计,支持基本的内存分配、释放等功能,并通过模板元编程提供了一定程度的类型安全性。
/*====================================================================
Copyright (C) 2016-2016 Ruler. All rights reserved.
Author:  Ruler
Address: Nan'an District,Chongqing,China
Contact: 26105499@qq.com

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer. 
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in
   the documentation and/or other materials provided with the
   distribution.
3. The name of the author may not be used to endorse or promote
   products derived from this software without specific prior
   written permission.

THIS SOFTWARE IS PROVIDED BY RULER ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
====================================================================*/
#pragma once

#ifndef __CORE_SAMPLE_ALLOCATOR_H__
#define __CORE_SAMPLE_ALLOCATOR_H__

#include <memory>
#include <exception>

namespace core
{
	template <class T> class sample_allocator;

	// Specialize for void
	template <>
	class sample_allocator<void>
	{
	public:
		typedef size_t            size_type;
		typedef ptrdiff_t         difference_type;
		typedef void              value_type;
		typedef void*             pointer;
		typedef const void*       const_pointer;

		template <class U> struct rebind
		{
			typedef sample_allocator<U> other;
		};
	};

	// Template class sample_allocator
	template <class T>
	class sample_allocator
	{
	public:
		typedef size_t            size_type;
		typedef ptrdiff_t         difference_type;
		typedef T                 value_type;
		typedef value_type*       pointer;
		typedef const value_type* const_pointer;
		typedef value_type&       reference;
		typedef const value_type& const_reference;

		template <class U> struct rebind
		{
			typedef sample_allocator<U> other;
		};

		typedef ::std::true_type  propagate_on_container_move_assignment;
		typedef ::std::true_type  is_always_equal;
	public:
		// Construct default sample_allocator
		sample_allocator(void) noexcept
		{
		}
		// Construct by copying
		sample_allocator(const sample_allocator&) noexcept
		{
		}
		// Construct by moving
		sample_allocator(const sample_allocator&&) noexcept
		{
		}
		// Construct from a related sample_allocator
		template<class U>
		sample_allocator(const sample_allocator<U>&) noexcept
		{
		}
		// Return address of mutable x
		pointer address(reference x) const noexcept
		{
			return (::std::addressof(x));
		}
		// Return address of nonmutable x
		const_pointer address(const_reference x) const noexcept
		{
			return (::std::addressof(x));
		}
		// Allocate array of n elements
		pointer allocate(size_type n, typename sample_allocator<void>::const_pointer hint = nullptr)
		{
			return (static_cast<pointer>(malloc(n * sizeof(value_type))));
		}
		// Deallocate object at p
		void deallocate(pointer p, size_type n)
		{
			free(p);
		}
		// Estimate maximum array size
		size_type max_size(void) const noexcept
		{
			return (static_cast<size_t>(-1) / sizeof(value_type));
		}
		// Construct U(Args...) at p
		template<class U, class... Args>
		void construct(U* p, Args&&... args)
		{
			::new ((void*)p) U(::std::forward<Args>(args)...);
		}
		// Destroy object at p
		template <class U>
		void destroy(U* p)
		{
			p->~U();
		}
	};

	template <class T1, class T2>
	inline bool operator==(const sample_allocator<T1>&, const sample_allocator<T2>&)
	{
		return true;
	}
	template <class T1, class T2>
	inline bool operator!=(const sample_allocator<T1>&, const sample_allocator<T2>&)
	{
		return false;
	}

} // namespace core

#endif
`get_allocator`是C++标准库中部分容器的公有成员函数,用于获取与容器关联的allocator对象的副本。以下是不同容器中`get_allocator`的定义及相关信息: ### 定义 - **std::forward_list::get_allocator**:在`<forward_list>`头文件中,其定义为`allocator_type get_allocator() const noexcept;`,该函数用于返回与`std::forward_list`容器关联的allocator对象的副本[^1]。 - **std::list::get_allocator**:在`<list>`头文件中,C++98标准下定义为`allocator_type get_allocator() const;`,C++11及以后定义为`allocator_type get_allocator() const noexcept;`,可返回与`std::list`容器关联的allocator对象的副本[^2]。 - **std::deque::get_allocator**:在`<deque>`头文件中,C++98标准下定义为`allocator_type get_allocator() const;`,C++11及以后定义为`allocator_type get_allocator() const noexcept;`,用于返回与`std::deque`对象关联的allocator对象的副本[^3]。 ### 使用方法 以下是一个简单的使用示例,展示如何使用`std::list`的`get_allocator`函数: ```cpp #include <iostream> #include <list> int main() { std::list<int> myList; auto alloc = myList.get_allocator(); // 这里可以使用alloc进行一些内存分配操作,不过通常不需要手动操作 return 0; } ``` ### 作用 `get_allocator`的主要作用是获取与容器关联的分配器。分配器负责容器内存的分配和释放,通过`get_allocator`可以获取该分配器,从而在需要时手动管理内存分配,或者在自定义内存管理场景中使用。例如,在某些特殊的内存池管理场景下,可以通过获取分配器来实现更高效的内存使用,减少内存碎片化等问题[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值