|
0
|
1 //================================================================= |
|
|
2 // |
|
|
3 // malloc2.c |
|
|
4 // |
|
|
5 // Stress testcase for C library malloc(), calloc() and free() |
|
|
6 // |
|
|
7 //================================================================= |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
|
13 // compliance with the License. You may obtain a copy of the License at |
|
|
14 // http://sourceware.cygnus.com/ecos |
|
|
15 // |
|
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
18 // License for the specific language governing rights and limitations under |
|
|
19 // the License. |
|
|
20 // |
|
|
21 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
2
|
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //================================================================= |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
2
|
32 // Author(s): jlarmour |
|
|
33 // Contributors: jlarmour |
|
0
|
34 // Date: 1998/6/3 |
|
|
35 // Description: Contains testcode to stress-test C library malloc(), |
|
|
36 // calloc() and free() functions |
|
|
37 // |
|
|
38 // |
|
|
39 //####DESCRIPTIONEND#### |
|
|
40 |
|
|
41 // Declarations for test system: |
|
|
42 // |
|
|
43 // TESTCASE_TYPE=CYG_TEST_MODULE |
|
|
44 // CYG_TEST_HEARTBEAT=120 |
|
|
45 |
|
|
46 // INCLUDES |
|
|
47 |
|
2
|
48 #include <pkgconf/system.h> // Overall system configuration |
|
|
49 #include <pkgconf/libc.h> // config header for C library so we can know |
|
|
50 // size of malloc pool |
|
0
|
51 #include <stdlib.h> |
|
|
52 #include <cyg/infra/testcase.h> |
|
|
53 #include <sys/cstartup.h> // C library initialisation |
|
2
|
54 #ifdef CYGPKG_KERNEL |
|
|
55 # include <pkgconf/kernel.h> // CYGSEM_KERNEL_MEMORY_COALESCE |
|
|
56 #endif |
|
0
|
57 |
|
|
58 // CONSTANTS |
|
|
59 |
|
|
60 #define NUM_ITERATIONS 1000 |
|
|
61 |
|
|
62 // GLOBALS |
|
|
63 |
|
2
|
64 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && \ |
|
|
65 defined(CYGSEM_KERNEL_MEMORY_COALESCE) |
|
0
|
66 static int problem=0; |
|
|
67 #endif |
|
|
68 |
|
|
69 // FUNCTIONS |
|
|
70 |
|
|
71 |
|
|
72 externC void |
|
|
73 cyg_package_start( void ) |
|
|
74 { |
|
|
75 #ifdef CYGPKG_LIBC |
|
|
76 cyg_iso_c_start(); |
|
|
77 #else |
|
|
78 (void)main(0, NULL); |
|
|
79 #endif |
|
|
80 } // cyg_package_start() |
|
|
81 |
|
|
82 |
|
2
|
83 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && \ |
|
|
84 defined(CYGSEM_KERNEL_MEMORY_COALESCE) |
|
0
|
85 |
|
|
86 static void * |
|
|
87 safe_malloc( size_t size ) |
|
|
88 { |
|
|
89 void *ptr; |
|
|
90 |
|
|
91 ptr=malloc(size); |
|
|
92 |
|
|
93 if (ptr==NULL) |
|
|
94 { |
|
|
95 CYG_TEST_FAIL( "malloc returned NULL! " |
|
|
96 "Perhaps the allocator doesn't coalesce?" ); |
|
|
97 problem++; |
|
|
98 } // if |
|
|
99 |
|
|
100 return ptr; |
|
|
101 } // safe_malloc() |
|
|
102 |
|
|
103 |
|
|
104 static void * |
|
|
105 safe_calloc( size_t size ) |
|
|
106 { |
|
|
107 void *ptr; |
|
|
108 int i; |
|
|
109 |
|
|
110 ptr=calloc(size,1); |
|
|
111 |
|
|
112 if (ptr==NULL) |
|
|
113 { |
|
|
114 CYG_TEST_FAIL( "calloc returned NULL! " |
|
|
115 "Perhaps the allocator doesn't coalesce" ); |
|
|
116 problem++; |
|
|
117 } // if |
|
|
118 else |
|
|
119 { |
|
|
120 for (i=0; i < size; i++) |
|
|
121 { |
|
|
122 if (((char *)ptr)[i] != 0) |
|
|
123 { |
|
|
124 CYG_TEST_FAIL("calloc didn't clear data completely"); |
|
|
125 problem++; |
|
|
126 return ptr; |
|
|
127 } // if |
|
|
128 } // for |
|
|
129 } // else |
|
|
130 |
|
|
131 return ptr; |
|
|
132 } // safe_calloc() |
|
|
133 |
|
|
134 |
|
|
135 static void |
|
|
136 fill_with_data( char *buf, int size ) |
|
|
137 { |
|
|
138 int i; |
|
|
139 |
|
|
140 for (i=0; i < size; i++) |
|
|
141 buf[i] = 'f'; |
|
|
142 |
|
|
143 for (i=0; i < size; i++) |
|
|
144 if (buf[i] != 'f') |
|
|
145 { |
|
|
146 CYG_TEST_FAIL( "data written to buffer does not compare " |
|
|
147 "correctly! #1" ); |
|
|
148 problem++; |
|
|
149 return; |
|
|
150 } // if |
|
|
151 |
|
|
152 |
|
|
153 for (i=0; i < size; i++) |
|
|
154 buf[i] = 'z'; |
|
|
155 |
|
|
156 for (i=0; i < size; i++) |
|
|
157 if (buf[i] != 'z') |
|
|
158 { |
|
|
159 CYG_TEST_FAIL( "data written to buffer does not compare " |
|
|
160 "correctly! #2" ); |
|
|
161 problem++; |
|
|
162 return; |
|
|
163 } // if |
|
|
164 |
|
|
165 } // fill_with_data() |
|
|
166 |
|
2
|
167 #endif // if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && |
|
|
168 // defined(CYGSEM_KERNEL_MEMORY_COALESCE) |
|
0
|
169 |
|
|
170 int |
|
|
171 main( int argc, char *argv[] ) |
|
|
172 { |
|
2
|
173 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && \ |
|
|
174 defined(CYGSEM_KERNEL_MEMORY_COALESCE) |
|
|
175 |
|
0
|
176 char *str1, *str2, *str3; |
|
|
177 int j; |
|
|
178 #endif |
|
|
179 |
|
|
180 CYG_TEST_INIT(); |
|
|
181 |
|
|
182 CYG_TEST_INFO("Starting stress tests from testcase " __FILE__ " for C " |
|
|
183 "library malloc(), calloc() and free() functions"); |
|
|
184 |
|
2
|
185 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && \ |
|
|
186 defined(CYGSEM_KERNEL_MEMORY_COALESCE) |
|
0
|
187 |
|
|
188 if ( CYGNUM_LIBC_MALLOC_MEMPOOL_SIZE < 300 ) |
|
|
189 { |
|
|
190 CYG_TEST_FAIL_FINISH("This testcase cannot safely be used with a " |
|
|
191 "memory pool for malloc less than 300 bytes"); |
|
|
192 } // if |
|
|
193 |
|
|
194 |
|
|
195 for ( j=1; j < NUM_ITERATIONS; j++) |
|
|
196 { |
|
|
197 // if ((j % 100) == 0) |
|
|
198 // CYG_TEST_STILL_ALIVE( j, "Multiple mallocs and frees continuing" ); |
|
|
199 |
|
|
200 |
|
|
201 str1 = (char *)safe_malloc(50); |
|
|
202 fill_with_data( str1, 50 ); |
|
|
203 str2 = (char *)safe_calloc(11); |
|
|
204 fill_with_data( str2, 11 ); |
|
|
205 str3 = (char *)safe_malloc(32); |
|
|
206 fill_with_data( str3, 32 ); |
|
|
207 |
|
|
208 free(str2); |
|
|
209 free(str1); |
|
|
210 |
|
|
211 str2 = (char *)safe_calloc(11); |
|
|
212 fill_with_data( str2, 11 ); |
|
|
213 free(str2); |
|
|
214 |
|
|
215 str1 = (char *)safe_calloc(50); |
|
|
216 fill_with_data( str1, 50 ); |
|
|
217 free(str3); |
|
|
218 |
|
|
219 str3 = (char *)safe_malloc(32); |
|
|
220 fill_with_data( str3, 32 ); |
|
|
221 free(str1); |
|
|
222 |
|
|
223 str2 = (char *)safe_calloc(11); |
|
|
224 fill_with_data( str2, 11 ); |
|
|
225 str1 = (char *)safe_malloc(50); |
|
|
226 fill_with_data( str1, 50 ); |
|
|
227 |
|
|
228 free(str3); |
|
|
229 free(str1); |
|
|
230 free(str2); |
|
|
231 |
|
|
232 if (problem != 0) |
|
|
233 break; |
|
|
234 } // for |
|
|
235 |
|
|
236 // Did it completely successfully? |
|
|
237 if (j==NUM_ITERATIONS) |
|
|
238 CYG_TEST_PASS("Stress test completed successfully"); |
|
|
239 |
|
|
240 #else |
|
2
|
241 CYG_TEST_NA("Testing is not applicable to this configuration"); |
|
|
242 #endif // if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && |
|
|
243 // defined(CYGSEM_KERNEL_MEMORY_COALESCE) |
|
0
|
244 |
|
|
245 |
|
|
246 CYG_TEST_FINISH("Finished stress tests from testcase " __FILE__ " for C " |
|
|
247 "library malloc(), calloc() and free() functions"); |
|
|
248 } // main() |
|
|
249 |
|
|
250 // EOF malloc2.c |