1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup RTEMSScoreHeap |
---|
7 | * |
---|
8 | * @brief This source file contains the implementation of |
---|
9 | * _Heap_Greedy_allocate(), _Heap_Greedy_allocate_all_except_largest(), and |
---|
10 | * _Heap_Greedy_free(). |
---|
11 | */ |
---|
12 | |
---|
13 | /* |
---|
14 | * Copyright (c) 2012 embedded brains GmbH. All rights reserved. |
---|
15 | * |
---|
16 | * Redistribution and use in source and binary forms, with or without |
---|
17 | * modification, are permitted provided that the following conditions |
---|
18 | * are met: |
---|
19 | * 1. Redistributions of source code must retain the above copyright |
---|
20 | * notice, this list of conditions and the following disclaimer. |
---|
21 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
22 | * notice, this list of conditions and the following disclaimer in the |
---|
23 | * documentation and/or other materials provided with the distribution. |
---|
24 | * |
---|
25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
29 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
35 | * POSSIBILITY OF SUCH DAMAGE. |
---|
36 | */ |
---|
37 | |
---|
38 | #ifdef HAVE_CONFIG_H |
---|
39 | #include "config.h" |
---|
40 | #endif |
---|
41 | |
---|
42 | #include <rtems/score/heapimpl.h> |
---|
43 | |
---|
44 | Heap_Block *_Heap_Greedy_allocate( |
---|
45 | Heap_Control *heap, |
---|
46 | const uintptr_t *block_sizes, |
---|
47 | size_t block_count |
---|
48 | ) |
---|
49 | { |
---|
50 | Heap_Block *const free_list_tail = _Heap_Free_list_tail( heap ); |
---|
51 | Heap_Block *allocated_blocks = NULL; |
---|
52 | Heap_Block *blocks = NULL; |
---|
53 | Heap_Block *current; |
---|
54 | size_t i; |
---|
55 | |
---|
56 | _Heap_Protection_free_all_delayed_blocks( heap ); |
---|
57 | |
---|
58 | for (i = 0; i < block_count; ++i) { |
---|
59 | void *next = _Heap_Allocate( heap, block_sizes [i] ); |
---|
60 | |
---|
61 | if ( next != NULL ) { |
---|
62 | Heap_Block *next_block = _Heap_Block_of_alloc_area( |
---|
63 | (uintptr_t) next, |
---|
64 | heap->page_size |
---|
65 | ); |
---|
66 | |
---|
67 | next_block->next = allocated_blocks; |
---|
68 | allocated_blocks = next_block; |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | while ( (current = _Heap_Free_list_first( heap )) != free_list_tail ) { |
---|
73 | _Heap_Block_allocate( |
---|
74 | heap, |
---|
75 | current, |
---|
76 | _Heap_Alloc_area_of_block( current ), |
---|
77 | _Heap_Block_size( current ) - HEAP_BLOCK_HEADER_SIZE |
---|
78 | ); |
---|
79 | |
---|
80 | current->next = blocks; |
---|
81 | blocks = current; |
---|
82 | } |
---|
83 | |
---|
84 | while ( allocated_blocks != NULL ) { |
---|
85 | current = allocated_blocks; |
---|
86 | allocated_blocks = allocated_blocks->next; |
---|
87 | _Heap_Free( heap, (void *) _Heap_Alloc_area_of_block( current ) ); |
---|
88 | } |
---|
89 | |
---|
90 | return blocks; |
---|
91 | } |
---|
92 | |
---|
93 | Heap_Block *_Heap_Greedy_allocate_all_except_largest( |
---|
94 | Heap_Control *heap, |
---|
95 | uintptr_t *allocatable_size |
---|
96 | ) |
---|
97 | { |
---|
98 | Heap_Information info; |
---|
99 | |
---|
100 | _Heap_Get_free_information( heap, &info ); |
---|
101 | |
---|
102 | if ( info.largest > 0 ) { |
---|
103 | *allocatable_size = info.largest - HEAP_BLOCK_HEADER_SIZE + HEAP_ALLOC_BONUS; |
---|
104 | } else { |
---|
105 | *allocatable_size = 0; |
---|
106 | } |
---|
107 | |
---|
108 | return _Heap_Greedy_allocate( heap, allocatable_size, 1 ); |
---|
109 | } |
---|
110 | |
---|
111 | void _Heap_Greedy_free( |
---|
112 | Heap_Control *heap, |
---|
113 | Heap_Block *blocks |
---|
114 | ) |
---|
115 | { |
---|
116 | while ( blocks != NULL ) { |
---|
117 | Heap_Block *current = blocks; |
---|
118 | |
---|
119 | blocks = blocks->next; |
---|
120 | _Heap_Free( heap, (void *) _Heap_Alloc_area_of_block( current ) ); |
---|
121 | } |
---|
122 | } |
---|