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
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
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
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
59 ACE_Allocator::allocator_
=
60 new (&allocator_instance
) ACE_New_Allocator
;
64 return ACE_Allocator::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
;
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 /******************************************************************************/
113 ACE_New_Allocator::malloc (size_t nbytes
)
118 ACE_NEW_RETURN (ptr
, char[nbytes
], 0);
123 ACE_New_Allocator::calloc (size_t nbytes
,
128 ACE_NEW_RETURN (ptr
, char[nbytes
], 0);
130 ACE_OS::memset (ptr
, initial_value
, nbytes
);
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
);
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)
214 ACE_New_Allocator::print_stats (void) const
217 #endif /* ACE_HAS_MALLOC_STATS */
220 ACE_New_Allocator::dump (void) const
222 #if defined (ACE_HAS_DUMP)
223 #endif /* ACE_HAS_DUMP */
226 /******************************************************************************/
229 ACE_Static_Allocator_Base::malloc (size_t nbytes
)
231 if (this->offset_
+ nbytes
> this->size_
)
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
;
247 ACE_Static_Allocator_Base::calloc (size_t nbytes
,
250 void *ptr
= this->malloc (nbytes
);
252 ACE_OS::memset (ptr
, initial_value
, nbytes
);
257 ACE_Static_Allocator_Base::calloc (size_t n_elem
,
261 return this->calloc (n_elem
* elem_size
, initial_value
);
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)
279 ACE_Static_Allocator_Base::bind (const char *, void *, int)
285 ACE_Static_Allocator_Base::trybind (const char *, void *&)
291 ACE_Static_Allocator_Base::find (const char *, void *&)
297 ACE_Static_Allocator_Base::find (const char *)
303 ACE_Static_Allocator_Base::unbind (const char *)
309 ACE_Static_Allocator_Base::unbind (const char *, void *&)
315 ACE_Static_Allocator_Base::sync (ssize_t
, int)
321 ACE_Static_Allocator_Base::sync (void *, size_t, int)
327 ACE_Static_Allocator_Base::protect (ssize_t
, int)
333 ACE_Static_Allocator_Base::protect (void *, size_t, int)
338 #if defined (ACE_HAS_MALLOC_STATS)
340 ACE_Static_Allocator_Base::print_stats (void) const
343 #endif /* ACE_HAS_MALLOC_STATS */
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