Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / Malloc_Allocator.cpp
blobd9ea403635591a27c55055b0326af01124d9b18c
1 #include "ace/Malloc_Allocator.h"
2 #include "ace/Object_Manager.h"
4 #if !defined (__ACE_INLINE__)
5 #include "ace/Malloc_Allocator.inl"
6 #endif /* __ACE_INLINE__ */
8 #include "ace/Guard_T.h"
9 #include "ace/Recursive_Thread_Mutex.h"
10 #include "ace/Log_Category.h" // for ACE_ASSERT
11 #include "ace/OS_NS_string.h"
13 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
15 ACE_Allocator *
16 ACE_Allocator::instance ()
18 // ACE_TRACE ("ACE_Allocator::instance");
20 if (ACE_Allocator::allocator_ == 0)
22 // Perform Double-Checked Locking Optimization.
23 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
24 *ACE_Static_Object_Lock::instance (), 0));
26 if (ACE_Allocator::allocator_ == 0)
28 // Have a seat. We want to avoid ever having to delete the
29 // ACE_Allocator instance, to avoid shutdown order
30 // dependencies. ACE_New_Allocator never needs to be
31 // destroyed: its destructor is empty and its instance
32 // doesn't have any state. Therefore, sizeof
33 // ACE_New_Allocator is equal to sizeof void *. It's
34 // instance just contains a pointer to its virtual function
35 // table.
37 // So, we allocate space for the ACE_New_Allocator instance
38 // in the data segment. Because its size is the same as
39 // that of a pointer, we allocate it as a pointer so that it
40 // doesn't get constructed statically. We never bother to
41 // destroy it.
42 static void *allocator_instance = 0;
44 // Check this critical assumption. We put it in a variable
45 // first to avoid stupid compiler warnings that the
46 // condition may always be true/false.
47 # if !defined (ACE_NDEBUG)
48 int assertion = (sizeof allocator_instance ==
49 sizeof (ACE_New_Allocator));
50 ACE_ASSERT (assertion);
51 # endif /* !ACE_NDEBUG */
53 // Initialize the allocator_instance by using a placement
54 // new.
55 ACE_Allocator::allocator_ =
56 new (&allocator_instance) ACE_New_Allocator;
60 return ACE_Allocator::allocator_;
63 ACE_Allocator *
64 ACE_Allocator::instance (ACE_Allocator *r)
66 ACE_TRACE ("ACE_Allocator::instance");
67 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
68 *ACE_Static_Object_Lock::instance (), 0));
69 ACE_Allocator *t = ACE_Allocator::allocator_;
71 ACE_Allocator::allocator_ = r;
73 return t;
76 void
77 ACE_Allocator::close_singleton ()
79 ACE_TRACE ("ACE_Allocator::close_singleton");
82 ACE_Allocator::~ACE_Allocator ()
84 ACE_TRACE ("ACE_Allocator::~ACE_Allocator");
87 ACE_Allocator::ACE_Allocator ()
89 ACE_TRACE ("ACE_Allocator::ACE_Allocator");
92 /******************************************************************************/
94 void *
95 ACE_New_Allocator::malloc (size_t nbytes)
97 char *ptr = 0;
98 if (nbytes > 0)
99 ACE_NEW_RETURN (ptr, char[nbytes], 0);
100 return (void *) ptr;
103 void *
104 ACE_New_Allocator::calloc (size_t nbytes,
105 char initial_value)
107 char *ptr = 0;
109 ACE_NEW_RETURN (ptr, char[nbytes], 0);
111 ACE_OS::memset (ptr, initial_value, nbytes);
112 return (void *) ptr;
115 void *
116 ACE_New_Allocator::calloc (size_t n_elem, size_t elem_size, char initial_value)
118 return ACE_New_Allocator::calloc (n_elem * elem_size, initial_value);
121 void
122 ACE_New_Allocator::free (void *ptr)
124 #ifdef ACE_FACE_SAFETY_BASE
125 ACE_UNUSED_ARG (ptr);
126 #else
127 delete [] (char *) ptr;
128 #endif
132 ACE_New_Allocator::remove ()
134 ACE_NOTSUP_RETURN (-1);
138 ACE_New_Allocator::bind (const char *, void *, int)
140 ACE_NOTSUP_RETURN (-1);
144 ACE_New_Allocator::trybind (const char *, void *&)
146 ACE_NOTSUP_RETURN (-1);
150 ACE_New_Allocator::find (const char *, void *&)
152 ACE_NOTSUP_RETURN (-1);
156 ACE_New_Allocator::find (const char *)
158 ACE_NOTSUP_RETURN (-1);
162 ACE_New_Allocator::unbind (const char *)
164 ACE_NOTSUP_RETURN (-1);
168 ACE_New_Allocator::unbind (const char *, void *&)
170 ACE_NOTSUP_RETURN (-1);
174 ACE_New_Allocator::sync (ssize_t, int)
176 ACE_NOTSUP_RETURN (-1);
180 ACE_New_Allocator::sync (void *, size_t, int)
182 ACE_NOTSUP_RETURN (-1);
186 ACE_New_Allocator::protect (ssize_t, int)
188 ACE_NOTSUP_RETURN (-1);
192 ACE_New_Allocator::protect (void *, size_t, int)
194 ACE_NOTSUP_RETURN (-1);
197 #if defined (ACE_HAS_MALLOC_STATS)
198 void
199 ACE_New_Allocator::print_stats () const
202 #endif /* ACE_HAS_MALLOC_STATS */
204 void
205 ACE_New_Allocator::dump () const
207 #if defined (ACE_HAS_DUMP)
208 #endif /* ACE_HAS_DUMP */
211 /******************************************************************************/
213 void *
214 ACE_Static_Allocator_Base::malloc (size_t nbytes)
216 if (this->offset_ + nbytes > this->size_)
218 errno = ENOMEM;
219 return 0;
221 else
223 // Record the current offset, increment the offset by the number
224 // of bytes requested, and return the original offset.
225 char *ptr = &this->buffer_[this->offset_];
226 this->offset_ += nbytes;
227 return (void *) ptr;
231 void *
232 ACE_Static_Allocator_Base::calloc (size_t nbytes,
233 char initial_value)
235 void *ptr = this->malloc (nbytes);
237 ACE_OS::memset (ptr, initial_value, nbytes);
238 return (void *) ptr;
241 void *
242 ACE_Static_Allocator_Base::calloc (size_t n_elem,
243 size_t elem_size,
244 char initial_value)
246 return this->calloc (n_elem * elem_size, initial_value);
249 void
250 ACE_Static_Allocator_Base::free (void *ptr)
252 // Check to see if ptr is within our pool?!
253 ACE_UNUSED_ARG (ptr);
254 ACE_ASSERT (ptr >= this->buffer_ && ptr < this->buffer_ + this->size_);
258 ACE_Static_Allocator_Base::remove ()
260 return -1;
264 ACE_Static_Allocator_Base::bind (const char *, void *, int)
266 return -1;
270 ACE_Static_Allocator_Base::trybind (const char *, void *&)
272 return -1;
276 ACE_Static_Allocator_Base::find (const char *, void *&)
278 return -1;
282 ACE_Static_Allocator_Base::find (const char *)
284 return -1;
288 ACE_Static_Allocator_Base::unbind (const char *)
290 return -1;
294 ACE_Static_Allocator_Base::unbind (const char *, void *&)
296 return -1;
300 ACE_Static_Allocator_Base::sync (ssize_t, int)
302 return -1;
306 ACE_Static_Allocator_Base::sync (void *, size_t, int)
308 return -1;
312 ACE_Static_Allocator_Base::protect (ssize_t, int)
314 return -1;
318 ACE_Static_Allocator_Base::protect (void *, size_t, int)
320 return -1;
323 #if defined (ACE_HAS_MALLOC_STATS)
324 void
325 ACE_Static_Allocator_Base::print_stats () const
328 #endif /* ACE_HAS_MALLOC_STATS */
330 void
331 ACE_Static_Allocator_Base::dump () const
333 #if defined (ACE_HAS_DUMP)
334 ACE_TRACE ("ACE_Static_Allocator_Base::dump");
336 ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
337 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\noffset_ = %d"), this->offset_));
338 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\nsize_ = %d\n"), this->size_));
339 ACELIB_HEX_DUMP ((LM_DEBUG, this->buffer_, this->size_));
340 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
342 ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
343 #endif /* ACE_HAS_DUMP */
346 ACE_END_VERSIONED_NAMESPACE_DECL