1 /*==========================================================================
5 // Kernel C API Fixed memory pool test 1
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.
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.
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
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.
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.
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.
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####
43 // Author(s): dsm, jlarmour
46 // Description: Tests basic fixed memory pool functionality
47 //####DESCRIPTIONEND####
50 #include <pkgconf/memalloc.h>
52 #include <cyg/infra/testcase.h>
54 #ifdef CYGFUN_MEMALLOC_KAPI
56 #include <cyg/hal/hal_arch.h> // CYGNUM_HAL_STACK_SIZE_TYPICAL
58 #include <pkgconf/kernel.h>
60 #include <cyg/kernel/kapi.h>
63 #define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
65 static cyg_handle_t thread[NTHREADS];
67 static cyg_thread thread_obj[NTHREADS];
68 static char stack[NTHREADS][STACKSIZE];
73 static cyg_uint8 mem[2][MEMSIZE];
75 static cyg_mempool_fix mempool_obj[2];
76 static cyg_handle_t mempool0, mempool1;
78 static void check_in_mp0(cyg_uint8 *p, cyg_int32 size)
80 CYG_TEST_CHECK(NULL != p,
82 CYG_TEST_CHECK(mem[0] <= p && p+size < mem[1],
83 "Block outside memory pool");
86 static void entry0( cyg_addrword_t data )
88 cyg_uint8 *p0, *p1, *p2;
89 cyg_mempool_info info0, info1, info2;
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");
95 CYG_TEST_CHECK(0 < info0.maxfree && info0.maxfree <= info0.size,
96 "get_arena: maxfree wildly wrong");
98 CYG_TEST_CHECK(100 == info0.blocksize, "get_blocksize wrong" );
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" );
107 CYG_TEST_CHECK( !cyg_mempool_fix_waiting(mempool0) ,
108 "Thread waiting for memory; there shouldn't be");
110 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_FIXED_THREADAWARE
111 p0 = cyg_mempool_fix_alloc(mempool0);
112 check_in_mp0(p0, 100);
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" );
120 while((p2 = cyg_mempool_fix_try_alloc(mempool0) ))
123 cyg_mempool_fix_get_info(mempool0, &info1);
124 cyg_mempool_fix_free(mempool0, p0);
126 cyg_mempool_fix_get_info(mempool0, &info2);
127 CYG_TEST_CHECK(info2.freemem > info1.freemem,
128 "Free memory didn't increase after free" );
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);
135 CYG_TEST_CHECK(p1+100 <= p0 || p1 >= p0+100,
136 "Ranges of allocated memory overlap");
138 cyg_mempool_fix_free(mempool0, p0);
139 cyg_mempool_fix_free(mempool0, p1);
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");
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");
158 CYG_TEST_PASS_FINISH("Kernel C API Fixed memory pool 1 OK");
161 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_FIXED_THREADAWARE
162 static void entry1( cyg_addrword_t data )
164 while(NULL != cyg_mempool_fix_alloc(mempool1))
166 CYG_TEST_FAIL("alloc returned NULL");
171 void kmemfix1_main( void )
174 CYG_TEST_INFO("Starting Kernel C API Fixed memory pool 1 test");
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]);
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]);
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]);
189 cyg_scheduler_start();
191 CYG_TEST_FAIL_FINISH("Not reached");
200 #else /* def CYGFUN_MEMALLOC_KAPI */
205 CYG_TEST_NA("Kernel C API layer disabled");
207 #endif /* def CYGFUN_MEMALLOC_KAPI */