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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值