Based_Pointer_Repository.cpp

Go to the documentation of this file.
00001 // Based_Pointer_Repository.cpp,v 4.21 2006/04/19 19:16:54 jwillemsen Exp
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,
00054                                     void *&base_addr)
00055 {
00056   ACE_TRACE ("ACE_Based_Pointer_Repository::find");
00057   ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, mon, this->rep_->lock_, -1);
00058   ACE_Based_Pointer_Repository_Rep::MAP_ENTRY *ce = 0;
00059 
00060   for (ACE_Based_Pointer_Repository_Rep::MAP_ITERATOR iter (this->rep_->addr_map_);
00061        iter.next (ce) != 0;
00062        iter.advance ())
00063     // Check to see if <addr> is within any of the regions.
00064     if (addr >= ce->ext_id_
00065         && addr < ((char *) ce->ext_id_ + ce->int_id_))
00066       {
00067         // Assign the base address.
00068         base_addr = ce->ext_id_;
00069         return 1;
00070       }
00071 
00072   // Assume base address 0 (e.g., if new'ed).
00073   base_addr = 0;
00074   return 0;
00075 }
00076 
00077 // Bind a new entry to the repository or update the size of an
00078 // existing entry.
00079 
00080 int
00081 ACE_Based_Pointer_Repository::bind (void *addr,
00082                                     size_t size)
00083 {
00084   ACE_TRACE ("ACE_Based_Pointer_Repository::bind");
00085   ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, mon, this->rep_->lock_, -1);
00086 
00087   return this->rep_->addr_map_.rebind (addr, size);
00088 }
00089 
00090 // Unbind a base from the repository.
00091 
00092 int
00093 ACE_Based_Pointer_Repository::unbind (void *addr)
00094 {
00095   ACE_TRACE ("ACE_Based_Pointer_Repository::unbind");
00096   ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, mon, this->rep_->lock_, -1);
00097   ACE_Based_Pointer_Repository_Rep::MAP_ENTRY *ce = 0;
00098 
00099   // Search for service handlers that requested notification.
00100 
00101   for (ACE_Based_Pointer_Repository_Rep::MAP_ITERATOR iter (this->rep_->addr_map_);
00102        iter.next (ce) != 0;
00103        iter.advance ())
00104     {
00105       // Check to see if <addr> is within any of the regions and if
00106       // so, unbind the key from the map.
00107       if (addr >= ce->ext_id_
00108           && addr < ((char *) ce->ext_id_ + ce->int_id_))
00109         // Unbind base address.
00110         return this->rep_->addr_map_.unbind (ce->ext_id_);
00111     }
00112 
00113   return 0;
00114 }
00115 
00116 #if defined (ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION)
00117 template ACE_Singleton<ACE_Based_Pointer_Repository, ACE_SYNCH_RW_MUTEX> *
00118   ACE_Singleton<ACE_Based_Pointer_Repository, ACE_SYNCH_RW_MUTEX>::singleton_;
00119 #endif /* ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION */
00120 
00121 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 09:41:47 2006 for ACE by doxygen 1.3.6