1 //==========================================================================
5 // Variable memory pool test 1
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.
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.
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
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.
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.
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.
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####
43 // Author(s): dsm, jlarmour
46 // Description: Tests basic variable memory pool functionality
47 //####DESCRIPTIONEND####
49 #include <pkgconf/memalloc.h>
50 #include <pkgconf/system.h>
53 #include <pkgconf/kernel.h>
55 #include <cyg/kernel/sched.hxx> // Cyg_Scheduler::start()
56 #include <cyg/kernel/thread.hxx> // Cyg_Thread
58 #include <cyg/kernel/sched.inl>
59 #include <cyg/kernel/thread.inl>
61 #include <cyg/kernel/timer.hxx> // Cyg_Timer
62 #include <cyg/kernel/clock.inl> // Cyg_Clock
65 #include "testaux.hxx"
69 #include <cyg/memalloc/memvar.hxx>
71 #include <cyg/infra/testcase.h>
73 static const cyg_int32 memsize = 10240;
75 static cyg_uint8 mem[2][memsize];
77 static Cyg_Mempool_Variable mempool0(mem[0], memsize);
79 static Cyg_Mempool_Variable mempool1(mem[1], memsize);
82 static void check_in_mp0(cyg_uint8 *p, cyg_int32 size)
84 CYG_TEST_CHECK(NULL != p,
86 CYG_TEST_CHECK(mem[0] <= p && p+size < mem[1],
87 "Block outside memory pool");
91 static void entry0( CYG_ADDRWORD data )
93 cyg_int32 f0,f1,f2,t0;
95 cyg_int32 most_of_mem=memsize/4*3;
96 Cyg_Mempool_Status stat;
98 mempool0.get_status( CYG_MEMPOOL_STAT_ORIGBASE|
99 CYG_MEMPOOL_STAT_BLOCKSIZE|
100 CYG_MEMPOOL_STAT_MAXFREE|
101 CYG_MEMPOOL_STAT_ORIGSIZE, stat );
103 CYG_TEST_CHECK(mem[0] == stat.origbase, "get_status: base wrong");
104 CYG_TEST_CHECK(memsize == stat.origsize, "get_status: size wrong");
106 CYG_TEST_CHECK(0 < stat.maxfree && stat.maxfree <= stat.origsize,
107 "get_status: maxfree wildly wrong");
109 CYG_TEST_CHECK(-1 == stat.blocksize, "blocksize wrong" );
111 mempool0.get_status( CYG_MEMPOOL_STAT_TOTALFREE|
112 CYG_MEMPOOL_STAT_ARENASIZE, stat );
114 CYG_TEST_CHECK(t0 > 0, "Negative total memory" );
116 CYG_TEST_CHECK(f0 > 0, "Negative free memory" );
117 CYG_TEST_CHECK(t0 <= memsize, "get_totalsize: Too much memory");
118 CYG_TEST_CHECK(f0 <= t0 , "More memory free than possible" );
120 mempool0.get_status( CYG_MEMPOOL_STAT_WAITING, stat );
121 CYG_TEST_CHECK( !stat.waiting,
122 "Thread waiting for memory; there shouldn't be");
124 CYG_TEST_CHECK( NULL == mempool0.try_alloc(memsize+1),
125 "Managed to allocate too much memory");
127 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
128 p0 = mempool0.alloc(most_of_mem);
130 p0 = mempool0.try_alloc(most_of_mem);
132 check_in_mp0(p0, most_of_mem);
134 mempool0.get_status( CYG_MEMPOOL_STAT_TOTALFREE, stat );
136 CYG_TEST_CHECK(f1 > 0, "Negative free memory" );
137 CYG_TEST_CHECK(f1 < f0, "Free memory didn't decrease after allocation" );
139 CYG_TEST_CHECK( NULL == mempool0.try_alloc(most_of_mem),
140 "Managed to allocate too much memory");
142 CYG_TEST_CHECK(mempool0.free(p0, most_of_mem), "Couldn't free");
144 mempool0.get_status( CYG_MEMPOOL_STAT_TOTALFREE, stat );
146 CYG_TEST_CHECK(f2 > f1, "Free memory didn't increase after free" );
148 // should be able to reallocate now memory is free
149 p0 = mempool0.try_alloc(most_of_mem);
150 check_in_mp0(p0, most_of_mem);
152 p1 = mempool0.try_alloc(10);
153 check_in_mp0(p1, 10);
155 CYG_TEST_CHECK(p1+10 <= p0 || p1 >= p0+most_of_mem,
156 "Ranges of allocated memory overlap");
158 CYG_TEST_CHECK(mempool0.free(p0, 0), "Couldn't free");
159 CYG_TEST_CHECK(mempool0.free(p1, 10), "Couldn't free");
161 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
162 # ifdef CYGFUN_KERNEL_THREADS_TIMER
163 // This shouldn't have to wait
164 p0 = mempool0.alloc(most_of_mem,
165 Cyg_Clock::real_time_clock->current_value() + 100000);
166 check_in_mp0(p0, most_of_mem);
167 p1 = mempool0.alloc(most_of_mem,
168 Cyg_Clock::real_time_clock->current_value() + 2);
169 CYG_TEST_CHECK(NULL == p1, "Timed alloc unexpectedly worked");
170 p1 = mempool0.alloc(10,
171 Cyg_Clock::real_time_clock->current_value() + 2);
172 check_in_mp0(p1, 10);
174 // Expect thread 1 to have run while processing previous timed
175 // allocation. It should therefore tbe waiting.
176 mempool1.get_status( CYG_MEMPOOL_STAT_WAITING, stat );
177 CYG_TEST_CHECK(stat.waiting, "There should be a thread waiting");
181 CYG_TEST_PASS_FINISH("Variable memory pool 1 OK");
184 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
185 static void entry1( CYG_ADDRWORD data )
187 mempool1.alloc(memsize+1);
188 CYG_TEST_FAIL("Oversized alloc returned");
192 void memvar1_main( void )
195 CYG_TEST_INFO("Starting Variable memory pool 1 test");
197 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
198 new_thread(entry0, 0);
199 new_thread(entry1, 1);
201 Cyg_Scheduler::start();
202 #elif defined(CYGPKG_KERNEL)
203 new_thread(entry0, 0);
205 Cyg_Scheduler::start();
210 CYG_TEST_FAIL_FINISH("Not reached");
216 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
217 cyg_hal_invoke_constructors();