Mercurial > flash_v2
comparison packages/services/memalloc/common/current/tests/malloc2.c @ 115:6ed91473a1cd ecos-sw-2000-08-21
Merge from eCos master repository on 2000-08-21-22:40:54-BST
| author | jlarmour |
|---|---|
| date | Fri, 25 Aug 2000 17:32:38 +0000 |
| parents | |
| children | 518f42066aba |
comparison
equal
deleted
inserted
replaced
| 114:5ad2b71d525e | 115:6ed91473a1cd |
|---|---|
| 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 Red Hat eCos Public License | |
| 12 // Version 1.1 (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://www.redhat.com/ | |
| 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 Configurable Operating System, | |
| 22 // released September 30, 1998. | |
| 23 // | |
| 24 // The Initial Developer of the Original Code is Red Hat. | |
| 25 // Portions created by Red Hat are | |
| 26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. | |
| 27 // All Rights Reserved. | |
| 28 // ------------------------------------------- | |
| 29 // | |
| 30 //####COPYRIGHTEND#### | |
| 31 //================================================================= | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 34 // Author(s): jlarmour | |
| 35 // Contributors: | |
| 36 // Date: 2000-04-30 | |
| 37 // Description: Contains testcode to stress-test C library malloc(), | |
| 38 // calloc() and free() functions | |
| 39 // | |
| 40 // | |
| 41 //####DESCRIPTIONEND#### | |
| 42 | |
| 43 // INCLUDES | |
| 44 | |
| 45 #include <pkgconf/system.h> // Overall system configuration | |
| 46 #include <pkgconf/memalloc.h> // config header so we can know size of malloc pool | |
| 47 #ifdef CYGPKG_ISOINFRA | |
| 48 # include <pkgconf/isoinfra.h> | |
| 49 # include <stdlib.h> | |
| 50 #endif | |
| 51 | |
| 52 #include <cyg/infra/testcase.h> | |
| 53 | |
| 54 #if !defined(CYGPKG_ISOINFRA) | |
| 55 # define NA_MSG "Requires isoinfra package" | |
| 56 #elif !CYGINT_ISO_MAIN_STARTUP | |
| 57 # define NA_MSG "Requires main() to be called" | |
| 58 #elif !CYGINT_ISO_MALLOC | |
| 59 # define NA_MSG "Requires malloc" | |
| 60 #elif !CYGINT_ISO_MALLINFO | |
| 61 # define NA_MSG "Requires mallinfo" | |
| 62 #endif | |
| 63 | |
| 64 #ifdef NA_MSG | |
| 65 void | |
| 66 cyg_start() | |
| 67 { | |
| 68 CYG_TEST_INIT(); | |
| 69 CYG_TEST_NA( NA_MSG ); | |
| 70 CYG_TEST_FINISH("Done"); | |
| 71 } | |
| 72 #else | |
| 73 | |
| 74 // CONSTANTS | |
| 75 | |
| 76 #define NUM_ITERATIONS 1000 | |
| 77 | |
| 78 // GLOBALS | |
| 79 | |
| 80 static int problem=0; | |
| 81 | |
| 82 // FUNCTIONS | |
| 83 | |
| 84 extern int | |
| 85 cyg_memalloc_maxalloc( void ); | |
| 86 | |
| 87 static void * | |
| 88 safe_malloc( size_t size ) | |
| 89 { | |
| 90 void *ptr; | |
| 91 | |
| 92 ptr=malloc(size); | |
| 93 | |
| 94 if (ptr==NULL) | |
| 95 { | |
| 96 CYG_TEST_FAIL( "malloc returned NULL! " | |
| 97 "Perhaps the allocator doesn't coalesce?" ); | |
| 98 problem++; | |
| 99 } // if | |
| 100 | |
| 101 return ptr; | |
| 102 } // safe_malloc() | |
| 103 | |
| 104 | |
| 105 static void * | |
| 106 safe_calloc( size_t size ) | |
| 107 { | |
| 108 void *ptr; | |
| 109 int i; | |
| 110 | |
| 111 ptr=calloc(size,1); | |
| 112 | |
| 113 if (ptr==NULL) | |
| 114 { | |
| 115 CYG_TEST_FAIL( "calloc returned NULL! " | |
| 116 "Perhaps the allocator doesn't coalesce" ); | |
| 117 problem++; | |
| 118 } // if | |
| 119 else | |
| 120 { | |
| 121 for (i=0; i < size; i++) | |
| 122 { | |
| 123 if (((char *)ptr)[i] != 0) | |
| 124 { | |
| 125 CYG_TEST_FAIL("calloc didn't clear data completely"); | |
| 126 problem++; | |
| 127 return ptr; | |
| 128 } // if | |
| 129 } // for | |
| 130 } // else | |
| 131 | |
| 132 return ptr; | |
| 133 } // safe_calloc() | |
| 134 | |
| 135 | |
| 136 static void | |
| 137 fill_with_data( char *buf, int size ) | |
| 138 { | |
| 139 int i; | |
| 140 | |
| 141 for (i=0; i < size; i++) | |
| 142 buf[i] = 'f'; | |
| 143 | |
| 144 for (i=0; i < size; i++) | |
| 145 if (buf[i] != 'f') | |
| 146 { | |
| 147 CYG_TEST_FAIL( "data written to buffer does not compare " | |
| 148 "correctly! #1" ); | |
| 149 problem++; | |
| 150 return; | |
| 151 } // if | |
| 152 | |
| 153 | |
| 154 for (i=0; i < size; i++) | |
| 155 buf[i] = 'z'; | |
| 156 | |
| 157 for (i=0; i < size; i++) | |
| 158 if (buf[i] != 'z') | |
| 159 { | |
| 160 CYG_TEST_FAIL( "data written to buffer does not compare " | |
| 161 "correctly! #2" ); | |
| 162 problem++; | |
| 163 return; | |
| 164 } // if | |
| 165 | |
| 166 } // fill_with_data() | |
| 167 | |
| 168 | |
| 169 int | |
| 170 main( int argc, char *argv[] ) | |
| 171 { | |
| 172 char *str1, *str2, *str3; | |
| 173 int j; | |
| 174 int poolmax; | |
| 175 | |
| 176 CYG_TEST_INIT(); | |
| 177 | |
| 178 CYG_TEST_INFO("Starting stress tests from testcase " __FILE__ " for C " | |
| 179 "library malloc(), calloc() and free() functions"); | |
| 180 | |
| 181 poolmax = mallinfo().maxfree; | |
| 182 | |
| 183 if ( poolmax <= 0 ) { | |
| 184 CYG_TEST_FAIL_FINISH( "Can't determine allocation size to use" ); | |
| 185 } | |
| 186 | |
| 187 if ( poolmax < 300 ) | |
| 188 { | |
| 189 CYG_TEST_FAIL_FINISH("This testcase cannot safely be used with a " | |
| 190 "memory pool for malloc less than 300 bytes"); | |
| 191 } // if | |
| 192 | |
| 193 | |
| 194 for ( j=1; j < NUM_ITERATIONS; j++) | |
| 195 { | |
| 196 // if ((j % 100) == 0) | |
| 197 // CYG_TEST_STILL_ALIVE( j, "Multiple mallocs and frees continuing" ); | |
| 198 | |
| 199 | |
| 200 str1 = (char *)safe_malloc(50); | |
| 201 fill_with_data( str1, 50 ); | |
| 202 str2 = (char *)safe_calloc(11); | |
| 203 fill_with_data( str2, 11 ); | |
| 204 str3 = (char *)safe_malloc(32); | |
| 205 fill_with_data( str3, 32 ); | |
| 206 | |
| 207 free(str2); | |
| 208 free(str1); | |
| 209 | |
| 210 str2 = (char *)safe_calloc(11); | |
| 211 fill_with_data( str2, 11 ); | |
| 212 free(str2); | |
| 213 | |
| 214 str1 = (char *)safe_calloc(50); | |
| 215 fill_with_data( str1, 50 ); | |
| 216 free(str3); | |
| 217 | |
| 218 str3 = (char *)safe_malloc(32); | |
| 219 fill_with_data( str3, 32 ); | |
| 220 free(str1); | |
| 221 | |
| 222 str2 = (char *)safe_calloc(11); | |
| 223 fill_with_data( str2, 11 ); | |
| 224 str1 = (char *)safe_malloc(50); | |
| 225 fill_with_data( str1, 50 ); | |
| 226 | |
| 227 free(str3); | |
| 228 free(str1); | |
| 229 free(str2); | |
| 230 | |
| 231 if (problem != 0) | |
| 232 break; | |
| 233 } // for | |
| 234 | |
| 235 // Did it completely successfully? | |
| 236 if (j==NUM_ITERATIONS) | |
| 237 CYG_TEST_PASS("Stress test completed successfully"); | |
| 238 | |
| 239 CYG_TEST_FINISH("Finished stress tests from testcase " __FILE__ " for C " | |
| 240 "library malloc(), calloc() and free() functions"); | |
| 241 } // main() | |
| 242 | |
| 243 #endif // ifndef NA_MSG | |
| 244 | |
| 245 // EOF malloc2.c |
