作者:PeakGao 来源:C++博客   酷勤网收集 2007-11-13

摘要
  针对我的前两篇文章《基于ACE实现的一个内存池》和《基于ACE实现的一个内存池-续篇》后,发现缓存ACE_Message_Block的时候还是不太方便,然后干脆实现了ACE_Allocator接口。

针对我的前两篇文章《基于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

来自:http://www.cppblog.com/PeakGao/archive/2007/10/29/35435.html

分类: C++名库 编程语言

上一篇:基于ACE实现的一个内存池-续篇   下一篇:悬挂指针与boost::weak_ptr