OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [services/] [memalloc/] [common/] [current/] [tests/] [kmemfix1.c] - Blame information for rev 838

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
/*==========================================================================
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 Free Software Foundation, 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
16
// version.
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21
// for more details.
22
//
23
// You should have received a copy of the GNU General Public License
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
26
//
27
// As a special exception, if other files instantiate templates or use
28
// macros or inline functions from this file, or you compile this file
29
// and link it with other works to produce a work based on this file,
30
// this file does not by itself cause the resulting work to be covered by
31
// the GNU General Public License. However the source code for this file
32
// must still be made available in accordance with section (3) of the GNU
33
// General Public License v2.
34
//
35
// This exception does not invalidate any other reasons why a work based
36
// on this file might be covered by the GNU General Public License.
37
// -------------------------------------------
38
// ####ECOSGPLCOPYRIGHTEND####
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):     dsm, jlarmour
43
// Contributors:
44
// Date:          2000-06-19
45
// Description:   Tests basic fixed memory pool functionality
46
//####DESCRIPTIONEND####
47
*/
48
 
49
#include <pkgconf/memalloc.h>
50
 
51
#include <cyg/infra/testcase.h>
52
 
53
#ifdef CYGFUN_MEMALLOC_KAPI
54
 
55
#include <cyg/hal/hal_arch.h>           // CYGNUM_HAL_STACK_SIZE_TYPICAL
56
 
57
#include <pkgconf/kernel.h>
58
 
59
#include <cyg/kernel/kapi.h>
60
 
61
#define NTHREADS 2
62
#define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
63
 
64
static cyg_handle_t thread[NTHREADS];
65
 
66
static cyg_thread thread_obj[NTHREADS];
67
static char stack[NTHREADS][STACKSIZE];
68
 
69
 
70
#define MEMSIZE 10240
71
 
72
static cyg_uint8 mem[2][MEMSIZE];
73
 
74
static cyg_mempool_fix mempool_obj[2];
75
static cyg_handle_t mempool0, mempool1;
76
 
77
static void check_in_mp0(cyg_uint8 *p, cyg_int32 size)
78
{
79
    CYG_TEST_CHECK(NULL != p,
80
                   "Allocation failed");
81
    CYG_TEST_CHECK(mem[0] <= p && p+size < mem[1],
82
                   "Block outside memory pool");
83
}
84
 
