Use =default for skeleton copy constructor
[ACE_TAO.git] / ACE / tests / Cached_Allocator_Test.cpp
blob0012f88c57f39fac74fa405169808c815c79d840
2 //=============================================================================
3 /**
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>
9 */
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>;
24 static int
25 speed_test (ACE_UINT32 loops)
27 double tt = 0.0,
28 ut = 0.0,
29 utus = 0.0,
30 speed = 0.0;
32 ACE_Time_Value tc;
33 void *ptr = 0;
34 ACE_UINT32 i = loops;
35 size_t n_chunks = 10;
36 size_t chunk_size = 8;
38 ACE_DEBUG ((LM_INFO,
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;
45 timer.reset ();
47 timer.start ();
49 while (i--)
51 ptr = allocator.malloc (chunk_size);
52 allocator.free (ptr);
55 timer.stop ();
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;
61 ut = tt/loops;
62 utus = ut*1.0e6;
63 speed = loops/tt;
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));
69 return 0;
72 typedef char MEMBLOCK[8];
73 using STATIC_ALLOCATOR = ACE_Cached_Allocator<MEMBLOCK, ACE_MT_SYNCH::NULL_MUTEX>;
75 static int
76 stdspeed_test (ACE_UINT32 loops)
78 double tt = 0.0,
79 ut = 0.0,
80 utus = 0.0,
81 speed = 0.0;
83 ACE_Time_Value tc;
84 void *ptr = 0;
85 ACE_UINT32 i = loops;
86 size_t n_chunks = 10,
87 chunk_size = 8;
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;
95 timer.reset ();
97 timer.start ();
98 while (i--)
100 ptr = allocator.malloc (chunk_size);
101 allocator.free (ptr);
103 timer.stop ();
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;
109 ut = tt/loops;
110 utus = ut*1.0e6;
111 speed = loops/tt;
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));
117 return 0;
121 run_main (int argc, ACE_TCHAR *argv[])
123 ACE_START_TEST (ACE_TEXT ("Cached_Allocator_Test"));
125 size_t chunk_size = 0;
126 size_t n_chunks = 0;
127 size_t requested_size = 0;
128 size_t depth = 0;
129 char *ptr1 = 0;
130 char *ptr2 = 0;
131 char *ptr3 = 0;
132 char *ptr4 = 0;
133 ACE_UINT32 loops = 0;
135 const char *str1 = "12345678";
136 const char *str3 = "ABCDEFGH";
138 if (argc < 2)
139 loops = 10000000;
140 else
141 loops = ACE_OS::atoi (argv[1]);
143 n_chunks = 2;
145 for (chunk_size = 1; chunk_size <= 9; ++chunk_size) // 9 is strlen(str1 or str2) + 1
147 ACE_DEBUG ((LM_INFO,
148 ACE_TEXT (" (%t) Creating allocator: ")
149 ACE_TEXT ("%B chunks, %B bytes each\n"),
150 n_chunks,
151 chunk_size));
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"),
158 n_chunks, depth));
159 requested_size = chunk_size;
160 ACE_DEBUG ((LM_INFO,
161 ACE_TEXT (" (%t) Allocating chunk 1: %B bytes, should succeed...\n"),
162 requested_size));
164 ptr1 = (char *) allocator.malloc (requested_size);
165 if (!ptr1)
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;
175 ACE_DEBUG ((LM_INFO,
176 ACE_TEXT (" (%t) Allocating chunk 2: %B bytes, too big, should fail...\n"),
177 requested_size));
179 ptr2 = (char *) allocator.malloc (requested_size);
180 if (!ptr2)
181 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) OK, failed.\n")));
182 else
183 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT (" (%t) Something is wrong...\n")), -1);
185 requested_size = chunk_size - 1;
186 ACE_DEBUG ((LM_INFO,
187 ACE_TEXT (" (%t) Allocating chunk 3: %B bytes, ")
188 ACE_TEXT ("should succeed...\n"),
189 requested_size));
190 ptr3 = (char *) allocator.malloc (requested_size);
191 if (!ptr3)
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"),
200 depth));
201 requested_size = chunk_size;
202 ACE_DEBUG ((LM_INFO,
203 ACE_TEXT (" (%t) Allocating chunk 4: %B bytes, no free chunks,")
204 ACE_TEXT (" should fail...\n"),
205 requested_size));
207 ptr4 = (char *) allocator.malloc (requested_size);
208 if (!ptr4)
209 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) OK, failed.\n")));
210 else
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"),
229 requested_size));
230 ptr1 = (char *) allocator.malloc (requested_size);
231 if (!ptr1)
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);
242 speed_test (loops);
243 stdspeed_test (loops);
245 ACE_END_TEST;
246 return 0;