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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [services/] [memalloc/] [common/] [v2_0/] [tests/] [dlmalloc1.cxx] - Blame information for rev 27

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

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//        dlmalloc1.cxx
4
//
5
//        dlmalloc memory pool test 1
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):     dsm, jlarmour
44
// Contributors:  
45
// Date:          2000-06-18
46
// Description:   Tests basic dlmalloc memory pool functionality
47
//####DESCRIPTIONEND####
48
 
49
#include <pkgconf/memalloc.h>
50
#include <pkgconf/system.h>
51
 
52
#ifdef CYGPKG_KERNEL
53
#include <pkgconf/kernel.h>
54
 
55
#include <cyg/kernel/sched.hxx>        // Cyg_Scheduler::start()
56
#include <cyg/kernel/thread.hxx>       // Cyg_Thread
57
 
58
#include <cyg/kernel/sched.inl>
59
#include <cyg/kernel/thread.inl>
60
 
61
#include <cyg/kernel/timer.hxx>         // Cyg_Timer
62
#include <cyg/kernel/clock.inl>         // Cyg_Clock
63
 
64
#define STACKSIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 20*CYGNUM_HAL_STACK_FRAME_SIZE)
65
#define NTHREADS 2
66
#include "testaux.hxx"
67
 
68
#endif
69
 
70
#include <cyg/memalloc/dlmalloc.hxx>
71
 
72
#include <cyg/infra/testcase.h>
73
 
74
static const cyg_int32 memsize = 10240;
75
 
76
static cyg_uint8 mem[2][memsize];
77
 
78
static Cyg_Mempool_dlmalloc mempool0(mem[0], memsize);
79
 
80
static Cyg_Mempool_dlmalloc mempool1(mem[1], memsize);
81
 
82
 
83
static void check_in_mp0(cyg_uint8 *p, cyg_int32 size)
84
{
85
    CYG_TEST_CHECK(NULL != p,
86
                   "Allocation failed");
87
    CYG_TEST_CHECK(mem[0] <= p && p+size < mem[1],
88
                   "Block outside memory pool");
89
}
90
 
91
 
