|
0
|
1 /*========================================================================== |
|
|
2 // |
|
|
3 // kmemvar1.cxx |
|
|
4 // |
|
|
5 // Kernel C API Variable 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 |
|
|
25 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved. |
|
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //========================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author(s): dsm |
|
|
33 // Contributors: dsm |
|
|
34 // Date: 1998-06-08 |
|
|
35 // Description: Tests basic variable memory pool functionality |
|
|
36 //####DESCRIPTIONEND#### |
|
|
37 */ |
|
|
38 |
|
|
39 #include <pkgconf/kernel.h> |
|
|
40 |
|
|
41 #include <cyg/kernel/kapi.h> |
|
|
42 |
|
|
43 #include <cyg/infra/testcase.h> |
|
|
44 |
|
|
45 #ifdef CYGFUN_KERNEL_API_C |
|
|
46 |
|
|
47 #include "testaux.h" |
|
|
48 |
|
|
49 #define NTHREADS 2 |
|
|
50 #define STACKSIZE 4096 |
|
|
51 |
|
|
52 static cyg_handle_t thread[NTHREADS]; |
|
|
53 |
|
|
54 static cyg_thread thread_obj[NTHREADS]; |
|
|
55 static char stack[NTHREADS][STACKSIZE]; |
|
|
56 |
|
|
57 |
|
|
58 #define MEMSIZE 10240 |
|
|
59 |
|
|
60 static cyg_uint8 mem[2][MEMSIZE]; |
|
|
61 |
|
|
62 static cyg_mempool_var mempool_obj[2]; |
|
|
63 static cyg_handle_t mempool0, mempool1; |
|
|
64 |
|
|
65 static void check_in_mp0(cyg_uint8 *p, cyg_int32 size) |
|
|
66 { |
|
|
67 CYG_TEST_CHECK(NULL != p, |
|
|
68 "Allocation failed"); |
|
|
69 CYG_TEST_CHECK(mem[0] <= p && p+size < mem[1], |
|
|
70 "Block outside memory pool"); |
|
|
71 } |
|
|
72 |
|
|
73 static void entry0( cyg_addrword_t data ) |
|
|
74 { |
|
|
75 cyg_uint8 *p0, *p1; |
|
|
76 cyg_int32 most_of_mem=MEMSIZE/4*3; |
|
|
77 cyg_mempool_info info0, info1, info2; |
|
|
78 |
|
|
79 cyg_mempool_var_get_info(mempool0, &info0); |
|
|
80 |
|
|
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(-1 == 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_var_waiting(mempool0), |
|
|
97 "Thread waiting for memory; there shouldn't be"); |
|
|
98 |
|
|
99 CYG_TEST_CHECK( NULL == cyg_mempool_var_try_alloc(mempool0, MEMSIZE+1), |
|
|
100 "Managed to allocate too much memory"); |
|
|
101 |
|
|
102 p0 = cyg_mempool_var_alloc(mempool0, most_of_mem); |
|
|
103 check_in_mp0(p0, most_of_mem); |
|
|
104 |
|
|
105 cyg_mempool_var_get_info(mempool0, &info1); |
|
|
106 |
|
|
107 CYG_TEST_CHECK(info1.freemem > 0, "Negative free memory" ); |
|
|
108 CYG_TEST_CHECK(info1.freemem < info0.freemem, |
|
|
109 "Free memory didn't decrease after allocation" ); |
|
|
110 |
|
|
111 CYG_TEST_CHECK( NULL == cyg_mempool_var_try_alloc(mempool0, most_of_mem), |
|
|
112 "Managed to allocate too much memory"); |
|
|
113 |
|
|
114 cyg_mempool_var_free(mempool0, p0); |
|
|
115 |
|
|
116 cyg_mempool_var_get_info(mempool0, &info2); |
|
|
117 CYG_TEST_CHECK(info2.freemem > info1.freemem, |
|
|
118 "Free memory didn't increase after free" ); |
|
|
119 |
|
|
120 // should be able to reallocate now memory is free |
|
|
121 p0 = cyg_mempool_var_try_alloc(mempool0, most_of_mem); |
|
|
122 check_in_mp0(p0, most_of_mem); |
|
|
123 |
|
|
124 p1 = cyg_mempool_var_try_alloc(mempool0, 10); |
|
|
125 check_in_mp0(p1, 10); |
|
|
126 |
|
|
127 CYG_TEST_CHECK(p1+10 <= p0 || p1 >= p0+MEMSIZE, |
|
|
128 "Ranges of allocated memory overlap"); |
|
|
129 |
|
|
130 cyg_mempool_var_free(mempool0, p0); |
|
|
131 cyg_mempool_var_free(mempool0, p1); |
|
|
132 |
|
|
133 #ifdef CYGFUN_KERNEL_THREADS_TIMER |
|
|
134 // This shouldn't have to wait |
|
|
135 p0 = cyg_mempool_var_timed_alloc(mempool0, most_of_mem, |
|
|
136 cyg_current_time()+100000); |
|
|
137 check_in_mp0(p0, most_of_mem); |
|
|
138 p1 = cyg_mempool_var_timed_alloc(mempool0, most_of_mem, |
|
|
139 cyg_current_time()+2); |
|
|
140 CYG_TEST_CHECK(NULL == p1, "Timed alloc unexpectedly worked"); |
|
|
141 p1 = cyg_mempool_var_timed_alloc(mempool0, 10, |
|
|
142 cyg_current_time()+2); |
|
|
143 check_in_mp0(p1, 10); |
|
|
144 |
|
|
145 // Expect thread 1 to have run while processing previous timed |
|
|
146 // allocation. It should therefore tbe waiting. |
|
|
147 CYG_TEST_CHECK(cyg_mempool_var_waiting(mempool1), "There should be a thread waiting"); |
|
|
148 #endif |
|
|
149 |
|
|
150 CYG_TEST_PASS_FINISH("Kernel C API Variable memory pool 1 OK"); |
|
|
151 } |
|
|
152 |
|
|
153 static void entry1( cyg_addrword_t data ) |
|
|
154 { |
|
|
155 cyg_mempool_var_alloc(mempool1, MEMSIZE+1); |
|
|
156 CYG_TEST_FAIL("Oversized alloc returned"); |
|
|
157 } |
|
|
158 |
|
|
159 |
|
|
160 void kmemvar1_main( void ) |
|
|
161 { |
|
|
162 CYG_TEST_INIT(); |
|
|
163 |
|
|
164 cyg_thread_create(4, entry0 , (cyg_addrword_t)0, "kmemvar1-0", |
|
|
165 (void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]); |
|
|
166 cyg_thread_resume(thread[0]); |
|
|
167 |
|
|
168 cyg_thread_create(4, entry1 , (cyg_addrword_t)1, "kmemvar1-1", |
|
|
169 (void *)stack[1], STACKSIZE, &thread[1], &thread_obj[1]); |
|
|
170 cyg_thread_resume(thread[1]); |
|
|
171 |
|
|
172 cyg_mempool_var_create(mem[0], MEMSIZE, &mempool0, &mempool_obj[0]); |
|
|
173 cyg_mempool_var_create(mem[1], MEMSIZE, &mempool1, &mempool_obj[1]); |
|
|
174 |
|
|
175 cyg_scheduler_start(); |
|
|
176 |
|
|
177 CYG_TEST_FAIL_FINISH("Not reached"); |
|
|
178 } |
|
|
179 |
|
|
180 externC void |
|
|
181 cyg_start( void ) |
|
|
182 { |
|
|
183 kmemvar1_main(); |
|
|
184 } |
|
|
185 |
|
|
186 #else /* def CYGFUN_KERNEL_API_C */ |
|
|
187 externC void |
|
|
188 cyg_start( void ) |
|
|
189 { |
|
|
190 CYG_TEST_INIT(); |
|
|
191 CYG_TEST_PASS_FINISH("Kernel C API layer disabled"); |
|
|
192 } |
|
|
193 #endif /* def CYGFUN_KERNEL_API_C */ |
|
|
194 |
|
|
195 /* EOF kmemvar1.c */ |