1 // This program tests out all the various ACE_Malloc combinations and
2 // the ACE_Allocator_Adapter.
4 #include "ace/Thread.h"
5 #include "ace/Thread_Manager.h"
6 #include "ace/Malloc.h"
7 #include "ace/Signal.h"
8 #include "ace/Truncate.h"
9 #include "ace/OS_NS_stdio.h"
10 #include "ace/OS_NS_string.h"
11 #include "ace/OS_NS_sys_wait.h"
12 #include "ace/OS_NS_unistd.h"
20 #if defined (ACE_HAS_THREADS)
22 static_cast<unsigned int> (reinterpret_cast<uintptr_t> (&seed
));
25 ACE_Utils::truncate_cast
<int> (
26 ACE_OS::rand_r (&seed
) % Options::instance ()->max_msg_size ()) + 1);
29 ACE_Utils::truncate_cast
<int> (
30 ACE_OS::rand () % Options::instance ()->max_msg_size ()) + 1);
31 #endif /* ACE_HAS_THREADS */
34 // Recursively allocate and deallocate dynamic memory.
37 malloc_recurse (int count
)
39 static char default_char
= 0;
43 if (Options::instance ()->debug ())
45 // Note that you'll need to #define ACE_HAS_MALLOC_STATS in
46 // the main ACE config.h file and remake ACE to enable this.
47 ACE_MALLOC_STATS (Malloc::instance ()->print_stats ());
52 int alloc_size
= gen_size ();
53 void *ptr
= Malloc::instance ()->malloc (alloc_size
);
57 "(%P|%t) *** malloc of size %d failed, %p\n%a",
62 ACE_OS::memset (ptr
, default_char
++, alloc_size
);
64 if (Options::instance ()->debug ())
66 "(%P|%t) %u (alloc), size = %d\n",
70 // Call ourselves recursively
71 malloc_recurse (count
- 1);
73 if (Options::instance ()->debug ())
75 "(%P|%t) %u (free), size = %d\n",
78 Malloc::instance ()->free (ptr
);
85 #if defined (ACE_HAS_THREADS)
89 // Cast the arg to a long, first, because a pointer is the same
90 // size as a long on all current ACE platforms.
91 malloc_recurse (static_cast<int> (reinterpret_cast<intptr_t> (arg
)));
95 #endif /* ACE_HAS_THREADS */
97 // Create the appropriate type of process/thread.
102 if (Options::instance ()->spawn_threads ())
104 #if defined (ACE_HAS_THREADS)
105 if (ACE_Thread_Manager::instance ()->spawn (ACE_THR_FUNC (worker
),
106 (void *) Options::instance ()->iteration_count (),
108 ACE_ERROR ((LM_ERROR
, "%p\n%a", "thread create failed"));
110 if (Options::instance ()->spawn_count () > 1)
111 ACE_ERROR ((LM_ERROR
,
112 "only one thread may be run in a process on this platform\n%a",
114 #endif /* ACE_HAS_THREADS */
116 #if !defined (ACE_WIN32)
117 else if (ACE_OS::fork (ACE_TEXT_CHAR_TO_TCHAR (Options::instance ()->program_name ())) == 0)
119 if (Options::instance ()->exec_slave ())
124 ACE_OS::sprintf (iterations
, "%lu",
126 Options::instance ()->iteration_count ());
127 ACE_OS::sprintf (msg_size
, "%lu",
129 Options::instance ()->max_msg_size ());
132 if (Options::instance ()->debug ())
139 Options::instance ()->slave_name (),
149 if (ACE_OS::execv (Options::instance ()->program_name (),
150 (char *const *) argv
) == -1)
151 ACE_ERROR ((LM_ERROR
, "%p\n", "exec failed"));
157 "(%P|%t) about to recurse with iteration count = %d\n",
158 Options::instance ()->iteration_count ()));
160 malloc_recurse (Options::instance ()->iteration_count ());
161 Malloc::instance ()->remove ();
165 #endif /* ACE_WIN32 */
168 // Wait for all the child processes/threads to exit.
173 if (Options::instance ()->spawn_threads ())
175 #if defined (ACE_HAS_THREADS)
176 // Wait for the threads to terminate.
177 ACE_Thread_Manager::instance ()->wait ();
179 malloc_recurse (Options::instance ()->iteration_count ());
180 #endif /* ACE_HAS_THREADS */
182 #if !defined (ACE_WIN32)
187 while ((pid
= ACE_OS::wait (0)) != -1)
188 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) reaped pid = %d\n", pid
));
190 #endif /* ACE_WIN32 */
196 Malloc::instance ()->remove ();
197 ACE_ERROR ((LM_ERROR
, "(%P|%t) removed handler\n%a", 0));
201 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
203 // Register a signal handler.
204 ACE_Sig_Action
sa ((ACE_SignalHandler
) handler
, SIGINT
);
207 Options::instance ()->parse_args (argc
, argv
);
209 #if !defined (ACE_WIN32)
210 if (Options::instance ()->child ())
213 "(%P|%t) about to recurse with iteration count = %d, debug = %d\n",
214 Options::instance ()->iteration_count ()));
216 // We've been forked...
217 malloc_recurse (Options::instance ()->iteration_count ());
218 Malloc::instance ()->remove ();
221 #endif /* ACE_WIN32 */
224 i
< Options::instance ()->spawn_count ();
228 wait_for_children ();
229 Malloc::instance ()->remove ();