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
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
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
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
55 ACE_Allocator::allocator_
=
56 new (&allocator_instance
) ACE_New_Allocator
;
60 return ACE_Allocator::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
;
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 /******************************************************************************/
95 ACE_New_Allocator::malloc (size_t nbytes
)
99 ACE_NEW_RETURN (ptr
, char[nbytes
], 0);
104 ACE_New_Allocator::calloc (size_t nbytes
,
109 ACE_NEW_RETURN (ptr
, char[nbytes
], 0);
111 ACE_OS::memset (ptr
, initial_value
, nbytes
);
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
);
122 ACE_New_Allocator::free (void *ptr
)
124 #ifdef ACE_FACE_SAFETY_BASE
125 ACE_UNUSED_ARG (ptr
);
127 delete [] (char *) ptr
;
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)
199 ACE_New_Allocator::print_stats () const
202 #endif /* ACE_HAS_MALLOC_STATS */
205 ACE_New_Allocator::dump () const
207 #if defined (ACE_HAS_DUMP)
208 #endif /* ACE_HAS_DUMP */
211 /******************************************************************************/
214 ACE_Static_Allocator_Base::malloc (size_t nbytes
)
216 if (this->offset_
+ nbytes
> this->size_
)
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
;
232 ACE_Static_Allocator_Base::calloc (size_t nbytes
,
235 void *ptr
= this->malloc (nbytes
);
237 ACE_OS::memset (ptr
, initial_value
, nbytes
);
242 ACE_Static_Allocator_Base::calloc (size_t n_elem
,
246 return this->calloc (n_elem
* elem_size
, initial_value
);
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 ()
264 ACE_Static_Allocator_Base::bind (const char *, void *, int)
270 ACE_Static_Allocator_Base::trybind (const char *, void *&)
276 ACE_Static_Allocator_Base::find (const char *, void *&)
282 ACE_Static_Allocator_Base::find (const char *)
288 ACE_Static_Allocator_Base::unbind (const char *)
294 ACE_Static_Allocator_Base::unbind (const char *, void *&)
300 ACE_Static_Allocator_Base::sync (ssize_t
, int)
306 ACE_Static_Allocator_Base::sync (void *, size_t, int)
312 ACE_Static_Allocator_Base::protect (ssize_t
, int)
318 ACE_Static_Allocator_Base::protect (void *, size_t, int)
323 #if defined (ACE_HAS_MALLOC_STATS)
325 ACE_Static_Allocator_Base::print_stats () const
328 #endif /* ACE_HAS_MALLOC_STATS */
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