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