]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/services/memalloc/common/v2_0/tests/sepmeta1.cxx
Initial revision
[karo-tx-redboot.git] / packages / services / memalloc / common / v2_0 / tests / sepmeta1.cxx
1 //==========================================================================
2 //
3 //        sepmeta1.cxx
4 //
5 //        Variable memory pool with separate metadata 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:          2001-06-28
46 // Description:   Tests basic variable 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/sepmeta.hxx>
70
71 #include <cyg/infra/testcase.h>
72
73 static const cyg_int32 memsize = 10240;
74 static const cyg_int32 metadatasize = 2048;
75
76 static cyg_uint8 mem[2][memsize];
77 static cyg_uint8 metadata[2][metadatasize];
78
79 static Cyg_Mempool_Sepmeta mempool0(mem[0], memsize, 8,
80                                     metadata[0], metadatasize);
81
82 static Cyg_Mempool_Sepmeta mempool1(mem[1], memsize, 8,
83                                     metadata[1], metadatasize);
84
85
86 static void check_in_mp0(cyg_uint8 *p, cyg_int32 size)
87 {
88     CYG_TEST_CHECK(NULL != p,
89                    "Allocation failed");
90     CYG_TEST_CHECK(mem[0] <= p && p+size <= mem[1],
91                    "Block outside memory pool");
92 }
93
94
95 static void entry0( CYG_ADDRWORD data )
96 {
97     cyg_int32 f0,f1,f2,t0;
98     cyg_uint8 *p0, *p1;
99     cyg_int32 most_of_mem=memsize/4*3;
100     Cyg_Mempool_Status stat;
101     
102     mempool0.get_status( CYG_MEMPOOL_STAT_ORIGBASE|
103                          CYG_MEMPOOL_STAT_BLOCKSIZE|
104                          CYG_MEMPOOL_STAT_MAXFREE|
105                          CYG_MEMPOOL_STAT_ORIGSIZE, stat );
106     
107     CYG_TEST_CHECK(mem[0] == stat.origbase, "get_status: base wrong");
108     CYG_TEST_CHECK(memsize == stat.origsize, "get_status: size wrong");
109
110     CYG_TEST_CHECK(0 < stat.maxfree && stat.maxfree <= stat.origsize,
111                    "get_status: maxfree wildly wrong");
112     
113     CYG_TEST_CHECK(-1 == stat.blocksize, "blocksize wrong" );
114
115     mempool0.get_status( CYG_MEMPOOL_STAT_TOTALFREE|
116                          CYG_MEMPOOL_STAT_ARENASIZE, stat );
117     t0 = stat.arenasize;
118     CYG_TEST_CHECK(t0 > 0, "Negative total memory" );
119     f0 = stat.totalfree;
120     CYG_TEST_CHECK(f0 > 0, "Negative free memory" );
121     CYG_TEST_CHECK(t0 <= memsize, "get_totalsize: Too much memory");
122     CYG_TEST_CHECK(f0 <= t0 , "More memory free than possible" );
123
124     mempool0.get_status( CYG_MEMPOOL_STAT_WAITING, stat );
125     CYG_TEST_CHECK( !stat.waiting,
126                     "Thread waiting for memory; there shouldn't be");
127     
128     CYG_TEST_CHECK( NULL == mempool0.try_alloc(memsize+1),
129                     "Managed to allocate too much memory");
130     
131 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_SEPMETA_THREADAWARE
132     p0 = mempool0.alloc(most_of_mem);
133 #else
134     p0 = mempool0.try_alloc(most_of_mem);
135 #endif
136     check_in_mp0(p0, most_of_mem);
137
138     mempool0.get_status( CYG_MEMPOOL_STAT_TOTALFREE, stat );
139     f1 = stat.totalfree;
140     CYG_TEST_CHECK(f1 > 0, "Negative free memory" );
141     CYG_TEST_CHECK(f1 < f0, "Free memory didn't decrease after allocation" );
142
143     CYG_TEST_CHECK( NULL == mempool0.try_alloc(most_of_mem),
144                     "Managed to allocate too much memory");
145     
146     CYG_TEST_CHECK(mempool0.free(p0, most_of_mem), "Couldn't free");
147
148     mempool0.get_status( CYG_MEMPOOL_STAT_TOTALFREE, stat );
149     f2 = stat.totalfree;
150     CYG_TEST_CHECK(f2 > f1, "Free memory didn't increase after free" );
151     
152     // should be able to reallocate now memory is free
153     p0 = mempool0.try_alloc(most_of_mem);
154     check_in_mp0(p0, most_of_mem);
155
156     p1 = mempool0.try_alloc(10);
157     check_in_mp0(p1, 10);
158     
159     CYG_TEST_CHECK(p1+10 <= p0 || p1 >= p0+most_of_mem,
160                    "Ranges of allocated memory overlap");
161
162     CYG_TEST_CHECK(mempool0.free(p0, 0), "Couldn't free");
163     CYG_TEST_CHECK(mempool0.free(p1, 10), "Couldn't free");
164     
165 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_SEPMETA_THREADAWARE
166 # ifdef CYGFUN_KERNEL_THREADS_TIMER
167     // This shouldn't have to wait
168     p0 = mempool0.alloc(most_of_mem,
169         Cyg_Clock::real_time_clock->current_value() + 100000);
170     check_in_mp0(p0, most_of_mem);
171     p1 = mempool0.alloc(most_of_mem,
172        Cyg_Clock::real_time_clock->current_value() + 2);
173     CYG_TEST_CHECK(NULL == p1, "Timed alloc unexpectedly worked");
174     p1 = mempool0.alloc(10,
175         Cyg_Clock::real_time_clock->current_value() + 2);
176     check_in_mp0(p1, 10);
177     
178     // Expect thread 1 to have run while processing previous timed
179     // allocation.  It should therefore tbe waiting.
180     mempool1.get_status( CYG_MEMPOOL_STAT_WAITING, stat );
181     CYG_TEST_CHECK(stat.waiting, "There should be a thread waiting");
182 # endif
183 #endif
184     
185     CYG_TEST_PASS_FINISH("Sepmeta memory pool 1 OK");
186 }
187
188 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_SEPMETA_THREADAWARE
189 static void entry1( CYG_ADDRWORD data )
190 {
191     mempool1.alloc(memsize+1);
192     CYG_TEST_FAIL("Oversized alloc returned");
193 }
194 #endif
195
196 void sepmeta1_main( void )
197 {
198     CYG_TEST_INIT();
199     CYG_TEST_INFO("Starting Seperate metadata pool 1 test");
200
201 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_SEPMETA_THREADAWARE
202     new_thread(entry0, 0);
203     new_thread(entry1, 1);
204
205     Cyg_Scheduler::start();
206 #elif defined(CYGPKG_KERNEL)
207     new_thread(entry0, 0);
208
209     Cyg_Scheduler::start();
210 #else
211     entry0(0);
212 #endif
213
214     CYG_TEST_FAIL_FINISH("Not reached");
215 }
216
217 externC void
218 cyg_start( void )
219
220 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
221     cyg_hal_invoke_constructors();
222 #endif
223     sepmeta1_main();
224 }
225 // EOF sepmeta1.cxx