[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 |
---|
[dd687d97] | 9 | * http://www.rtems.com/license/LICENSE. |
---|
[93b4e6ef] | 10 | * |
---|
| 11 | * $Id$ |
---|
| 12 | */ |
---|
| 13 | |
---|
[a8eed23] | 14 | #if HAVE_CONFIG_H |
---|
| 15 | #include "config.h" |
---|
| 16 | #endif |
---|
[93b4e6ef] | 17 | |
---|
| 18 | #include <rtems/system.h> |
---|
| 19 | #include <rtems/score/sysstate.h> |
---|
| 20 | #include <rtems/score/heap.h> |
---|
| 21 | |
---|
| 22 | /*PAGE |
---|
| 23 | * |
---|
| 24 | * _Heap_Allocate |
---|
| 25 | * |
---|
| 26 | * This kernel routine allocates the requested size of memory |
---|
| 27 | * from the specified heap. |
---|
| 28 | * |
---|
| 29 | * Input parameters: |
---|
| 30 | * the_heap - pointer to heap header. |
---|
| 31 | * size - size in bytes of the memory block to allocate. |
---|
| 32 | * |
---|
| 33 | * Output parameters: |
---|
| 34 | * returns - starting address of memory block allocated |
---|
| 35 | */ |
---|
| 36 | |
---|
| 37 | void *_Heap_Allocate( |
---|
| 38 | Heap_Control *the_heap, |
---|
[8022a68] | 39 | size_t size |
---|
[93b4e6ef] | 40 | ) |
---|
| 41 | { |
---|
[962e894f] | 42 | uint32_t the_size; |
---|
| 43 | uint32_t search_count; |
---|
[93b4e6ef] | 44 | Heap_Block *the_block; |
---|
[962e894f] | 45 | void *ptr = NULL; |
---|
| 46 | Heap_Statistics *const stats = &the_heap->stats; |
---|
| 47 | Heap_Block *const tail = _Heap_Tail(the_heap); |
---|
| 48 | |
---|
| 49 | the_size = |
---|
| 50 | _Heap_Calc_block_size(size, the_heap->page_size, the_heap->min_block_size); |
---|
| 51 | if(the_size == 0) |
---|
| 52 | return NULL; |
---|
| 53 | |
---|
| 54 | /* Find large enough free block. */ |
---|
| 55 | for(the_block = _Heap_First(the_heap), search_count = 0; |
---|
| 56 | the_block != tail; |
---|
| 57 | the_block = the_block->next, ++search_count) |
---|
| 58 | { |
---|
| 59 | /* As we always coalesce free blocks, prev block must have been used. */ |
---|
| 60 | _HAssert(_Heap_Is_prev_used(the_block)); |
---|
[66fedb46] | 61 | |
---|
[962e894f] | 62 | /* Don't bother to mask out the HEAP_PREV_USED bit as it won't change the |
---|
| 63 | result of the comparison. */ |
---|
| 64 | if(the_block->size >= the_size) { |
---|
[80f2885b] | 65 | (void)_Heap_Block_allocate(the_heap, the_block, the_size ); |
---|
[05279b84] | 66 | |
---|
[962e894f] | 67 | ptr = _Heap_User_area(the_block); |
---|
[93b4e6ef] | 68 | |
---|
[962e894f] | 69 | stats->allocs += 1; |
---|
| 70 | stats->searches += search_count + 1; |
---|
[93b4e6ef] | 71 | |
---|
[962e894f] | 72 | _HAssert(_Heap_Is_aligned_ptr(ptr, the_heap->page_size)); |
---|
[93b4e6ef] | 73 | break; |
---|
[962e894f] | 74 | } |
---|
[93b4e6ef] | 75 | } |
---|
| 76 | |
---|
[962e894f] | 77 | if(stats->max_search < search_count) |
---|
| 78 | stats->max_search = search_count; |
---|
[93b4e6ef] | 79 | |
---|
| 80 | return ptr; |
---|
| 81 | } |
---|