自己实现的一个ACE内存分配器

针对我的前两篇文章《基于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 = -1int flags = MS_SYNC);
  
virtual int sync (void *addr, size_t len, int flags = MS_SYNC);
  
virtual int protect (ssize_t len = -1int 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 (voidconst;
#endif /* ACE_HAS_MALLOC_STATS */
  
virtual void dump (voidconst;

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 (
voidconst
    
{
    }

    
#endif /* ACE_HAS_MALLOC_STATS */

    
void
    My_Allocator::dump (
voidconst
    
{
    
#if defined (ACE_HAS_DUMP)
    
#endif /* ACE_HAS_DUMP */
    }



}
 // namespace om

posted on 2007-10-29 12:48 PeakGao 阅读(1878) 评论(1)  编辑 收藏 引用 所属分类: C++技术

评论

# re: 自己实现的一个ACE内存分配器 2008-03-14 23:27 happychui

1 class My_Allocator 继承CachePool时必须要模版实例化才行,所以必须加上<ACE_SYNCH_MUTEX>
2 因为ACE_SYNCH_MUTEX需要头文件 Task.h所以加上了。
  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2007年10月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

导航

统计

常用链接

留言簿(9)

随笔分类(67)

随笔档案(65)

搜索

最新评论

阅读排行榜

评论排行榜