Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSScoreWorkspace
0007  *
0008  * @brief This header file provides the implementation of
0009  *   _Workspace_Initialize_for_multiple_areas().
0010  */
0011 
0012 /*
0013  * Copyright (C) 2012, 2020 embedded brains GmbH & Co. KG
0014  *
0015  * Redistribution and use in source and binary forms, with or without
0016  * modification, are permitted provided that the following conditions
0017  * are met:
0018  * 1. Redistributions of source code must retain the above copyright
0019  *    notice, this list of conditions and the following disclaimer.
0020  * 2. Redistributions in binary form must reproduce the above copyright
0021  *    notice, this list of conditions and the following disclaimer in the
0022  *    documentation and/or other materials provided with the distribution.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  */
0036 
0037 #ifndef _RTEMS_SCORE_WKSPACEINITMULTI_H
0038 #define _RTEMS_SCORE_WKSPACEINITMULTI_H
0039 
0040 #include <rtems/score/wkspace.h>
0041 #include <rtems/score/heapimpl.h>
0042 #include <rtems/score/interr.h>
0043 #include <rtems/score/memory.h>
0044 #include <rtems/config.h>
0045 
0046 #ifdef __cplusplus
0047 extern "C" {
0048 #endif
0049 
0050 /**
0051  * @ingroup RTEMSScoreWorkspace
0052  *
0053  * @brief Initializes the RTEMS Workspace with support for more than one memory
0054  *   area.
0055  *
0056  * This implementation should be used by BSPs which provide more than one
0057  * memory area via _Memory_Get() to implement
0058  * _Workspace_Handler_initialization().
0059  */
0060 static inline void _Workspace_Initialize_for_multiple_areas( void )
0061 {
0062   const Memory_Information             *mem;
0063   Heap_Initialization_or_extend_handler init_or_extend;
0064   uintptr_t                             remaining;
0065   bool                                  unified;
0066   uintptr_t                             page_size;
0067   uintptr_t                             overhead;
0068   size_t                                i;
0069 
0070   mem = _Memory_Get();
0071   page_size = CPU_HEAP_ALIGNMENT;
0072   remaining = rtems_configuration_get_work_space_size();
0073   init_or_extend = _Heap_Initialize;
0074   unified = rtems_configuration_get_unified_work_area();
0075   overhead = _Heap_Area_overhead( page_size );
0076 
0077   for ( i = 0; i < _Memory_Get_count( mem ); ++i ) {
0078     Memory_Area *area;
0079     uintptr_t    free_size;
0080 
0081     area = _Memory_Get_area( mem, i );
0082     free_size = _Memory_Get_free_size( area );
0083 
0084     if ( free_size > overhead ) {
0085       uintptr_t space_available;
0086       uintptr_t size;
0087 
0088       if ( unified ) {
0089         size = free_size;
0090       } else {
0091         if ( remaining > 0 ) {
0092           size = remaining < free_size - overhead ?
0093             remaining + overhead : free_size;
0094         } else {
0095           size = 0;
0096         }
0097       }
0098 
0099       space_available = ( *init_or_extend )(
0100         &_Workspace_Area,
0101         _Memory_Get_free_begin( area ),
0102         size,
0103         page_size
0104       );
0105 
0106       _Memory_Consume( area, size );
0107 
0108       if ( space_available < remaining ) {
0109         remaining -= space_available;
0110       } else {
0111         remaining = 0;
0112       }
0113 
0114       init_or_extend = _Heap_Extend;
0115     }
0116   }
0117 
0118   if ( remaining > 0 ) {
0119     _Internal_error( INTERNAL_ERROR_TOO_LITTLE_WORKSPACE );
0120   }
0121 
0122   _Heap_Protection_set_delayed_free_fraction( &_Workspace_Area, 1 );
0123 }
0124 
0125 #ifdef __cplusplus
0126 }
0127 #endif
0128 
0129 #endif /* _RTEMS_SCORE_WKSPACEINITMULTI_H */