]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/services/memalloc/common/v2_0/tests/kmemfix1.c
Initial revision
[karo-tx-redboot.git] / packages / services / memalloc / common / v2_0 / tests / kmemfix1.c
1 /*==========================================================================
2 //
3 //        kmemfix1.cxx
4 //
5 //        Kernel C API Fixed 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, jlarmour
44 // Contributors:  
45 // Date:          2000-06-19
46 // Description:   Tests basic fixed 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_fix 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, *p2;
89     cyg_mempool_info info0, info1, info2;
90
91     cyg_mempool_fix_get_info(mempool0, &info0);
92     CYG_TEST_CHECK(mem[0] == info0.base, "get_arena: base wrong");
93     CYG_TEST_CHECK(MEMSIZE == info0.size, "get_arena: size wrong");
94
95     CYG_TEST_CHECK(0 < info0.maxfree && info0.maxfree <= info0.size,
96                    "get_arena: maxfree wildly wrong");
97     
98     CYG_TEST_CHECK(100 == info0.blocksize, "get_blocksize wrong" );
99
100     CYG_TEST_CHECK(info0.totalmem > 0, "Negative total memory" );
101     CYG_TEST_CHECK(info0.freemem > 0, "Negative free memory" );
102     CYG_TEST_CHECK(info0.totalmem <= MEMSIZE, 
103                    "info.totalsize: Too much memory");
104     CYG_TEST_CHECK(info0.freemem <= info0.totalmem ,
105                    "More memory free than possible" );
106
107     CYG_TEST_CHECK( !cyg_mempool_fix_waiting(mempool0)    ,
108                     "Thread waiting for memory; there shouldn't be");
109     
110 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_FIXED_THREADAWARE
111     p0 = cyg_mempool_fix_alloc(mempool0);
112     check_in_mp0(p0, 100);
113
114     cyg_mempool_fix_get_info(mempool0, &info1);
115     CYG_TEST_CHECK(info1.freemem > 0, "Negative free memory" );
116     CYG_TEST_CHECK(info1.freemem < info0.freemem,
117                    "Free memory didn't decrease after allocation" );
118
119     p1 = NULL;
120     while((p2 = cyg_mempool_fix_try_alloc(mempool0)    ))
121         p1 = p2;
122     
123     cyg_mempool_fix_get_info(mempool0, &info1);
124     cyg_mempool_fix_free(mempool0, p0);
125
126     cyg_mempool_fix_get_info(mempool0, &info2);
127     CYG_TEST_CHECK(info2.freemem > info1.freemem,
128                    "Free memory didn't increase after free" );
129 #endif
130     
131     // should be able to reallocate now a block is free
132     p0 = cyg_mempool_fix_try_alloc(mempool0);
133     check_in_mp0(p0, 100);
134
135     CYG_TEST_CHECK(p1+100 <= p0 || p1 >= p0+100,
136                    "Ranges of allocated memory overlap");
137
138     cyg_mempool_fix_free(mempool0, p0);
139     cyg_mempool_fix_free(mempool0, p1);
140     
141 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_FIXED_THREADAWARE
142 # ifdef CYGFUN_KERNEL_THREADS_TIMER
143     // This shouldn't have to wait
144     p0 = cyg_mempool_fix_timed_alloc(mempool0, cyg_current_time()+100000);
145     check_in_mp0(p0, 100);
146     p1 = cyg_mempool_fix_timed_alloc(mempool0, cyg_current_time()+20);
147     check_in_mp0(p1, 10);
148     p1 = cyg_mempool_fix_timed_alloc(mempool0, cyg_current_time()+20);
149     CYG_TEST_CHECK(NULL == p1, "Timed alloc unexpectedly worked");
150     
151     // Expect thread 1 to have run while processing previous timed
152     // allocation.  It should therefore be waiting.
153     CYG_TEST_CHECK(cyg_mempool_fix_waiting(mempool1),
154                    "There should be a thread waiting");
155 # endif
156 #endif
157     
158     CYG_TEST_PASS_FINISH("Kernel C API Fixed memory pool 1 OK");
159 }
160
161 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_FIXED_THREADAWARE
162 static void entry1( cyg_addrword_t data )
163 {
164     while(NULL != cyg_mempool_fix_alloc(mempool1))
165         ;
166     CYG_TEST_FAIL("alloc returned NULL");
167 }
168 #endif
169
170
171 void kmemfix1_main( void )
172 {
173     CYG_TEST_INIT();
174     CYG_TEST_INFO("Starting Kernel C API Fixed memory pool 1 test");
175
176     cyg_thread_create(4, entry0 , (cyg_addrword_t)0, "kmemfix1-0",
177         (void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]);
178     cyg_thread_resume(thread[0]);
179
180 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_FIXED_THREADAWARE
181     cyg_thread_create(4, entry1 , (cyg_addrword_t)1, "kmemfix1-1",
182         (void *)stack[1], STACKSIZE, &thread[1], &thread_obj[1]);
183     cyg_thread_resume(thread[1]);
184 #endif
185
186     cyg_mempool_fix_create(mem[0], MEMSIZE, 100, &mempool0, &mempool_obj[0]);
187     cyg_mempool_fix_create(mem[1], MEMSIZE, 316, &mempool1, &mempool_obj[1]);
188
189     cyg_scheduler_start();
190
191     CYG_TEST_FAIL_FINISH("Not reached");
192 }
193
194 externC void
195 cyg_start( void )
196
197     kmemfix1_main();
198 }
199
200 #else /* def CYGFUN_MEMALLOC_KAPI */
201 externC void
202 cyg_start( void )
203 {
204     CYG_TEST_INIT();
205     CYG_TEST_NA("Kernel C API layer disabled");
206 }
207 #endif /* def CYGFUN_MEMALLOC_KAPI */
208
209 /* EOF kmemfix1.c */