支持HW团队,就支付宝领取下面的红包吧!(2018年3月31前,就几毛,也会几块,可以和其他红包叠加使用),你领取消费,HW有奖励。红包使用无条件限制,有条件请注意是不是有病毒。

小伙伴们,给大家发红包喽!人人可领,领完就能用。祝大家领取的红包金额大大大!#吱口令#长按复制此消息,打开支付宝就能领取!er1OEj73Uj

登入 注册 | 验证
| 搜索
HelloWorld论坛 : > 计算机科学、技术、教学> 编程专题> 开源免费项目> [原创]ACE内存分配——定长数据分配(二)
 
 
 
 
类别:ACE 阅读:3056 评论:0 时间:五月 17, 2013, 4:04 p.m. 关键字:ACE 内存

 

 这篇是另一种实现方法。刚开始,我想是不是可以继承ACE_Cached_Allocator,但是他的成员free_list_是private权限,基类无法动手脚。没办法,拷贝一份,改写。下面的实现每次申请的小内存,但是没有数量限制,直到系统内存耗尽。我认为可以同时作为ACE_Data_Block分配,ACE_Message_Block分配器。只要T参数用内存占用大的类。碰到的困难就是ACE_Locked_Free_List里面存放的类型不包括节点数据,及要实现了set_next和get_next,看源代码,发现ACE_Cached_Mem_Pool巧妙的实现了这一点。但是 我们这里free_list_要new这个对象,显然内存大小是不满足T的大小,所以。我做了改动,在前面加了一个公用体,使内存能满足T的大小。这里有一点要注意,申请和归还内存,都没有调用T的构造和析构,也许你申请来是给内存更小的B用,所以自己要显示调用构造和析构。ACE定义了相应的宏,可以去参考学习一下
申请:ACE_NEW_MALLOC_RETURN ACE_NEW_MALLOC ACE_NEW_MALLOC_NORETURN
归还:ACE_DES_FREE ACE_DES_ARRAY_FREE

/***************************************************************
version:  1.0
date: 16/5/2013   17:04
FileName:     BlockAllocator.h
Author:       丁灵峰
Compiled on:   VC.net 2008
-------------------------------------------------------------
Description:
Modification history:
Other:

-------------------------------------------------------------
Copyright (C) 2010 - All Rights Reserved
***************************************************************

****************************************************************/
#if (defined HW_ACE) && (defined HW_ACE_ALLOCATOR)  //使用自定义内存管理
#ifndef _H_hwnet_block_allocator_
#define _H_hwnet_block_allocator_

#include "ace/Malloc.h"
#include <ace/Malloc_T.h>

namespace hwnet
{
        template <class T>
        class HwCachedMemPoolNode
        {
        public:
                /// Get the next HwCachedMemPoolNode in a list.
                HwCachedMemPoolNode<T> *get_next (void)
                {
                        return next_;
                }

                /// Set the next HwCachedMemPoolNode.
                void set_next (HwCachedMemPoolNode<T> *ptr)
                {
                        next_ = ptr;
                }

        private:
                union
                {
                        char m_bDate[sizeof(T)];
                        /**
                        * Since memory is not used when placed in a free list,
                        * we can use it to maintain the structure of free list.
                        * I was using union to hide the fact of overlapping memory
                        * usage.  However, that cause problem on MSVC.  So, I now turn
                        * back to hack this with casting.
                        */
                        HwCachedMemPoolNode<T> *next_;
                };
        };
        template <class T, class ACE_LOCK>
        class HwCachedAllocator : public ACE_New_Allocator
        {
        public:
                /// Create a cached memory pool with @a n_chunks chunks
                /// each with sizeof (TYPE) size.
                HwCachedAllocator (size_t prealloc = 1024
                        , size_t lwm = 0
                        , size_t hwm = 1024*1024*1024
                        , size_t inc = 128)
                        : free_list_ (ACE_FREE_LIST_WITH_POOL, prealloc, lwm, hwm, inc)
                {
                }
                /// Clear things up.
                ~HwCachedAllocator (void)
                {
                }

                /**
                * Get a chunk of memory from free list cache.  Note that @a nbytes is
                * only checked to make sure that it's less or equal to sizeof T, and is
                * otherwise ignored since @c malloc() always returns a pointer to an
                * item of sizeof (T).
                */
                void *malloc (size_t nbytes = sizeof (T))
                {
                        // Check if size requested fits within pre-determined size.
                        if (nbytes > sizeof (T))
                                return 0;

                        // addr() call is really not absolutely necessary because of the way
                        // ACE_Cached_Mem_Pool_Node's internal structure arranged.
                        return this->free_list_.remove ();
                }

                /**
                * Get a chunk of memory from free list cache, giving them
                * @a initial_value.  Note that @a nbytes is only checked to make sure
                * that it's less or equal to sizeof T, and is otherwise ignored since
                * calloc() always returns a pointer to an item of sizeof (T).
                */
                virtual void *calloc (size_t nbytes,
                        char initial_value = '\0')
                {
                        // Check if size requested fits within pre-determined size.
                        if (nbytes > sizeof (T))
                                return 0;

                        // addr() call is really not absolutely necessary because of the way
                        // ACE_Cached_Mem_Pool_Node's internal structure arranged.
                        void *ptr = this->free_list_.remove ();
                        if (ptr != 0)
                                ACE_OS::memset (ptr, initial_value, sizeof (T));
                        return ptr;
                }

                /// This method is a no-op and just returns 0 since the free list
                /// only works with fixed sized entities.
                virtual void *calloc (size_t n_elem,
                        size_t elem_size,
                        char initial_value = '\0')
                {
                        return calloc (n_elem * elem_size, initial_value);
                }

                /// Return a chunk of memory back to free list cache.
                void free (void * ptr)
                {
                        if (ptr != 0)
                                this->free_list_.add ((HwCachedMemPoolNode<T>*)ptr) ;
                }
                /// Return the number of chunks available in the cache.
                size_t pool_depth (void)
                {
                        return this->free_list_.size ();
                }
                // 提供这个方法,让外部空闲的时候,释放多余空间
                // Resizes the free list to <newsize>
                void resize (size_t newsize)
                {
                        return this->free_list_.resize (newsize);
                }

        private:
                /// Maintain a cached memory free list.
                ACE_Locked_Free_List<HwCachedMemPoolNode<T>, ACE_LOCK> free_list_;
        };
}
#endif // #ifndef _H_hwnet_block_allocator_
#endif // HW_ACE HW_ACE_ALLOCATOR  //使用自定义内存管理

 

[挂载人]初学MPEG [审核人]初学MPEG 推荐

个人签名--------------------------------------------------------------------------------

Please Login (or Sign Up) to leave a comment