1 //=================================================================
5 // Testcase for C library malloc(), calloc() and free()
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: Contains testcode for C library malloc(), calloc() and
50 //####DESCRIPTIONEND####
54 #include <pkgconf/system.h>
55 #include <pkgconf/memalloc.h> // config header
56 #ifdef CYGPKG_ISOINFRA
57 # include <pkgconf/isoinfra.h>
60 #include <cyg/infra/testcase.h>
61 #include <limits.h> // INT_MAX
64 #if !defined(CYGPKG_ISOINFRA)
65 # define NA_MSG "Requires isoinfra package"
66 #elif !CYGINT_ISO_MAIN_STARTUP
67 # define NA_MSG "Requires main() to be called"
68 #elif !CYGINT_ISO_MALLOC
69 # define NA_MSG "Requires malloc"
70 #elif !CYGINT_ISO_MALLINFO
71 # define NA_MSG "Requires mallinfo"
79 CYG_TEST_NA( NA_MSG );
80 CYG_TEST_FINISH("Done");
88 main( int argc, char *argv[] )
91 char *str, *str2, *str3;
97 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
98 "malloc(), calloc() and free() functions");
100 poolmax = mallinfo().maxfree;
102 if ( poolmax <= 0 ) {
103 CYG_TEST_FAIL_FINISH( "Can't determine allocation size to use" );
107 i = (int *) malloc( sizeof(int) );
109 // check if it should fit into pool
110 if (sizeof(int) > poolmax)
112 // didn't fit into pool, so should be NULL
113 CYG_TEST_PASS_FAIL( i == NULL,
114 "1 int malloc with no space left works" );
118 // since it should fit into pool, we can fiddle with i
120 CYG_TEST_PASS_FAIL( i && (*i==-12345),
121 "1 int malloc with space left works" );
126 str=(char *) malloc( 4096 );
130 // didn't fit into pool, so should be NULL
131 CYG_TEST_PASS_FAIL( str == NULL,"4K string with no space left works" );
135 // since it should fit into pool, we can fiddle with it.
136 for (j=0; j<1024; j++)
144 for (j=0; j<1024; j++)
146 if ( ((str[j*4] != 'f') ||
147 (str[(j*4)+1] != 'r') ||
148 (str[(j*4)+2] != 'e') ||
149 (str[(j*4)+3] != 'd')) )
153 // did j reach the top?
154 CYG_TEST_PASS_FAIL( j==1024, "4K string with space left works" );
161 str=(char *) calloc( 2, 1024 );
165 // didn't fit into pool, so should be NULL
166 CYG_TEST_PASS_FAIL( str == NULL,
167 "calloc 2K string with no space left works" );
172 for ( j=0; j<2048; j++ )
178 CYG_TEST_PASS_FAIL( j==2048, "calloc 2K string is cleared" );
180 // since it should fit into pool, we can fiddle with it.
181 for (j=0; j<512; j++)
189 for (j=0; j<512; j++)
191 if ( ((str[j*4] != 'j') ||
192 (str[(j*4)+1] != 'i') ||
193 (str[(j*4)+2] != 'f') ||
194 (str[(j*4)+3] != 'l')) )
198 // did j reach the top?
199 CYG_TEST_PASS_FAIL( j==512,
200 "calloc 2K string - with space left works" );
206 #if defined(CYGIMP_MEMALLOC_MALLOC_VARIABLE_SIMPLE) && \
207 defined(CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_COALESCE)
208 poolmax = mallinfo().maxfree; // recalculate for non-coalescing allocator
210 str=(char *)malloc( poolmax+1 );
211 CYG_TEST_PASS_FAIL( str==NULL, "malloc too much data returns NULL" );
214 str=(char *)calloc( 1, poolmax+1 );
215 CYG_TEST_PASS_FAIL( str==NULL, "calloc too much data returns NULL" );
218 str=(char *)malloc(0); if (str != NULL) free(str);
219 str=(char *)calloc(0, 1); if (str != NULL) free(str);
220 str=(char *)calloc(1, 0); if (str != NULL) free(str);
221 str=(char *)calloc(0, 0); if (str != NULL) free(str);
222 // simply shouldn't barf by this point
224 CYG_TEST_PASS_FAIL( 1, "malloc and calloc of 0 bytes doesn't crash" );
227 str = (char *)malloc(10);
228 i = (int *)malloc(sizeof(int));
229 str2 = (char *)malloc(10);
233 CYG_TEST_PASS_FAIL( ((str3 <= str-sizeof(int)) || (str3 >= &str[10])) &&
234 ((str3 <= str2-sizeof(int)) || (str3 >= &str2[10])) &&
235 ((str+10 <= str2) || (str2+10 <= str)),
236 "Objects don't overlap" );
241 i=(int *)malloc(sizeof(int)*2);
244 CYG_TEST_PASS_FAIL( ((str3 <= str-sizeof(int)) || (str3 >= &str[10])) &&
245 ((str3 <= str2-sizeof(int)) || (str3 >= &str2[10])) &&
246 ((&str[10] <= str2) || (&str2[10] <= str)),
247 "Objects don't overlap when middle is freed" );
255 #if defined(CYGIMP_MEMALLOC_MALLOC_VARIABLE_SIMPLE) && \
256 defined(CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_COALESCE)
257 poolmax = mallinfo().maxfree; // recalculate for non-coalescing allocator
259 str = (char *)malloc( poolmax );
260 CYG_TEST_PASS_FAIL( str != NULL, "malloc of maximum free block size works");
263 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library "
264 "malloc(), calloc() and free() functions");
269 #endif // ifndef NA_MSG