Mercurial > ecos
annotate packages/kernel/current/tests/kmemvar1.c @ 66:bf00f99aec69 ecos-sw-2000-02-02
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
| author | jlarmour |
|---|---|
| date | Wed, 02 Feb 2000 19:57:02 +0000 |
| parents | c38311975d4f |
| children |
| rev | line source |
|---|---|
| 0 | 1 /*========================================================================== |
| 2 // | |
| 3 // kmemvar1.cxx | |
| 4 // | |
| 5 // Kernel C API Variable memory pool test 1 | |
| 6 // | |
| 7 //========================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
9 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
10 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
11 // The contents of this file are subject to the Red Hat eCos Public License |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
12 // Version 1.1 (the "License"); you may not use this file except in |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
13 // compliance with the License. You may obtain a copy of the License at |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
14 // http://www.redhat.com/ |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
15 // |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
18 // License for the specific language governing rights and limitations under |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
19 // the License. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
20 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
21 // The Original Code is eCos - Embedded Configurable Operating System, |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
22 // released September 30, 1998. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
23 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
24 // The Initial Developer of the Original Code is Red Hat. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
25 // Portions created by Red Hat are |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
27 // All Rights Reserved. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
28 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
29 // |
| 0 | 30 //####COPYRIGHTEND#### |
| 31 //========================================================================== | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 34 // Author(s): dsm | |
| 35 // Contributors: dsm | |
| 36 // Date: 1998-06-08 | |
| 37 // Description: Tests basic variable memory pool functionality | |
| 38 //####DESCRIPTIONEND#### | |
| 39 */ | |
| 40 | |
| 2 | 41 #include <cyg/hal/hal_arch.h> // CYGNUM_HAL_STACK_SIZE_TYPICAL |
| 42 | |
| 0 | 43 #include <pkgconf/kernel.h> |
| 44 | |
| 45 #include <cyg/kernel/kapi.h> | |
| 46 | |
| 47 #include <cyg/infra/testcase.h> | |
| 48 | |
| 49 #ifdef CYGFUN_KERNEL_API_C | |
| 50 | |
| 51 #include "testaux.h" | |
| 52 | |
| 53 #define NTHREADS 2 | |
| 2 | 54 #define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL |
| 0 | 55 |
| 56 static cyg_handle_t thread[NTHREADS]; | |
| 57 | |
| 58 static cyg_thread thread_obj[NTHREADS]; | |
| 59 static char stack[NTHREADS][STACKSIZE]; | |
| 60 | |
| 61 | |
| 62 #define MEMSIZE 10240 | |
| 63 | |
| 64 static cyg_uint8 mem[2][MEMSIZE]; | |
| 65 | |
| 66 static cyg_mempool_var mempool_obj[2]; | |
| 67 static cyg_handle_t mempool0, mempool1; | |
| 68 | |
| 69 static void check_in_mp0(cyg_uint8 *p, cyg_int32 size) | |
| 70 { | |
| 71 CYG_TEST_CHECK(NULL != p, | |
| 72 "Allocation failed"); | |
| 73 CYG_TEST_CHECK(mem[0] <= p && p+size < mem[1], | |
| 74 "Block outside memory pool"); | |
| 75 } | |
| 76 | |
| 77 static void entry0( cyg_addrword_t data ) | |
| 78 { | |
| 79 cyg_uint8 *p0, *p1; | |
| 80 cyg_int32 most_of_mem=MEMSIZE/4*3; | |
| 81 cyg_mempool_info info0, info1, info2; | |
| 82 | |
| 83 cyg_mempool_var_get_info(mempool0, &info0); | |
| 84 | |
| 85 CYG_TEST_CHECK(mem[0] == info0.base, "get_arena: base wrong"); | |
| 86 CYG_TEST_CHECK(MEMSIZE == info0.size, "get_arena: size wrong"); | |
| 87 | |
| 88 CYG_TEST_CHECK(0 < info0.maxfree && info0.maxfree <= info0.size, | |
| 89 "get_arena: maxfree wildly wrong"); | |
| 90 | |
| 91 CYG_TEST_CHECK(-1 == info0.blocksize, "get_blocksize wrong" ); | |
| 92 | |
| 93 CYG_TEST_CHECK(info0.totalmem > 0, "Negative total memory" ); | |
| 94 CYG_TEST_CHECK(info0.freemem > 0, "Negative free memory" ); | |
| 95 CYG_TEST_CHECK(info0.totalmem <= MEMSIZE, | |
| 96 "info.totalsize: Too much memory"); | |
| 97 CYG_TEST_CHECK(info0.freemem <= info0.totalmem , | |
| 98 "More memory free than possible" ); | |
| 99 | |
| 100 CYG_TEST_CHECK( !cyg_mempool_var_waiting(mempool0), | |
| 101 "Thread waiting for memory; there shouldn't be"); | |
| 102 | |
| 103 CYG_TEST_CHECK( NULL == cyg_mempool_var_try_alloc(mempool0, MEMSIZE+1), | |
| 104 "Managed to allocate too much memory"); | |
| 105 | |
| 106 p0 = cyg_mempool_var_alloc(mempool0, most_of_mem); | |
| 107 check_in_mp0(p0, most_of_mem); | |
| 108 | |
| 109 cyg_mempool_var_get_info(mempool0, &info1); | |
| 110 | |
| 111 CYG_TEST_CHECK(info1.freemem > 0, "Negative free memory" ); | |
| 112 CYG_TEST_CHECK(info1.freemem < info0.freemem, | |
| 113 "Free memory didn't decrease after allocation" ); | |
| 114 | |
| 115 CYG_TEST_CHECK( NULL == cyg_mempool_var_try_alloc(mempool0, most_of_mem), | |
| 116 "Managed to allocate too much memory"); | |
| 117 | |
| 118 cyg_mempool_var_free(mempool0, p0); | |
| 119 | |
| 120 cyg_mempool_var_get_info(mempool0, &info2); | |
| 121 CYG_TEST_CHECK(info2.freemem > info1.freemem, | |
| 122 "Free memory didn't increase after free" ); | |
| 123 | |
| 124 // should be able to reallocate now memory is free | |
| 125 p0 = cyg_mempool_var_try_alloc(mempool0, most_of_mem); | |
| 126 check_in_mp0(p0, most_of_mem); | |
| 127 | |
| 128 p1 = cyg_mempool_var_try_alloc(mempool0, 10); | |
| 129 check_in_mp0(p1, 10); | |
| 130 | |
| 131 CYG_TEST_CHECK(p1+10 <= p0 || p1 >= p0+MEMSIZE, | |
| 132 "Ranges of allocated memory overlap"); | |
| 133 | |
| 134 cyg_mempool_var_free(mempool0, p0); | |
| 135 cyg_mempool_var_free(mempool0, p1); | |
| 136 | |
| 137 #ifdef CYGFUN_KERNEL_THREADS_TIMER | |
| 138 // This shouldn't have to wait | |
| 139 p0 = cyg_mempool_var_timed_alloc(mempool0, most_of_mem, | |
| 140 cyg_current_time()+100000); | |
| 141 check_in_mp0(p0, most_of_mem); | |
| 142 p1 = cyg_mempool_var_timed_alloc(mempool0, most_of_mem, | |
| 143 cyg_current_time()+2); | |
| 144 CYG_TEST_CHECK(NULL == p1, "Timed alloc unexpectedly worked"); | |
| 145 p1 = cyg_mempool_var_timed_alloc(mempool0, 10, | |
| 146 cyg_current_time()+2); | |
| 147 check_in_mp0(p1, 10); | |
| 148 | |
| 149 // Expect thread 1 to have run while processing previous timed | |
| 150 // allocation. It should therefore tbe waiting. | |
| 151 CYG_TEST_CHECK(cyg_mempool_var_waiting(mempool1), "There should be a thread waiting"); | |
| 152 #endif | |
| 153 | |
| 154 CYG_TEST_PASS_FINISH("Kernel C API Variable memory pool 1 OK"); | |
| 155 } | |
| 156 | |
| 157 static void entry1( cyg_addrword_t data ) | |
| 158 { | |
| 159 cyg_mempool_var_alloc(mempool1, MEMSIZE+1); | |
| 160 CYG_TEST_FAIL("Oversized alloc returned"); | |
| 161 } | |
| 162 | |
| 163 | |
| 164 void kmemvar1_main( void ) | |
| 165 { | |
| 166 CYG_TEST_INIT(); | |
| 167 | |
| 168 cyg_thread_create(4, entry0 , (cyg_addrword_t)0, "kmemvar1-0", | |
| 2 | 169 (void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]); |
| 0 | 170 cyg_thread_resume(thread[0]); |
| 171 | |
| 172 cyg_thread_create(4, entry1 , (cyg_addrword_t)1, "kmemvar1-1", | |
| 2 | 173 (void *)stack[1], STACKSIZE, &thread[1], &thread_obj[1]); |
| 0 | 174 cyg_thread_resume(thread[1]); |
| 175 | |
| 176 cyg_mempool_var_create(mem[0], MEMSIZE, &mempool0, &mempool_obj[0]); | |
| 177 cyg_mempool_var_create(mem[1], MEMSIZE, &mempool1, &mempool_obj[1]); | |
| 178 | |
| 179 cyg_scheduler_start(); | |
| 180 | |
| 181 CYG_TEST_FAIL_FINISH("Not reached"); | |
| 182 } | |
| 183 | |
| 184 externC void | |
| 185 cyg_start( void ) | |
| 186 { | |
| 187 kmemvar1_main(); | |
| 188 } | |
| 189 | |
| 190 #else /* def CYGFUN_KERNEL_API_C */ | |
| 191 externC void | |
| 192 cyg_start( void ) | |
| 193 { | |
| 194 CYG_TEST_INIT(); | |
| 2 | 195 CYG_TEST_NA("Kernel C API layer disabled"); |
| 0 | 196 } |
| 197 #endif /* def CYGFUN_KERNEL_API_C */ | |
| 198 | |
| 199 /* EOF kmemvar1.c */ |
