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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [services/] [memalloc/] [common/] [current/] [tests/] [kmemvar1.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
//        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 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
43
// Contributors:  dsm
44
// Date:          1998-06-08
45
// Description:   Tests basic variable 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_var 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;
88
    cyg_int32 most_of_mem=MEMSIZE/4*3;
89
    cyg_mempool_info info0, info1, info2;
90
 
91
    cyg_mempool_var_get_info(mempool0, &info0);
92
 
93
    CYG_TEST_CHECK(mem[0] == info0.base, "get_arena: base wrong");
94
    CYG_TEST_CHECK(MEMSIZE == info0.size, "get_arena: size wrong");
95
 
96
    CYG_TEST_CHECK(0 < info0.maxfree && info0.maxfree <= info0.size,
97
                   "get_arena: maxfree wildly wrong");
98
 
99
    CYG_TEST_CHECK(-1 == info0.blocksize, "get_blocksize wrong" );
100
 
101
    CYG_TEST_CHECK(info0.totalmem > 0, "Negative total memory" );
102
    CYG_TEST_CHECK(info0.freemem > 0, "Negative free memory" );
103
    CYG_TEST_CHECK(info0.totalmem <= MEMSIZE,
104
                   "info.totalsize: Too much memory");
105
    CYG_TEST_CHECK(info0.freemem <= info0.totalmem ,
106
                   "More memory free than possible" );
107
 
108
    CYG_TEST_CHECK( !cyg_mempool_var_waiting(mempool0),
109
                    "Thread waiting for memory; there shouldn't be");
110
 
111
    CYG_TEST_CHECK( NULL == cyg_mempool_var_try_alloc(mempool0, MEMSIZE+1),
112
                    "Managed to allocate too much memory");
113
 
114
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
115
    p0 = cyg_mempool_var_alloc(mempool0, most_of_mem);
116
    check_in_mp0(p0, most_of_mem);
117
 
118
    cyg_mempool_var_get_info(mempool0, &info1);
119
 
120
    CYG_TEST_CHECK(info1.freemem > 0, "Negative free memory" );
121
    CYG_TEST_CHECK(info1.freemem < info0.freemem,
122
                   "Free memory didn't decrease after allocation" );
123
 
124
    CYG_TEST_CHECK( NULL == cyg_mempool_var_try_alloc(mempool0, most_of_mem),
125
                    "Managed to allocate too much memory");
126
 
127
    cyg_mempool_var_free(mempool0, p0);
128
 
129
    cyg_mempool_var_get_info(mempool0, &info2);
130
    CYG_TEST_CHECK(info2.freemem > info1.freemem,
131
                   "Free memory didn't increase after free" );
132
#endif
133
 
134
    // should be able to reallocate now memory is free
135
    p0 = cyg_mempool_var_try_alloc(mempool0, most_of_mem);
136
    check_in_mp0(p0, most_of_mem);
137
 
138
    p1 = cyg_mempool_var_try_alloc(mempool0, 10);
139
    check_in_mp0(p1, 10);
140
 
141
    CYG_TEST_CHECK(p1+10 <= p0 || p1 >= p0+MEMSIZE,
142
                   "Ranges of allocated memory overlap");
143
 
144
    cyg_mempool_var_free(mempool0, p0);
145
    cyg_mempool_var_free(mempool0, p1);
146
 
147
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
148
# ifdef CYGFUN_KERNEL_THREADS_TIMER
149
    // This shouldn't have to wait
150
    p0 = cyg_mempool_var_timed_alloc(mempool0, most_of_mem,
151
                                     cyg_current_time()+100000);
152
    check_in_mp0(p0, most_of_mem);
153
    p1 = cyg_mempool_var_timed_alloc(mempool0, most_of_mem,
154
                                     cyg_current_time()+2);
155
    CYG_TEST_CHECK(NULL == p1, "Timed alloc unexpectedly worked");
156
    p1 = cyg_mempool_var_timed_alloc(mempool0, 10,
157
                                     cyg_current_time()+2);
158
    check_in_mp0(p1, 10);
159
 
160
    // Expect thread 1 to have run while processing previous timed
161
    // allocation.  It should therefore tbe waiting.
162
    CYG_TEST_CHECK(cyg_mempool_var_waiting(mempool1), "There should be a thread waiting");
163
# endif
164
#endif
165
 
166
    CYG_TEST_PASS_FINISH("Kernel C API Variable memory pool 1 OK");
167
}
168
 
169
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
170
static void entry1( cyg_addrword_t data )
171
{
172
    cyg_mempool_var_alloc(mempool1, MEMSIZE+1);
173
    CYG_TEST_FAIL("Oversized alloc returned");
174
}
175
#endif
176
 
177
 
178
void kmemvar1_main( void )
179
{
180
    CYG_TEST_INIT();
181
    CYG_TEST_INFO("Starting Kernel C API Variable memory pool 1 test");
182
 
183
    cyg_thread_create(4, entry0 , (cyg_addrword_t)0, "kmemvar1-0",
184
        (void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]);
185
    cyg_thread_resume(thread[0]);
186
 
187
#ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
188
    cyg_thread_create(4, entry1 , (cyg_addrword_t)1, "kmemvar1-1",
189
        (void *)stack[1], STACKSIZE, &thread[1], &thread_obj[1]);
190
    cyg_thread_resume(thread[1]);
191
#endif
192
 
193
    cyg_mempool_var_create(mem[0], MEMSIZE, &mempool0, &mempool_obj[0]);
194
    cyg_mempool_var_create(mem[1], MEMSIZE, &mempool1, &mempool_obj[1]);
195
 
196
    cyg_scheduler_start();
197
 
198
    CYG_TEST_FAIL_FINISH("Not reached");
199
}
200
 
201
externC void
202
cyg_start( void )
203
{
204
    kmemvar1_main();
205
}
206
 
207
#else /* ifdef CYGFUN_MEMALLOC_KAPI */
208
externC void
209
cyg_start( void )
210
{
211
    CYG_TEST_INIT();
212
    CYG_TEST_NA("Kernel C API layer disabled");
213
}
214
#endif /* ifdef CYGFUN_MEMALLOC_KAPI */
215
 
216
/* EOF kmemvar1.c */

powered by: WebSVN 2.1.0

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