|
0
|
1 /*========================================================================== |
|
|
2 // |
|
|
3 // kmemfix1.cxx |
|
|
4 // |
|
|
5 // Kernel C API 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 |
|
2
|
39 #include <cyg/hal/hal_arch.h> // CYGNUM_HAL_STACK_SIZE_TYPICAL |
|
|
40 |
|
0
|
41 #include <pkgconf/kernel.h> |
|
|
42 |
|
|
43 #include <cyg/kernel/kapi.h> |
|
|
44 |
|
|
45 #include <cyg/infra/testcase.h> |
|
|
46 |
|
|
47 #ifdef CYGFUN_KERNEL_API_C |
|
|
48 |
|
|
49 #include "testaux.h" |
|
|
50 |
|
|
51 #define NTHREADS 2 |
|
2
|
52 #define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL |
|
0
|
53 |
|
|
54 static cyg_handle_t thread[NTHREADS]; |
|
|
55 |
|
|
56 static cyg_thread thread_obj[NTHREADS]; |
|
|
57 static char stack[NTHREADS][STACKSIZE]; |
|
|
58 |
|
|
59 |
|
|
60 #define MEMSIZE 10240 |
|
|
61 |
|
|
62 static cyg_uint8 mem[2][MEMSIZE]; |
|
|
63 |
|
|
64 static cyg_mempool_fix mempool_obj[2]; |
|
|
65 static cyg_handle_t mempool0, mempool1; |
|
|
66 |
|
|
67 static void check_in_mp0(cyg_uint8 *p, cyg_int32 size) |
|
|
68 { |
|
|
69 CYG_TEST_CHECK(NULL != p, |
|
|
70 "Allocation failed"); |
|
|
71 CYG_TEST_CHECK(mem[0] <= p && p+size < mem[1], |
|
|
72 "Block outside memory pool"); |
|
|
73 } |
|
|
74 |
|
|
75 static void entry0( cyg_addrword_t data ) |
|
|
76 { |
|
|
77 cyg_uint8 *p0, *p1, *p2; |
|
|
78 cyg_mempool_info info0, info1, info2; |
|
|
79 |
|
|
80 cyg_mempool_fix_get_info(mempool0, &info0); |
|
|
81 CYG_TEST_CHECK(mem[0] == info0.base, "get_arena: base wrong"); |
|
|
82 CYG_TEST_CHECK(MEMSIZE == info0.size, "get_arena: size wrong"); |
|
|
83 |
|
|
84 CYG_TEST_CHECK(0 < info0.maxfree && info0.maxfree <= info0.size, |
|
|
85 "get_arena: maxfree wildly wrong"); |
|
|
86 |
|
|
87 CYG_TEST_CHECK(100 == info0.blocksize, "get_blocksize wrong" ); |
|
|
88 |
|
|
89 CYG_TEST_CHECK(info0.totalmem > 0, "Negative total memory" ); |
|
|
90 CYG_TEST_CHECK(info0.freemem > 0, "Negative free memory" ); |
|
|
91 CYG_TEST_CHECK(info0.totalmem <= MEMSIZE, |
|
|
92 "info.totalsize: Too much memory"); |
|
|
93 CYG_TEST_CHECK(info0.freemem <= info0.totalmem , |
|
|
94 "More memory free than possible" ); |
|
|
95 |
|
|
96 CYG_TEST_CHECK( !cyg_mempool_fix_waiting(mempool0) , |
|
|
97 "Thread waiting for memory; there shouldn't be"); |
|
|
98 |
|
|
99 p0 = cyg_mempool_fix_alloc(mempool0); |
|
|
100 check_in_mp0(p0, 100); |
|
|
101 |
|
|
102 cyg_mempool_fix_get_info(mempool0, &info1); |
|
|
103 CYG_TEST_CHECK(info1.freemem > 0, "Negative free memory" ); |
|
|
104 CYG_TEST_CHECK(info1.freemem < info0.freemem, |
|
|
105 "Free memory didn't decrease after allocation" ); |
|
|
106 |
|
|
107 p1 = NULL; |
|
|
108 while((p2 = cyg_mempool_fix_try_alloc(mempool0) )) |
|
2
|
109 p1 = p2; |
|
0
|
110 |
|
|
111 cyg_mempool_fix_get_info(mempool0, &info1); |
|
|
112 cyg_mempool_fix_free(mempool0, p0); |
|
|
113 |
|
|
114 cyg_mempool_fix_get_info(mempool0, &info2); |
|
|
115 CYG_TEST_CHECK(info2.freemem > info1.freemem, |
|
|
116 "Free memory didn't increase after free" ); |
|
|
117 |
|
|
118 // should be able to reallocate now a block is free |
|
|
119 p0 = cyg_mempool_fix_try_alloc(mempool0); |
|
|
120 check_in_mp0(p0, 100); |
|
|
121 |
|
|
122 CYG_TEST_CHECK(p1+100 <= p0 || p1 >= p0+100, |
|
|
123 "Ranges of allocated memory overlap"); |
|
|
124 |
|
|
125 cyg_mempool_fix_free(mempool0, p0); |
|
|
126 cyg_mempool_fix_free(mempool0, p1); |
|
|
127 |
|
|
128 #ifdef CYGFUN_KERNEL_THREADS_TIMER |
|
|
129 // This shouldn't have to wait |
|
|
130 p0 = cyg_mempool_fix_timed_alloc(mempool0, cyg_current_time()+100000); |
|
|
131 check_in_mp0(p0, 100); |
|
2
|
132 p1 = cyg_mempool_fix_timed_alloc(mempool0, cyg_current_time()+20); |
|
0
|
133 check_in_mp0(p1, 10); |
|
2
|
134 p1 = cyg_mempool_fix_timed_alloc(mempool0, cyg_current_time()+20); |
|
0
|
135 CYG_TEST_CHECK(NULL == p1, "Timed alloc unexpectedly worked"); |
|
|
136 |
|
|
137 // Expect thread 1 to have run while processing previous timed |
|
2
|
138 // allocation. It should therefore be waiting. |
|
0
|
139 CYG_TEST_CHECK(cyg_mempool_fix_waiting(mempool1), |
|
|
140 "There should be a thread waiting"); |
|
|
141 #endif |
|
|
142 |
|
|
143 CYG_TEST_PASS_FINISH("Kernal C API Fixed memory pool 1 OK"); |
|
|
144 } |
|
|
145 |
|
|
146 static void entry1( cyg_addrword_t data ) |
|
|
147 { |
|
|
148 while(NULL != cyg_mempool_fix_alloc(mempool1)) |
|
|
149 ; |
|
|
150 CYG_TEST_FAIL("alloc returned NULL"); |
|
|
151 } |
|
|
152 |
|
|
153 |
|
|
154 void kmemfix1_main( void ) |
|
|
155 { |
|
|
156 CYG_TEST_INIT(); |
|
|
157 |
|
|
158 cyg_thread_create(4, entry0 , (cyg_addrword_t)0, "kmemfix1-0", |
|
2
|
159 (void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]); |
|
0
|
160 cyg_thread_resume(thread[0]); |
|
|
161 |
|
|
162 cyg_thread_create(4, entry1 , (cyg_addrword_t)1, "kmemfix1-1", |
|
2
|
163 (void *)stack[1], STACKSIZE, &thread[1], &thread_obj[1]); |
|
0
|
164 cyg_thread_resume(thread[1]); |
|
|
165 |
|
|
166 cyg_mempool_fix_create(mem[0], MEMSIZE, 100, &mempool0, &mempool_obj[0]); |
|
|
167 cyg_mempool_fix_create(mem[1], MEMSIZE, 316, &mempool1, &mempool_obj[1]); |
|
|
168 |
|
|
169 cyg_scheduler_start(); |
|
|
170 |
|
|
171 CYG_TEST_FAIL_FINISH("Not reached"); |
|
|
172 } |
|
|
173 |
|
|
174 externC void |
|
|
175 cyg_start( void ) |
|
|
176 { |
|
|
177 kmemfix1_main(); |
|
|
178 } |
|
|
179 |
|
|
180 #else /* def CYGFUN_KERNEL_API_C */ |
|
|
181 externC void |
|
|
182 cyg_start( void ) |
|
|
183 { |
|
|
184 CYG_TEST_INIT(); |
|
2
|
185 CYG_TEST_NA("Kernel C API layer disabled"); |
|
0
|
186 } |
|
|
187 #endif /* def CYGFUN_KERNEL_API_C */ |
|
|
188 |
|
|
189 /* EOF kmemfix1.c */ |