Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:26

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSScoreHeap
0007  *
0008  * @brief This source file contains the implementation of
0009  *   _Heap_Greedy_allocate(), _Heap_Greedy_allocate_all_except_largest(), and
0010  *   _Heap_Greedy_free().
0011  */
0012 
0013 /*
0014  * Copyright (c) 2012 embedded brains GmbH & Co. KG
0015  *
0016  * Redistribution and use in source and binary forms, with or without
0017  * modification, are permitted provided that the following conditions
0018  * are met:
0019  * 1. Redistributions of source code must retain the above copyright
0020  *    notice, this list of conditions and the following disclaimer.
0021  * 2. Redistributions in binary form must reproduce the above copyright
0022  *    notice, this list of conditions and the following disclaimer in the
0023  *    documentation and/or other materials provided with the distribution.
0024  *
0025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0028  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0029  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0030  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0031  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0033  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0034  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0035  * POSSIBILITY OF SUCH DAMAGE.
0036  */
0037 
0038 #ifdef HAVE_CONFIG_H
0039 #include "config.h"
0040 #endif
0041 
0042 #include <rtems/score/heapimpl.h>
0043 
0044 Heap_Block *_Heap_Greedy_allocate(
0045   Heap_Control *heap,
0046   const uintptr_t *block_sizes,
0047   size_t block_count
0048 )
0049 {
0050   Heap_Block *const free_list_tail = _Heap_Free_list_tail( heap );
0051   Heap_Block *allocated_blocks = NULL;
0052   Heap_Block *blocks = NULL;
0053   Heap_Block *current;
0054   size_t i;
0055 
0056   _Heap_Protection_free_all_delayed_blocks( heap );
0057 
0058   for (i = 0; i < block_count; ++i) {
0059     void *next = _Heap_Allocate( heap, block_sizes [i] );
0060 
0061     if ( next != NULL ) {
0062       Heap_Block *next_block = _Heap_Block_of_alloc_area(
0063         (uintptr_t) next,
0064         heap->page_size
0065       );
0066 
0067       next_block->next = allocated_blocks;
0068       allocated_blocks = next_block;
0069     }
0070   }
0071 
0072   while ( (current = _Heap_Free_list_first( heap )) != free_list_tail ) {
0073     _Heap_Block_allocate(
0074       heap,
0075       current,
0076       _Heap_Alloc_area_of_block( current ),
0077       _Heap_Block_size( current ) - HEAP_BLOCK_HEADER_SIZE
0078     );
0079 
0080     current->next = blocks;
0081     blocks = current;
0082   }
0083 
0084   while ( allocated_blocks != NULL ) {
0085     current = allocated_blocks;
0086     allocated_blocks = allocated_blocks->next;
0087     _Heap_Free( heap, (void *) _Heap_Alloc_area_of_block( current ) );
0088   }
0089 
0090   return blocks;
0091 }
0092 
0093 Heap_Block *_Heap_Greedy_allocate_all_except_largest(
0094   Heap_Control *heap,
0095   uintptr_t *allocatable_size
0096 )
0097 {
0098   Heap_Information info;
0099 
0100   _Heap_Get_free_information( heap, &info );
0101 
0102   if ( info.largest > 0 ) {
0103     *allocatable_size = info.largest - HEAP_BLOCK_HEADER_SIZE + HEAP_ALLOC_BONUS;
0104   } else {
0105     *allocatable_size = 0;
0106   }
0107 
0108   return _Heap_Greedy_allocate( heap, allocatable_size, 1 );
0109 }
0110 
0111 void _Heap_Greedy_free(
0112   Heap_Control *heap,
0113   Heap_Block *blocks
0114 )
0115 {
0116   while ( blocks != NULL ) {
0117     Heap_Block *current = blocks;
0118 
0119     blocks = blocks->next;
0120     _Heap_Free( heap, (void *) _Heap_Alloc_area_of_block( current ) );
0121   }
0122 }