特别声明:
建议使用google游览器,火狐也可以
论坛处于测试阶段,一切资料都为测试资料,在论坛正式运行的时候,会尽量保存网友的劳动成果!
HelloWorld论坛秉持互惠互利,共同学习与进步,一个人的梦想大家实现的理想,一直坚持着,望广大网友多多支持,提供宝贵意见
来论坛做什么?
可以先转载你平时学习查找的资料(先论坛查找),自己可以写写体会
把平时碰到的问题,如何解决可以先记录在论坛,以备后来的人学习
可以和会员一起参加一些开源项目的学习,汉化,推广,甚至可以加入团队
|
|
来源:http://www.cppblog.com/PeakGao/archive/2007/10/29/35437.html 针对我的前两篇文章《基于ACE实现的一个内存池》和《基于ACE实现的一个内存池-续篇》后,发现缓存ACE_Message_Block的时候还是不太方便,然后干脆实现了ACE_Allocator接口,代码如下,利用这个分配器的ACE_Message_Block将会很快贴出来。 //MemPoolAllocator.h /** * @date 2007.10.29 * @author PeakGao <peakgao163@163.com> */ #ifndef OM_MEMPOOLALLOCATOR_H #define OM_MEMPOOLALLOCATOR_H #include <ace/pre.h> //#include <ace/ACE_export.h> #if !defined (ACE_LACKS_PRAGMA_ONCE) # pragma once #endif /* ACE_LACKS_PRAGMA_ONCE */ #include <ace/Malloc_Base.h> #if defined (ACE_HAS_MALLOC_STATS) #if defined (ACE_HAS_THREADS) #include "ace/Process_Mutex.h" #define ACE_PROCESS_MUTEX ACE_Process_Mutex #else #include "ace/SV_Semaphore_Simple.h" #define ACE_PROCESS_MUTEX ACE_SV_Semaphore_Simple #endif /* ACE_HAS_THREADS */ #endif /* ACE_HAS_MALLOC_STATS */ #include "MemPoolT.h" namespace om{ class My_Allocator : public ACE_Allocator, public CachePool { public: /// These methods are defined. virtual void *malloc (size_t nbytes); virtual void *calloc (size_t nbytes, char initial_value = '\0'); virtual void *calloc (size_t n_elem, size_t elem_size, char initial_value = '\0'); virtual void free (void *ptr); /// These methods are no-ops. virtual int remove (void); virtual int bind (const char *name, void *pointer, int duplicates = 0); virtual int trybind (const char *name, void *&pointer); virtual int find (const char *name, void *&pointer); virtual int find (const char *name); virtual int unbind (const char *name); virtual int unbind (const char *name, void *&pointer); virtual int sync (ssize_t len = -1, int flags = MS_SYNC); virtual int sync (void *addr, size_t len, int flags = MS_SYNC); virtual int protect (ssize_t len = -1, int prot = PROT_RDWR); virtual int protect (void *addr, size_t len, int prot = PROT_RDWR); #if defined (ACE_HAS_MALLOC_STATS) virtual void print_stats (void) const; #endif /* ACE_HAS_MALLOC_STATS */ virtual void dump (void) const; private: // DO NOT ADD ANY STATE (DATA MEMBERS) TO THIS CLASS!!!! See the // <ACE_Allocator::instance> implementation for explanation. }; #include /**/ <ace/post.h> } // namespace om #endif // OM_MEMPOOLALLOCATOR_H
// MemPoolAllocator.cpp /** * @date 2007.10.29 * @author PeakGao <peakgao163@163.com> */ #include "MemPoolAllocator.h" #include <ace/OS_NS_string.h> namespace om{ void * My_Allocator::malloc (size_t nbytes) { if (nbytes > 0 && nbytes <= CachePool::getBlockSize()) return CachePool::alloc(); return NULL; } void * My_Allocator::calloc (size_t nbytes, char initial_value) { void *ptr = malloc(nbytes); if (!ptr) return NULL; ACE_OS::memset (ptr, initial_value, nbytes); return (void *) ptr; } void * My_Allocator::calloc (size_t n_elem, size_t elem_size, char initial_value) { return My_Allocator::calloc (n_elem * elem_size, initial_value); } void My_Allocator::free (void *ptr) { CachePool::free(ptr); } int My_Allocator::remove (void) { ACE_NOTSUP_RETURN (-1); } int My_Allocator::bind (const char *, void *, int) { ACE_NOTSUP_RETURN (-1); } int My_Allocator::trybind (const char *, void *&) { ACE_NOTSUP_RETURN (-1); } int My_Allocator::find (const char *, void *&) { ACE_NOTSUP_RETURN (-1); } int My_Allocator::find (const char *) { ACE_NOTSUP_RETURN (-1); } int My_Allocator::unbind (const char *) { ACE_NOTSUP_RETURN (-1); } int My_Allocator::unbind (const char *, void *&) { ACE_NOTSUP_RETURN (-1); } int My_Allocator::sync (ssize_t, int) { ACE_NOTSUP_RETURN (-1); } int My_Allocator::sync (void *, size_t, int) { ACE_NOTSUP_RETURN (-1); } int My_Allocator::protect (ssize_t, int) { ACE_NOTSUP_RETURN (-1); } int My_Allocator::protect (void *, size_t, int) { ACE_NOTSUP_RETURN (-1); } #if defined (ACE_HAS_MALLOC_STATS) void My_Allocator::print_stats (void) const { } #endif /* ACE_HAS_MALLOC_STATS */ void My_Allocator::dump (void) const { #if defined (ACE_HAS_DUMP) #endif /* ACE_HAS_DUMP */ } } // namespace om
|
|
|
Please Login (or Sign Up) to leave a comment |