支持HW团队,就支付宝领取下面的红包吧! (打开支付宝就能领取!er1OEj73Uj), (打开支付宝收索“516503473”), 你领取消费,HW有奖励。红包使用无条件限制,有条件请注意是不是有病毒。

Login or Sign up | Validate
| Search
HelloWorld论坛 : > 计算机科学、技术、教学> 编程专题> 开源免费项目> [原创]ACE内存分配——new delete重载
 
 
 
 
类别:ACE 阅读:5497 评论:0 时间:June 4, 2013, 5:45 p.m. 关键字:ACE delete new 内存

 

全局的new有六种重载形式,其中placement版本不能重载

void *operator new(std::size_t count)throw(std::bad_alloc); // 一般的版本
void *operator new(std::size_t count,const std::nothrow_t&) throw(); // 兼容早版本的new 内存分配失败不会抛出异常
void *operator new(std::size_t count, void *ptr) throw(); // placement版本


void *operator new[](std::size_t count) throw(std::bad_alloc); // 一般的版本
void *operator new[](std::size_t count,const std::nothrow_t&) throw(); // 兼容早版本的new 内存分配失败不会抛出异常
void *operator new[](std::size_t count, void *ptr) throw(); // placement版本

 

#ifdef HW_ACE_ALLOCATOR //使用自定义内存管理
hwnet::ManageAllocator<ACE_SYNCH_RECURSIVE_MUTEX>* gManageBlockAllocator;
// 一般的版本(plain new)
inline void * operator new(size_t size) throw(std::bad_alloc)
{
        if (gManageBlockAllocator == false)
        {
                gManageBlockAllocator = hwnet::ManageAllocator<ACE_SYNCH_RECURSIVE_MUTEX>::CreateInstance(1024*16, 512, 512);
                gManageBlockAllocator->Create(1024*16, sizeof(hwnet::Block));
                gManageBlockAllocator->Create(1024*16, 512);
                ACE_Allocator::instance (dynamic_cast<ACE_Allocator*>(gManageBlockAllocator));
        }
        void* pVoid = gManageBlockAllocator->malloc (size);
        if (pVoid)
        {
                return pVoid;
        }
        pVoid = gManageBlockAllocator->malloc (size);
        static const std::bad_alloc nomem;
        throw nomem;
}
//兼容早版本, new内存分配失败不会抛出异常(nothrow new)
void *operator new(std::size_t size,  const std::nothrow_t& ) throw()
{
        if (gManageBlockAllocator == false)
        {
                gManageBlockAllocator = hwnet::ManageAllocator<ACE_SYNCH_RECURSIVE_MUTEX>::CreateInstance(1024*16, 512, 512);
                gManageBlockAllocator->Create(1024*16, sizeof(hwnet::Block));
                gManageBlockAllocator->Create(1024*16, 512);
                ACE_Allocator::instance (dynamic_cast<ACE_Allocator*>(gManageBlockAllocator));
        }
        return gManageBlockAllocator->malloc (size);
}
////placement版本(placement new)
//void *operator new(std::size_t count, void *ptr) throw()
//{
//      return ;
//}
inline void operator delete(void *p)
{
        return gManageBlockAllocator->free (p);
}

void *operator new[](std::size_t size) throw(std::bad_alloc)
{
        if (gManageBlockAllocator == false)
        {
                gManageBlockAllocator = hwnet::ManageAllocator<ACE_SYNCH_RECURSIVE_MUTEX>::CreateInstance(1024*16, 512, 512);
                gManageBlockAllocator->Create(1024*16, sizeof(hwnet::Block));
                gManageBlockAllocator->Create(1024*16, 512);
                ACE_Allocator::instance (dynamic_cast<ACE_Allocator*>(gManageBlockAllocator));
        }
        void* pVoid = gManageBlockAllocator->malloc (size);
        if (pVoid)
        {
                return pVoid;
        }
        static const std::bad_alloc nomem;
        throw nomem;
}
void *operator new[](std::size_t size,  const std::nothrow_t&) throw()
{
        if (gManageBlockAllocator == false)
        {
                gManageBlockAllocator = hwnet::ManageAllocator<ACE_SYNCH_RECURSIVE_MUTEX>::CreateInstance(1024*16, 512, 512);
                gManageBlockAllocator->Create(1024*16, sizeof(hwnet::Block));
                gManageBlockAllocator->Create(1024*16, 512);
                ACE_Allocator::instance (dynamic_cast<ACE_Allocator*>(gManageBlockAllocator));
        }
        return gManageBlockAllocator->malloc (size);
}
//void *operator new[](std::size_t count, void *ptr) throw()
//{
//      return ;
//}

inline void operator delete[](void *p)
{
        return gManageBlockAllocator->free (p);
}
#endif // HW_ACE_ALLOCATOR

 

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

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

Please Login (or Sign Up) to leave a comment