|
0
|
1 //========================================================================== |
|
|
2 // |
|
|
3 // memvar2.cxx |
|
|
4 // |
|
|
5 // Variable memory pool test 2 |
|
|
6 // |
|
|
7 //========================================================================== |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (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://sourceware.cygnus.com/ecos |
|
|
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 Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
2
|
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //========================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author(s): dsm |
|
|
33 // Contributors: dsm |
|
|
34 // Date: 1998-06-08 |
|
|
35 // Description: test allocation and freeing in variable memory pools |
|
|
36 //####DESCRIPTIONEND#### |
|
|
37 |
|
|
38 #include <pkgconf/kernel.h> |
|
|
39 |
|
|
40 #include <cyg/kernel/sched.hxx> // Cyg_Scheduler::start() |
|
|
41 #include <cyg/kernel/thread.hxx> // Cyg_Thread |
|
|
42 #include <cyg/kernel/thread.inl> |
|
|
43 #include <cyg/kernel/sema.hxx> |
|
|
44 |
|
|
45 #include <cyg/kernel/memvar.hxx> |
|
|
46 |
|
|
47 #include <cyg/infra/testcase.h> |
|
|
48 |
|
|
49 #include <cyg/kernel/sched.inl> |
|
|
50 |
|
|
51 |
|
|
52 #define NTHREADS 1 |
|
|
53 #include "testaux.hxx" |
|
|
54 |
|
|
55 static const cyg_int32 memsize = 10240; |
|
|
56 |
|
|
57 static cyg_uint8 mem[memsize]; |
|
|
58 |
|
|
59 static Cyg_Mempool_Variable mempool(mem, memsize); |
|
|
60 |
|
|
61 #define NUM_PTRS 16 // Should be even |
|
|
62 |
|
|
63 static cyg_uint8 *ptr[NUM_PTRS]; |
|
|
64 static cyg_int32 size[NUM_PTRS]; |
|
|
65 |
|
|
66 // We make a number of passes over a table of pointers which point to |
|
|
67 // blocks of allocated memory. The block is freed and a new block |
|
|
68 // allocated. The size and the order of the processing of blocks |
|
|
69 // is varied. |
|
|
70 static void entry( CYG_ADDRWORD data ) |
|
|
71 { |
|
|
72 cyg_uint32 s = 1; |
|
|
73 |
|
|
74 // The number of passes that can be successfully performed |
|
|
75 // depends on the fragmentation performance of the memory |
|
|
76 // allocator. |
|
|
77 for(cyg_ucount32 passes = 0; passes < 10; passes++) { |
|
|
78 |
|
|
79 |
|
|
80 // The order which the table is processed varies according to |
|
|
81 // stepsize. |
|
|
82 cyg_ucount8 stepsize = (passes*2 + 1) % NUM_PTRS; // odd |
|
|
83 |
|
|
84 |
|
|
85 for(cyg_ucount8 c=0, i=0; c < NUM_PTRS; c++) { |
|
|
86 i = (i+stepsize) % NUM_PTRS; |
|
|
87 if(ptr[i]) { |
|
|
88 for(cyg_ucount32 j=size[i];j--;) { |
|
|
89 CYG_TEST_CHECK(ptr[i][j]==i, "Memory corrupted"); |
|
|
90 } |
|
|
91 CYG_TEST_CHECK(mempool.free(ptr[i], size[i]), |
|
|
92 "bad free"); |
|
|
93 } |
|
|
94 s = (s*2 + 17) % 100; // size always odds therefore non-0 |
|
|
95 ptr[i] = mempool.try_alloc(s); |
|
|
96 size[i] = s; |
|
|
97 |
|
|
98 CYG_TEST_CHECK(NULL != ptr[i], "Memory pool not big enough"); |
|
|
99 CYG_TEST_CHECK(mem<=ptr[i] && ptr[i]+s < mem+memsize, |
|
|
100 "Allocated region not within pool"); |
|
|
101 |
|
|
102 // Scribble over memory to check whether region overlaps |
|
|
103 // with other regions. The contents of the memory are |
|
|
104 // checked on freeing. This also tests that the memory |
|
|
105 // does not overlap with allocator memory structures. |
|
|
106 for(cyg_ucount32 j=size[i];j--;) { |
|
|
107 ptr[i][j]=i; |
|
|
108 } |
|
|
109 } |
|
|
110 } |
|
|
111 |
|
|
112 CYG_TEST_PASS_FINISH("Variable memory pool 2 OK"); |
|
|
113 } |
|
|
114 |
|
|
115 |
|
|
116 void memvar2_main( void ) |
|
|
117 { |
|
|
118 CYG_TEST_INIT(); |
|
|
119 |
|
|
120 new_thread(entry, 0); |
|
|
121 |
|
|
122 for(cyg_ucount32 i = 0; i<NUM_PTRS; i++) { |
|
|
123 ptr[i] = NULL; |
|
|
124 } |
|
|
125 |
|
|
126 Cyg_Scheduler::start(); |
|
|
127 |
|
|
128 CYG_TEST_FAIL_FINISH("Not reached"); |
|
|
129 } |
|
|
130 |
|
|
131 externC void |
|
|
132 cyg_start( void ) |
|
|
133 { |
|
|
134 memvar2_main(); |
|
|
135 } |
|
|
136 // EOF memvar1.cxx |