Mercurial > ecos
annotate packages/kernel/current/include/mfiximpl.inl @ 94:4642a6d35749 ecos-sw-2000-06-02
Merge from eCos master repository on 2000-06-02-07:47:04-BST
| author | jlarmour |
|---|---|
| date | Fri, 02 Jun 2000 17:35:00 +0000 |
| parents | bf00f99aec69 |
| children |
| rev | line source |
|---|---|
| 0 | 1 #ifndef CYGONCE_KERNEL_MFIXIMPL_INL |
| 2 #define CYGONCE_KERNEL_MFIXIMPL_INL | |
| 3 | |
| 4 //========================================================================== | |
| 5 // | |
| 2 | 6 // mfiximpl.inl |
| 0 | 7 // |
| 2 | 8 // Memory pool with fixed block class declarations |
| 0 | 9 // |
| 10 //========================================================================== | |
| 11 //####COPYRIGHTBEGIN#### | |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
12 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
13 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
14 // 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
|
15 // 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
|
16 // 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
|
17 // http://www.redhat.com/ |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
18 // |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
19 // 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
|
20 // 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
|
21 // 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
|
22 // the License. |
|
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 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
|
25 // released September 30, 1998. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
26 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
27 // 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
|
28 // 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
|
29 // 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
|
30 // All Rights Reserved. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
31 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
32 // |
| 0 | 33 //####COPYRIGHTEND#### |
| 34 //========================================================================== | |
| 35 //#####DESCRIPTIONBEGIN#### | |
| 36 // | |
| 2 | 37 // Author(s): hmt |
| 38 // Contributors: hmt | |
| 39 // Date: 1998-03-23 | |
| 40 // Purpose: Define Mfiximpl class interface | |
| 41 // Description: Inline class for constructing a fixed block allocator | |
| 42 // Usage: #include <cyg/kernel/mfiximpl.hxx> | |
| 43 // #include <cyg/kernel/mfiximpl.inl> | |
| 0 | 44 // |
| 45 //####DESCRIPTIONEND#### | |
| 46 // | |
| 47 //========================================================================== | |
| 48 | |
| 49 #include <cyg/hal/hal_arch.h> // HAL_LSBIT_INDEX magic asm code | |
| 50 | |
| 51 | |
| 52 Cyg_Mempool_Fixed_Implementation::Cyg_Mempool_Fixed_Implementation( | |
| 53 cyg_uint8 *base, | |
| 54 cyg_int32 size, | |
| 55 CYG_ADDRWORD alloc_unit ) | |
| 56 { | |
| 57 cyg_int32 i; | |
| 58 bitmap = (cyg_uint32 *)base; | |
| 59 blocksize = alloc_unit; | |
| 60 | |
| 61 CYG_ASSERT( blocksize > 0, "Bad blocksize" ); | |
| 62 CYG_ASSERT( size > 2, "Bad blocksize" ); | |
| 63 CYG_ASSERT( blocksize < size, "blocksize, size bad" ); | |
| 2 | 64 |
| 65 numblocks = size / blocksize; | |
| 66 top = base + size; | |
| 67 | |
| 0 | 68 CYG_ASSERT( numblocks >= 2, "numblocks bad" ); |
| 69 | |
| 70 i = (numblocks + 31)/32; // number of words to map blocks | |
| 71 while ( (i * 4 + numblocks * blocksize) > size ) { | |
| 72 numblocks --; // steal one block for admin | |
| 73 i = (numblocks + 31)/32; // number of words to map blocks | |
| 74 } | |
| 75 | |
| 76 CYG_ASSERT( 0 < i, "Bad word count for bitmap after fitment" ); | |
| 77 CYG_ASSERT( 0 < numblocks, "Bad block count after fitment" ); | |
| 78 | |
| 79 maptop = i; | |
| 80 // this should leave space for the bitmap and maintain alignment | |
| 81 mempool = top - (numblocks * blocksize); | |
| 82 CYG_ASSERT( base < mempool && mempool < top, "mempool escaped" ); | |
| 83 CYG_ASSERT( (cyg_uint8 *)(&bitmap[ maptop ]) <= mempool, | |
| 84 "mempool overwrites bitmap" ); | |
| 85 CYG_ASSERT( &mempool[ numblocks * blocksize ] <= top, | |
| 86 "mempool overflows top" ); | |
| 87 freeblocks = numblocks; | |
| 88 firstfree = 0; | |
| 89 | |
| 90 // clear out the bitmap; no blocks allocated yet | |
| 91 for ( i = 0; i < maptop; i++ ) | |
| 92 bitmap[ i ] = 0; | |
| 93 // apart from the non-existent ones at the top | |
| 94 for ( i = ((numblocks-1)&31) + 1; i < 32; i++ ) | |
| 95 bitmap[ maptop - 1 ] |= ( 1 << i ); | |
| 96 } | |
| 97 | |
| 98 Cyg_Mempool_Fixed_Implementation::~Cyg_Mempool_Fixed_Implementation() | |
| 99 { | |
| 100 } | |
| 101 | |
| 102 inline cyg_uint8 * | |
| 103 Cyg_Mempool_Fixed_Implementation::alloc( cyg_int32 size ) | |
| 104 { | |
| 105 // size parameter is not used | |
| 106 CYG_UNUSED_PARAM( cyg_int32, size ); | |
| 107 if ( 0 >= freeblocks ) | |
| 108 return NULL; | |
| 109 cyg_int32 i = firstfree; | |
| 110 cyg_uint8 *p = NULL; | |
| 111 do { | |
| 112 if ( 0xffffffff != bitmap[ i ] ) { | |
| 113 // then there is a free block in this bucket | |
| 114 register cyg_uint32 j, k; | |
| 115 k = ~bitmap[ i ]; // look for a 1 in complement | |
| 116 HAL_LSBIT_INDEX( j, k ); | |
| 117 CYG_ASSERT( 0 <= j && j <= 31, "Bad bit index" ); | |
| 118 CYG_ASSERT( 0 == (bitmap[ i ] & (1 << j)), "Found bit not clear" ); | |
| 119 bitmap[ i ] |= (1 << j); // set it allocated | |
| 120 firstfree = i; | |
| 121 freeblocks--; | |
| 122 CYG_ASSERT( freeblocks >= 0, "allocated too many" ); | |
| 123 p = &mempool[ ((32 * i) + j) * blocksize ]; | |
| 124 break; | |
| 125 } | |
| 126 if ( ++i >= maptop ) | |
| 127 i = 0; // wrap if at top | |
| 128 } while ( i != firstfree ); // prevent hang if internal error | |
| 129 CYG_ASSERT( NULL != p, "Should have a block here" ); | |
| 130 CYG_ASSERT( mempool <= p && p <= top, "alloc mem escaped" ); | |
| 131 return p; | |
| 132 } | |
| 133 | |
| 134 inline cyg_bool | |
| 135 Cyg_Mempool_Fixed_Implementation::free( cyg_uint8 *p, cyg_int32 size ) | |
| 136 { | |
| 137 // size parameter is not used | |
| 138 CYG_UNUSED_PARAM( cyg_int32, size ); | |
| 139 if ( p < mempool || p >= top ) | |
| 140 return false; // address way out of bounds | |
| 141 cyg_int32 i = p - mempool; | |
| 142 i = i / blocksize; | |
| 143 if ( &mempool[ i * blocksize ] != p ) | |
| 144 return false; // address not aligned | |
| 145 cyg_int32 j = i / 32; | |
| 146 CYG_ASSERT( 0 <= j && j < maptop, "map index escaped" ); | |
| 147 i = i - 32 * j; | |
| 148 CYG_ASSERT( 0 <= i && i < 32, "map bit index escaped" ); | |
| 149 if ( ! ((1 << i) & bitmap[ j ] ) ) | |
| 150 return false; // block was not allocated | |
| 151 bitmap[ j ] &=~(1 << i); // clear the bit | |
| 152 freeblocks++; // count the block | |
| 153 CYG_ASSERT( freeblocks <= numblocks, "freeblocks overflow" ); | |
| 154 return true; | |
| 155 } | |
| 156 | |
| 157 // ------------------------------------------------------------------------- | |
| 158 #endif // ifndef CYGONCE_KERNEL_MFIXIMPL_INL | |
| 159 // EOF mfiximpl.inl |
