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 MallocSupport
0007  *
0008  * @ingroup RTEMSAPIMalloc
0009  *
0010  * @brief This header file defines interfaces to support and use dynamic memory
0011  *   allocation.
0012  */
0013 
0014 /*
0015  * COPYRIGHT (c) 1989-2011.
0016  * On-Line Applications Research Corporation (OAR).
0017  *
0018  * Redistribution and use in source and binary forms, with or without
0019  * modification, are permitted provided that the following conditions
0020  * are met:
0021  * 1. Redistributions of source code must retain the above copyright
0022  *    notice, this list of conditions and the following disclaimer.
0023  * 2. Redistributions in binary form must reproduce the above copyright
0024  *    notice, this list of conditions and the following disclaimer in the
0025  *    documentation and/or other materials provided with the distribution.
0026  *
0027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0028  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0029  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0030  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0031  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0032  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0033  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0034  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0035  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0036  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0037  * POSSIBILITY OF SUCH DAMAGE.
0038  */
0039 
0040 #ifndef _RTEMS_MALLOC_H
0041 #define _RTEMS_MALLOC_H
0042 
0043 #include <rtems.h>
0044 #include <rtems/bspIo.h>
0045 #include <rtems/libcsupport.h> /* for malloc_walk() */
0046 
0047 #include <stdint.h>
0048 
0049 #ifdef __cplusplus
0050 extern "C" {
0051 #endif
0052 
0053 /**
0054  * @defgroup MallocSupport Malloc Support
0055  *
0056  * @ingroup libcsupport
0057  *
0058  * @brief This group contains interfaces to support dynamic memory allocation.
0059  *
0060  * @{
0061  */
0062 
0063 /**
0064  *  @brief C program heap control.
0065  *
0066  *  This is the pointer to the heap control structure used to manage the C
0067  *  program heap.
0068  */
0069 extern Heap_Control *RTEMS_Malloc_Heap;
0070 
0071 void _Malloc_Initialize( void );
0072 
0073 typedef void *(*rtems_heap_extend_handler)(
0074   Heap_Control *heap,
0075   size_t alloc_size
0076 );
0077 
0078 /**
0079  *  @brief RTEMS Extend Heap via Sbrk
0080  */
0081 void *rtems_heap_extend_via_sbrk(
0082   Heap_Control *heap,
0083   size_t alloc_size
0084 );
0085 
0086 void *rtems_heap_null_extend(
0087   Heap_Control *heap,
0088   size_t alloc_size
0089 );
0090 
0091 extern const rtems_heap_extend_handler rtems_malloc_extend_handler;
0092 
0093 /*
0094  * Malloc Plugin to Dirty Memory at Allocation Time
0095  */
0096 typedef void (*rtems_malloc_dirtier_t)(void *, size_t);
0097 extern rtems_malloc_dirtier_t rtems_malloc_dirty_helper;
0098 
0099 /** @} */
0100 
0101 /**
0102  * @defgroup RTEMSAPIMalloc Dynamic Memory Allocation
0103  *
0104  * @ingroup RTEMSAPI
0105  *
0106  * @brief This group contains non-standard interfaces to use dynamic memory
0107  *   allocation.
0108  *
0109  * @{
0110  */
0111 
0112 void rtems_heap_set_sbrk_amount( ptrdiff_t sbrk_amount );
0113 
0114 /**
0115  * @brief Greedy allocate that empties the sbrk memory
0116  *
0117  * Afterwards all the sbrk avialable memory will have been allocated
0118  * to the provided heap.
0119  *
0120  * @see rtems_heap_extend_via_sbrk().
0121  */
0122 void rtems_heap_sbrk_greedy_allocate(
0123   Heap_Control *heap,
0124   size_t alloc_size
0125 );
0126 
0127 /**
0128  *  @brief Dirty Memory Function
0129  *
0130  *  This method fills the specified area with a non-zero pattern
0131  *  to aid in debugging programs which do not initialize their
0132  *  memory allocated from the heap.
0133  */
0134 void rtems_malloc_dirty_memory(
0135   void   *start,
0136   size_t  size
0137 );
0138 
0139 /**
0140  *  @brief RTEMS Variation on Aligned Memory Allocation
0141  *
0142  *  This method is a help memalign implementation which does all
0143  *  error checking done by posix_memalign() EXCEPT it does NOT
0144  *  place numeric restrictions on the alignment value.
0145  *
0146  *  @param[in] pointer points to the user pointer
0147  *  @param[in] alignment is the desired alignment
0148  *  @param[in] size is the allocation request size in bytes
0149  *
0150  *  @return This methods returns zero on success and a POSIX errno
0151  *          value to indicate the failure condition.  On success
0152  *          *pointer will contain the address of the allocated memory.
0153  */
0154 int rtems_memalign(
0155   void   **pointer,
0156   size_t   alignment,
0157   size_t   size
0158 );
0159 
0160 /**
0161  * @brief Allocates a memory area of size @a size bytes from the heap.
0162  *
0163  * If the alignment parameter @a alignment is not equal to zero, the allocated
0164  * memory area will begin at an address aligned by this value.
0165  *
0166  * If the boundary parameter @a boundary is not equal to zero, the allocated
0167  * memory area will comply with a boundary constraint.  The boundary value
0168  * specifies the set of addresses which are aligned by the boundary value.  The
0169  * interior of the allocated memory area will not contain an element of this
0170  * set.  The begin or end address of the area may be a member of the set.
0171  *
0172  * A size value of zero will return a unique address which may be freed with
0173  * free().
0174  *
0175  * The memory allocated by this function can be released with a call to free().
0176  *
0177  * @return A pointer to the begin of the allocated memory area, or @c NULL if
0178  * no memory is available or the parameters are inconsistent.
0179  */
0180 void *rtems_heap_allocate_aligned_with_boundary(
0181   size_t size,
0182   uintptr_t alignment,
0183   uintptr_t boundary
0184 ) RTEMS_MALLOCLIKE RTEMS_ALLOC_SIZE(1) RTEMS_ALLOC_ALIGN(2)
0185   RTEMS_WARN_UNUSED_RESULT;
0186 
0187 /**
0188  * @brief Allocates a memory area of the specified size from the heap.
0189  *
0190  * This function is almost identical to malloc(). The only exception is that
0191  * errno is not set in case of a memory allocation failure.
0192  *
0193  * @param[in] size The memory area size in bytes.
0194  *
0195  * @retval NULL The memory allocation failed or @a size is zero.
0196  * @retval otherwise The begin address of the allocated memory area.
0197  */
0198 void *rtems_malloc(size_t size)
0199   RTEMS_MALLOCLIKE RTEMS_ALLOC_SIZE(1) RTEMS_WARN_UNUSED_RESULT;
0200 
0201 /**
0202  * @brief Allocates a memory area for the specified count of elements from the
0203  * heap.
0204  *
0205  * The allocated memory area is fully filled with zero bits.
0206  *
0207  * This function is almost identical to calloc(). The only exception is that
0208  * errno is not set in case of a memory allocation failure.
0209  *
0210  * @param[in] nelem The count of elements.
0211  * @param[in] elsize The size of each elements.
0212  *
0213  * @retval NULL The memory allocation failed or @a nelem is zero or @a elsize
0214  *   is zero.
0215  * @retval otherwise The begin address of the allocated memory area.
0216  */
0217 void *rtems_calloc(size_t nelem, size_t elsize)
0218   RTEMS_MALLOCLIKE RTEMS_ALLOC_SIZE_2(1, 2) RTEMS_WARN_UNUSED_RESULT;
0219 
0220 /**
0221  * @brief Extends the memory available for the heap using the memory area
0222  * starting at @a area_begin of size @a area_size bytes.
0223  *
0224  * There are no alignment requirements.  The memory area must be big enough to
0225  * contain some maintenance blocks.  It must not overlap parts of the current
0226  * heap areas.  Disconnected subordinate heap areas will lead to used blocks
0227  * which cover the gaps.  Extending with an inappropriate memory area will
0228  * corrupt the heap.
0229  *
0230  * @retval RTEMS_SUCCESSFUL Successful operation.
0231  * @retval RTEMS_INVALID_ADDRESS Invalid memory area.
0232  */
0233 rtems_status_code rtems_heap_extend(
0234   void *area_begin,
0235   uintptr_t area_size
0236 );
0237 
0238 /**
0239  * @brief Greedy allocate that empties the heap.
0240  *
0241  * Afterwards the heap has at most @a block_count allocatable blocks of sizes
0242  * specified by @a block_sizes.  The @a block_sizes must point to an array with
0243  * @a block_count members.  All other blocks are used.
0244  *
0245  * @see rtems_heap_greedy_free().
0246  */
0247 void *rtems_heap_greedy_allocate(
0248   const uintptr_t *block_sizes,
0249   size_t block_count
0250 );
0251 
0252 /**
0253  * @brief Greedy allocate all blocks except the largest free block.
0254  *
0255  * Afterwards the heap has at most one allocatable block.  This block is the
0256  * largest free block if it exists.  The allocatable size of this block is
0257  * stored in @a allocatable_size.  All other blocks are used.
0258  *
0259  * @see rtems_heap_greedy_free().
0260  */
0261 void *rtems_heap_greedy_allocate_all_except_largest(
0262   uintptr_t *allocatable_size
0263 );
0264 
0265 /**
0266  * @brief Frees space of a greedy allocation.
0267  *
0268  * The @a opaque argument must be the return value of
0269  * rtems_heap_greedy_allocate() or
0270  * rtems_heap_greedy_allocate_all_except_largest().
0271  */
0272 void rtems_heap_greedy_free( void *opaque );
0273 
0274 /** @} */
0275 
0276 #ifdef __cplusplus
0277 }
0278 #endif
0279 
0280 #endif