92
static void entry0( CYG_ADDRWORD data )
93
{
94
    cyg_int32 f0,f1,f2,t0;
95
    cyg_uint8 *p0, *p1;
96
    cyg_int32 most_of_mem=memsize/4*3;
97
    Cyg_Mempool_Status stat;
98
 
99
    mempool0.get_status( CYG_MEMPOOL_STAT_ORIGBASE|
100
                         CYG_MEMPOOL_STAT_BLOCKSIZE|
101
                         CYG_MEMPOOL_STAT_MAXFREE|
102
                         CYG_MEMPOOL_STAT_ORIGSIZE, stat );
103
 
104
    CYG_TEST_CHECK(mem[0] == stat.origbase, "get_status: base wrong");
105
    CYG_TEST_CHECK(memsize == stat.origsize, "get_status: size wrong");
106
 
107
    CYG_TEST_CHECK(0 < stat.maxfree && stat.maxfree <= stat.origsize,
108
                   "get_status: maxfree wildly wrong");
109
 
110
    CYG_TEST_CHECK(-1 == stat.blocksize, "blocksize wrong" );
111
 
112
    mempool0.get_status( CYG_MEMPOOL_STAT_TOTALFREE|
113
                         CYG_MEMPOOL_STAT_ARENASIZE, stat );
114
    t0 = stat.arenasize;
115
    CYG_TEST_CHECK(t0 > 0, "Negative total memory" );
116
    f0 = stat.totalfree;
117
    CYG_TEST_CHECK(f0 > 0, "Negative free memory" );
118
    CYG_TEST_CHECK(t0 <= memsize, "get_totalsize: Too much memory");
119
    CYG_TEST_CHECK(f0 <= t0 , "More memory free than possible" );
120
 
121
    mempool0.get_status( CYG_MEMPOOL_STAT_WAITING, stat );
122
    CYG_TEST_CHECK( !stat.waiting,
123
                    "Thread waiting for memory; there shouldn't be");
124
 
125
    CYG_TEST_CHECK( NULL == mempool0.try_alloc(memsize+1),
126
                    "Managed to allocate too much memory");
127
 
128
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
129
    p0 = mempool0.alloc(most_of_mem);
130
#else
131
    p0 = mempool0.try_alloc(most_of_mem);
132
#endif
133
    check_in_mp0(p0, most_of_mem);
134
 
135
    mempool0.get_status( CYG_MEMPOOL_STAT_TOTALFREE, stat );
136
    f1 = stat.totalfree;
137
    CYG_TEST_CHECK(f1 > 0, "Negative free memory" );
138
    CYG_TEST_CHECK(f1 < f0, "Free memory didn't decrease after allocation" );
139
 
140
    CYG_TEST_CHECK( NULL == mempool0.try_alloc(most_of_mem),
141
                    "Managed to allocate too much memory");
142
 
143
    CYG_TEST_CHECK(mempool0.free(p0, most_of_mem), "Couldn't free");
144
 
145
    mempool0.get_status( CYG_MEMPOOL_STAT_TOTALFREE, stat );
146
    f2 = stat.totalfree;
147
    CYG_TEST_CHECK(f2 > f1, "Free memory didn't increase after free" );
148
 
149
    // should be able to reallocate now memory is free
150
    p0 = mempool0.try_alloc(most_of_mem);
151
    check_in_mp0(p0, most_of_mem);
152
 
153
    p1 = mempool0.try_alloc(10);
154
    check_in_mp0(p1, 10);
155
 
156
    CYG_TEST_CHECK(p1+10 <= p0 || p1 >= p0+most_of_mem,
157
                   "Ranges of allocated memory overlap");
158
 
159
    CYG_TEST_CHECK(mempool0.free(p0, 0), "Couldn't free");
160
    CYG_TEST_CHECK(mempool0.free(p1, 10), "Couldn't free");
161
 
162
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
163
# ifdef CYGFUN_KERNEL_THREADS_TIMER
164
    // This shouldn't have to wait
165
    p0 = mempool0.alloc(most_of_mem,
166
        Cyg_Clock::real_time_clock->current_value() + 100000);
167
    check_in_mp0(p0, most_of_mem);
168
    p1 = mempool0.alloc(most_of_mem,
169
        Cyg_Clock::real_time_clock->current_value() + 2);
170
    CYG_TEST_CHECK(NULL == p1, "Timed alloc unexpectedly worked");
171
    p1 = mempool0.alloc(10,
172
        Cyg_Clock::real_time_clock->current_value() + 2);
173
    check_in_mp0(p1, 10);
174
 
175
    // Expect thread 1 to have run while processing previous timed
176
    // allocation.  It should therefore tbe waiting.
177
    mempool1.get_status( CYG_MEMPOOL_STAT_WAITING, stat );
178
    CYG_TEST_CHECK(stat.waiting, "There should be a thread waiting");
179
# endif
180
#endif
181
 
182
    CYG_TEST_PASS_FINISH("dlmalloc memory pool 1 OK");
183
}
184
 
185
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
186
static void entry1( CYG_ADDRWORD data )
187
{
188
    mempool1.alloc(memsize+1);
189
    CYG_TEST_FAIL("Oversized alloc returned");
190
}
191
#endif
192
 
193
void dlmalloc1_main( void )
194
{
195
    CYG_TEST_INIT();
196
    CYG_TEST_INFO("Starting dlmalloc memory pool 1 test");
197
 
198
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
199
    new_thread(entry0, 0);
200
    new_thread(entry1, 1);
201
 
202
    Cyg_Scheduler::start();
203
#elif defined(CYGPKG_KERNEL)
204
    new_thread(entry0, 0);
205
 
206
    Cyg_Scheduler::start();
207
#else
208
    entry0(0);
209
#endif
210
 
211
    CYG_TEST_FAIL_FINISH("Not reached");
212
}
213
 
214
externC void
215
cyg_start( void )
216
{
217
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
218
    cyg_hal_invoke_constructors();
219
#endif
220
    dlmalloc1_main();
221
}
222
// EOF dlmalloc1.cxx

powered by: WebSVN 2.1.0

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