Based_Pointer_Repository.cpp

Go to the documentation of this file.
00001 // $Id: Based_Pointer_Repository.cpp 80826 2008-03-04 14:51:23Z wotte $
00002 
00003 #include "ace/Map_Manager.h"
00004 #include "ace/Based_Pointer_Repository.h"
00005 #include "ace/Guard_T.h"
00006 #include "ace/Null_Mutex.h"
00007 #include "ace/Synch_Traits.h"
00008 #include "ace/RW_Thread_Mutex.h"
00009 
00010 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00011 
00012 /**
00013  * @class ACE_Based_Pointer_Repository_Rep
00014  *
00015  * @brief Implementation for the ACE_Based_Pointer_Repository.
00016  *
00017  * Every memory pool in ACE binds it's mapping base address and
00018  * the mapped size to this repository every time it maps/remaps a
00019  * new chunk of memory successfully.
00020  */
00021 class ACE_Based_Pointer_Repository_Rep
00022 {
00023 public:
00024   // Useful typedefs.
00025   typedef ACE_Map_Manager <void *, size_t, ACE_Null_Mutex> MAP_MANAGER;
00026   typedef ACE_Map_Iterator <void *, size_t, ACE_Null_Mutex> MAP_ITERATOR;
00027   typedef ACE_Map_Entry <void *, size_t> MAP_ENTRY;
00028 
00029   /// Keeps track of the mapping between addresses and their associated
00030   /// values.
00031   MAP_MANAGER addr_map_;
00032 
00033   /// Synchronize concurrent access to the map.
00034   ACE_SYNCH_MUTEX lock_;
00035 };
00036 
00037 ACE_Based_Pointer_Repository::ACE_Based_Pointer_Repository (void)
00038 {
00039   ACE_TRACE ("ACE_Based_Pointer_Repository::ACE_Based_Pointer_Repository");
00040   ACE_NEW (this->rep_,
00041            ACE_Based_Pointer_Repository_Rep);
00042 }
00043 
00044 ACE_Based_Pointer_Repository::~ACE_Based_Pointer_Repository (void)
00045 {
00046   ACE_TRACE ("ACE_Based_Pointer_Repository::~ACE_Based_Pointer_Repository");
00047   delete this->rep_;
00048 }
00049 
00050 // Search for appropriate base address in repository
00051 
00052 int
00053 ACE_Based_Pointer_Repository::find (void *addr, void *&base_addr)
00054 {
00055   ACE_TRACE ("ACE_Based_Pointer_Repository::find");
00056   ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, mon, this->rep_->lock_, -1);
00057   ACE_Based_Pointer_Repository_Rep::MAP_ENTRY *ce = 0;
00058 
00059   for (ACE_Based_Pointer_Repository_Rep::MAP_ITERATOR iter (this->rep_->addr_map_);
00060        iter.next (ce) != 0;
00061        iter.advance ())
00062     // Check to see if <addr> is within any of the regions.
00063     if (addr >= ce->ext_id_
00064         && addr < ((char *)ce->ext_id_ + ce->int_id_))
00065       {
00066         // Assign the base address.
00067         base_addr = ce->ext_id_;
00068         return 1;
00069       }
00070 
00071   // Assume base address 0 (e.g., if new'ed).
00072   base_addr = 0;
00073   return 0;
00074 }
00075 
00076 // Bind a new entry to the repository or update the size of an
00077 // existing entry.
00078 
00079 int
00080 ACE_Based_Pointer_Repository::bind (void *addr, size_t size)
00081 {
00082   ACE_TRACE ("ACE_Based_Pointer_Repository::bind");
00083   ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, mon, this->rep_->lock_, -1);
00084 
00085   return this->rep_->addr_map_.rebind (addr, size);
00086 }
00087 
00088 // Unbind a base from the repository.
00089 
00090 int
00091 ACE_Based_Pointer_Repository::unbind (void *addr)
00092 {
00093   ACE_TRACE ("ACE_Based_Pointer_Repository::unbind");
00094   ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, mon, this->rep_->lock_, -1);
00095   ACE_Based_Pointer_Repository_Rep::MAP_ENTRY *ce = 0;
00096 
00097   // Search for service handlers that requested notification.
00098 
00099   for (ACE_Based_Pointer_Repository_Rep::MAP_ITERATOR iter (this->rep_->addr_map_);
00100        iter.next (ce) != 0;
00101        iter.advance ())
00102     {
00103       // Check to see if <addr> is within any of the regions and if
00104       // so, unbind the key from the map.
00105       if (addr >= ce->ext_id_
00106           && addr < ((char *)ce->ext_id_ + ce->int_id_))
00107         // Unbind base address.
00108         return this->rep_->addr_map_.unbind (ce->ext_id_);
00109     }
00110 
00111   return 0;
00112 }
00113 
00114 #if defined (ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION)
00115 template ACE_Singleton<ACE_Based_Pointer_Repository, ACE_SYNCH_RW_MUTEX> *
00116   ACE_Singleton<ACE_Based_Pointer_Repository, ACE_SYNCH_RW_MUTEX>::singleton_;
00117 #endif /* ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION */
00118 
00119 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Tue Feb 2 17:18:38 2010 for ACE by  doxygen 1.4.7