1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup ScoreHeap |
---|
5 | * |
---|
6 | * @brief Heap Handler implementation. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * COPYRIGHT (c) 1989-1999. |
---|
11 | * On-Line Applications Research Corporation (OAR). |
---|
12 | * |
---|
13 | * Copyright (c) 2009 embedded brains GmbH. |
---|
14 | * |
---|
15 | * The license and distribution terms for this file may be |
---|
16 | * found in the file LICENSE in this distribution or at |
---|
17 | * http://www.rtems.org/license/LICENSE. |
---|
18 | */ |
---|
19 | |
---|
20 | #if HAVE_CONFIG_H |
---|
21 | #include "config.h" |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <rtems/system.h> |
---|
25 | #include <rtems/score/heapimpl.h> |
---|
26 | |
---|
27 | static Heap_Resize_status _Heap_Resize_block_checked( |
---|
28 | Heap_Control *heap, |
---|
29 | Heap_Block *block, |
---|
30 | uintptr_t alloc_begin, |
---|
31 | uintptr_t new_alloc_size, |
---|
32 | uintptr_t *old_size, |
---|
33 | uintptr_t *new_size |
---|
34 | ) |
---|
35 | { |
---|
36 | Heap_Statistics *const stats = &heap->stats; |
---|
37 | |
---|
38 | uintptr_t const block_begin = (uintptr_t) block; |
---|
39 | uintptr_t block_size = _Heap_Block_size( block ); |
---|
40 | uintptr_t block_end = block_begin + block_size; |
---|
41 | |
---|
42 | uintptr_t alloc_size = block_end - alloc_begin + HEAP_ALLOC_BONUS; |
---|
43 | |
---|
44 | Heap_Block *next_block = _Heap_Block_at( block, block_size ); |
---|
45 | uintptr_t next_block_size = _Heap_Block_size( next_block ); |
---|
46 | bool next_block_is_free = _Heap_Is_free( next_block ); |
---|
47 | |
---|
48 | _HAssert( _Heap_Is_block_in_heap( heap, next_block ) ); |
---|
49 | _HAssert( _Heap_Is_prev_used( next_block ) ); |
---|
50 | |
---|
51 | *old_size = alloc_size; |
---|
52 | |
---|
53 | if ( next_block_is_free ) { |
---|
54 | block_size += next_block_size; |
---|
55 | alloc_size += next_block_size; |
---|
56 | } |
---|
57 | |
---|
58 | if ( new_alloc_size > alloc_size ) { |
---|
59 | return HEAP_RESIZE_UNSATISFIED; |
---|
60 | } |
---|
61 | |
---|
62 | if ( next_block_is_free ) { |
---|
63 | _Heap_Block_set_size( block, block_size ); |
---|
64 | |
---|
65 | _Heap_Free_list_remove( next_block ); |
---|
66 | |
---|
67 | next_block = _Heap_Block_at( block, block_size ); |
---|
68 | next_block->size_and_flag |= HEAP_PREV_BLOCK_USED; |
---|
69 | |
---|
70 | /* Statistics */ |
---|
71 | --stats->free_blocks; |
---|
72 | stats->free_size -= next_block_size; |
---|
73 | } |
---|
74 | |
---|
75 | block = _Heap_Block_allocate( heap, block, alloc_begin, new_alloc_size ); |
---|
76 | |
---|
77 | block_size = _Heap_Block_size( block ); |
---|
78 | next_block = _Heap_Block_at( block, block_size ); |
---|
79 | *new_size = (uintptr_t) next_block - alloc_begin + HEAP_ALLOC_BONUS; |
---|
80 | |
---|
81 | /* Statistics */ |
---|
82 | ++stats->resizes; |
---|
83 | |
---|
84 | return HEAP_RESIZE_SUCCESSFUL; |
---|
85 | } |
---|
86 | |
---|
87 | Heap_Resize_status _Heap_Resize_block( |
---|
88 | Heap_Control *heap, |
---|
89 | void *alloc_begin_ptr, |
---|
90 | uintptr_t new_alloc_size, |
---|
91 | uintptr_t *old_size, |
---|
92 | uintptr_t *new_size |
---|
93 | ) |
---|
94 | { |
---|
95 | uintptr_t const page_size = heap->page_size; |
---|
96 | |
---|
97 | uintptr_t const alloc_begin = (uintptr_t) alloc_begin_ptr; |
---|
98 | |
---|
99 | Heap_Block *const block = _Heap_Block_of_alloc_area( alloc_begin, page_size ); |
---|
100 | |
---|
101 | *old_size = 0; |
---|
102 | *new_size = 0; |
---|
103 | |
---|
104 | if ( _Heap_Is_block_in_heap( heap, block ) ) { |
---|
105 | _Heap_Protection_block_check( heap, block ); |
---|
106 | |
---|
107 | /* TODO: Free only the next block if necessary */ |
---|
108 | _Heap_Protection_free_all_delayed_blocks( heap ); |
---|
109 | |
---|
110 | return _Heap_Resize_block_checked( |
---|
111 | heap, |
---|
112 | block, |
---|
113 | alloc_begin, |
---|
114 | new_alloc_size, |
---|
115 | old_size, |
---|
116 | new_size |
---|
117 | ); |
---|
118 | } |
---|
119 | return HEAP_RESIZE_FATAL_ERROR; |
---|
120 | } |
---|