[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / dep / ACE_wrappers / ace / Malloc_Allocator.cpp
blob4da0e5f8fef19216ea3cdd908cda51faf0b8169d
1 // $Id: Malloc_Allocator.cpp 80826 2008-03-04 14:51:23Z wotte $
3 #include "ace/Malloc_Allocator.h"
4 #include "ace/Object_Manager.h"
6 #if !defined (__ACE_INLINE__)
7 #include "ace/Malloc_Allocator.inl"
8 #endif /* __ACE_INLINE__ */
10 #include "ace/Guard_T.h"
11 #include "ace/Recursive_Thread_Mutex.h"
12 #include "ace/Log_Msg.h" // for ACE_ASSERT
13 #include "ace/OS_NS_string.h"
15 ACE_RCSID (ace, Malloc_Allocator, "$Id: Malloc_Allocator.cpp 80826 2008-03-04 14:51:23Z wotte $")
17 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
19 ACE_Allocator *
20 ACE_Allocator::instance (void)
22 // ACE_TRACE ("ACE_Allocator::instance");
24 if (ACE_Allocator::allocator_ == 0)
26 // Perform Double-Checked Locking Optimization.
27 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
28 *ACE_Static_Object_Lock::instance (), 0));
30 if (ACE_Allocator::allocator_ == 0)
32 // Have a seat. We want to avoid ever having to delete the
33 // ACE_Allocator instance, to avoid shutdown order
34 // dependencies. ACE_New_Allocator never needs to be
35 // destroyed: its destructor is empty and its instance
36 // doesn't have any state. Therefore, sizeof
37 // ACE_New_Allocator is equal to sizeof void *. It's
38 // instance just contains a pointer to its virtual function
39 // table.
41 // So, we allocate space for the ACE_New_Allocator instance
42 // in the data segment. Because its size is the same as
43 // that of a pointer, we allocate it as a pointer so that it
44 // doesn't get constructed statically. We never bother to
45 // destroy it.
46 static void *allocator_instance = 0;
48 // Check this critical assumption. We put it in a variable
49 // first to avoid stupid compiler warnings that the
50 // condition may always be true/false.
51 # if !defined (ACE_NDEBUG)
52 int assertion = (sizeof allocator_instance ==
53 sizeof (ACE_New_Allocator));
54 ACE_ASSERT (assertion);
55 # endif /* !ACE_NDEBUG */
57 // Initialize the allocator_instance by using a placement
58 // new.
59 ACE_Allocator::allocator_ =
60 new (&allocator_instance) ACE_New_Allocator;
64 return ACE_Allocator::allocator_;
67 ACE_Allocator *
68 ACE_Allocator::instance (ACE_Allocator *r)
70 ACE_TRACE ("ACE_Allocator::instance");
71 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
72 *ACE_Static_Object_Lock::instance (), 0));
73 ACE_Allocator *t = ACE_Allocator::allocator_;
75 // We can't safely delete it since we don't know who created it!
76 ACE_Allocator::delete_allocator_ = 0;
78 ACE_Allocator::allocator_ = r;
79 return t;
82 void
83 ACE_Allocator::close_singleton (void)
85 ACE_TRACE ("ACE_Allocator::close_singleton");
87 ACE_MT (ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon,
88 *ACE_Static_Object_Lock::instance ()));
90 if (ACE_Allocator::delete_allocator_)
92 // This should never be executed.... See the
93 // ACE_Allocator::instance (void) method for an explanation.
94 delete ACE_Allocator::allocator_;
95 ACE_Allocator::allocator_ = 0;
96 ACE_Allocator::delete_allocator_ = 0;
100 ACE_Allocator::~ACE_Allocator (void)
102 ACE_TRACE ("ACE_Allocator::~ACE_Allocator");
105 ACE_Allocator::ACE_Allocator (void)
107 ACE_TRACE ("ACE_Allocator::ACE_Allocator");
110 /******************************************************************************/
112 void *
113 ACE_New_Allocator::malloc (size_t nbytes)
115 char *ptr = 0;
117 if (nbytes > 0)
118 ACE_NEW_RETURN (ptr, char[nbytes], 0);
119 return (void *) ptr;
122 void *
123 ACE_New_Allocator::calloc (size_t nbytes,
124 char initial_value)
126 char *ptr = 0;
128 ACE_NEW_RETURN (ptr, char[nbytes], 0);
130 ACE_OS::memset (ptr, initial_value, nbytes);
131 return (void *) ptr;
134 void *
135 ACE_New_Allocator::calloc (size_t n_elem, size_t elem_size, char initial_value)
137 return ACE_New_Allocator::calloc (n_elem * elem_size, initial_value);
140 void
141 ACE_New_Allocator::free (void *ptr)
143 delete [] (char *) ptr;
147 ACE_New_Allocator::remove (void)
149 ACE_NOTSUP_RETURN (-1);
153 ACE_New_Allocator::bind (const char *, void *, int)
155 ACE_NOTSUP_RETURN (-1);
159 ACE_New_Allocator::trybind (const char *, void *&)
161 ACE_NOTSUP_RETURN (-1);
165 ACE_New_Allocator::find (const char *, void *&)
167 ACE_NOTSUP_RETURN (-1);
171 ACE_New_Allocator::find (const char *)
173 ACE_NOTSUP_RETURN (-1);
177 ACE_New_Allocator::unbind (const char *)
179 ACE_NOTSUP_RETURN (-1);
183 ACE_New_Allocator::unbind (const char *, void *&)
185 ACE_NOTSUP_RETURN (-1);
189 ACE_New_Allocator::sync (ssize_t, int)
191 ACE_NOTSUP_RETURN (-1);
195 ACE_New_Allocator::sync (void *, size_t, int)
197 ACE_NOTSUP_RETURN (-1);
201 ACE_New_Allocator::protect (ssize_t, int)
203 ACE_NOTSUP_RETURN (-1);
207 ACE_New_Allocator::protect (void *, size_t, int)
209 ACE_NOTSUP_RETURN (-1);
212 #if defined (ACE_HAS_MALLOC_STATS)
213 void
214 ACE_New_Allocator::print_stats (void) const
217 #endif /* ACE_HAS_MALLOC_STATS */
219 void
220 ACE_New_Allocator::dump (void) const
222 #if defined (ACE_HAS_DUMP)
223 #endif /* ACE_HAS_DUMP */
226 /******************************************************************************/
228 void *
229 ACE_Static_Allocator_Base::malloc (size_t nbytes)
231 if (this->offset_ + nbytes > this->size_)
233 errno = ENOMEM;
234 return 0;
236 else
238 // Record the current offset, increment the offset by the number
239 // of bytes requested, and return the original offset.
240 char *ptr = &this->buffer_[this->offset_];
241 this->offset_ += nbytes;
242 return (void *) ptr;
246 void *
247 ACE_Static_Allocator_Base::calloc (size_t nbytes,
248 char initial_value)
250 void *ptr = this->malloc (nbytes);
252 ACE_OS::memset (ptr, initial_value, nbytes);
253 return (void *) ptr;
256 void *
257 ACE_Static_Allocator_Base::calloc (size_t n_elem,
258 size_t elem_size,
259 char initial_value)
261 return this->calloc (n_elem * elem_size, initial_value);
264 void
265 ACE_Static_Allocator_Base::free (void *ptr)
267 // Check to see if ptr is within our pool?!
268 ACE_UNUSED_ARG (ptr);
269 ACE_ASSERT (ptr >= this->buffer_ && ptr < this->buffer_ + this->size_);
273 ACE_Static_Allocator_Base::remove (void)
275 return -1;
279 ACE_Static_Allocator_Base::bind (const char *, void *, int)
281 return -1;
285 ACE_Static_Allocator_Base::trybind (const char *, void *&)
287 return -1;
291 ACE_Static_Allocator_Base::find (const char *, void *&)
293 return -1;
297 ACE_Static_Allocator_Base::find (const char *)
299 return -1;
303 ACE_Static_Allocator_Base::unbind (const char *)
305 return -1;
309 ACE_Static_Allocator_Base::unbind (const char *, void *&)
311 return -1;
315 ACE_Static_Allocator_Base::sync (ssize_t, int)
317 return -1;
321 ACE_Static_Allocator_Base::sync (void *, size_t, int)
323 return -1;
327 ACE_Static_Allocator_Base::protect (ssize_t, int)
329 return -1;
333 ACE_Static_Allocator_Base::protect (void *, size_t, int)
335 return -1;
338 #if defined (ACE_HAS_MALLOC_STATS)
339 void
340 ACE_Static_Allocator_Base::print_stats (void) const
343 #endif /* ACE_HAS_MALLOC_STATS */
345 void
346 ACE_Static_Allocator_Base::dump (void) const
348 #if defined (ACE_HAS_DUMP)
349 ACE_TRACE ("ACE_Static_Allocator_Base::dump");
351 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
352 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\noffset_ = %d"), this->offset_));
353 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nsize_ = %d\n"), this->size_));
354 ACE_HEX_DUMP ((LM_DEBUG, this->buffer_, this->size_));
355 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
357 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
358 #endif /* ACE_HAS_DUMP */
361 ACE_END_VERSIONED_NAMESPACE_DECL