OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [services/] [memalloc/] [common/] [current/] [tests/] [malloc2.c] - Blame information for rev 819

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//=================================================================
2
//
3
//        malloc2.c
4
//
5
//        Stress 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 to stress-test C library malloc(),
46
//                calloc() and 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 so we can know size of malloc pool
55
#ifdef CYGPKG_ISOINFRA
56
# include <pkgconf/isoinfra.h>
57
# include <stdlib.h>
58
#endif
59
 
60
#include <cyg/infra/testcase.h>
61
 
62
#if !defined(CYGPKG_ISOINFRA)
63
# define NA_MSG "Requires isoinfra package"
64
#elif !CYGINT_ISO_MAIN_STARTUP
65
# define NA_MSG "Requires main() to be called"
66
#elif !CYGINT_ISO_MALLOC
67
# define NA_MSG "Requires malloc"
68
#elif !CYGINT_ISO_MALLINFO
69
# define NA_MSG "Requires mallinfo"
70
#endif
71
 
72
#ifdef NA_MSG
73
void
74
cyg_start(void)
75
{
76
    CYG_TEST_INIT();
77
    CYG_TEST_NA( NA_MSG );
78
    CYG_TEST_FINISH("Done");
79
}
80
#else
81
 
82
// CONSTANTS
83
 
84
#define NUM_ITERATIONS 1000
85
 
86
// GLOBALS
87
 
88
static int problem=0;
89
 
90
// FUNCTIONS
91
 
92
extern int
93
cyg_memalloc_maxalloc( void );
94
 
95
static void *
96
safe_malloc( size_t size )
97
{
98
    void *ptr;
99
 
100
    ptr=malloc(size);
101
 
102
    if (ptr==NULL)
103
    {
104
        CYG_TEST_FAIL( "malloc returned NULL! "
105
                       "Perhaps the allocator doesn't coalesce?" );
106
        problem++;
107
    } // if
108
 
109
    return ptr;
110
} // safe_malloc()
111
 
112
 
113
static void *
114
safe_calloc( size_t size )
115
{
116
    void *ptr;
117
    int i;
118
 
119
    ptr=calloc(size,1);
120
 
121
    if (ptr==NULL)
122
    {
123
        CYG_TEST_FAIL( "calloc returned NULL! "
124
                       "Perhaps the allocator doesn't coalesce" );
125
        problem++;
126
    } // if
127
    else
128
    {
129
        for (i=0; i < size; i++)
130
        {
131
            if (((char *)ptr)[i] != 0)
132
            {
133
                CYG_TEST_FAIL("calloc didn't clear data completely");
134
                problem++;
135
                return ptr;
136
            } // if
137
        } // for
138
    } // else
139
 
140
    return ptr;
141
} // safe_calloc()
142
 
143
 
144
static void
145
fill_with_data( char *buf, int size )
146
{
147
    int i;
148
 
149
    for (i=0; i < size; i++)
150
        buf[i] = 'f';
151
 
152
    for (i=0; i < size; i++)
153
        if (buf[i] != 'f')
154
        {
155
            CYG_TEST_FAIL( "data written to buffer does not compare "
156
                           "correctly! #1" );
157
            problem++;
158
            return;
159
        } // if
160
 
161
 
162
    for (i=0; i < size; i++)
163
        buf[i] = 'z';
164
 
165
    for (i=0; i < size; i++)
166
        if (buf[i] != 'z')
167
        {
168
            CYG_TEST_FAIL( "data written to buffer does not compare "
169
                           "correctly! #2" );
170
            problem++;
171
            return;
172
        } // if
173
 
174
} // fill_with_data()
175
 
176
 
177
int
178
main( int argc, char *argv[] )
179
{
180
    char *str1, *str2, *str3;
181
    int j;
182
    int poolmax;
183
 
184
    CYG_TEST_INIT();
185
 
186
    CYG_TEST_INFO("Starting stress tests from testcase " __FILE__ " for C "
187
                  "library malloc(), calloc() and free() functions");
188
 
189
    poolmax = mallinfo().maxfree;
190
 
191
    if ( poolmax <= 0 ) {
192
        CYG_TEST_FAIL_FINISH( "Can't determine allocation size to use" );
193
    }
194
 
195
    if ( poolmax < 300 )
196
    {
197
        CYG_TEST_FAIL_FINISH("This testcase cannot safely be used with a "
198
                             "memory pool for malloc less than 300 bytes");
199
    } // if
200
 
201
 
202
    for ( j=1; j < NUM_ITERATIONS; j++)
203
    {
204
//        if ((j % 100) == 0)
205
//            CYG_TEST_STILL_ALIVE( j, "Multiple mallocs and frees continuing" );
206
 
207
 
208
        str1 = (char *)safe_malloc(50);
209
        fill_with_data( str1, 50 );
210
        str2 = (char *)safe_calloc(11);
211
        fill_with_data( str2, 11 );
212
        str3 = (char *)safe_malloc(32);
213
        fill_with_data( str3, 32 );
214
 
215
        free(str2);
216
        free(str1);
217
 
218
        str2 = (char *)safe_calloc(11);
219
        fill_with_data( str2, 11 );
220
        free(str2);
221
 
222
        str1 = (char *)safe_calloc(50);
223
        fill_with_data( str1, 50 );
224
        free(str3);
225
 
226
        str3 = (char *)safe_malloc(32);
227
        fill_with_data( str3, 32 );
228
        free(str1);
229
 
230
        str2 = (char *)safe_calloc(11);
231
        fill_with_data( str2, 11 );
232
        str1 = (char *)safe_malloc(50);
233
        fill_with_data( str1, 50 );
234
 
235
        free(str3);
236
        free(str1);
237
        free(str2);
238
 
239
        if (problem != 0)
240
            break;
241
    } // for
242
 
243
    // Did it completely successfully?
244
    if (j==NUM_ITERATIONS)
245
        CYG_TEST_PASS("Stress test completed successfully");
246
 
247
    CYG_TEST_FINISH("Finished stress tests from testcase " __FILE__ " for C "
248
                    "library malloc(), calloc() and free() functions");
249
 
250
    return 0;
251
} // main()
252
 
253
#endif // ifndef NA_MSG
254
 
255
// EOF malloc2.c

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.