1 |
27 |
unneback |
/*==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// kmemvar1.cxx
|
4 |
|
|
//
|
5 |
|
|
// Kernel C API Variable memory pool 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
|
44 |
|
|
// Contributors: dsm
|
45 |
|
|
// Date: 1998-06-08
|
46 |
|
|
// Description: Tests basic variable memory pool functionality
|
47 |
|
|
//####DESCRIPTIONEND####
|
48 |
|
|
*/
|
49 |
|
|
|
50 |
|
|
#include <pkgconf/memalloc.h>
|
51 |
|
|
|
52 |
|
|
#include <cyg/infra/testcase.h>
|
53 |
|
|
|
54 |
|
|
#ifdef CYGFUN_MEMALLOC_KAPI
|
55 |
|
|
|
56 |
|
|
#include <cyg/hal/hal_arch.h> // CYGNUM_HAL_STACK_SIZE_TYPICAL
|
57 |
|
|
|
58 |
|
|
#include <pkgconf/kernel.h>
|
59 |
|
|
|
60 |
|
|
#include <cyg/kernel/kapi.h>
|
61 |
|
|
|
62 |
|
|
#define NTHREADS 2
|
63 |
|
|
#define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
|
64 |
|
|
|
65 |
|
|
static cyg_handle_t thread[NTHREADS];
|
66 |
|
|
|
67 |
|
|
static cyg_thread thread_obj[NTHREADS];
|
68 |
|
|
static char stack[NTHREADS][STACKSIZE];
|
69 |
|
|
|
70 |
|
|
|
71 |
|
|
#define MEMSIZE 10240
|
72 |
|
|
|
73 |
|
|
static cyg_uint8 mem[2][MEMSIZE];
|
74 |
|
|
|
75 |
|
|
static cyg_mempool_var mempool_obj[2];
|
76 |
|
|
static cyg_handle_t mempool0, mempool1;
|
77 |
|
|
|
78 |
|
|
static void check_in_mp0(cyg_uint8 *p, cyg_int32 size)
|
79 |
|
|
{
|
80 |
|
|
CYG_TEST_CHECK(NULL != p,
|
81 |
|
|
"Allocation failed");
|
82 |
|
|
CYG_TEST_CHECK(mem[0] <= p && p+size < mem[1],
|
83 |
|
|
"Block outside memory pool");
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
static void entry0( cyg_addrword_t data )
|
87 |
|
|
{
|
88 |
|
|
cyg_uint8 *p0, *p1;
|
89 |
|
|
cyg_int32 most_of_mem=MEMSIZE/4*3;
|
90 |
|
|
cyg_mempool_info info0, info1, info2;
|
91 |
|
|
|
92 |
|
|
cyg_mempool_var_get_info(mempool0, &info0);
|
93 |
|
|
|
94 |
|
|
CYG_TEST_CHECK(mem[0] == info0.base, "get_arena: base wrong");
|
95 |
|
|
CYG_TEST_CHECK(MEMSIZE == info0.size, "get_arena: size wrong");
|
96 |
|
|
|
97 |
|
|
CYG_TEST_CHECK(0 < info0.maxfree && info0.maxfree <= info0.size,
|
98 |
|
|
"get_arena: maxfree wildly wrong");
|
99 |
|
|
|
100 |
|
|
CYG_TEST_CHECK(-1 == info0.blocksize, "get_blocksize wrong" );
|
101 |
|
|
|
102 |
|
|
CYG_TEST_CHECK(info0.totalmem > 0, "Negative total memory" );
|
103 |
|
|
CYG_TEST_CHECK(info0.freemem > 0, "Negative free memory" );
|
104 |
|
|
CYG_TEST_CHECK(info0.totalmem <= MEMSIZE,
|
105 |
|
|
"info.totalsize: Too much memory");
|
106 |
|
|
CYG_TEST_CHECK(info0.freemem <= info0.totalmem ,
|
107 |
|
|
"More memory free than possible" );
|
108 |
|
|
|
109 |
|
|
CYG_TEST_CHECK( !cyg_mempool_var_waiting(mempool0),
|
110 |
|
|
"Thread waiting for memory; there shouldn't be");
|
111 |
|
|
|
112 |
|
|
CYG_TEST_CHECK( NULL == cyg_mempool_var_try_alloc(mempool0, MEMSIZE+1),
|
113 |
|
|
"Managed to allocate too much memory");
|
114 |
|
|
|
115 |
|
|
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
|
116 |
|
|
p0 = cyg_mempool_var_alloc(mempool0, most_of_mem);
|
117 |
|
|
check_in_mp0(p0, most_of_mem);
|
118 |
|
|
|
119 |
|
|
cyg_mempool_var_get_info(mempool0, &info1);
|
120 |
|
|
|
121 |
|
|
CYG_TEST_CHECK(info1.freemem > 0, "Negative free memory" );
|
122 |
|
|
CYG_TEST_CHECK(info1.freemem < info0.freemem,
|
123 |
|
|
"Free memory didn't decrease after allocation" );
|
124 |
|
|
|
125 |
|
|
CYG_TEST_CHECK( NULL == cyg_mempool_var_try_alloc(mempool0, most_of_mem),
|
126 |
|
|
"Managed to allocate too much memory");
|
127 |
|
|
|
128 |
|
|
cyg_mempool_var_free(mempool0, p0);
|
129 |
|
|
|
130 |
|
|
cyg_mempool_var_get_info(mempool0, &info2);
|
131 |
|
|
CYG_TEST_CHECK(info2.freemem > info1.freemem,
|
132 |
|
|
"Free memory didn't increase after free" );
|
133 |
|
|
#endif
|
134 |
|
|
|
135 |
|
|
// should be able to reallocate now memory is free
|
136 |
|
|
p0 = cyg_mempool_var_try_alloc(mempool0, most_of_mem);
|
137 |
|
|
check_in_mp0(p0, most_of_mem);
|
138 |
|
|
|
139 |
|
|
p1 = cyg_mempool_var_try_alloc(mempool0, 10);
|
140 |
|
|
check_in_mp0(p1, 10);
|
141 |
|
|
|
142 |
|
|
CYG_TEST_CHECK(p1+10 <= p0 || p1 >= p0+MEMSIZE,
|
143 |
|
|
"Ranges of allocated memory overlap");
|
144 |
|
|
|
145 |
|
|
cyg_mempool_var_free(mempool0, p0);
|
146 |
|
|
cyg_mempool_var_free(mempool0, p1);
|
147 |
|
|
|
148 |
|
|
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
|
149 |
|
|
# ifdef CYGFUN_KERNEL_THREADS_TIMER
|
150 |
|
|
// This shouldn't have to wait
|
151 |
|
|
p0 = cyg_mempool_var_timed_alloc(mempool0, most_of_mem,
|
152 |
|
|
cyg_current_time()+100000);
|
153 |
|
|
check_in_mp0(p0, most_of_mem);
|
154 |
|
|
p1 = cyg_mempool_var_timed_alloc(mempool0, most_of_mem,
|
155 |
|
|
cyg_current_time()+2);
|
156 |
|
|
CYG_TEST_CHECK(NULL == p1, "Timed alloc unexpectedly worked");
|
157 |
|
|
p1 = cyg_mempool_var_timed_alloc(mempool0, 10,
|
158 |
|
|
cyg_current_time()+2);
|
159 |
|
|
check_in_mp0(p1, 10);
|
160 |
|
|
|
161 |
|
|
// Expect thread 1 to have run while processing previous timed
|
162 |
|
|
// allocation. It should therefore tbe waiting.
|
163 |
|
|
CYG_TEST_CHECK(cyg_mempool_var_waiting(mempool1), "There should be a thread waiting");
|
164 |
|
|
# endif
|
165 |
|
|
#endif
|
166 |
|
|
|
167 |
|
|
CYG_TEST_PASS_FINISH("Kernel C API Variable memory pool 1 OK");
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
|
171 |
|
|
static void entry1( cyg_addrword_t data )
|
172 |
|
|
{
|
173 |
|
|
cyg_mempool_var_alloc(mempool1, MEMSIZE+1);
|
174 |
|
|
CYG_TEST_FAIL("Oversized alloc returned");
|
175 |
|
|
}
|
176 |
|
|
#endif
|
177 |
|
|
|
178 |
|
|
|
179 |
|
|
void kmemvar1_main( void )
|
180 |
|
|
{
|
181 |
|
|
CYG_TEST_INIT();
|
182 |
|
|
CYG_TEST_INFO("Starting Kernel C API Variable memory pool 1 test");
|
183 |
|
|
|
184 |
|
|
cyg_thread_create(4, entry0 , (cyg_addrword_t)0, "kmemvar1-0",
|
185 |
|
|
(void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]);
|
186 |
|
|
cyg_thread_resume(thread[0]);
|
187 |
|
|
|
188 |
|
|
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
|
189 |
|
|
cyg_thread_create(4, entry1 , (cyg_addrword_t)1, "kmemvar1-1",
|
190 |
|
|
(void *)stack[1], STACKSIZE, &thread[1], &thread_obj[1]);
|
191 |
|
|
cyg_thread_resume(thread[1]);
|
192 |
|
|
#endif
|
193 |
|
|
|
194 |
|
|
cyg_mempool_var_create(mem[0], MEMSIZE, &mempool0, &mempool_obj[0]);
|
195 |
|
|
cyg_mempool_var_create(mem[1], MEMSIZE, &mempool1, &mempool_obj[1]);
|
196 |
|
|
|
197 |
|
|
cyg_scheduler_start();
|
198 |
|
|
|
199 |
|
|
CYG_TEST_FAIL_FINISH("Not reached");
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
externC void
|
203 |
|
|
cyg_start( void )
|
204 |
|
|
{
|
205 |
|
|
kmemvar1_main();
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
#else /* ifdef CYGFUN_MEMALLOC_KAPI */
|
209 |
|
|
externC void
|
210 |
|
|
cyg_start( void )
|
211 |
|
|
{
|
212 |
|
|
CYG_TEST_INIT();
|
213 |
|
|
CYG_TEST_NA("Kernel C API layer disabled");
|
214 |
|
|
}
|
215 |
|
|
#endif /* ifdef CYGFUN_MEMALLOC_KAPI */
|
216 |
|
|
|
217 |
|
|
/* EOF kmemvar1.c */
|