85
static void entry0( cyg_addrword_t data )
86
{
87
    cyg_uint8 *p0, *p1, *p2;
88
    cyg_mempool_info info0, info1, info2;
89
 
90
    cyg_mempool_fix_get_info(mempool0, &info0);
91
    CYG_TEST_CHECK(mem[0] == info0.base, "get_arena: base wrong");
92
    CYG_TEST_CHECK(MEMSIZE == info0.size, "get_arena: size wrong");
93
 
94
    CYG_TEST_CHECK(0 < info0.maxfree && info0.maxfree <= info0.size,
95
                   "get_arena: maxfree wildly wrong");
96
 
97
    CYG_TEST_CHECK(100 == info0.blocksize, "get_blocksize wrong" );
98
 
99
    CYG_TEST_CHECK(info0.totalmem > 0, "Negative total memory" );
100
    CYG_TEST_CHECK(info0.freemem > 0, "Negative free memory" );
101
    CYG_TEST_CHECK(info0.totalmem <= MEMSIZE,
102
                   "info.totalsize: Too much memory");
103
    CYG_TEST_CHECK(info0.freemem <= info0.totalmem ,
104
                   "More memory free than possible" );
105
 
106
    CYG_TEST_CHECK( !cyg_mempool_fix_waiting(mempool0)    ,
107
                    "Thread waiting for memory; there shouldn't be");
108
 
109
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_FIXED_THREADAWARE
110
    p0 = cyg_mempool_fix_alloc(mempool0);
111
    check_in_mp0(p0, 100);
112
 
113
    cyg_mempool_fix_get_info(mempool0, &info1);
114
    CYG_TEST_CHECK(info1.freemem > 0, "Negative free memory" );
115
    CYG_TEST_CHECK(info1.freemem < info0.freemem,
116
                   "Free memory didn't decrease after allocation" );
117
 
118
    p1 = NULL;
119
    while((p2 = cyg_mempool_fix_try_alloc(mempool0)    ))
120
        p1 = p2;
121
 
122
    cyg_mempool_fix_get_info(mempool0, &info1);
123
    cyg_mempool_fix_free(mempool0, p0);
124
 
125
    cyg_mempool_fix_get_info(mempool0, &info2);
126
    CYG_TEST_CHECK(info2.freemem > info1.freemem,
127
                   "Free memory didn't increase after free" );
128
#endif
129
 
130
    // should be able to reallocate now a block is free
131
    p0 = cyg_mempool_fix_try_alloc(mempool0);
132
    check_in_mp0(p0, 100);
133
 
134
    CYG_TEST_CHECK(p1+100 <= p0 || p1 >= p0+100,
135
                   "Ranges of allocated memory overlap");
136
 
137
    cyg_mempool_fix_free(mempool0, p0);
138
    cyg_mempool_fix_free(mempool0, p1);
139
 
140
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_FIXED_THREADAWARE
141
# ifdef CYGFUN_KERNEL_THREADS_TIMER
142
    // This shouldn't have to wait
143
    p0 = cyg_mempool_fix_timed_alloc(mempool0, cyg_current_time()+100000);
144
    check_in_mp0(p0, 100);
145
    p1 = cyg_mempool_fix_timed_alloc(mempool0, cyg_current_time()+20);
146
    check_in_mp0(p1, 10);
147
    p1 = cyg_mempool_fix_timed_alloc(mempool0, cyg_current_time()+20);
148
    CYG_TEST_CHECK(NULL == p1, "Timed alloc unexpectedly worked");
149
 
150
    // Expect thread 1 to have run while processing previous timed
151
    // allocation.  It should therefore be waiting.
152
    CYG_TEST_CHECK(cyg_mempool_fix_waiting(mempool1),
153
                   "There should be a thread waiting");
154
# endif
155
#endif
156
 
157
    CYG_TEST_PASS_FINISH("Kernel C API Fixed memory pool 1 OK");
158
}
159
 
160
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_FIXED_THREADAWARE
161
static void entry1( cyg_addrword_t data )
162
{
163
    while(NULL != cyg_mempool_fix_alloc(mempool1))
164
        ;
165
    CYG_TEST_FAIL("alloc returned NULL");
166
}
167
#endif
168
 
169
 
170
void kmemfix1_main( void )
171
{
172
    CYG_TEST_INIT();
173
    CYG_TEST_INFO("Starting Kernel C API Fixed memory pool 1 test");
174
 
175
    cyg_thread_create(4, entry0 , (cyg_addrword_t)0, "kmemfix1-0",
176
        (void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]);
177
    cyg_thread_resume(thread[0]);
178
 
179
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_FIXED_THREADAWARE
180
    cyg_thread_create(4, entry1 , (cyg_addrword_t)1, "kmemfix1-1",
181
        (void *)stack[1], STACKSIZE, &thread[1], &thread_obj[1]);
182
    cyg_thread_resume(thread[1]);
183
#endif
184
 
185
    cyg_mempool_fix_create(mem[0], MEMSIZE, 100, &mempool0, &mempool_obj[0]);
186
    cyg_mempool_fix_create(mem[1], MEMSIZE, 316, &mempool1, &mempool_obj[1]);
187
 
188
    cyg_scheduler_start();
189
 
190
    CYG_TEST_FAIL_FINISH("Not reached");
191
}
192
 
193
externC void
194
cyg_start( void )
195
{
196
    kmemfix1_main();
197
}
198
 
199
#else /* def CYGFUN_MEMALLOC_KAPI */
200
externC void
201
cyg_start( void )
202
{
203
    CYG_TEST_INIT();
204
    CYG_TEST_NA("Kernel C API layer disabled");
205
}
206
#endif /* def CYGFUN_MEMALLOC_KAPI */
207
 
208
/* EOF kmemfix1.c */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.