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

Login or Sign up | Validate
| Search
HelloWorld论坛 : > 计算机科学、技术、教学> 编程专题> 开源免费项目> [转]自己实现的一个ACE内存分配器
 
 
 
 
类别:ACE 阅读:5129 评论:0 时间:April 11, 2012, 11:30 a.m. 关键字: ACE 内存

 

来源: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