|
0
|
1 //========================================================================== |
|
|
2 // |
|
|
3 // memfix1.cxx |
|
|
4 // |
|
|
5 // Fixed memory pool test 1 |
|
|
6 // |
|
|
7 //========================================================================== |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
|
13 // compliance with the License. You may obtain a copy of the License at |
|
|
14 // http://sourceware.cygnus.com/ecos |
|
|
15 // |
|
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
18 // License for the specific language governing rights and limitations under |
|
|
19 // the License. |
|
|
20 // |
|
|
21 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
2
|
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //========================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author(s): dsm |
|
|
33 // Contributors: dsm |
|
|
34 // Date: 1998-06-09 |
|
|
35 // Description: Tests basic fixed memory pool functionality |
|
|
36 //####DESCRIPTIONEND#### |
|
|
37 |
|
|
38 #include <pkgconf/kernel.h> |
|
|
39 |
|
|
40 #include <cyg/kernel/sched.hxx> // Cyg_Scheduler::start() |
|
|
41 #include <cyg/kernel/thread.hxx> // Cyg_Thread |
|
|
42 |
|
|
43 #include <cyg/kernel/memfixed.hxx> |
|
|
44 |
|
|
45 #include <cyg/infra/testcase.h> |
|
|
46 |
|
|
47 #include <cyg/kernel/sched.inl> |
|
|
48 #include <cyg/kernel/thread.inl> |
|
|
49 |
|
|
50 #include <cyg/kernel/timer.hxx> // Cyg_Timer |
|
|
51 #include <cyg/kernel/clock.inl> // Cyg_Clock |
|
|
52 |
|
|
53 |
|
|
54 #define NTHREADS 2 |
|
|
55 #include "testaux.hxx" |
|
|
56 |
|
|
57 static const cyg_int32 memsize = 10240; |
|
|
58 |
|
|
59 static cyg_uint8 mem[2][memsize]; |
|
|
60 |
|
|
61 static Cyg_Mempool_Fixed mempool0(mem[0], memsize, 100); |
|
|
62 |
|
|
63 static Cyg_Mempool_Fixed mempool1(mem[1], memsize, 316); |
|
|
64 |
|
|
65 |
|
|
66 static void check_in_mp0(cyg_uint8 *p, cyg_int32 size) |
|
|
67 { |
|
|
68 CYG_TEST_CHECK(NULL != p, |
|
|
69 "Allocation failed"); |
|
|
70 CYG_TEST_CHECK(mem[0] <= p && p+size < mem[1], |
|
|
71 "Block outside memory pool"); |
|
|
72 } |
|
|
73 |
|
|
74 |
|
|
75 static void entry0( CYG_ADDRWORD data ) |
|
|
76 { |
|
|
77 cyg_uint8 *base; |
|
|
78 cyg_int32 size; |
|
|
79 CYG_ADDRWORD bs; |
|
|
80 cyg_int32 f0,f1,f2,t0; |
|
|
81 cyg_uint8 *p0, *p1, *p2; |
|
|
82 |
|
|
83 mempool0.get_arena(base, size, bs); |
|
|
84 cyg_int32 blocksize=(cyg_int32)bs; |
|
|
85 CYG_TEST_CHECK(mem[0] == base, "get_arena: base wrong"); |
|
|
86 CYG_TEST_CHECK(memsize == size, "get_arena: size wrong"); |
|
|
87 CYG_TEST_CHECK(100 == blocksize, "get_arena: blocksize wrong"); |
|
|
88 |
|
|
89 CYG_TEST_CHECK(316 == mempool1.get_blocksize(), "get_blocksize wrong" ); |
|
|
90 |
|
|
91 t0=mempool0.get_totalmem(); |
|
|
92 CYG_TEST_CHECK(t0 > 0, "Negative total memory" ); |
|
|
93 f0=mempool0.get_freemem(); |
|
|
94 CYG_TEST_CHECK(f0 > 0, "Negative free memory" ); |
|
|
95 CYG_TEST_CHECK(t0 <= memsize, "get_totalsize: Too much memory"); |
|
|
96 CYG_TEST_CHECK(f0 <= t0 , "More memory free than possible" ); |
|
|
97 |
|
|
98 CYG_TEST_CHECK( !mempool0.waiting(), |
|
|
99 "Thread waiting for memory; there shouldn't be"); |
|
|
100 |
|
|
101 CYG_TEST_CHECK( !mempool0.free(NULL), "Bogus free worked"); |
|
|
102 |
|
|
103 p0 = mempool0.alloc(); |
|
|
104 check_in_mp0(p0, 100); |
|
|
105 |
|
|
106 f1 = mempool0.get_freemem(); |
|
|
107 CYG_TEST_CHECK(f1 > 0, "Negative free memory" ); |
|
|
108 CYG_TEST_CHECK(f1 < f0, "Free memory didn't decrease after allocation" ); |
|
|
109 |
|
|
110 p1 = NULL; |
|
|
111 while((p2 = mempool0.try_alloc())) |
|
2
|
112 p1 = p2; |
|
0
|
113 |
|
|
114 f1 = mempool0.get_freemem(); |
|
|
115 CYG_TEST_CHECK(mempool0.free(p0), "Couldn't free"); |
|
|
116 |
|
|
117 f2 = mempool0.get_freemem(); |
|
|
118 CYG_TEST_CHECK(f2 > f1, "Free memory didn't increase after free" ); |
|
|
119 |
|
|
120 // should be able to reallocate now a block is free |
|
|
121 p0 = mempool0.try_alloc(); |
|
|
122 check_in_mp0(p0, 100); |
|
|
123 |
|
|
124 CYG_TEST_CHECK(p1+100 <= p0 || p1 >= p0+100, |
|
|
125 "Ranges of allocated memory overlap"); |
|
|
126 |
|
|
127 CYG_TEST_CHECK(mempool0.free(p0), "Couldn't free"); |
|
|
128 CYG_TEST_CHECK(mempool0.free(p1), "Couldn't free"); |
|
|
129 |
|
|
130 #ifdef CYGFUN_KERNEL_THREADS_TIMER |
|
|
131 // This shouldn't have to wait |
|
|
132 p0 = mempool0.alloc(Cyg_Clock::real_time_clock->current_value()+100000); |
|
|
133 check_in_mp0(p0, 100); |
|
2
|
134 p1 = mempool0.alloc(Cyg_Clock::real_time_clock->current_value()+20); |
|
0
|
135 check_in_mp0(p1, 10); |
|
2
|
136 p1 = mempool0.alloc(Cyg_Clock::real_time_clock->current_value()+20); |
|
0
|
137 CYG_TEST_CHECK(NULL == p1, "Timed alloc unexpectedly worked"); |
|
|
138 |
|
|
139 // Expect thread 1 to have run while processing previous timed |
|
|
140 // allocation. It should therefore tbe waiting. |
|
|
141 CYG_TEST_CHECK(mempool1.waiting(), "There should be a thread waiting"); |
|
|
142 #endif |
|
|
143 |
|
|
144 CYG_TEST_PASS_FINISH("Fixed memory pool 1 OK"); |
|
|
145 } |
|
|
146 |
|
|
147 static void entry1( CYG_ADDRWORD data ) |
|
|
148 { |
|
|
149 while(NULL != mempool1.alloc()) |
|
|
150 ; |
|
|
151 CYG_TEST_FAIL("alloc returned NULL"); |
|
|
152 } |
|
|
153 |
|
|
154 |
|
|
155 void memfix1_main( void ) |
|
|
156 { |
|
|
157 CYG_TEST_INIT(); |
|
|
158 |
|
|
159 new_thread(entry0, 0); |
|
|
160 new_thread(entry1, 1); |
|
|
161 |
|
|
162 Cyg_Scheduler::start(); |
|
|
163 |
|
|
164 CYG_TEST_FAIL_FINISH("Not reached"); |
|
|
165 } |
|
|
166 |
|
|
167 externC void |
|
|
168 cyg_start( void ) |
|
|
169 { |
|
|
170 memfix1_main(); |
|
|
171 } |
|
|
172 // EOF memfix1.cxx |