Mercurial > ecos
annotate packages/kernel/current/tests/kmemfix1.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 // kmemfix1.cxx | |
| 4 // | |
| 5 // Kernel C API Fixed 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-09 | |
| 37 // Description: Tests basic fixed 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_fix 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, *p2; | |
| 80 cyg_mempool_info info0, info1, info2; | |
| 81 | |
| 82 cyg_mempool_fix_get_info(mempool0, &info0); | |
| 83 CYG_TEST_CHECK(mem[0] == info0.base, "get_arena: base wrong"); | |
| 84 CYG_TEST_CHECK(MEMSIZE == info0.size, "get_arena: size wrong"); | |
| 85 | |
| 86 CYG_TEST_CHECK(0 < info0.maxfree && info0.maxfree <= info0.size, | |
| 87 "get_arena: maxfree wildly wrong"); | |
| 88 | |
| 89 CYG_TEST_CHECK(100 == info0.blocksize, "get_blocksize wrong" ); | |
| 90 | |
| 91 CYG_TEST_CHECK(info0.totalmem > 0, "Negative total memory" ); | |
| 92 CYG_TEST_CHECK(info0.freemem > 0, "Negative free memory" ); | |
| 93 CYG_TEST_CHECK(info0.totalmem <= MEMSIZE, | |
| 94 "info.totalsize: Too much memory"); | |
| 95 CYG_TEST_CHECK(info0.freemem <= info0.totalmem , | |
| 96 "More memory free than possible" ); | |
| 97 | |
| 98 CYG_TEST_CHECK( !cyg_mempool_fix_waiting(mempool0) , | |
| 99 "Thread waiting for memory; there shouldn't be"); | |
| 100 | |
| 101 p0 = cyg_mempool_fix_alloc(mempool0); | |
| 102 check_in_mp0(p0, 100); | |
| 103 | |
| 104 cyg_mempool_fix_get_info(mempool0, &info1); | |
| 105 CYG_TEST_CHECK(info1.freemem > 0, "Negative free memory" ); | |
| 106 CYG_TEST_CHECK(info1.freemem < info0.freemem, | |
| 107 "Free memory didn't decrease after allocation" ); | |
| 108 | |
| 109 p1 = NULL; | |
| 110 while((p2 = cyg_mempool_fix_try_alloc(mempool0) )) | |
| 2 | 111 p1 = p2; |
| 0 | 112 |
| 113 cyg_mempool_fix_get_info(mempool0, &info1); | |
| 114 cyg_mempool_fix_free(mempool0, p0); | |
| 115 | |
| 116 cyg_mempool_fix_get_info(mempool0, &info2); | |
| 117 CYG_TEST_CHECK(info2.freemem > info1.freemem, | |
| 118 "Free memory didn't increase after free" ); | |
| 119 | |
| 120 // should be able to reallocate now a block is free | |
| 121 p0 = cyg_mempool_fix_try_alloc(mempool0); | |
| 122 check_in_mp0(p0, 100); | |
| 123 | |
| 124 CYG_TEST_CHECK(p1+100 <= p0 || p1 >= p0+100, | |
| 125 "Ranges of allocated memory overlap"); | |
| 126 | |
| 127 cyg_mempool_fix_free(mempool0, p0); | |
| 128 cyg_mempool_fix_free(mempool0, p1); | |
| 129 | |
| 130 #ifdef CYGFUN_KERNEL_THREADS_TIMER | |
| 131 // This shouldn't have to wait | |
| 132 p0 = cyg_mempool_fix_timed_alloc(mempool0, cyg_current_time()+100000); | |
| 133 check_in_mp0(p0, 100); | |
| 2 | 134 p1 = cyg_mempool_fix_timed_alloc(mempool0, cyg_current_time()+20); |
| 0 | 135 check_in_mp0(p1, 10); |
| 2 | 136 p1 = cyg_mempool_fix_timed_alloc(mempool0, cyg_current_time()+20); |
| 0 | 137 CYG_TEST_CHECK(NULL == p1, "Timed alloc unexpectedly worked"); |
| 138 | |
| 139 // Expect thread 1 to have run while processing previous timed | |
| 2 | 140 // allocation. It should therefore be waiting. |
| 0 | 141 CYG_TEST_CHECK(cyg_mempool_fix_waiting(mempool1), |
| 142 "There should be a thread waiting"); | |
| 143 #endif | |
| 144 | |
| 145 CYG_TEST_PASS_FINISH("Kernal C API Fixed memory pool 1 OK"); | |
| 146 } | |
| 147 | |
| 148 static void entry1( cyg_addrword_t data ) | |
| 149 { | |
| 150 while(NULL != cyg_mempool_fix_alloc(mempool1)) | |
| 151 ; | |
| 152 CYG_TEST_FAIL("alloc returned NULL"); | |
| 153 } | |
| 154 | |
| 155 | |
| 156 void kmemfix1_main( void ) | |
| 157 { | |
| 158 CYG_TEST_INIT(); | |
| 159 | |
| 160 cyg_thread_create(4, entry0 , (cyg_addrword_t)0, "kmemfix1-0", | |
| 2 | 161 (void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]); |
| 0 | 162 cyg_thread_resume(thread[0]); |
| 163 | |
| 164 cyg_thread_create(4, entry1 , (cyg_addrword_t)1, "kmemfix1-1", | |
| 2 | 165 (void *)stack[1], STACKSIZE, &thread[1], &thread_obj[1]); |
| 0 | 166 cyg_thread_resume(thread[1]); |
| 167 | |
| 168 cyg_mempool_fix_create(mem[0], MEMSIZE, 100, &mempool0, &mempool_obj[0]); | |
| 169 cyg_mempool_fix_create(mem[1], MEMSIZE, 316, &mempool1, &mempool_obj[1]); | |
| 170 | |
| 171 cyg_scheduler_start(); | |
| 172 | |
| 173 CYG_TEST_FAIL_FINISH("Not reached"); | |
| 174 } | |
| 175 | |
| 176 externC void | |
| 177 cyg_start( void ) | |
| 178 { | |
| 179 kmemfix1_main(); | |
| 180 } | |
| 181 | |
| 182 #else /* def CYGFUN_KERNEL_API_C */ | |
| 183 externC void | |
| 184 cyg_start( void ) | |
| 185 { | |
| 186 CYG_TEST_INIT(); | |
| 2 | 187 CYG_TEST_NA("Kernel C API layer disabled"); |
| 0 | 188 } |
| 189 #endif /* def CYGFUN_KERNEL_API_C */ | |
| 190 | |
| 191 /* EOF kmemfix1.c */ |
