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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [services/] [memalloc/] [common/] [v2_0/] [tests/] [malloc2.c] - Blame information for rev 174

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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