comparison packages/services/memalloc/common/current/tests/memvar1.cxx @ 115:6ed91473a1cd ecos-sw-2000-08-21

Merge from eCos master repository on 2000-08-21-22:40:54-BST
author jlarmour
date Fri, 25 Aug 2000 17:32:38 +0000
parents
children eb9fd8c04db3
comparison
equal deleted inserted replaced
114:5ad2b71d525e 115:6ed91473a1cd
1 //==========================================================================
2 //
3 // memvar1.cxx
4 //
5 // Variable memory pool test 1
6 //
7 //==========================================================================
8 //####COPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 // The contents of this file are subject to the Red Hat eCos Public License
12 // Version 1.1 (the "License"); you may not use this file except in
13 // compliance with the License. You may obtain a copy of the License at
14 // http://www.redhat.com/
15 //
16 // Software distributed under the License is distributed on an "AS IS"
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18 // License for the specific language governing rights and limitations under
19 // the License.
20 //
21 // The Original Code is eCos - Embedded Configurable Operating System,
22 // released September 30, 1998.
23 //
24 // The Initial Developer of the Original Code is Red Hat.
25 // Portions created by Red Hat are
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
27 // All Rights Reserved.
28 // -------------------------------------------
29 //
30 //####COPYRIGHTEND####
31 //==========================================================================
32 //#####DESCRIPTIONBEGIN####
33 //
34 // Author(s): dsm, jlarmour
35 // Contributors:
36 // Date: 2000-06-18
37 // Description: Tests basic variable memory pool functionality
38 //####DESCRIPTIONEND####
39
40 #include <pkgconf/memalloc.h>
41 #include <pkgconf/system.h>
42
43 #ifdef CYGPKG_KERNEL
44 #include <pkgconf/kernel.h>
45
46 #include <cyg/kernel/sched.hxx> // Cyg_Scheduler::start()
47 #include <cyg/kernel/thread.hxx> // Cyg_Thread
48
49 #include <cyg/kernel/sched.inl>
50 #include <cyg/kernel/thread.inl>
51
52 #include <cyg/kernel/timer.hxx> // Cyg_Timer
53 #include <cyg/kernel/clock.inl> // Cyg_Clock
54
55 #define NTHREADS 2
56 #include "testaux.hxx"
57
58 #endif
59
60 #include <cyg/memalloc/memvar.hxx>
61
62 #include <cyg/infra/testcase.h>
63
64 static const cyg_int32 memsize = 10240;
65
66 static cyg_uint8 mem[2][memsize];
67
68 static Cyg_Mempool_Variable mempool0(mem[0], memsize);
69
70 static Cyg_Mempool_Variable mempool1(mem[1], memsize);
71
72
73 static void check_in_mp0(cyg_uint8 *p, cyg_int32 size)
74 {
75 CYG_TEST_CHECK(NULL != p,
76 "Allocation failed");
77 CYG_TEST_CHECK(mem[0] <= p && p+size < mem[1],
78 "Block outside memory pool");
79 }
80
81
82 static void entry0( CYG_ADDRWORD data )
83 {
84 cyg_int32 f0,f1,f2,t0;
85 cyg_uint8 *p0, *p1;
86 cyg_int32 most_of_mem=memsize/4*3;
87 Cyg_Mempool_Status stat;
88
89 mempool0.get_status( CYG_MEMPOOL_STAT_ORIGBASE|
90 CYG_MEMPOOL_STAT_BLOCKSIZE|
91 CYG_MEMPOOL_STAT_MAXFREE|
92 CYG_MEMPOOL_STAT_ORIGSIZE, stat );
93
94 CYG_TEST_CHECK(mem[0] == stat.origbase, "get_status: base wrong");
95 CYG_TEST_CHECK(memsize == stat.origsize, "get_status: size wrong");
96
97 CYG_TEST_CHECK(0 < stat.maxfree && stat.maxfree <= stat.origsize,
98 "get_status: maxfree wildly wrong");
99
100 CYG_TEST_CHECK(-1 == stat.blocksize, "blocksize wrong" );
101
102 mempool0.get_status( CYG_MEMPOOL_STAT_TOTALFREE|
103 CYG_MEMPOOL_STAT_ARENASIZE, stat );
104 t0 = stat.arenasize;
105 CYG_TEST_CHECK(t0 > 0, "Negative total memory" );
106 f0 = stat.totalfree;
107 CYG_TEST_CHECK(f0 > 0, "Negative free memory" );
108 CYG_TEST_CHECK(t0 <= memsize, "get_totalsize: Too much memory");
109 CYG_TEST_CHECK(f0 <= t0 , "More memory free than possible" );
110
111 mempool0.get_status( CYG_MEMPOOL_STAT_WAITING, stat );
112 CYG_TEST_CHECK( !stat.waiting,
113 "Thread waiting for memory; there shouldn't be");
114
115 CYG_TEST_CHECK( NULL == mempool0.try_alloc(memsize+1),
116 "Managed to allocate too much memory");
117
118 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
119 p0 = mempool0.alloc(most_of_mem);
120 #else
121 p0 = mempool0.try_alloc(most_of_mem);
122 #endif
123 check_in_mp0(p0, most_of_mem);
124
125 mempool0.get_status( CYG_MEMPOOL_STAT_TOTALFREE, stat );
126 f1 = stat.totalfree;
127 CYG_TEST_CHECK(f1 > 0, "Negative free memory" );
128 CYG_TEST_CHECK(f1 < f0, "Free memory didn't decrease after allocation" );
129
130 CYG_TEST_CHECK( NULL == mempool0.try_alloc(most_of_mem),
131 "Managed to allocate too much memory");
132
133 CYG_TEST_CHECK(mempool0.free(p0, most_of_mem), "Couldn't free");
134
135 mempool0.get_status( CYG_MEMPOOL_STAT_TOTALFREE, stat );
136 f2 = stat.totalfree;
137 CYG_TEST_CHECK(f2 > f1, "Free memory didn't increase after free" );
138
139 // should be able to reallocate now memory is free
140 p0 = mempool0.try_alloc(most_of_mem);
141 check_in_mp0(p0, most_of_mem);
142
143 p1 = mempool0.try_alloc(10);
144 check_in_mp0(p1, 10);
145
146 CYG_TEST_CHECK(p1+10 <= p0 || p1 >= p0+most_of_mem,
147 "Ranges of allocated memory overlap");
148
149 CYG_TEST_CHECK(mempool0.free(p0, 0), "Couldn't free");
150 CYG_TEST_CHECK(mempool0.free(p1, 10), "Couldn't free");
151
152 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
153 # ifdef CYGFUN_KERNEL_THREADS_TIMER
154 // This shouldn't have to wait
155 p0 = mempool0.alloc(most_of_mem,
156 Cyg_Clock::real_time_clock->current_value() + 100000);
157 check_in_mp0(p0, most_of_mem);
158 p1 = mempool0.alloc(most_of_mem,
159 Cyg_Clock::real_time_clock->current_value() + 2);
160 CYG_TEST_CHECK(NULL == p1, "Timed alloc unexpectedly worked");
161 p1 = mempool0.alloc(10,
162 Cyg_Clock::real_time_clock->current_value() + 2);
163 check_in_mp0(p1, 10);
164
165 // Expect thread 1 to have run while processing previous timed
166 // allocation. It should therefore tbe waiting.
167 mempool1.get_status( CYG_MEMPOOL_STAT_WAITING, stat );
168 CYG_TEST_CHECK(stat.waiting, "There should be a thread waiting");
169 # endif
170 #endif
171
172 CYG_TEST_PASS_FINISH("Variable memory pool 1 OK");
173 }
174
175 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
176 static void entry1( CYG_ADDRWORD data )
177 {
178 mempool1.alloc(memsize+1);
179 CYG_TEST_FAIL("Oversized alloc returned");
180 }
181 #endif
182
183 void memvar1_main( void )
184 {
185 CYG_TEST_INIT();
186 CYG_TEST_INFO("Starting Variable memory pool 1 test");
187
188 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
189 new_thread(entry0, 0);
190 new_thread(entry1, 1);
191
192 Cyg_Scheduler::start();
193 #elif defined(CYGPKG_KERNEL)
194 new_thread(entry0, 0);
195
196 Cyg_Scheduler::start();
197 #else
198 entry0(0);
199 #endif
200
201 CYG_TEST_FAIL_FINISH("Not reached");
202 }
203
204 externC void
205 cyg_start( void )
206 {
207 memvar1_main();
208 }
209 // EOF memvar1.cxx