2 //=============================================================================
4 * @file Cached_Allocator_Test.cpp
6 * Simple test of ACE_Dynamic_Cached_Allocator and ACE_Cached_Allocator.
8 * @author Jaroslaw Nozderko <jareknz@polbox.com>
10 //=============================================================================
13 #include "test_config.h"
14 #include "ace/OS_NS_string.h"
15 #include "ace/Malloc_T.h"
16 #include "ace/High_Res_Timer.h"
19 #include "ace/Synch_Traits.h"
20 #include "ace/Null_Mutex.h"
22 using DYNAMIC_ALLOCATOR
= ACE_Dynamic_Cached_Allocator
<ACE_MT_SYNCH::NULL_MUTEX
>;
25 speed_test (ACE_UINT32 loops
)
36 size_t chunk_size
= 8;
39 ACE_TEXT (" (%t) ACE_Dynamic_Cached_Allocator ")
40 ACE_TEXT ("speed test...\n")));
42 DYNAMIC_ALLOCATOR
allocator (n_chunks
, chunk_size
);
44 ACE_High_Res_Timer timer
;
51 ptr
= allocator
.malloc (chunk_size
);
57 timer
.elapsed_time (tc
);
59 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) Iterations : %d\n"), loops
));
60 tt
= tc
.sec () + tc
.usec ()*1.0e-6;
65 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) Total time : %.6g [s]\n"), tt
));
66 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) Unit time : %.6g [us]\n"), utus
));
67 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) Speed : %.6g [1/s]\n"), speed
));
72 typedef char MEMBLOCK
[8];
73 using STATIC_ALLOCATOR
= ACE_Cached_Allocator
<MEMBLOCK
, ACE_MT_SYNCH::NULL_MUTEX
>;
76 stdspeed_test (ACE_UINT32 loops
)
89 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) ACE_Cached_Allocator ")
90 ACE_TEXT ("speed test...\n")));
92 STATIC_ALLOCATOR
allocator (n_chunks
);
94 ACE_High_Res_Timer timer
;
100 ptr
= allocator
.malloc (chunk_size
);
101 allocator
.free (ptr
);
105 timer
.elapsed_time (tc
);
107 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) Iterations : %d\n"), loops
));
108 tt
= tc
.sec () + tc
.usec ()*1.0e-6;
113 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) Total time : %.6g [s]\n"), tt
));
114 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) Unit time : %.6g [us]\n"), utus
));
115 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) Speed : %.6g [1/s]\n"), speed
));
121 run_main (int argc
, ACE_TCHAR
*argv
[])
123 ACE_START_TEST (ACE_TEXT ("Cached_Allocator_Test"));
125 size_t chunk_size
= 0;
127 size_t requested_size
= 0;
133 ACE_UINT32 loops
= 0;
135 const char *str1
= "12345678";
136 const char *str3
= "ABCDEFGH";
141 loops
= ACE_OS::atoi (argv
[1]);
145 for (chunk_size
= 1; chunk_size
<= 9; ++chunk_size
) // 9 is strlen(str1 or str2) + 1
148 ACE_TEXT (" (%t) Creating allocator: ")
149 ACE_TEXT ("%B chunks, %B bytes each\n"),
153 DYNAMIC_ALLOCATOR
allocator (n_chunks
, chunk_size
);
155 if ((depth
= allocator
.pool_depth ()) != n_chunks
)
156 ACE_ERROR ((LM_ERROR
,
157 ACE_TEXT ("Expected pool depth %B but reported %B\n"),
159 requested_size
= chunk_size
;
161 ACE_TEXT (" (%t) Allocating chunk 1: %B bytes, should succeed...\n"),
164 ptr1
= (char *) allocator
.malloc (requested_size
);
166 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT (" (%t) Failed, exiting.\n")), -1);
168 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) OK, succeeded.\n")));
169 if ((depth
= allocator
.pool_depth ()) != (n_chunks
- 1))
170 ACE_ERROR ((LM_ERROR
,
171 ACE_TEXT ("Expected pool depth %B but reported %B\n"),
172 n_chunks
- 1, depth
));
174 requested_size
= chunk_size
+ 1;
176 ACE_TEXT (" (%t) Allocating chunk 2: %B bytes, too big, should fail...\n"),
179 ptr2
= (char *) allocator
.malloc (requested_size
);
181 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) OK, failed.\n")));
183 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT (" (%t) Something is wrong...\n")), -1);
185 requested_size
= chunk_size
- 1;
187 ACE_TEXT (" (%t) Allocating chunk 3: %B bytes, ")
188 ACE_TEXT ("should succeed...\n"),
190 ptr3
= (char *) allocator
.malloc (requested_size
);
192 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT (" (%t) Failed, exiting.\n")), -1);
194 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) OK, succeeded.\n")));
196 // One chunk too far...
197 if ((depth
= allocator
.pool_depth ()) != 0)
198 ACE_ERROR ((LM_ERROR
,
199 ACE_TEXT ("Expected pool depth 0 but reported %B\n"),
201 requested_size
= chunk_size
;
203 ACE_TEXT (" (%t) Allocating chunk 4: %B bytes, no free chunks,")
204 ACE_TEXT (" should fail...\n"),
207 ptr4
= (char *) allocator
.malloc (requested_size
);
209 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) OK, failed.\n")));
211 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT (" (%t) Something is wrong\n")), -1);
213 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) Writing to chunk 1: %C\n"), str1
));
214 ACE_OS::memcpy (ptr1
, str1
, chunk_size
);
215 ptr1
[chunk_size
- 1] = '\0';
216 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) Reading from chunk 1: %C\n"), ptr1
));
218 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) Writing to chunk 3: %C\n"), str3
));
219 ACE_OS::memcpy (ptr3
, str3
, chunk_size
);
220 ptr3
[chunk_size
- 1] = '\0';
221 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) Reading from chunk 3: %C\n"), ptr3
));
223 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) Deallocating chunk 1\n")));
224 allocator
.free (ptr1
);
226 requested_size
= chunk_size
;
227 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) Allocating chunk: %B bytes, ")
228 ACE_TEXT ("should succeed...\n"),
230 ptr1
= (char *) allocator
.malloc (requested_size
);
232 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT (" (%t) Failed, exiting.\n")), -1);
234 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) OK, succeeded.\n")));
236 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) Deallocating chunk 1\n")));
237 allocator
.free (ptr1
);
238 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) Deallocating chunk 3\n")));
239 allocator
.free (ptr3
);
243 stdspeed_test (loops
);