Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / ace / Framework_Component.cpp
blob64b24c65cd567f76dd6714fdcda2ae3b004e332d
1 #include "ace/Framework_Component.h"
3 #if !defined (__ACE_INLINE__)
4 #include "ace/Framework_Component.inl"
5 #endif /* __ACE_INLINE__ */
7 #include "ace/Object_Manager.h"
8 #include "ace/Log_Category.h"
9 #include "ace/DLL_Manager.h"
10 #include "ace/Recursive_Thread_Mutex.h"
11 #include "ace/OS_NS_string.h"
13 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
15 ACE_Framework_Component::~ACE_Framework_Component ()
17 ACE_TRACE ("ACE_Framework_Component::~ACE_Framework_Component");
19 ACE::strdelete (const_cast<ACE_TCHAR*> (this->dll_name_));
20 ACE::strdelete (const_cast<ACE_TCHAR*> (this->name_));
23 /***************************************************************/
25 ACE_ALLOC_HOOK_DEFINE(ACE_Framework_Repository)
27 sig_atomic_t ACE_Framework_Repository::shutting_down_ = 0;
29 // Pointer to the Singleton instance.
30 ACE_Framework_Repository *ACE_Framework_Repository::repository_ = 0;
32 ACE_Framework_Repository::~ACE_Framework_Repository ()
34 ACE_TRACE ("ACE_Framework_Repository::~ACE_Framework_Repository");
35 this->close ();
38 int
39 ACE_Framework_Repository::open (int size)
41 ACE_TRACE ("ACE_Framework_Repository::open");
43 ACE_Framework_Component **temp = 0;
45 #if defined (ACE_HAS_ALLOC_HOOKS)
46 ACE_ALLOCATOR_RETURN (temp,
47 static_cast<ACE_Framework_Component**> (ACE_Allocator::instance()->malloc(sizeof(ACE_Framework_Component*) * size)),
48 -1);
49 #else
50 ACE_NEW_RETURN (temp,
51 ACE_Framework_Component *[size],
52 -1);
53 #endif /* ACE_HAS_ALLOC_HOOKS */
55 this->component_vector_ = temp;
56 this->total_size_ = size;
57 return 0;
60 int
61 ACE_Framework_Repository::close ()
63 ACE_TRACE ("ACE_Framework_Repository::close");
64 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, -1);
66 this->shutting_down_ = 1;
68 if (this->component_vector_ != 0)
70 // Delete components in reverse order.
71 for (int i = this->current_size_ - 1; i >= 0; i--)
72 if (this->component_vector_[i])
74 ACE_Framework_Component *s =
75 const_cast<ACE_Framework_Component *> (
76 this->component_vector_[i]);
78 this->component_vector_[i] = 0;
79 delete s;
82 #if defined (ACE_HAS_ALLOC_HOOKS)
83 ACE_Allocator::instance()->free(this->component_vector_);
84 #else
85 delete [] this->component_vector_;
86 #endif /* ACE_HAS_ALLOC_HOOKS */
87 this->component_vector_ = 0;
88 this->current_size_ = 0;
91 ACE_DLL_Manager::close_singleton ();
92 return 0;
95 ACE_Framework_Repository *
96 ACE_Framework_Repository::instance (int size)
98 ACE_TRACE ("ACE_Framework_Repository::instance");
100 if (ACE_Framework_Repository::repository_ == 0)
102 // Perform Double-Checked Locking Optimization.
103 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
104 *ACE_Static_Object_Lock::instance (), 0));
105 if (ACE_Framework_Repository::repository_ == 0)
107 if (ACE_Object_Manager::starting_up () ||
108 !ACE_Object_Manager::shutting_down ())
110 ACE_NEW_RETURN (ACE_Framework_Repository::repository_,
111 ACE_Framework_Repository (size),
117 return ACE_Framework_Repository::repository_;
120 void
121 ACE_Framework_Repository::close_singleton ()
123 ACE_TRACE ("ACE_Framework_Repository::close_singleton");
125 ACE_MT (ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon,
126 *ACE_Static_Object_Lock::instance ()));
128 delete ACE_Framework_Repository::repository_;
129 ACE_Framework_Repository::repository_ = 0;
133 ACE_Framework_Repository::register_component (ACE_Framework_Component *fc)
135 ACE_TRACE ("ACE_Framework_Repository::register_component");
136 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, -1);
137 int i;
139 // Check to see if it's already registered
140 for (i = 0; i < this->current_size_; i++)
141 if (this->component_vector_[i] &&
142 fc->this_ == this->component_vector_[i]->this_)
144 ACELIB_ERROR_RETURN ((LM_ERROR,
145 "AFR::register_component: error, compenent already registered\n"),
146 -1);
149 if (i < this->total_size_)
151 this->component_vector_[i] = fc;
152 ++this->current_size_;
153 return 0;
156 return -1;
160 ACE_Framework_Repository::remove_component (const ACE_TCHAR *name)
162 ACE_TRACE ("ACE_Framework_Repository::remove_component");
163 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, -1);
164 int i;
166 for (i = 0; i < this->current_size_; i++)
167 if (this->component_vector_[i] &&
168 ACE_OS::strcmp (this->component_vector_[i]->name_, name) == 0)
170 delete this->component_vector_[i];
171 this->component_vector_[i] = 0;
172 this->compact ();
173 return 0;
176 return -1;
180 ACE_Framework_Repository::remove_dll_components (const ACE_TCHAR *dll_name)
182 ACE_TRACE ("ACE_Framework_Repository::remove_dll_components");
184 if (this->shutting_down_)
185 return this->remove_dll_components_i (dll_name);
186 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, -1);
188 return this->remove_dll_components_i (dll_name);
192 ACE_Framework_Repository::remove_dll_components_i (const ACE_TCHAR *dll_name)
194 ACE_TRACE ("ACE_Framework_Repository::remove_dll_components_i");
196 int i;
197 int retval = -1;
199 for (i = 0; i < this->current_size_; i++)
200 if (this->component_vector_[i] &&
201 ACE_OS::strcmp (this->component_vector_[i]->dll_name_, dll_name) == 0)
203 if (ACE::debug ())
204 ACELIB_DEBUG ((LM_DEBUG,
205 ACE_TEXT ("AFR::remove_dll_components_i (%s) ")
206 ACE_TEXT ("component \"%s\"\n"),
207 dll_name, this->component_vector_[i]->name_));
208 delete this->component_vector_[i];
209 this->component_vector_[i] = 0;
210 ++retval;
213 this->compact ();
215 return retval == -1 ? -1 : 0;
218 void
219 ACE_Framework_Repository::compact ()
221 ACE_TRACE ("ACE_Framework_Repository::compact");
223 int i;
224 int start_hole;
225 int end_hole;
229 start_hole = this->current_size_;
230 end_hole = this->current_size_;
232 // Find hole
233 for (i = 0; i < this->current_size_; ++i)
235 if (this->component_vector_[i] == 0)
237 if (start_hole == this->current_size_)
239 start_hole = i;
240 end_hole = i;
242 else
243 end_hole = i;
245 else if (end_hole != this->current_size_)
246 break;
249 if (start_hole != this->current_size_)
251 // move the contents and reset current_size_
252 while (end_hole + 1 < this->current_size_)
254 this->component_vector_[start_hole++] =
255 this->component_vector_[++end_hole];
257 // Since start_hole is now one past the last
258 // active slot.
259 this->current_size_ = start_hole;
262 } while (start_hole != this->current_size_);
265 void
266 ACE_Framework_Repository::dump () const
268 #if defined (ACE_HAS_DUMP)
269 ACE_TRACE ("ACE_Framework_Repository::dump");
270 #endif /* ACE_HAS_DUMP */
273 ACE_Framework_Repository::ACE_Framework_Repository (int size)
274 : current_size_ (0)
276 ACE_TRACE ("ACE_Framework_Repository::ACE_Framework_Repository");
278 if (this->open (size) == -1)
279 ACELIB_ERROR ((LM_ERROR,
280 ACE_TEXT ("%p\n"),
281 ACE_TEXT ("ACE_Framework_Repository")));
284 ACE_END_VERSIONED_NAMESPACE_DECL