Mercurial > flash_v2
annotate packages/kernel/current/include/mvarimpl.inl @ 115:6ed91473a1cd ecos-sw-2000-08-21
Merge from eCos master repository on 2000-08-21-22:40:54-BST
| author | jlarmour |
|---|---|
| date | Fri, 25 Aug 2000 17:32:38 +0000 |
| parents | da3908c9cd10 |
| children |
| rev | line source |
|---|---|
| 0 | 1 #ifndef CYGONCE_KERNEL_MVARIMPL_INL |
| 2 #define CYGONCE_KERNEL_MVARIMPL_INL | |
| 3 | |
| 4 //========================================================================== | |
| 5 // | |
| 2 | 6 // mvarimpl.inl |
| 0 | 7 // |
| 2 | 8 // Memory pool with variable 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 Mvarimpl class interface | |
| 41 // Description: Inline class for constructing a variable block allocator | |
| 42 // Usage: #include <cyg/kernel/mvarimpl.hxx> | |
| 43 // #include <cyg/kernel/mvarimpl.inl> | |
| 0 | 44 // |
| 45 //####DESCRIPTIONEND#### | |
| 46 // | |
| 47 //========================================================================== | |
| 48 | |
| 49 // Simple non-coalescing allocator | |
| 50 | |
| 51 // The free list is stored on a doubly linked list, each member of | |
| 52 // which is stored in the body of the free memory. The head of the | |
| 53 // list has the same structure but its size field is zero. This | |
| 54 // resides in the memory pool structure. Always having at least one | |
| 55 // item on the list simplifies the alloc and free code. | |
| 56 | |
| 57 // | |
| 58 inline cyg_int32 | |
| 59 Cyg_Mempool_Variable_Implementation::roundup( cyg_int32 size ) | |
| 60 { | |
| 61 | |
| 62 size += sizeof(struct memdq); | |
| 63 size = (size + alignment - 1) & -alignment; | |
| 64 return size; | |
| 65 } | |
| 66 | |
| 67 Cyg_Mempool_Variable_Implementation::Cyg_Mempool_Variable_Implementation( | |
| 68 cyg_uint8 *base, | |
| 69 cyg_int32 size, | |
| 70 CYG_ADDRWORD align ) | |
| 71 { | |
| 72 CYG_ASSERT( align > 0, "Bad alignment" ); | |
| 73 CYG_ASSERT( size > 0, "Bad size" ); | |
| 74 CYG_ASSERT(0!=align ,"align is zero"); | |
| 75 CYG_ASSERT(0==(align & align-1),"align not a power of 2"); | |
| 76 | |
| 77 obase=base; | |
| 78 osize=size; | |
| 79 oalign=align; | |
| 80 | |
| 81 alignment = align; | |
| 82 while(alignment < (cyg_int32)sizeof(struct memdq)) | |
| 83 alignment += alignment; | |
| 84 CYG_ASSERT(0==(alignment & alignment-1),"alignment not a power of 2"); | |
| 85 | |
| 86 bottom = (cyg_uint8 *)roundup((cyg_int32)base); | |
| 87 top = (cyg_uint8 *)((cyg_int32)(base+size) & -alignment); | |
| 88 | |
| 89 CYG_ASSERT( top > bottom , "heap too small" ); | |
| 90 CYG_ASSERT( ((cyg_int32)bottom & alignment-1)==0, "bottom badly aligned" ); | |
| 91 CYG_ASSERT( ((cyg_int32)top & alignment-1)==0, "top badly aligned" ); | |
| 92 | |
| 93 struct memdq *hdq = &head, *dq = (struct memdq *)bottom; | |
| 94 | |
| 95 hdq->prev = hdq->next = dq; | |
| 96 hdq->size = 0; | |
| 97 dq->prev = dq->next = hdq; | |
| 98 | |
| 99 freemem = dq->size = top-bottom; | |
| 100 } | |
| 101 | |
| 102 Cyg_Mempool_Variable_Implementation::~Cyg_Mempool_Variable_Implementation() | |
| 103 { | |
| 104 } | |
| 105 | |
| 106 // allocation is simple | |
| 107 // First we look down the free list for a large enough block | |
| 108 // If we find a block the right size, we unlink the block from | |
| 109 // the free list and return a pointer to it. | |
| 110 // If we find a larger block, we chop a piece off the end | |
| 111 // and return that | |
| 112 // Otherwise we will eventually get back to the head of the list | |
| 113 // and return NULL | |
| 114 inline cyg_uint8 * | |
| 115 Cyg_Mempool_Variable_Implementation::alloc( cyg_int32 size ) | |
| 116 { | |
| 117 struct memdq *dq = &head; | |
| 118 cyg_uint8 *alloced; | |
| 119 | |
| 120 size = roundup(size); | |
| 121 | |
| 122 do { | |
| 123 CYG_ASSERT( dq->next->prev==dq, "Bad link in dq"); | |
| 124 dq = dq->next; | |
| 125 if(0 == dq->size) { | |
| 126 CYG_ASSERT(dq == &head, "bad free block"); | |
| 127 return NULL; | |
| 128 } | |
| 129 } while(dq->size < size); | |
| 130 | |
| 131 if( size == dq->size ) { | |
| 132 // exact fit -- unlink from free list | |
| 133 dq->prev->next = dq->next; | |
| 134 dq->next->prev = dq->prev; | |
| 135 alloced = (cyg_uint8 *)dq; | |
| 136 } else { | |
| 137 | |
| 138 CYG_ASSERT( dq->size > size, "block found is too small"); | |
| 139 | |
| 140 // allocate portion of memory from end of block | |
| 141 | |
| 142 dq->size -=size; | |
| 143 | |
| 144 // The portion left over has to be large enough to store a | |
| 145 // struct memdq. This is guaranteed because the alignment is | |
| 146 // larger than the size of this structure. | |
| 147 | |
| 148 CYG_ASSERT( (cyg_int32)sizeof(struct memdq)<=dq->size , | |
| 149 "not enough space for list item" ); | |
| 150 | |
| 151 alloced = (cyg_uint8 *)dq + dq->size; | |
| 152 } | |
| 153 | |
| 154 CYG_ASSERT( bottom<=alloced && alloced<=top, "alloced outside pool" ); | |
| 155 | |
| 156 // Set size on allocated block | |
| 157 | |
| 158 dq = (struct memdq *)alloced; | |
| 159 dq->size = size; | |
| 160 dq->next = dq->prev = (struct memdq *)0xd530d53; // magic number | |
| 161 | |
| 162 freemem -=size; | |
| 163 return alloced + sizeof(struct memdq); | |
| 164 } | |
| 165 | |
|
72
da3908c9cd10
Merge from eCos master repository on 2000-02-17-18:56:14-GMT
jlarmour
parents:
66
diff
changeset
|
166 // When no coalescing is done, free is simply a matter of using the |
| 0 | 167 // freed memory as an element of the free list linking it in at the |
| 168 // start. | |
| 169 | |
| 170 inline cyg_bool | |
| 171 Cyg_Mempool_Variable_Implementation::free( cyg_uint8 *p, cyg_int32 size ) | |
| 172 { | |
| 173 if(!(bottom<=p&&p<=top)) | |
| 174 return false; | |
| 175 | |
| 176 struct memdq *hdq=&head,*dq=(struct memdq *)(p-sizeof(struct memdq)); | |
| 177 | |
| 178 // check magic number in block to be freed | |
| 179 if(dq->next != dq->prev || dq->next != (struct memdq *)0xd530d53) | |
| 180 return false; | |
| 181 | |
| 182 if(0==size) { | |
| 183 size = dq->size; | |
| 184 } else { | |
| 185 size = roundup(size); | |
| 186 } | |
| 187 | |
| 188 if(dq->size != size) | |
| 189 return false; | |
| 190 | |
| 191 CYG_ASSERT( (cyg_int32)sizeof(struct memdq)<=size , | |
| 192 "not enough space for list item" ); | |
| 193 | |
| 194 #ifdef CYGSEM_KERNEL_MEMORY_COALESCE | |
| 195 // For simple coalescing have the free list be sorted by memory base address | |
| 196 struct memdq *idq; | |
| 197 | |
| 198 for (idq = hdq->next; idq != hdq; idq = idq->next) { | |
| 2 | 199 if (idq->next > dq) |
| 200 break; | |
| 0 | 201 } |
| 202 if (idq != hdq) { | |
| 203 dq->prev = idq; | |
| 204 dq->next = idq->next; | |
| 205 idq->next = dq; | |
| 206 dq->next->prev = dq; | |
| 207 } else { | |
| 208 dq->next = idq; | |
| 2 | 209 dq->prev = idq->prev; |
| 210 idq->prev = dq; | |
| 211 dq->prev->next = dq; | |
| 0 | 212 } |
|
72
da3908c9cd10
Merge from eCos master repository on 2000-02-17-18:56:14-GMT
jlarmour
parents:
66
diff
changeset
|
213 // Now do coalescing, but leave the head of the list alone. |
|
da3908c9cd10
Merge from eCos master repository on 2000-02-17-18:56:14-GMT
jlarmour
parents:
66
diff
changeset
|
214 if (dq->next != hdq && (char *)dq + dq->size == (char *)dq->next) { |
| 2 | 215 dq->size += dq->next->size; |
| 0 | 216 dq->next = dq->next->next; |
| 2 | 217 dq->next->prev = dq; |
| 0 | 218 } |
|
72
da3908c9cd10
Merge from eCos master repository on 2000-02-17-18:56:14-GMT
jlarmour
parents:
66
diff
changeset
|
219 if (dq->prev != hdq && (char *)dq->prev + dq->prev->size == (char *)dq) { |
| 0 | 220 dq->prev->size += dq->size; |
| 2 | 221 dq->prev->next = dq->next; |
| 222 dq->next->prev = dq->prev; | |
| 223 dq = dq->prev; | |
| 0 | 224 } |
| 225 #else | |
| 226 dq->prev = hdq; | |
| 227 dq->next = hdq->next; | |
| 228 dq->size = size; | |
| 229 hdq->next = dq; | |
| 230 dq->next->prev=dq; | |
| 231 #endif | |
| 232 | |
| 233 freemem +=size; | |
| 234 return true; | |
| 235 } | |
| 236 | |
| 237 | |
| 238 inline void | |
| 239 Cyg_Mempool_Variable_Implementation::get_arena( | |
| 240 cyg_uint8 * &base, | |
| 241 cyg_int32 &size, | |
| 242 CYG_ADDRWORD &maxfree) | |
| 243 { | |
| 244 struct memdq *dq = &head; | |
| 245 cyg_int32 mf = 0; | |
| 246 | |
| 247 do { | |
| 248 CYG_ASSERT( dq->next->prev==dq, "Bad link in dq"); | |
| 249 dq = dq->next; | |
| 250 if(0 == dq->size) { | |
| 251 CYG_ASSERT(dq == &head, "bad free block"); | |
| 252 break; | |
| 253 } | |
| 254 if(dq->size > mf) | |
| 255 mf = dq->size; | |
| 256 } while(1); | |
| 257 | |
| 258 base = obase; | |
| 259 size = osize; | |
| 260 maxfree = mf; | |
| 261 } | |
| 262 | |
| 263 | |
| 264 inline cyg_int32 | |
| 265 Cyg_Mempool_Variable_Implementation::get_allocation_size( cyg_uint8 *ptr ) | |
| 266 { | |
| 267 CYG_CHECK_DATA_PTR(ptr, "Requested allocation size of bad data pointer!"); | |
| 268 | |
| 269 if(!(bottom<=ptr && ptr<=top)) | |
| 270 return -1; | |
| 271 | |
| 272 struct memdq *dq=(struct memdq *)(ptr-sizeof(struct memdq)); | |
| 273 | |
| 274 // check magic number in block to be freed | |
| 275 if(dq->next != dq->prev || dq->next != (struct memdq *)0xd530d53) | |
| 276 return -1; | |
| 277 | |
| 278 return dq->size; | |
| 279 | |
| 280 } // get_allocation_size() | |
| 281 | |
| 282 // ------------------------------------------------------------------------- | |
| 283 #endif // ifndef CYGONCE_KERNEL_MVARIMPL_INL | |
| 284 // EOF mvarimpl.inl |
