1 |
786 |
skrzyp |
//=================================================================
|
2 |
|
|
//
|
3 |
|
|
// malloc3.c
|
4 |
|
|
//
|
5 |
|
|
// Testcase for C library malloc(), calloc() and free()
|
6 |
|
|
//
|
7 |
|
|
//=================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
12 |
|
|
//
|
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
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//=================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): jlarmour
|
43 |
|
|
// Contributors:
|
44 |
|
|
// Date: 2000-04-30
|
45 |
|
|
// Description: Contains testcode for C library malloc(), calloc() and
|
46 |
|
|
// free() functions
|
47 |
|
|
//
|
48 |
|
|
//
|
49 |
|
|
//####DESCRIPTIONEND####
|
50 |
|
|
|
51 |
|
|
// INCLUDES
|
52 |
|
|
|
53 |
|
|
#include <pkgconf/system.h> // Overall system configuration
|
54 |
|
|
#include <pkgconf/memalloc.h> // config header
|
55 |
|
|
#ifdef CYGPKG_ISOINFRA
|
56 |
|
|
# include <pkgconf/isoinfra.h>
|
57 |
|
|
# include <stdlib.h>
|
58 |
|
|
#endif
|
59 |
|
|
#include <cyg/infra/testcase.h>
|
60 |
|
|
|
61 |
|
|
#if !defined(CYGPKG_ISOINFRA)
|
62 |
|
|
# define NA_MSG "Requires isoinfra package"
|
63 |
|
|
#elif !CYGINT_ISO_MAIN_STARTUP
|
64 |
|
|
# define NA_MSG "Requires main() to be called"
|
65 |
|
|
#elif !CYGINT_ISO_MALLOC
|
66 |
|
|
# define NA_MSG "Requires malloc"
|
67 |
|
|
#elif !CYGINT_ISO_MALLINFO
|
68 |
|
|
# define NA_MSG "Requires mallinfo"
|
69 |
|
|
#endif
|
70 |
|
|
|
71 |
|
|
#ifdef NA_MSG
|
72 |
|
|
void
|
73 |
|
|
cyg_start(void)
|
74 |
|
|
{
|
75 |
|
|
CYG_TEST_INIT();
|
76 |
|
|
CYG_TEST_NA( NA_MSG );
|
77 |
|
|
CYG_TEST_FINISH("Done");
|
78 |
|
|
}
|
79 |
|
|
#else
|
80 |
|
|
|
81 |
|
|
// FUNCTIONS
|
82 |
|
|
|
83 |
|
|
extern int
|
84 |
|
|
cyg_memalloc_maxalloc( void );
|
85 |
|
|
|
86 |
|
|
static int
|
87 |
|
|
fill_with_data( char *buf, int size )
|
88 |
|
|
{
|
89 |
|
|
int i;
|
90 |
|
|
|
91 |
|
|
for (i=0; i < size; i++)
|
92 |
|
|
buf[i] = 'f';
|
93 |
|
|
|
94 |
|
|
for (i=0; i < size; i++)
|
95 |
|
|
if (buf[i] != 'f') {
|
96 |
|
|
CYG_TEST_FAIL( "data written to buffer does not compare "
|
97 |
|
|
"correctly! #1" );
|
98 |
|
|
return 0;
|
99 |
|
|
} // if
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
for (i=0; i < size; i++)
|
103 |
|
|
buf[i] = 'z';
|
104 |
|
|
|
105 |
|
|
for (i=0; i < size; i++)
|
106 |
|
|
if (buf[i] != 'z') {
|
107 |
|
|
CYG_TEST_FAIL( "data written to buffer does not compare "
|
108 |
|
|
"correctly! #2" );
|
109 |
|
|
return 0;
|
110 |
|
|
} // if
|
111 |
|
|
|
112 |
|
|
return 1;
|
113 |
|
|
} // fill_with_data()
|
114 |
|
|
|
115 |
|
|
int
|
116 |
|
|
main( int argc, char *argv[] )
|
117 |
|
|
{
|
118 |
|
|
char *str;
|
119 |
|
|
int size;
|
120 |
|
|
int poolmax;
|
121 |
|
|
|
122 |
|
|
CYG_TEST_INIT();
|
123 |
|
|
|
124 |
|
|
CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
|
125 |
|
|
"malloc() and free() functions");
|
126 |
|
|
CYG_TEST_INFO("This checks allocation and freeing of large regions");
|
127 |
|
|
|
128 |
|
|
poolmax = mallinfo().maxfree;
|
129 |
|
|
|
130 |
|
|
if ( poolmax <= 0 ) {
|
131 |
|
|
CYG_TEST_FAIL_FINISH( "Can't determine allocation size to use" );
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
size = poolmax/2;
|
135 |
|
|
|
136 |
|
|
// Don't allocate all the memory at once - leave room for any structures
|
137 |
|
|
// used to manage the memory
|
138 |
|
|
str = (char *)malloc( size );
|
139 |
|
|
CYG_TEST_PASS_FAIL( str != NULL, "allocation 1");
|
140 |
|
|
CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 1 usability");
|
141 |
|
|
free( str );
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
str = (char *)malloc( size );
|
145 |
|
|
CYG_TEST_PASS_FAIL( str != NULL, "allocation 2");
|
146 |
|
|
CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 2 usability");
|
147 |
|
|
free( str );
|
148 |
|
|
|
149 |
|
|
str = (char *)malloc( size );
|
150 |
|
|
CYG_TEST_PASS_FAIL( str != NULL, "allocation 3");
|
151 |
|
|
CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 3 usability");
|
152 |
|
|
free( str );
|
153 |
|
|
|
154 |
|
|
str = (char *)malloc( size );
|
155 |
|
|
CYG_TEST_PASS_FAIL( str != NULL, "allocation 4");
|
156 |
|
|
CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 4 usability");
|
157 |
|
|
free( str );
|
158 |
|
|
|
159 |
|
|
str = (char *)malloc( size );
|
160 |
|
|
CYG_TEST_PASS_FAIL( str != NULL, "allocation 5");
|
161 |
|
|
CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 5 usability");
|
162 |
|
|
free( str );
|
163 |
|
|
|
164 |
|
|
str = (char *)malloc( size );
|
165 |
|
|
CYG_TEST_PASS_FAIL( str != NULL, "allocation 6");
|
166 |
|
|
CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 6 usability");
|
167 |
|
|
free( str );
|
168 |
|
|
|
169 |
|
|
str = (char *)malloc( size );
|
170 |
|
|
CYG_TEST_PASS_FAIL( str != NULL, "allocation 7");
|
171 |
|
|
CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 7 usability");
|
172 |
|
|
free( str );
|
173 |
|
|
|
174 |
|
|
str = (char *)malloc( size );
|
175 |
|
|
CYG_TEST_PASS_FAIL( str != NULL, "allocation 8");
|
176 |
|
|
CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 8 usability");
|
177 |
|
|
free( str );
|
178 |
|
|
|
179 |
|
|
str = (char *)malloc( size );
|
180 |
|
|
CYG_TEST_PASS_FAIL( str != NULL, "allocation 9");
|
181 |
|
|
CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 9 usability");
|
182 |
|
|
free( str );
|
183 |
|
|
|
184 |
|
|
str = (char *)malloc( size );
|
185 |
|
|
CYG_TEST_PASS_FAIL( str != NULL, "allocation 10");
|
186 |
|
|
CYG_TEST_PASS_FAIL( fill_with_data( str, size ),"allocation 10 usability");
|
187 |
|
|
free( str );
|
188 |
|
|
|
189 |
|
|
CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library "
|
190 |
|
|
"malloc() and free() functions");
|
191 |
|
|
|
192 |
|
|
return 0;
|
193 |
|
|
} // main()
|
194 |
|
|
|
195 |
|
|
#endif // ifndef NA_MSG
|
196 |
|
|
|
197 |
|
|
// EOF malloc3.c
|