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