source: rtems/cpukit/include/rtems/malloc.h @ 426154f

Last change on this file since 426154f was 03dff201, checked in by Chris Johns <chrisj@…>, on 02/09/21 at 01:48:07

libcsupport: Add sbrk greedy support to consume all sbrk memory

  • Move the heap sbrk code into a separate routnine.
  • Update heap and workspace greedy allocators to use the common sbrk greedy support.

Closes #3982

  • Property mode set to 100644
File size: 6.9 KB
Line 
1/**
2 * @file
3 *
4 * This file defines the interface to RTEMS extensions to the Malloc Family.
5 */
6
7/*
8 *  COPYRIGHT (c) 1989-2011.
9 *  On-Line Applications Research Corporation (OAR).
10 *
11 *  The license and distribution terms for this file may in
12 *  the file LICENSE in this distribution or at
13 *  http://www.rtems.org/license/LICENSE.
14 */
15
16#ifndef _RTEMS_MALLOC_H
17#define _RTEMS_MALLOC_H
18
19#include <rtems.h>
20#include <rtems/bspIo.h>
21#include <rtems/libcsupport.h> /* for malloc_walk() */
22#include <rtems/score/memory.h>
23
24#include <stdint.h>
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30/**
31 *  @defgroup MallocSupport Malloc Support
32 *
33 *  @ingroup libcsupport
34 *
35 *  @brief RTEMS extensions to the Malloc Family
36 */
37
38/**
39 *  @brief C program heap control.
40 *
41 *  This is the pointer to the heap control structure used to manage the C
42 *  program heap.
43 */
44extern Heap_Control *RTEMS_Malloc_Heap;
45
46Heap_Control *RTEMS_Malloc_Initialize(
47  const Memory_Information              *mem,
48  Heap_Initialization_or_extend_handler  extend
49);
50
51extern ptrdiff_t RTEMS_Malloc_Sbrk_amount;
52
53static inline void rtems_heap_set_sbrk_amount( ptrdiff_t sbrk_amount )
54{
55  RTEMS_Malloc_Sbrk_amount = sbrk_amount;
56}
57
58typedef void *(*rtems_heap_extend_handler)(
59  Heap_Control *heap,
60  size_t alloc_size
61);
62
63/**
64 *  @brief RTEMS Extend Heap via Sbrk
65 */
66void *rtems_heap_extend_via_sbrk(
67  Heap_Control *heap,
68  size_t alloc_size
69);
70
71/**
72 * @brief Greedy allocate that empties the sbrk memory
73 *
74 * Afterwards all the sbrk avialable memory will have been allocated
75 * to the provided heap.
76 *
77 * @see rtems_heap_extend_via_sbrk().
78 */
79void rtems_heap_sbrk_greedy_allocate(
80  Heap_Control *heap,
81  size_t alloc_size
82);
83
84void *rtems_heap_null_extend(
85  Heap_Control *heap,
86  size_t alloc_size
87);
88
89extern const rtems_heap_extend_handler rtems_malloc_extend_handler;
90
91/*
92 * Malloc Plugin to Dirty Memory at Allocation Time
93 */
94typedef void (*rtems_malloc_dirtier_t)(void *, size_t);
95extern rtems_malloc_dirtier_t rtems_malloc_dirty_helper;
96
97/**
98 *  @brief Dirty Memory Function
99 *
100 *  This method fills the specified area with a non-zero pattern
101 *  to aid in debugging programs which do not initialize their
102 *  memory allocated from the heap.
103 */
104void rtems_malloc_dirty_memory(
105  void   *start,
106  size_t  size
107);
108
109/**
110 *  @brief RTEMS Variation on Aligned Memory Allocation
111 *
112 *  This method is a help memalign implementation which does all
113 *  error checking done by posix_memalign() EXCEPT it does NOT
114 *  place numeric restrictions on the alignment value.
115 *
116 *  @param[in] pointer points to the user pointer
117 *  @param[in] alignment is the desired alignment
118 *  @param[in] size is the allocation request size in bytes
119 *
120 *  @return This methods returns zero on success and a POSIX errno
121 *          value to indicate the failure condition.  On success
122 *          *pointer will contain the address of the allocated memory.
123 */
124int rtems_memalign(
125  void   **pointer,
126  size_t   alignment,
127  size_t   size
128);
129
130/**
131 * @brief Allocates a memory area of size @a size bytes from the heap.
132 *
133 * If the alignment parameter @a alignment is not equal to zero, the allocated
134 * memory area will begin at an address aligned by this value.
135 *
136 * If the boundary parameter @a boundary is not equal to zero, the allocated
137 * memory area will comply with a boundary constraint.  The boundary value
138 * specifies the set of addresses which are aligned by the boundary value.  The
139 * interior of the allocated memory area will not contain an element of this
140 * set.  The begin or end address of the area may be a member of the set.
141 *
142 * A size value of zero will return a unique address which may be freed with
143 * free().
144 *
145 * The memory allocated by this function can be released with a call to free().
146 *
147 * @return A pointer to the begin of the allocated memory area, or @c NULL if
148 * no memory is available or the parameters are inconsistent.
149 */
150void *rtems_heap_allocate_aligned_with_boundary(
151  size_t size,
152  uintptr_t alignment,
153  uintptr_t boundary
154) RTEMS_MALLOCLIKE RTEMS_ALLOC_SIZE(1) RTEMS_ALLOC_ALIGN(2)
155  RTEMS_WARN_UNUSED_RESULT;
156
157/**
158 * @brief Allocates a memory area of the specified size from the heap.
159 *
160 * This function is almost identical to malloc(). The only exception is that
161 * errno is not set in case of a memory allocation failure.
162 *
163 * @param[in] size The memory area size in bytes.
164 *
165 * @retval NULL The memory allocation failed or @a size is zero.
166 * @retval otherwise The begin address of the allocated memory area.
167 */
168void *rtems_malloc(size_t size)
169  RTEMS_MALLOCLIKE RTEMS_ALLOC_SIZE(1) RTEMS_WARN_UNUSED_RESULT;
170
171/**
172 * @brief Allocates a memory area for the specified count of elements from the
173 * heap.
174 *
175 * The allocated memory area is fully filled with zero bits.
176 *
177 * This function is almost identical to calloc(). The only exception is that
178 * errno is not set in case of a memory allocation failure.
179 *
180 * @param[in] nelem The count of elements.
181 * @param[in] elsize The size of each elements.
182 *
183 * @retval NULL The memory allocation failed or @a nelem is zero or @a elsize
184 *   is zero.
185 * @retval otherwise The begin address of the allocated memory area.
186 */
187void *rtems_calloc(size_t nelem, size_t elsize)
188  RTEMS_MALLOCLIKE RTEMS_ALLOC_SIZE_2(1, 2) RTEMS_WARN_UNUSED_RESULT;
189
190/**
191 * @brief Extends the memory available for the heap using the memory area
192 * starting at @a area_begin of size @a area_size bytes.
193 *
194 * There are no alignment requirements.  The memory area must be big enough to
195 * contain some maintenance blocks.  It must not overlap parts of the current
196 * heap areas.  Disconnected subordinate heap areas will lead to used blocks
197 * which cover the gaps.  Extending with an inappropriate memory area will
198 * corrupt the heap.
199 *
200 * @retval RTEMS_SUCCESSFUL Successful operation.
201 * @retval RTEMS_INVALID_ADDRESS Invalid memory area.
202 */
203rtems_status_code rtems_heap_extend(
204  void *area_begin,
205  uintptr_t area_size
206);
207
208/**
209 * @brief Greedy allocate that empties the heap.
210 *
211 * Afterwards the heap has at most @a block_count allocatable blocks of sizes
212 * specified by @a block_sizes.  The @a block_sizes must point to an array with
213 * @a block_count members.  All other blocks are used.
214 *
215 * @see rtems_heap_greedy_free().
216 */
217void *rtems_heap_greedy_allocate(
218  const uintptr_t *block_sizes,
219  size_t block_count
220);
221
222/**
223 * @brief Greedy allocate all blocks except the largest free block.
224 *
225 * Afterwards the heap has at most one allocatable block.  This block is the
226 * largest free block if it exists.  The allocatable size of this block is
227 * stored in @a allocatable_size.  All other blocks are used.
228 *
229 * @see rtems_heap_greedy_free().
230 */
231void *rtems_heap_greedy_allocate_all_except_largest(
232  uintptr_t *allocatable_size
233);
234
235/**
236 * @brief Frees space of a greedy allocation.
237 *
238 * The @a opaque argument must be the return value of
239 * rtems_heap_greedy_allocate() or
240 * rtems_heap_greedy_allocate_all_except_largest().
241 */
242void rtems_heap_greedy_free( void *opaque );
243
244#ifdef __cplusplus
245}
246#endif
247
248#endif
Note: See TracBrowser for help on using the repository browser.