1 //=================================================================
5 // Test all the memory used by heaps to check it's all valid
7 //=================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //=================================================================
41 //#####DESCRIPTIONBEGIN####
43 // Author(s): jlarmour
46 // Description: Tests all memory allocated for use by heaps.
49 //####DESCRIPTIONEND####
53 #include <pkgconf/system.h>
54 #include <pkgconf/hal.h>
55 #include <pkgconf/memalloc.h> // config header
56 #ifdef CYGPKG_ISOINFRA
57 # include <pkgconf/isoinfra.h>
60 #include <cyg/infra/testcase.h>
62 #if !defined(CYGPKG_ISOINFRA)
63 # define NA_MSG "Requires isoinfra package"
64 #elif !CYGINT_ISO_MALLOC
65 # define NA_MSG "Requires malloc"
66 #elif !CYGINT_ISO_MALLINFO
67 # define NA_MSG "Requires mallinfo"
76 CYG_TEST_NA( NA_MSG );
77 CYG_TEST_FINISH("Done");
81 #include <cyg/infra/diag.h>
83 #define ERRORTHRESHOLD 10
84 #define ITERS (cyg_test_is_simulator ? 1 : 10)
85 #define INTALIGNED(_x_) (!((unsigned long)(_x_) & (sizeof(int)-1)))
88 test_pat(unsigned char *buf, int size,
89 unsigned int pat, cyg_bool addrpat,
92 unsigned char *bufptr=buf;
93 register unsigned int *ibufptr;
94 unsigned char *endptr=buf+size;
95 register unsigned int *endptra; // int aligned
97 unsigned char bpat = pat & 0xFF;
99 endptra = (unsigned int *)((unsigned long)endptr & ~(sizeof(int)-1));
101 // Set to the pattern
102 while (!INTALIGNED(bufptr)) {
104 bpat = ((int)bufptr)&0xFF;
108 ibufptr = (unsigned int *)bufptr;
110 while ( ibufptr < endptra ) {
112 pat = (unsigned int)ibufptr;
116 bufptr = (unsigned char *)ibufptr;
117 while ( bufptr < endptr ) {
119 bpat = ((int)bufptr)&0xFF;
123 // Now compare to the pattern
125 while ( !INTALIGNED(bufptr) ) {
127 bpat = ((int)bufptr)&0xFF;
128 if ( *bufptr != bpat ) {
129 diag_printf( "FAIL:<Memory at 0x%08x: expected 0x%02x, read 0x%02x>\n",
130 (unsigned int)bufptr, (unsigned int)bpat,
131 (unsigned int)*bufptr );
132 if ( errors++ == ERRORTHRESHOLD )
133 CYG_TEST_FAIL_FINISH( testname );
138 ibufptr = (unsigned int *)bufptr;
140 while ( ibufptr < endptra ) {
142 pat = (unsigned int)ibufptr;
143 if ( *ibufptr != pat ) {
144 diag_printf( "FAIL:<Memory at 0x%08x: expected 0x%08x, read 0x%08x>\n",
145 (unsigned int) ibufptr, pat, *ibufptr );
146 if ( errors++ == ERRORTHRESHOLD )
147 CYG_TEST_FAIL_FINISH( testname );
152 bufptr = (unsigned char *)ibufptr;
153 while ( bufptr < endptr ) {
155 bpat = ((int)bufptr)&0xFF;
156 if ( *bufptr != bpat ) {
157 diag_printf( "FAIL:<Memory at 0x%08x: expected 0x%02x, read 0x%02x>\n",
158 (unsigned int) bufptr, (unsigned int)bpat,
159 (unsigned int)*bufptr );
160 if ( errors++ == ERRORTHRESHOLD )
161 CYG_TEST_FAIL_FINISH( testname );
166 CYG_TEST_FAIL( testname );
168 CYG_TEST_PASS( testname );
175 unsigned int allonesint=0, checkerboardint1=0, checkerboardint2=0;
179 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
180 cyg_hal_invoke_constructors();
183 CYG_TEST_INFO("Starting heaptest - testing all memory usable as heap");
184 CYG_TEST_INFO("Any failures reported may indicate failing RAM hardware,");
185 CYG_TEST_INFO("or an invalid memory map");
187 for (i=0; i<sizeof(int); i++) {
188 allonesint = allonesint << 8;
190 checkerboardint1 = checkerboardint1 << 8;
191 checkerboardint1 |= 0xAA;
192 checkerboardint2 = checkerboardint2 << 8;
193 checkerboardint2 |= 0x55;
197 struct mallinfo info;
202 if ( info.maxfree <= 0 )
205 buf = malloc(info.maxfree);
207 diag_printf("Couldn't malloc %d bytes claimed as available",
209 CYG_TEST_FAIL_FINISH("heaptest");
212 diag_printf( "INFO:<Testing memory at 0x%08x of size %d for %d iterations>\n",
213 (unsigned int)buf, info.maxfree, ITERS );
214 for (i=0; i<ITERS; i++) {
215 errors += test_pat( buf, info.maxfree, 0, 0, "all zeroes" );
216 errors += test_pat( buf, info.maxfree, allonesint, 0,
218 errors += test_pat( buf, info.maxfree, checkerboardint1, 0,
220 errors += test_pat( buf, info.maxfree, checkerboardint2, 0,
222 errors += test_pat( buf, info.maxfree, 0, 1,
226 // deliberately don't free so we get the next space
230 CYG_TEST_FAIL_FINISH( "heaptest errors found" );
232 CYG_TEST_PASS_FINISH( "heaptest OK" );