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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [services/] [memalloc/] [common/] [current/] [tests/] [memvar1.cxx] - Blame information for rev 786

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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