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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [services/] [memalloc/] [common/] [current/] [tests/] [memfix1.cxx] - 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
//        memfix1.cxx
4
//
5
//        Fixed 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 fixed 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/memfixed.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_Fixed mempool0(mem[0], memsize, 100);
77
 
78
static Cyg_Mempool_Fixed mempool1(mem[1], memsize, 316);
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, *p2;
94
    Cyg_Mempool_Status stat;
95
 
96
    mempool0.get_status( CYG_MEMPOOL_STAT_ORIGBASE|
97
                         CYG_MEMPOOL_STAT_BLOCKSIZE|
98
                         CYG_MEMPOOL_STAT_ORIGSIZE, stat );
99
    CYG_TEST_CHECK(mem[0] == stat.origbase, "get_status: base wrong");
100
    CYG_TEST_CHECK(memsize == stat.origsize, "get_status: size wrong");
101
    CYG_TEST_CHECK(100 == stat.blocksize, "get_status: blocksize wrong");
102
 
103
    mempool1.get_status( CYG_MEMPOOL_STAT_BLOCKSIZE, stat );
104
    CYG_TEST_CHECK(316 == stat.blocksize, "get_status: pool1 blocksize wrong" );
105
 
106
    mempool0.get_status( CYG_MEMPOOL_STAT_TOTALFREE|
107
                         CYG_MEMPOOL_STAT_ARENASIZE, stat );
108
    t0 = stat.arenasize;
109
    CYG_TEST_CHECK(t0 > 0, "Negative total memory" );
110
    f0 = stat.totalfree;
111
    CYG_TEST_CHECK(f0 > 0, "Negative free memory" );
112
    CYG_TEST_CHECK(t0 <= memsize, "get_totalsize: Too much memory");
113
    CYG_TEST_CHECK(f0 <= t0 , "More memory free than possible" );
114
 
115
    mempool0.get_status( CYG_MEMPOOL_STAT_WAITING, stat );
116
    CYG_TEST_CHECK( !stat.waiting,
117
                    "Thread waiting for memory; there shouldn't be");
118
 
119
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_FIXED_THREADAWARE
120
    p0 = mempool0.alloc();
121
#else
122
    p0 = mempool0.try_alloc();
123
#endif
124
    check_in_mp0(p0, 100);
125
 
126
    mempool0.get_status( CYG_MEMPOOL_STAT_TOTALFREE, stat );
127
    f1 = stat.totalfree;
128
    CYG_TEST_CHECK(f1 > 0, "Negative free memory" );
129
    CYG_TEST_CHECK(f1 < f0, "Free memory didn't decrease after allocation" );
130
 
131
    p1 = NULL;
132
    while((p2 = mempool0.try_alloc()))
133
        p1 = p2;
134
 
135
    mempool0.get_status( CYG_MEMPOOL_STAT_TOTALFREE, stat );
136
    f1 = stat.totalfree;
137
    CYG_TEST_CHECK(mempool0.free(p0), "Couldn't free");
138
 
139
    mempool0.get_status( CYG_MEMPOOL_STAT_TOTALFREE, stat );
140
    f2 = stat.totalfree;
141
    CYG_TEST_CHECK(f2 > f1, "Free memory didn't increase after free" );
142
 
143
    // should be able to reallocate now a block is free
144
    p0 = mempool0.try_alloc();
145
    check_in_mp0(p0, 100);
146
 
147
    CYG_TEST_CHECK(p1+100 <= p0 || p1 >= p0+100,
148
                   "Ranges of allocated memory overlap");
149
 
150
    CYG_TEST_CHECK(mempool0.free(p0), "Couldn't free");
151
    CYG_TEST_CHECK(mempool0.free(p1), "Couldn't free");
152
 
153
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_FIXED_THREADAWARE
154
# ifdef CYGFUN_KERNEL_THREADS_TIMER
155
    // This shouldn't have to wait
156
    p0 = mempool0.alloc(Cyg_Clock::real_time_clock->current_value()+100000);
157
    check_in_mp0(p0, 100);
158
    p1 = mempool0.alloc(Cyg_Clock::real_time_clock->current_value()+20);
159
    check_in_mp0(p1, 10);
160
    p1 = mempool0.alloc(Cyg_Clock::real_time_clock->current_value()+20);
161
    CYG_TEST_CHECK(NULL == p1, "Timed alloc unexpectedly worked");
162
 
163
    // Expect thread 1 to have run while processing previous timed
164
    // allocation.  It should therefore tbe waiting.
165
    mempool1.get_status( CYG_MEMPOOL_STAT_WAITING, stat );
166
    CYG_TEST_CHECK(stat.waiting, "There should be a thread waiting");
167
# endif
168
#endif
169
 
170
    CYG_TEST_PASS_FINISH("Fixed memory pool 1 OK");
171
}
172
 
173
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_FIXED_THREADAWARE
174
static void entry1( CYG_ADDRWORD data )
175
{
176
    while(NULL != mempool1.alloc())
177
        ;
178
    CYG_TEST_FAIL("alloc returned NULL");
179
}
180
#endif
181
 
182
 
183
void memfix1_main( void )
184
{
185
    CYG_TEST_INIT();
186
    CYG_TEST_INFO("Starting Fixed memory pool 1 test");
187
 
188
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_FIXED_THREADAWARE
189
    new_thread(entry0, 0);
190
    new_thread(entry1, 1);
191
 
192
    Cyg_Scheduler::start();
193
#elif defined(CYGPKG_KERNEL)
194
    new_thread(entry0, 0);
195
 
196
    Cyg_Scheduler::start();
197
#else
198
    entry0(0);
199
#endif
200
 
201
    CYG_TEST_FAIL_FINISH("Not reached");
202
}
203
 
204
externC void
205
cyg_start( void )
206
{
207
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
208
    cyg_hal_invoke_constructors();
209
#endif
210
    memfix1_main();
211
}
212
// EOF memfix1.cxx

powered by: WebSVN 2.1.0

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