#include <Persistent_Naming_Context.h>
Inheritance diagram for TAO_Persistent_Bindings_Map:
Public Types | |
typedef ACE_Hash_Map_With_Allocator< TAO_Persistent_ExtId, TAO_Persistent_IntId > | HASH_MAP |
Underlying data structure - typedef for ease of use. | |
Public Member Functions | |
TAO_Persistent_Bindings_Map (CORBA::ORB_ptr orb) | |
Constructor. | |
int | open (size_t hash_map_size, ACE_Allocator *alloc) |
void | set (HASH_MAP *map, ACE_Allocator *alloc) |
virtual | ~TAO_Persistent_Bindings_Map (void) |
void | destroy (void) |
HASH_MAP * | map (void) |
Get a pointer to the underlying hash map. | |
size_t | total_size (void) |
Return the size of the underlying hash table. | |
virtual size_t | current_size (void) |
Return the size of the underlying hash table. | |
virtual int | bind (const char *id, const char *kind, CORBA::Object_ptr obj, CosNaming::BindingType type) |
virtual int | rebind (const char *id, const char *kind, CORBA::Object_ptr obj, CosNaming::BindingType type) |
virtual int | unbind (const char *id, const char *kind) |
virtual int | find (const char *id, const char *kind, CORBA::Object_ptr &obj, CosNaming::BindingType &type) |
Protected Member Functions | |
int | open_helper (size_t hash_table_size, void *buffer) |
int | shared_bind (const char *id, const char *kind, CORBA::Object_ptr obj, CosNaming::BindingType type, int rebind) |
Helper: factors common code from <bind> and <rebind>. | |
Protected Attributes | |
ACE_Allocator * | allocator_ |
Pointer to the allocator we use to make bindings persistent. | |
HASH_MAP * | map_ |
Pointer to the underlying hash map. | |
CORBA::ORB_var | orb_ |
Pointer to the orb. We need it to do string/object conversions. |
Wrapper on top of ACE_Hash_Map_With_Allocator (which is a wrapper around ACE_Hash_Map_Manager). Uses ACE_Allocator (allocating from persistent storage) to make bindings persistent and supports TAO_Bindings_Map interface. Used by TAO_Persistent_Naming_Context.
Definition at line 35 of file Persistent_Naming_Context.h.
typedef ACE_Hash_Map_With_Allocator<TAO_Persistent_ExtId, TAO_Persistent_IntId> TAO_Persistent_Bindings_Map::HASH_MAP |
Underlying data structure - typedef for ease of use.
Definition at line 41 of file Persistent_Naming_Context.h.
TAO_Persistent_Bindings_Map::TAO_Persistent_Bindings_Map | ( | CORBA::ORB_ptr | orb | ) |
Constructor.
Definition at line 71 of file Persistent_Naming_Context.cpp.
00072 : allocator_ (0), 00073 map_ (0), 00074 orb_ (CORBA::ORB::_duplicate (orb)) 00075 { 00076 }
TAO_Persistent_Bindings_Map::~TAO_Persistent_Bindings_Map | ( | void | ) | [virtual] |
Destructor. Does not deallocate the hash map: if an instance of this class goes out of scope, its hash_map remains in persistent storage.
Definition at line 78 of file Persistent_Naming_Context.cpp.
int TAO_Persistent_Bindings_Map::bind | ( | const char * | id, | |
const char * | kind, | |||
CORBA::Object_ptr | obj, | |||
CosNaming::BindingType | type | |||
) | [virtual] |
Add a binding with the specified parameters to the table. Return 0 on success and -1 on failure, 1 if there already is a binding with <id> and <kind>.
Implements TAO_Bindings_Map.
Definition at line 34 of file Persistent_Naming_Context.cpp.
References shared_bind().
00038 { 00039 return this->shared_bind (id, kind, obj, type, 0); 00040 }
size_t TAO_Persistent_Bindings_Map::current_size | ( | void | ) | [virtual] |
Return the size of the underlying hash table.
Implements TAO_Bindings_Map.
Definition at line 102 of file Persistent_Naming_Context.cpp.
References ACE_Hash_Map_Manager_Ex< EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK >::current_size(), and map_.
00103 { 00104 return map_->current_size (); 00105 }
void TAO_Persistent_Bindings_Map::destroy | ( | void | ) |
This method removes the hash map from persistent storage/frees up the memory. The hash map better be empty, since we are not cleaning up the insides. (We could add <close> to clean entries, but not the data inside the entries.
Definition at line 83 of file Persistent_Naming_Context.cpp.
References allocator_, ACE_Allocator::free(), and map_.
Referenced by TAO_Persistent_Naming_Context::~TAO_Persistent_Naming_Context().
00084 { 00085 this->map_->ACE_Hash_Map_With_Allocator<TAO_Persistent_ExtId, TAO_Persistent_IntId>::~ACE_Hash_Map_With_Allocator (); 00086 this->allocator_->free (map_); 00087 }
int TAO_Persistent_Bindings_Map::find | ( | const char * | id, | |
const char * | kind, | |||
CORBA::Object_ptr & | obj, | |||
CosNaming::BindingType & | type | |||
) | [virtual] |
Find the binding containing <id> and <kind> in the table, and pass binding's type and object back to the caller by reference. Return 0 on success and -1 on failure. Note: a 'duplicated' object reference is assigned to <obj>, so the caller is responsible for its deallocation.
Implements TAO_Bindings_Map.
Definition at line 52 of file Persistent_Naming_Context.cpp.
References orb_, TAO_Persistent_IntId::ref_, and TAO_Persistent_IntId::type_.
00056 { 00057 TAO_Persistent_ExtId name (id, kind); 00058 TAO_Persistent_IntId entry; 00059 00060 if (this->map_->find (name, entry, this->allocator_) != 0) 00061 return -1; 00062 else 00063 { 00064 obj = orb_->string_to_object (entry.ref_); 00065 type = entry.type_; 00066 00067 return 0; 00068 } 00069 }
TAO_Persistent_Bindings_Map::HASH_MAP * TAO_Persistent_Bindings_Map::map | ( | void | ) |
Get a pointer to the underlying hash map.
Definition at line 90 of file Persistent_Naming_Context.cpp.
References map_.
Referenced by TAO_Persistent_Naming_Context::list(), TAO_Persistent_Naming_Context::make_new_context(), and set().
00091 { 00092 return this->map_; 00093 }
int TAO_Persistent_Bindings_Map::open | ( | size_t | hash_map_size, | |
ACE_Allocator * | alloc | |||
) |
Allocate hash map of size <hash_map_size> from persistent storage using the <alloc>.
Definition at line 108 of file Persistent_Naming_Context.cpp.
References allocator_, ACE_Allocator::free(), ACE_Allocator::malloc(), and open_helper().
Referenced by TAO_Persistent_Naming_Context::init().
00109 { 00110 allocator_ = alloc; 00111 00112 // Use allocator to allocate space for the hash map. 00113 void *hash_map = 0; 00114 size_t map_size = sizeof (HASH_MAP); 00115 hash_map = this->allocator_->malloc (map_size); 00116 00117 // If allocation failed ... 00118 if (hash_map == 0) 00119 return -1; 00120 00121 // Initialize allocated hash map through placement new. 00122 if (open_helper (hash_table_size, hash_map) == -1) 00123 this->allocator_->free (hash_map); 00124 00125 return 0; 00126 }
int TAO_Persistent_Bindings_Map::open_helper | ( | size_t | hash_table_size, | |
void * | buffer | |||
) | [protected] |
Helper to the <open> method. By isolating placement new into a separate method, we can deal with memory allocation failures more efficiently. If there is a problem in HASH_MAP constructor, we can clean up preallocated space.
Definition at line 129 of file Persistent_Naming_Context.cpp.
References map_.
Referenced by open().
00131 { 00132 this->map_ = new (buffer) HASH_MAP (hash_table_size, this->allocator_); 00133 return 0; 00134 }
int TAO_Persistent_Bindings_Map::rebind | ( | const char * | id, | |
const char * | kind, | |||
CORBA::Object_ptr | obj, | |||
CosNaming::BindingType | type | |||
) | [virtual] |
Overwrite a binding containing <id> and <kind> (or create a new one if one doesn't exist) with the specified parameters. Return 0 or 1 on success. Return -1 or -2 on failure. (-2 is returned if the new and old bindings differ in type).
Implements TAO_Bindings_Map.
Definition at line 43 of file Persistent_Naming_Context.cpp.
References shared_bind().
00047 { 00048 return this->shared_bind (id, kind, obj, type, 1); 00049 }
void TAO_Persistent_Bindings_Map::set | ( | HASH_MAP * | map, | |
ACE_Allocator * | alloc | |||
) |
The hash map has already been preallocated for us. We just need to set our data members take ownership of it.
Definition at line 137 of file Persistent_Naming_Context.cpp.
References allocator_, map(), and map_.
Referenced by TAO_Persistent_Naming_Context::TAO_Persistent_Naming_Context().
00138 { 00139 allocator_ = alloc; 00140 map_ = map; 00141 }
int TAO_Persistent_Bindings_Map::shared_bind | ( | const char * | id, | |
const char * | kind, | |||
CORBA::Object_ptr | obj, | |||
CosNaming::BindingType | type, | |||
int | rebind | |||
) | [protected] |
Helper: factors common code from <bind> and <rebind>.
Definition at line 144 of file Persistent_Naming_Context.cpp.
References allocator_, ACE_Hash_Map_With_Allocator< EXT_ID, INT_ID >::bind(), ACE_Allocator::free(), map_, orb_, ACE_Hash_Map_With_Allocator< EXT_ID, INT_ID >::rebind(), TAO_Persistent_IntId::ref_, ACE_OS::strcpy(), ACE_OS::strlen(), ACE_Allocator::sync(), and TAO_Persistent_IntId::type_.
Referenced by bind(), and rebind().
00149 { 00150 // Obtain a stringified ior of <obj> (i.e., the representation we can store). 00151 CORBA::String_var ref = orb_->object_to_string (obj); 00152 00153 // Calculate and allocate the memory we need to store this name to 00154 // object binding. 00155 size_t id_len = ACE_OS::strlen (id) + 1; 00156 size_t kind_len = ACE_OS::strlen (kind) + 1; 00157 size_t ref_len = ACE_OS::strlen (ref.in ()) + 1; 00158 size_t total_len = id_len + kind_len + ref_len; 00159 char *ptr = (char *) this->allocator_->malloc (total_len); 00160 00161 // Allocation failed - bail out. 00162 if (ptr == 0) 00163 return -1; 00164 00165 // Allocation succeded - place the data into the allocated memory 00166 // and procceed. 00167 else 00168 { 00169 // Note that the <ref> *must* come first to make sure we can 00170 // retrieve this pointer later on in unbind(). 00171 char * ref_ptr = ptr; 00172 char * id_ptr = ptr + ref_len; 00173 char * kind_ptr = ptr + ref_len + id_len; 00174 ACE_OS::strcpy (ref_ptr, ref.in ()); 00175 ACE_OS::strcpy (id_ptr, id); 00176 ACE_OS::strcpy (kind_ptr, kind); 00177 00178 TAO_Persistent_ExtId new_name (id_ptr, kind_ptr); 00179 TAO_Persistent_IntId new_entry (ref_ptr, type); 00180 int result = -1; 00181 00182 if (rebind == 0) 00183 { 00184 // Do a normal bind. This will fail if there's already an 00185 // <new_internal> with the same name. 00186 result = this->map_->bind (new_name, new_entry, this->allocator_); 00187 00188 if (result == 1) 00189 { 00190 // Entry already existed so bind failed. Free our 00191 // dynamically allocated memory. 00192 this->allocator_->free ((void *) ptr); 00193 return result; 00194 } 00195 } 00196 else 00197 // Rebind. 00198 { 00199 TAO_Persistent_ExtId old_name; 00200 TAO_Persistent_IntId old_entry; 00201 00202 // Check that the types of old and new entries match. 00203 if (this->map_->find (new_name, 00204 old_entry, 00205 this->allocator_) == 0 00206 && type != old_entry.type_) 00207 result = -2; 00208 00209 // If types match, perform rebind. 00210 else 00211 result = this->map_->rebind (new_name, new_entry, 00212 old_name, old_entry, 00213 this->allocator_); 00214 if (result == 1) 00215 { 00216 // Free up the old binding's memory, if it was replaced. 00217 // Note, this assumes that the "ref" pointer comes 00218 // first, and that the id, kind, and ref are contiguously 00219 // allocated (see beginning of this method for details). 00220 this->allocator_->free ((void *) old_entry.ref_); 00221 } 00222 } 00223 00224 // Check for failures, and clean up dynamically allocated memory 00225 // if necessary. 00226 if (result < 0) 00227 this->allocator_->free ((void *) ptr); 00228 00229 else 00230 // If bind() or rebind() succeeded, they will automatically sync 00231 // up the map manager entry. However, we must sync up our 00232 // name/value memory. 00233 this->allocator_->sync (ptr, total_len); 00234 00235 return result; 00236 } 00237 }
size_t TAO_Persistent_Bindings_Map::total_size | ( | void | ) |
Return the size of the underlying hash table.
Definition at line 96 of file Persistent_Naming_Context.cpp.
References map_, and ACE_Hash_Map_Manager_Ex< EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK >::total_size().
Referenced by TAO_Persistent_Naming_Context::new_context().
00097 { 00098 return this->map_->total_size (); 00099 }
TAO_BEGIN_VERSIONED_NAMESPACE_DECL int TAO_Persistent_Bindings_Map::unbind | ( | const char * | id, | |
const char * | kind | |||
) | [virtual] |
Remove a binding containing <id> and <kind> from the table. Return 0 on success and -1 on failure.
Implements TAO_Bindings_Map.
Definition at line 15 of file Persistent_Naming_Context.cpp.
References TAO_Persistent_IntId::ref_.
00017 { 00018 TAO_Persistent_ExtId name (id, kind); 00019 TAO_Persistent_IntId entry; 00020 if (this->map_->unbind (name, entry, this->allocator_) != 0) 00021 return -1; 00022 else 00023 { 00024 // Free up the memory we allocated in shared_bind(). Note that 00025 // this assumes that the "ref" pointer comes first and that 00026 // the ref, id and kind are contiguously allocated (see 00027 // shared_bind() for details). 00028 this->allocator_->free ((void *) (entry.ref_)); 00029 return 0; 00030 } 00031 }
ACE_Allocator* TAO_Persistent_Bindings_Map::allocator_ [protected] |
Pointer to the allocator we use to make bindings persistent.
Definition at line 142 of file Persistent_Naming_Context.h.
Referenced by destroy(), open(), set(), and shared_bind().
HASH_MAP* TAO_Persistent_Bindings_Map::map_ [protected] |
Pointer to the underlying hash map.
Definition at line 145 of file Persistent_Naming_Context.h.
Referenced by current_size(), destroy(), map(), open_helper(), set(), shared_bind(), and total_size().
CORBA::ORB_var TAO_Persistent_Bindings_Map::orb_ [protected] |
Pointer to the orb. We need it to do string/object conversions.
Definition at line 148 of file Persistent_Naming_Context.h.
Referenced by find(), and shared_bind().