xingd.net

.net related techonology

导航

基于STL分配器的ACE Allocator实现

Posted on 2005-04-03 21:53  xingd  阅读(1673)  评论(0编辑  收藏  举报
template<template <typename> class _Alloc>
 class ACE_STL_Allocator : public ACE_Allocator
 {
  public:
   typedef _Alloc<char> alloc_type;
   
  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:
   alloc_type allocator;
 };
 
 template<template <typename> class _Alloc>
 void* ACE_STL_Allocator<_Alloc>::malloc(size_t nbytes)
 {
  char* ptr = 0;
  
  if (nbytes > 0)
  {
   ptr = allocator.allocate(nbytes + sizeof(size_t)) + sizeof(size_t);
  }
  
  return  ptr;
 }
 
 template<template <typename> class _Alloc>
 void* ACE_STL_Allocator<_Alloc>::calloc(size_t nbytes, char initial_value)
 {
  char* ptr = 0;
  
  if (nbytes > 0)
  {
   ptr = allocator.allocate(nbytes + sizeof(size_t)) + sizeof(size_t);   
   std::uninitialized_fill_n(ptr, nbytes, initial_value);
  }
  
  return ptr;  
 }
 
 template<template <typename> class _Alloc>
 void* ACE_STL_Allocator<_Alloc>::calloc(size_t n_elem, size_t elem_size, char initial_value)
 {
  return ACE_STL_Allocator::calloc(n_elem * elem_size, initial_value);
 }
 
 template<template <typename> class _Alloc>
 void ACE_STL_Allocator<_Alloc>::free(void *ptr)
 {
  allocator.deallocate((char*)ptr - sizeof(size_t), (size_t)(*((char*)ptr - sizeof(size_t)))); 
 }
 
 template<template <typename> class _Alloc>
 int ACE_STL_Allocator<_Alloc>::remove(void)
 {
  ACE_NOTSUP_RETURN(-1); 
 }
 
 template<template <typename> class _Alloc>
 int ACE_STL_Allocator<_Alloc>::bind(const char*, void*, int)
 {
  ACE_NOTSUP_RETURN(-1); 
 }
 
 template<template <typename> class _Alloc>
 int ACE_STL_Allocator<_Alloc>::trybind(const char*, void *&)
 {
  ACE_NOTSUP_RETURN(-1); 
 }
 
 template<template <typename> class _Alloc>
 int ACE_STL_Allocator<_Alloc>::find(const char*)
 {
  ACE_NOTSUP_RETURN(-1); 
 }
 
 template<template <typename> class _Alloc>
 int ACE_STL_Allocator<_Alloc>::find(const char*, void *&)
 {
  ACE_NOTSUP_RETURN(-1); 
 }
 
 template<template <typename> class _Alloc>
 int ACE_STL_Allocator<_Alloc>::unbind(const char*)
 {
  ACE_NOTSUP_RETURN(-1); 
 }
 
 template<template <typename> class _Alloc>
 int ACE_STL_Allocator<_Alloc>::unbind(const char*, void *&)
 {
  ACE_NOTSUP_RETURN(-1); 
 }
 
 template<template <typename> class _Alloc>
 int ACE_STL_Allocator<_Alloc>::sync(ssize_t, int)
 {
  ACE_NOTSUP_RETURN(-1);
 }
 
 template<template <typename> class _Alloc>
 int ACE_STL_Allocator<_Alloc>::sync(void*, size_t, int)
 {
  ACE_NOTSUP_RETURN(-1);
 }
 
 template<template <typename> class _Alloc>
 int ACE_STL_Allocator<_Alloc>::protect(ssize_t, int)
 {
  ACE_NOTSUP_RETURN(-1);
 }
 
 template<template <typename> class _Alloc>
 int ACE_STL_Allocator<_Alloc>::protect(void*, size_t, int)
 {
  ACE_NOTSUP_RETURN(-1);
 }
 
 #if defined ACE_HAS_MALLOC_STATS
 template<template <typename> class _Alloc>
 void ACE_STL_Allocator<_Alloc>::print_stats(void) const
 {
  
 }
 #endif
 
 template<template <typename> class _Alloc>
 void ACE_STL_Allocator<_Alloc>::dump(void) const
 {
  
 }