00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 #ifndef CACHED_ALLOCATOR_WITH_OVERFLOW_T_H
00009 #define CACHED_ALLOCATOR_WITH_OVERFLOW_T_H
00010 
00011 #include "debug.h"
00012 #include "ace/Malloc_Allocator.h"
00013 #include "ace/Malloc_T.h"
00014 #include "ace/Free_List.h"
00015 #include "ace/Guard_T.h"
00016 #include "ace/Atomic_Op.h"
00017 
00018 #include "dds/DCPS/SafetyProfilePool.h"
00019 #include "PoolAllocationBase.h"
00020 
00021 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00022 # pragma once
00023 #endif 
00024 
00025 namespace OpenDDS {
00026 namespace DCPS {
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 template <class T, class ACE_LOCK>
00043 class Cached_Allocator_With_Overflow : public ACE_New_Allocator, public PoolAllocationBase {
00044 public:
00045 
00046 
00047   Cached_Allocator_With_Overflow(size_t n_chunks)
00048     : allocs_from_heap_(0),
00049       allocs_from_pool_(0),
00050       frees_to_heap_(0),
00051       frees_to_pool_(0),
00052       free_list_(ACE_PURE_FREE_LIST) {
00053     
00054     
00055     
00056     
00057     
00058     
00059     
00060     
00061     
00062     
00063     
00064     size_t chunk_size = sizeof(T);
00065     chunk_size = ACE_MALLOC_ROUNDUP(chunk_size, ACE_MALLOC_ALIGN);
00066     begin_ = static_cast<unsigned char*> (ACE_Allocator::instance()->malloc(n_chunks * chunk_size));
00067 
00068     
00069     end_ = begin_ + n_chunks * chunk_size;
00070 
00071     
00072     
00073     for (size_t c = 0; c < n_chunks; c++) {
00074       void* placement = begin_ + c * chunk_size;
00075       this->free_list_.add(new(placement) ACE_Cached_Mem_Pool_Node<T>);
00076     }
00077   }
00078 
00079 
00080   ~Cached_Allocator_With_Overflow() {
00081     ACE_Allocator::instance()->free(begin_);
00082   }
00083 
00084 
00085 
00086 
00087 
00088 
00089   void *malloc(size_t nbytes = sizeof(T)) {
00090     
00091     if (nbytes > sizeof(T))
00092       return 0;
00093 
00094     
00095     
00096     void* rtn =  this->free_list_.remove()->addr();
00097 
00098     if (0 == rtn) {
00099       rtn = ACE_Allocator::instance()->malloc(sizeof(T));
00100       allocs_from_heap_++;
00101 
00102       if (DCPS_debug_level >= 2) {
00103         if (allocs_from_heap_ == 1 && DCPS_debug_level >= 2)
00104           ACE_DEBUG((LM_DEBUG,
00105                      "(%P|%t) Cached_Allocator_With_Overflow::malloc %@"
00106                      " %Lu heap allocs with %Lu outstanding\n",
00107                      this, this->allocs_from_heap_.value(),
00108                      this->allocs_from_heap_.value() - this->frees_to_heap_.value()));
00109 
00110         if (DCPS_debug_level >= 6)
00111           if (allocs_from_heap_.value() % 500 == 0)
00112             ACE_DEBUG((LM_DEBUG,
00113                        "(%P|%t) Cached_Allocator_With_Overflow::malloc %@"
00114                        " %Lu heap allocs with %Lu outstanding\n",
00115                        this, this->allocs_from_heap_.value(),
00116                        this->allocs_from_heap_.value() - this->frees_to_heap_.value()));
00117       }
00118 
00119     } else {
00120       allocs_from_pool_++;
00121 
00122       if (DCPS_debug_level >= 6)
00123         if (allocs_from_pool_.value() % 500 == 0)
00124           ACE_DEBUG((LM_DEBUG,
00125                      "(%P|%t) Cached_Allocator_With_Overflow::malloc %@"
00126                      " %Lu pool allocs %Lu pool frees with %Lu available\n",
00127                      this, this->allocs_from_pool_.value(), this->frees_to_pool_.value(),
00128                      this->available()));
00129     }
00130 
00131     return rtn;
00132   }
00133 
00134 
00135 
00136 
00137 
00138 
00139 
00140   virtual void *calloc(size_t ,
00141                        char  = '\0') {
00142     ACE_NOTSUP_RETURN(0);
00143   }
00144 
00145 
00146 
00147   virtual void *calloc(size_t ,
00148                        size_t ,
00149                        char  = '\0') {
00150     ACE_NOTSUP_RETURN(0);
00151   }
00152 
00153 
00154   void free(void * ptr) {
00155     unsigned char* tmp = static_cast<unsigned char*>(ptr);
00156     if (tmp < begin_ ||
00157         tmp >= end_) {
00158       ACE_Allocator::instance()->free(tmp);
00159       frees_to_heap_++;
00160 
00161       if (frees_to_heap_ > allocs_from_heap_.value()) {
00162         ACE_ERROR((LM_ERROR,
00163                    "(%P|%t) ERROR:Cached_Allocator_With_Overflow::free %@"
00164                    " more deletes %Lu than allocs %Lu to the heap\n",
00165                    this,
00166                    this->frees_to_heap_.value(),
00167                    this->allocs_from_heap_.value()));
00168       }
00169 
00170       if (DCPS_debug_level >= 6) {
00171         if (frees_to_heap_.value() % 500 == 0) {
00172           ACE_DEBUG((LM_DEBUG,
00173                      "(%P|%t) Cached_Allocator_With_Overflow::free %@"
00174                      " %Lu heap allocs with %Lu outstanding\n",
00175                      this, this->allocs_from_heap_.value(),
00176                      this->allocs_from_heap_.value() - this->frees_to_heap_.value()));
00177         }
00178       }
00179 
00180     } else if (ptr != 0) {
00181       frees_to_pool_ ++;
00182 
00183       if (frees_to_pool_ > allocs_from_pool_.value()) {
00184         ACE_ERROR((LM_ERROR,
00185                    "(%P|%t) ERROR: Cached_Allocator_With_Overflow::free %@"
00186                    " more deletes %Lu than allocs %Lu from the pool ptr=%@ begin_=%@ end_=%@\n",
00187                    this,
00188                    this->frees_to_pool_.value(),
00189                    this->allocs_from_pool_.value(), ptr, begin_, end_));
00190       }
00191 
00192       this->free_list_.add((ACE_Cached_Mem_Pool_Node<T> *) ptr) ;
00193 
00194       if (DCPS_debug_level >= 6)
00195         if (this->available() % 500 == 0)
00196           ACE_DEBUG((LM_DEBUG,
00197                      "(%P|%t) Cached_Allocator_With_Overflow::malloc %@"
00198                      " %Lu pool allocs %Lu pool free with %Lu available\n",
00199                      this, this->allocs_from_pool_.value(),
00200                      this->frees_to_pool_.value(),
00201                      this->available()));
00202     }
00203   }
00204 
00205   
00206 
00207 
00208 
00209   size_t available() {
00210     return free_list_.size();
00211   };
00212 
00213   ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long> allocs_from_heap_;
00214   ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long> allocs_from_pool_;
00215   ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long> frees_to_heap_ ;
00216   ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long> frees_to_pool_;
00217 
00218 private:
00219 
00220 
00221   unsigned char* begin_;
00222 
00223   unsigned char* end_;
00224 
00225 
00226   ACE_Locked_Free_List<ACE_Cached_Mem_Pool_Node<T>, ACE_LOCK> free_list_;
00227 };
00228 
00229 } 
00230 } 
00231 
00232 #endif