1 | /* |
---|
2 | * Heap Handler |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-1999. |
---|
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.rtems.com/license/LICENSE. |
---|
10 | * |
---|
11 | * $Id$ |
---|
12 | */ |
---|
13 | |
---|
14 | #if HAVE_CONFIG_H |
---|
15 | #include "config.h" |
---|
16 | #endif |
---|
17 | |
---|
18 | #include <rtems/system.h> |
---|
19 | #include <rtems/score/sysstate.h> |
---|
20 | #include <rtems/score/heap.h> |
---|
21 | |
---|
22 | void *_Heap_Allocate( Heap_Control *heap, uintptr_t size ) |
---|
23 | { |
---|
24 | Heap_Statistics *const stats = &heap->stats; |
---|
25 | Heap_Block * const tail = _Heap_Free_list_tail( heap ); |
---|
26 | Heap_Block *block = _Heap_First_free_block( heap ); |
---|
27 | uint32_t search_count = 0; |
---|
28 | void *alloc_area_begin_ptr = NULL; |
---|
29 | |
---|
30 | size = _Heap_Calc_block_size( size, heap->page_size, heap->min_block_size ); |
---|
31 | if( size == 0 ) { |
---|
32 | return NULL; |
---|
33 | } |
---|
34 | |
---|
35 | /* |
---|
36 | * Find large enough free block. |
---|
37 | * |
---|
38 | * Do not bother to mask out the HEAP_PREV_BLOCK_USED bit as it will not |
---|
39 | * change the result of the size comparison. |
---|
40 | */ |
---|
41 | while (block != tail && block->size_and_flag < size) { |
---|
42 | _HAssert( _Heap_Is_prev_used( block )); |
---|
43 | |
---|
44 | block = block->next; |
---|
45 | ++search_count; |
---|
46 | } |
---|
47 | |
---|
48 | if (block != tail) { |
---|
49 | _Heap_Block_allocate( heap, block, size ); |
---|
50 | |
---|
51 | alloc_area_begin_ptr = (void *) _Heap_Alloc_area_of_block( block ); |
---|
52 | |
---|
53 | _HAssert( _Heap_Is_aligned( (uintptr_t) alloc_area_begin_ptr, heap->page_size )); |
---|
54 | |
---|
55 | /* Statistics */ |
---|
56 | ++stats->allocs; |
---|
57 | stats->searches += search_count; |
---|
58 | } |
---|
59 | |
---|
60 | /* Statistics */ |
---|
61 | if (stats->max_search < search_count) { |
---|
62 | stats->max_search = search_count; |
---|
63 | } |
---|
64 | |
---|
65 | return alloc_area_begin_ptr; |
---|
66 | } |
---|