comparison packages/kernel/current/include/mfiximpl.inl @ 0:3111d98ba7b3 ecos-v1_1-release

Initial commit of eCos version 1.1
author jlarmour
date Tue, 11 May 1999 11:16:07 +0000
parents
children 443894e2e912
comparison
equal deleted inserted replaced
-1:000000000000 0:3111d98ba7b3
1 #ifndef CYGONCE_KERNEL_MFIXIMPL_INL
2 #define CYGONCE_KERNEL_MFIXIMPL_INL
3
4 //==========================================================================
5 //
6 // mfiximpl.inl
7 //
8 // Memory pool with fixed block class declarations
9 //
10 //==========================================================================
11 //####COPYRIGHTBEGIN####
12 //
13 // -------------------------------------------
14 // The contents of this file are subject to the Cygnus eCos Public License
15 // Version 1.0 (the "License"); you may not use this file except in
16 // compliance with the License. You may obtain a copy of the License at
17 // http://sourceware.cygnus.com/ecos
18 //
19 // Software distributed under the License is distributed on an "AS IS"
20 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
21 // License for the specific language governing rights and limitations under
22 // the License.
23 //
24 // The Original Code is eCos - Embedded Cygnus Operating System, released
25 // September 30, 1998.
26 //
27 // The Initial Developer of the Original Code is Cygnus. Portions created
28 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved.
29 // -------------------------------------------
30 //
31 //####COPYRIGHTEND####
32 //==========================================================================
33 //#####DESCRIPTIONBEGIN####
34 //
35 // Author(s): hmt
36 // Contributors: hmt
37 // Date: 1998-03-23
38 // Purpose: Define Mfiximpl class interface
39 // Description: Inline class for constructing a fixed block allocator
40 // Usage: #include <cyg/kernel/mfiximpl.hxx>
41 // #include <cyg/kernel/mfiximpl.inl>
42 //
43 //####DESCRIPTIONEND####
44 //
45 //==========================================================================
46
47 #include <cyg/hal/hal_arch.h> // HAL_LSBIT_INDEX magic asm code
48
49
50 Cyg_Mempool_Fixed_Implementation::Cyg_Mempool_Fixed_Implementation(
51 cyg_uint8 *base,
52 cyg_int32 size,
53 CYG_ADDRWORD alloc_unit )
54 {
55 cyg_int32 i;
56 bitmap = (cyg_uint32 *)base;
57 blocksize = alloc_unit;
58 numblocks = size / blocksize;
59 top = base + size;
60
61 CYG_ASSERT( blocksize > 0, "Bad blocksize" );
62 CYG_ASSERT( size > 2, "Bad blocksize" );
63 CYG_ASSERT( blocksize < size, "blocksize, size bad" );
64 CYG_ASSERT( numblocks >= 2, "numblocks bad" );
65
66 i = (numblocks + 31)/32; // number of words to map blocks
67 while ( (i * 4 + numblocks * blocksize) > size ) {
68 numblocks --; // steal one block for admin
69 i = (numblocks + 31)/32; // number of words to map blocks
70 }
71
72 CYG_ASSERT( 0 < i, "Bad word count for bitmap after fitment" );
73 CYG_ASSERT( 0 < numblocks, "Bad block count after fitment" );
74
75 maptop = i;
76 // this should leave space for the bitmap and maintain alignment
77 mempool = top - (numblocks * blocksize);
78 CYG_ASSERT( base < mempool && mempool < top, "mempool escaped" );
79 CYG_ASSERT( (cyg_uint8 *)(&bitmap[ maptop ]) <= mempool,
80 "mempool overwrites bitmap" );
81 CYG_ASSERT( &mempool[ numblocks * blocksize ] <= top,
82 "mempool overflows top" );
83 freeblocks = numblocks;
84 firstfree = 0;
85
86 // clear out the bitmap; no blocks allocated yet
87 for ( i = 0; i < maptop; i++ )
88 bitmap[ i ] = 0;
89 // apart from the non-existent ones at the top
90 for ( i = ((numblocks-1)&31) + 1; i < 32; i++ )
91 bitmap[ maptop - 1 ] |= ( 1 << i );
92 }
93
94 Cyg_Mempool_Fixed_Implementation::~Cyg_Mempool_Fixed_Implementation()
95 {
96 }
97
98 inline cyg_uint8 *
99 Cyg_Mempool_Fixed_Implementation::alloc( cyg_int32 size )
100 {
101 // size parameter is not used
102 CYG_UNUSED_PARAM( cyg_int32, size );
103 if ( 0 >= freeblocks )
104 return NULL;
105 cyg_int32 i = firstfree;
106 cyg_uint8 *p = NULL;
107 do {
108 if ( 0xffffffff != bitmap[ i ] ) {
109 // then there is a free block in this bucket
110 register cyg_uint32 j, k;
111 k = ~bitmap[ i ]; // look for a 1 in complement
112 HAL_LSBIT_INDEX( j, k );
113 CYG_ASSERT( 0 <= j && j <= 31, "Bad bit index" );
114 CYG_ASSERT( 0 == (bitmap[ i ] & (1 << j)), "Found bit not clear" );
115 bitmap[ i ] |= (1 << j); // set it allocated
116 firstfree = i;
117 freeblocks--;
118 CYG_ASSERT( freeblocks >= 0, "allocated too many" );
119 p = &mempool[ ((32 * i) + j) * blocksize ];
120 break;
121 }
122 if ( ++i >= maptop )
123 i = 0; // wrap if at top
124 } while ( i != firstfree ); // prevent hang if internal error
125 CYG_ASSERT( NULL != p, "Should have a block here" );
126 CYG_ASSERT( mempool <= p && p <= top, "alloc mem escaped" );
127 return p;
128 }
129
130 inline cyg_bool
131 Cyg_Mempool_Fixed_Implementation::free( cyg_uint8 *p, cyg_int32 size )
132 {
133 // size parameter is not used
134 CYG_UNUSED_PARAM( cyg_int32, size );
135 if ( p < mempool || p >= top )
136 return false; // address way out of bounds
137 cyg_int32 i = p - mempool;
138 i = i / blocksize;
139 if ( &mempool[ i * blocksize ] != p )
140 return false; // address not aligned
141 cyg_int32 j = i / 32;
142 CYG_ASSERT( 0 <= j && j < maptop, "map index escaped" );
143 i = i - 32 * j;
144 CYG_ASSERT( 0 <= i && i < 32, "map bit index escaped" );
145 if ( ! ((1 << i) & bitmap[ j ] ) )
146 return false; // block was not allocated
147 bitmap[ j ] &=~(1 << i); // clear the bit
148 freeblocks++; // count the block
149 CYG_ASSERT( freeblocks <= numblocks, "freeblocks overflow" );
150 return true;
151 }
152
153 // -------------------------------------------------------------------------
154 #endif // ifndef CYGONCE_KERNEL_MFIXIMPL_INL
155 // EOF mfiximpl.inl