[93b4e6ef] | 1 | /* |
---|
| 2 | * Heap Handler |
---|
| 3 | * |
---|
[08311cc3] | 4 | * COPYRIGHT (c) 1989-1999. |
---|
[93b4e6ef] | 5 | * On-Line Applications Research Corporation (OAR). |
---|
| 6 | * |
---|
| 7 | * The license and distribution terms for this file may be |
---|
| 8 | * found in the file LICENSE in this distribution or at |
---|
| 9 | * http://www.OARcorp.com/rtems/license.html. |
---|
| 10 | * |
---|
| 11 | * $Id$ |
---|
| 12 | */ |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | #include <rtems/system.h> |
---|
| 16 | #include <rtems/score/sysstate.h> |
---|
| 17 | #include <rtems/score/heap.h> |
---|
| 18 | |
---|
| 19 | /*PAGE |
---|
| 20 | * |
---|
| 21 | * _Heap_Allocate |
---|
| 22 | * |
---|
| 23 | * This kernel routine allocates the requested size of memory |
---|
| 24 | * from the specified heap. |
---|
| 25 | * |
---|
| 26 | * Input parameters: |
---|
| 27 | * the_heap - pointer to heap header. |
---|
| 28 | * size - size in bytes of the memory block to allocate. |
---|
| 29 | * |
---|
| 30 | * Output parameters: |
---|
| 31 | * returns - starting address of memory block allocated |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | void *_Heap_Allocate( |
---|
| 35 | Heap_Control *the_heap, |
---|
| 36 | unsigned32 size |
---|
| 37 | ) |
---|
| 38 | { |
---|
| 39 | unsigned32 excess; |
---|
| 40 | unsigned32 the_size; |
---|
| 41 | Heap_Block *the_block; |
---|
| 42 | Heap_Block *next_block; |
---|
| 43 | Heap_Block *temporary_block; |
---|
| 44 | void *ptr; |
---|
| 45 | unsigned32 offset; |
---|
| 46 | |
---|
| 47 | excess = size % the_heap->page_size; |
---|
| 48 | the_size = size + the_heap->page_size + HEAP_BLOCK_USED_OVERHEAD; |
---|
| 49 | |
---|
| 50 | if ( excess ) |
---|
| 51 | the_size += the_heap->page_size - excess; |
---|
| 52 | |
---|
| 53 | if ( the_size < sizeof( Heap_Block ) ) |
---|
| 54 | the_size = sizeof( Heap_Block ); |
---|
| 55 | |
---|
| 56 | for ( the_block = the_heap->first; |
---|
| 57 | ; |
---|
| 58 | the_block = the_block->next ) { |
---|
| 59 | if ( the_block == _Heap_Tail( the_heap ) ) |
---|
| 60 | return( NULL ); |
---|
| 61 | if ( the_block->front_flag >= the_size ) |
---|
| 62 | break; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | if ( (the_block->front_flag - the_size) > |
---|
| 66 | (the_heap->page_size + HEAP_BLOCK_USED_OVERHEAD) ) { |
---|
| 67 | the_block->front_flag -= the_size; |
---|
| 68 | next_block = _Heap_Next_block( the_block ); |
---|
| 69 | next_block->back_flag = the_block->front_flag; |
---|
| 70 | |
---|
| 71 | temporary_block = _Heap_Block_at( next_block, the_size ); |
---|
| 72 | temporary_block->back_flag = |
---|
| 73 | next_block->front_flag = _Heap_Build_flag( the_size, |
---|
| 74 | HEAP_BLOCK_USED ); |
---|
| 75 | ptr = _Heap_Start_of_user_area( next_block ); |
---|
| 76 | } else { |
---|
| 77 | next_block = _Heap_Next_block( the_block ); |
---|
| 78 | next_block->back_flag = _Heap_Build_flag( the_block->front_flag, |
---|
| 79 | HEAP_BLOCK_USED ); |
---|
| 80 | the_block->front_flag = next_block->back_flag; |
---|
| 81 | the_block->next->previous = the_block->previous; |
---|
| 82 | the_block->previous->next = the_block->next; |
---|
| 83 | ptr = _Heap_Start_of_user_area( the_block ); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /* |
---|
| 87 | * round ptr up to a multiple of page size |
---|
| 88 | * Have to save the bump amount in the buffer so that free can figure it out |
---|
| 89 | */ |
---|
| 90 | |
---|
| 91 | offset = the_heap->page_size - (((unsigned32) ptr) & (the_heap->page_size - 1)); |
---|
| 92 | ptr = _Addresses_Add_offset( ptr, offset ); |
---|
| 93 | *(((unsigned32 *) ptr) - 1) = offset; |
---|
| 94 | |
---|
| 95 | #ifdef RTEMS_DEBUG |
---|
| 96 | { |
---|
| 97 | unsigned32 ptr_u32; |
---|
| 98 | ptr_u32 = (unsigned32) ptr; |
---|
| 99 | if (ptr_u32 & (the_heap->page_size - 1)) |
---|
| 100 | abort(); |
---|
| 101 | } |
---|
| 102 | #endif |
---|
| 103 | |
---|
| 104 | return ptr; |
---|
| 105 | } |
---|
| 106 | |
---|