⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ycpp_memory.hpp

📁 一个类STL的多平台可移植的算法容器库,主要用于嵌入式系统编程时的内存管理等方面
💻 HPP
字号:
/*
 * The young Library
 * Copyright (c) 2005 by Yang Huan(杨桓)

 * Permission to use, copy, modify, distribute and sell this software 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.
 * The author make no representations about the suitability of this software
 * for any purpose. It is provided "as is" without express or implied warranty.
 */

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#ifndef __MACRO_CPLUSPLUS_YOUNG_LIBRARY_MEMORY_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_MEMORY_HEADER_FILE__
//------------------------------------------------------------------------------
#include <new>
#include <cstring>
#include "../youngc/yc_memory.h"
#include "ycpp_traits.hpp"
//------------------------------------------------------------------------------
namespace youngcpp {
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

template< typename T1, typename T2 >
inline void ylib_construct( T1* ptr, const T2& value )
{
    typedef  typename property_traits<T1>::is_POD  POD;
    ylib_construct_aux( ptr, value, POD() );
}

template< typename T1, typename T2 >
inline void ylib_construct_aux( T1* ptr, const T2& value, false_type )
{
    new(ptr) T1(value);
}

template< typename T1, typename T2 >
inline void ylib_construct_aux( T1* ptr, const T2& value, true_type )
{
    *ptr = value;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

template< typename T >
inline void ylib_destroy( T* ptr )
{
    typedef  typename property_traits<T>::has_trivial_destructor  destructor;
    ylib_destroy_aux( ptr, destructor() );
}

template< typename T >
inline void ylib_destroy_aux( T* ptr, true_type )  {}

template< typename T >
inline void ylib_destroy_aux( T* ptr, false_type )  {  ptr->~T();  }

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

template< typename Alloc >
inline Alloc& alloc_obj()
{
    static Alloc byte_alloc;
    return byte_alloc;
}

template< typename Alloc >
void* alloc_adapter( size_t size )
{
    using youngc::ylib_byte_t;
    typedef  typename Alloc::template rebind<ylib_byte_t>::other  alloc_t;

    void* ptr;
    try  {  ptr = alloc_obj<alloc_t>().allocate( size );  }
    catch(...)  {  ptr = 0;  }
    return ptr;
}

template< typename Alloc >
void dealloc_adapter( void* ptr, size_t size )
{
    using youngc::ylib_byte_t;
    typedef  typename Alloc::template rebind<ylib_byte_t>::other  alloc_t;

    alloc_obj<alloc_t>().deallocate( static_cast<ylib_byte_t*>(ptr), size );
}

template< typename T >
void init_adapter( void* dst )
{
    try
    {
        new(dst) T();
    }
    catch( ... )
    {
        std::memset( dst, 0, sizeof(T) );
    }
}

template< typename T >
void destroy_adapter( void* dst )
{
    ((T*)dst)->~T();
}

template< typename T >
int init_copy_adapter( void* dst, const void* src )
{
    try
    {
        new(dst) T( *((T*)src) );
    }
    catch( ... )
    {
        init_adapter<T>( dst );
        return 0;
    }
    return 1;
}

template< typename T >
int assign_copy_adapter( void* dst, const void* src )
{
    try
    {
        *( (T*)dst ) = *( (const T*)src );
    }
    catch( ... )
    {
        return 0;
    }
    return 1;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

template< typename T >
class pool_allocator
{
public:
    typedef  T                  value_type;
    typedef  value_type&        reference;
    typedef  const value_type&  const_reference;
    typedef  value_type*        pointer;
    typedef  const value_type*  const_pointer;
    typedef  value_type*        iterator;
    typedef  const value_type*  const_iterator;
    typedef  size_t             size_type;
    typedef  ptrdiff_t          difference_type;
    typedef  pool_allocator<T>  self;

    template< typename U >
    struct rebind
    {
        typedef  pool_allocator<U>  other;
    };

    template< typename U >
    pool_allocator( const pool_allocator<U>& rhs )  {}

    template< typename U >
    self& operator=( const pool_allocator<U>& rhs )  {  return *this;  }

    pool_allocator()  {}

    pool_allocator( const self& rhs )  {}

    self& operator=( const self& rhs )  {  return *this;  }

    ~pool_allocator()  {  youngc::pool_free( NULL, sizeof(T) );  }

    pointer address( reference X )  {  return &X;  }

    const_pointer address( const_reference X )  {  return &X;  }

    size_type max_size()
    {
        return size_type( SIZE_MAX / sizeof(T) );
    }

    pointer allocate( size_type n )
    {
        return (pointer)( youngc::pool_alloc( sizeof(T) * n ) );
    }

    void deallocate( pointer ptr, size_type n )
    {
        youngc::pool_dealloc( ptr, sizeof(T) * n );
    }

    void construct( pointer ptr, const T& value )
    {
        ylib_construct( ptr, value );
    }

    void destroy( pointer ptr )
    {
        ylib_destroy( ptr );
    }
};

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
}  //end namespace
#endif
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -