source: rtems/cpukit/include/rtems/malloc.h @ 3d435898

Last change on this file since 3d435898 was 77d1ac6c, checked in by Joel Sherrill <joel@…>, on 03/24/22 at 21:35:19

cpukit/include/sys: Change license to BSD-2.

Updates #3053.

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