Back to home page

LXR

 
 

    


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

0001 /**
0002  * @file
0003  *
0004  * @ingroup RTEMSScoreMemory
0005  *
0006  * @brief This header file provides the interfaces of the
0007  *   @ref RTEMSScoreMemory.
0008  */
0009 
0010 /*
0011  * SPDX-License-Identifier: BSD-2-Clause
0012  *
0013  * Copyright (C) 2019, 2022 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_MEMORY_H
0038 #define _RTEMS_SCORE_MEMORY_H
0039 
0040 #include <rtems/score/basedefs.h>
0041 #include <rtems/score/assert.h>
0042 
0043 #ifdef __cplusplus
0044 extern "C" {
0045 #endif /* __cplusplus */
0046 
0047 /**
0048  * @defgroup RTEMSScoreMemory Memory Handler
0049  *
0050  * @ingroup RTEMSScore
0051  *
0052  * @brief This group contains the Memory Handler implementation.
0053  *
0054  * The Memory Handler provides memory areas supplied by the BSP to higher level memory
0055  * consumers such as the @ref RTEMSScoreWorkspace and the @ref MallocSupport.
0056  *
0057  * @{
0058  */
0059 
0060 /**
0061  * @brief The memory area description.
0062  */
0063 typedef struct {
0064   /**
0065    * @brief A pointer to the begin of the memory area.
0066    */
0067   const void *begin;
0068 
0069   /**
0070    * @brief A pointer to the begin of the free area of the memory area.
0071    */
0072   void *free;
0073 
0074   /**
0075    * @brief A pointer to the end of the memory area.
0076    */
0077   const void *end;
0078 } Memory_Area;
0079 
0080 /**
0081  * @brief The memory information.
0082  */
0083 typedef struct {
0084   /**
0085    * @brief The count of memory areas.
0086    */
0087   size_t count;
0088 
0089   /**
0090    * @brief The memory area table.
0091    */
0092   Memory_Area *areas;
0093 } Memory_Information;
0094 
0095 /**
0096  * @brief Statically initialize a memory information.
0097  *
0098  * @param areas The designator of an array of the memory areas.
0099  */
0100 #define MEMORY_INFORMATION_INITIALIZER( areas ) \
0101   { RTEMS_ARRAY_SIZE( areas ), ( areas ) }
0102 
0103 /**
0104  * @brief Statically initialize a memory area.
0105  *
0106  * @param begin The begin of the memory area.
0107  * @param end The end of the memory area.
0108  */
0109 #define MEMORY_INITIALIZER( begin, end ) { ( begin ), ( begin ), ( end ) }
0110 
0111 /**
0112  * @brief Get the memory area count.
0113  *
0114  * @param information The memory information.
0115  *
0116  * @return The memory area count.
0117  */
0118 static inline size_t _Memory_Get_count(
0119   const Memory_Information *information
0120 )
0121 {
0122   return information->count;
0123 }
0124 
0125 /**
0126  * @brief Get a memory area by index.
0127  *
0128  * @param information The memory information.
0129  * @param index The index of the memory area to return.
0130  *
0131  * @return The memory area of the specified index.
0132  */
0133 static inline Memory_Area *_Memory_Get_area(
0134   const Memory_Information *information,
0135   size_t                    index
0136 )
0137 {
0138   _Assert( index < _Memory_Get_count( information ) );
0139   return &information->areas[ index ];
0140 }
0141 
0142 /**
0143  * @brief Initialize the memory area.
0144  *
0145  * @param area The memory area.
0146  * @param begin The begin of the memory area.
0147  * @param end The end of the memory area.
0148  */
0149 static inline void _Memory_Initialize(
0150   Memory_Area *area,
0151   void        *begin,
0152   void        *end
0153 )
0154 {
0155   area->begin = begin;
0156   area->free = begin;
0157   area->end = end;
0158 }
0159 
0160 /**
0161  * @brief Initialize the memory area by size.
0162  *
0163  * @param area The memory area.
0164  * @param begin The begin of the memory area.
0165  * @param size The size of the memory area in bytes.
0166  */
0167 static inline void _Memory_Initialize_by_size(
0168   Memory_Area *area,
0169   void        *begin,
0170   uintptr_t    size
0171 )
0172 {
0173   area->begin = begin;
0174   area->free = begin;
0175   area->end = (char *) begin + size;
0176 }
0177 
0178 /**
0179  * @brief Get the memory area begin.
0180  *
0181  * @param area The memory area.
0182  *
0183  * @return The memory area begin.
0184  */
0185 static inline const void *_Memory_Get_begin( const Memory_Area *area )
0186 {
0187   return area->begin;
0188 }
0189 
0190 /**
0191  * @brief Set the memory area begin.
0192  *
0193  * @param area The memory area.
0194  * @param begin The memory area begin.
0195  */
0196 static inline void _Memory_Set_begin(
0197   Memory_Area *area,
0198   const void  *begin
0199 )
0200 {
0201   area->begin = begin;
0202 }
0203 
0204 /**
0205  * @brief Get the memory area end.
0206  *
0207  * @param area The memory area.
0208  *
0209  * @return The memory area end.
0210  */
0211 static inline const void *_Memory_Get_end( const Memory_Area *area )
0212 {
0213   return area->end;
0214 }
0215 
0216 /**
0217  * @brief Set the memory area end.
0218  *
0219  * @param area The memory area.
0220  * @param end The memory area end.
0221  */
0222 static inline void _Memory_Set_end(
0223   Memory_Area *area,
0224   const void  *end
0225 )
0226 {
0227   area->end = end;
0228 }
0229 
0230 /**
0231  * @brief Get the memory area size.
0232  *
0233  * @param area The memory area.
0234  *
0235  * @return The memory area size in bytes.
0236  */
0237 static inline uintptr_t _Memory_Get_size( const Memory_Area *area )
0238 {
0239   return (uintptr_t) area->end - (uintptr_t) area->begin;
0240 }
0241 
0242 /**
0243  * @brief Get the begin of the free area of the memory area.
0244  *
0245  * @param area The memory area.
0246  *
0247  * @return The free memory area begin the memory area.
0248  */
0249 static inline void *_Memory_Get_free_begin( const Memory_Area *area )
0250 {
0251   return area->free;
0252 }
0253 
0254 /**
0255  * @brief Set the begin of the free area of the memory area.
0256  *
0257  * @param area The memory area.
0258  * @param begin The free memory area begin the memory area.
0259  */
0260 static inline void _Memory_Set_free_begin(
0261   Memory_Area *area,
0262   void        *begin
0263 )
0264 {
0265   area->free = begin;
0266 }
0267 
0268 /**
0269  * @brief Get the size of the free memory area of the memory area.
0270  *
0271  * @param area The memory area.
0272  *
0273  * @return The free memory area size in bytes of the memory area.
0274  */
0275 static inline uintptr_t _Memory_Get_free_size( const Memory_Area *area )
0276 {
0277   return (uintptr_t) area->end - (uintptr_t) area->free;
0278 }
0279 
0280 /**
0281  * @brief Consume the specified size from the free memory area of the memory
0282  * area.
0283  *
0284  * @param area The memory area.
0285  * @param consume The bytes to consume from the free memory area of the memory
0286  *   area.
0287  */
0288 static inline void _Memory_Consume(
0289   Memory_Area *area,
0290   uintptr_t    consume
0291 )
0292 {
0293   area->free = (char *) area->free + consume;
0294 }
0295 
0296 /**
0297  * @brief Return the memory information of this platform.
0298  *
0299  * This function is provided by the Board Support Package (BSP).  Using a
0300  * function gives the BSPs a bit more freedom with respect to the
0301  * implementation.  Calling this function shall not have side-effects.
0302  * Initialization steps to set up the memory information shall be done in a
0303  * system initialization handler (RTEMS_SYSINIT_MEMORY).
0304  *
0305  * @return The memory information.
0306  */
0307 const Memory_Information *_Memory_Get( void );
0308 
0309 /**
0310  * @brief Allocate a memory area from the memory information.
0311  *
0312  * It is not possible to free the memory area allocated by this function.
0313  *
0314  * @param information The memory information.
0315  * @param size The size in bytes of the memory area to allocate.
0316  * @param alignment The alignment in bytes of the memory area to allocate.  It
0317  *   must be a power of two.
0318  *
0319  * @retval NULL No such memory area available.
0320  * @retval begin The begin of the allocated memory area.
0321  */
0322 void *_Memory_Allocate(
0323   const Memory_Information *information,
0324   uintptr_t                 size,
0325   uintptr_t                 alignment
0326 );
0327 
0328 /**
0329  * @brief Fill all free memory areas of the memory information with a constant
0330  * byte.
0331  *
0332  * @param information The memory information.
0333  * @param c The constant byte to fill the free memory areas.
0334  */
0335 void _Memory_Fill( const Memory_Information *information, int c );
0336 
0337 /**
0338  * @brief Indicates if the memory is zeroed during system initialization.
0339  *
0340  * This value is provided via <rtems/confdefs.h> in case
0341  * CONFIGURE_ZERO_WORKSPACE_AUTOMATICALLY is defined.
0342  */
0343 extern const bool _Memory_Zero_before_use;
0344 
0345 /**
0346  * @brief Zeros all free memory areas of the system.
0347  */
0348 void _Memory_Zero_free_areas( void );
0349 
0350 /**
0351  * @brief Dirty all free memory areas of the system.
0352  */
0353 void _Memory_Dirty_free_areas( void );
0354 
0355 /**
0356  * @brief This symbol marks the begin of the non-initialized section used by
0357  *   RTEMS.
0358  */
0359 extern char _Memory_Noinit_begin[];
0360 
0361 /**
0362  * @brief This symbol marks the end of the non-initialized section used by
0363  *   RTEMS.
0364  */
0365 extern char _Memory_Noinit_end[];
0366 
0367 /** @} */
0368 
0369 #ifdef __cplusplus
0370 }
0371 #endif /* __cplusplus */
0372 
0373 #endif /* _RTEMS_SCORE_MEMORY_H */