|
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 |
|
|
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-09 |
|
|
35 // Description: Tests basic fixed 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_fix 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, *p2; |
|
|
76 cyg_mempool_info info0, info1, info2; |
|
|
77 |
|
|
78 cyg_mempool_fix_get_info(mempool0, &info0); |
|
|
79 CYG_TEST_CHECK(mem[0] == info0.base, "get_arena: base wrong"); |
|
|
80 CYG_TEST_CHECK(MEMSIZE == info0.size, "get_arena: size wrong"); |
|
|
81 |
|
|
82 CYG_TEST_CHECK(0 < info0.maxfree && info0.maxfree <= info0.size, |
|
|
83 "get_arena: maxfree wildly wrong"); |
|
|
84 |
|
|
85 CYG_TEST_CHECK(100 == info0.blocksize, "get_blocksize wrong" ); |
|
|
86 |
|
|
87 CYG_TEST_CHECK(info0.totalmem > 0, "Negative total memory" ); |
|
|
88 CYG_TEST_CHECK(info0.freemem > 0, "Negative free memory" ); |
|
|
89 CYG_TEST_CHECK(info0.totalmem <= MEMSIZE, |
|
|
90 "info.totalsize: Too much memory"); |
|
|
91 CYG_TEST_CHECK(info0.freemem <= info0.totalmem , |
|
|
92 "More memory free than possible" ); |
|
|
93 |
|
|
94 CYG_TEST_CHECK( !cyg_mempool_fix_waiting(mempool0) , |
|
|
95 "Thread waiting for memory; there shouldn't be"); |
|
|
96 |
|
|
97 p0 = cyg_mempool_fix_alloc(mempool0); |
|
|
98 check_in_mp0(p0, 100); |
|
|
99 |
|
|
100 cyg_mempool_fix_get_info(mempool0, &info1); |
|
|
101 CYG_TEST_CHECK(info1.freemem > 0, "Negative free memory" ); |
|
|
102 CYG_TEST_CHECK(info1.freemem < info0.freemem, |
|
|
103 "Free memory didn't decrease after allocation" ); |
|
|
104 |
|
|
105 p1 = NULL; |
|
|
106 while((p2 = cyg_mempool_fix_try_alloc(mempool0) )) |
|
|
107 p1 = p2; |
|
|
108 |
|
|
109 cyg_mempool_fix_get_info(mempool0, &info1); |
|
|
110 cyg_mempool_fix_free(mempool0, p0); |
|
|
111 |
|
|
112 cyg_mempool_fix_get_info(mempool0, &info2); |
|
|
113 CYG_TEST_CHECK(info2.freemem > info1.freemem, |
|
|
114 "Free memory didn't increase after free" ); |
|
|
115 |
|
|
116 // should be able to reallocate now a block is free |
|
|
117 p0 = cyg_mempool_fix_try_alloc(mempool0); |
|
|
118 check_in_mp0(p0, 100); |
|
|
119 |
|
|
120 CYG_TEST_CHECK(p1+100 <= p0 || p1 >= p0+100, |
|
|
121 "Ranges of allocated memory overlap"); |
|
|
122 |
|
|
123 cyg_mempool_fix_free(mempool0, p0); |
|
|
124 cyg_mempool_fix_free(mempool0, p1); |
|
|
125 |
|
|
126 #ifdef CYGFUN_KERNEL_THREADS_TIMER |
|
|
127 // This shouldn't have to wait |
|
|
128 p0 = cyg_mempool_fix_timed_alloc(mempool0, cyg_current_time()+100000); |
|
|
129 check_in_mp0(p0, 100); |
|
|
130 p1 = cyg_mempool_fix_timed_alloc(mempool0, cyg_current_time()+2); |
|
|
131 check_in_mp0(p1, 10); |
|
|
132 p1 = cyg_mempool_fix_timed_alloc(mempool0, cyg_current_time()+2); |
|
|
133 CYG_TEST_CHECK(NULL == p1, "Timed alloc unexpectedly worked"); |
|
|
134 |
|
|
135 // Expect thread 1 to have run while processing previous timed |
|
|
136 // allocation. It should therefore tbe waiting. |
|
|
137 CYG_TEST_CHECK(cyg_mempool_fix_waiting(mempool1), |
|
|
138 "There should be a thread waiting"); |
|
|
139 #endif |
|
|
140 |
|
|
141 CYG_TEST_PASS_FINISH("Kernal C API Fixed memory pool 1 OK"); |
|
|
142 } |
|
|
143 |
|
|
144 static void entry1( cyg_addrword_t data ) |
|
|
145 { |
|
|
146 while(NULL != cyg_mempool_fix_alloc(mempool1)) |
|
|
147 ; |
|
|
148 CYG_TEST_FAIL("alloc returned NULL"); |
|
|
149 } |
|
|
150 |
|
|
151 |
|
|
152 void kmemfix1_main( void ) |
|
|
153 { |
|
|
154 CYG_TEST_INIT(); |
|
|
155 |
|
|
156 cyg_thread_create(4, entry0 , (cyg_addrword_t)0, "kmemfix1-0", |
|
|
157 (void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]); |
|
|
158 cyg_thread_resume(thread[0]); |
|
|
159 |
|
|
160 cyg_thread_create(4, entry1 , (cyg_addrword_t)1, "kmemfix1-1", |
|
|
161 (void *)stack[1], STACKSIZE, &thread[1], &thread_obj[1]); |
|
|
162 cyg_thread_resume(thread[1]); |
|
|
163 |
|
|
164 cyg_mempool_fix_create(mem[0], MEMSIZE, 100, &mempool0, &mempool_obj[0]); |
|
|
165 cyg_mempool_fix_create(mem[1], MEMSIZE, 316, &mempool1, &mempool_obj[1]); |
|
|
166 |
|
|
167 cyg_scheduler_start(); |
|
|
168 |
|
|
169 CYG_TEST_FAIL_FINISH("Not reached"); |
|
|
170 } |
|
|
171 |
|
|
172 externC void |
|
|
173 cyg_start( void ) |
|
|
174 { |
|
|
175 kmemfix1_main(); |
|
|
176 } |
|
|
177 |
|
|
178 #else /* def CYGFUN_KERNEL_API_C */ |
|
|
179 externC void |
|
|
180 cyg_start( void ) |
|
|
181 { |
|
|
182 CYG_TEST_INIT(); |
|
|
183 CYG_TEST_PASS_FINISH("Kernel C API layer disabled"); |
|
|
184 } |
|
|
185 #endif /* def CYGFUN_KERNEL_API_C */ |
|
|
186 |
|
|
187 /* EOF kmemfix1.c */ |