4 * Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifndef _RTE_MALLOC_H_
35 #define _RTE_MALLOC_H_
39 * RTE Malloc. This library provides methods for dynamically allocating memory
50 * Structure to hold heap statistics obtained from rte_malloc_get_socket_stats function.
52 struct rte_malloc_socket_stats {
53 size_t heap_totalsz_bytes; /**< Total bytes on heap */
54 size_t heap_freesz_bytes; /**< Total free bytes on heap */
55 size_t greatest_free_size; /**< Size in bytes of largest free block */
56 unsigned free_count; /**< Number of free elements on heap */
57 unsigned alloc_count; /**< Number of allocated elements on heap */
58 size_t heap_allocsz_bytes; /**< Total allocated bytes on heap */
62 * This function allocates memory from the huge-page area of memory. The memory
63 * is not cleared. In NUMA systems, the memory allocated resides on the same
64 * NUMA socket as the core that calls this function.
67 * A string identifying the type of allocated objects (useful for debug
68 * purposes, such as identifying the cause of a memory leak). Can be NULL.
70 * Size (in bytes) to be allocated.
72 * If 0, the return is a pointer that is suitably aligned for any kind of
73 * variable (in the same manner as malloc()).
74 * Otherwise, the return is a pointer that is a multiple of *align*. In
75 * this case, it must be a power of two. (Minimum alignment is the
76 * cacheline size, i.e. 64-bytes)
78 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
79 * align is not a power of two).
80 * - Otherwise, the pointer to the allocated object.
83 rte_malloc(const char *type, size_t size, unsigned align);
86 * Allocate zero'ed memory from the heap.
88 * Equivalent to rte_malloc() except that the memory zone is
89 * initialised with zeros. In NUMA systems, the memory allocated resides on the
90 * same NUMA socket as the core that calls this function.
93 * A string identifying the type of allocated objects (useful for debug
94 * purposes, such as identifying the cause of a memory leak). Can be NULL.
96 * Size (in bytes) to be allocated.
98 * If 0, the return is a pointer that is suitably aligned for any kind of
99 * variable (in the same manner as malloc()).
100 * Otherwise, the return is a pointer that is a multiple of *align*. In
101 * this case, it must obviously be a power of two. (Minimum alignment is the
102 * cacheline size, i.e. 64-bytes)
104 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
105 * align is not a power of two).
106 * - Otherwise, the pointer to the allocated object.
109 rte_zmalloc(const char *type, size_t size, unsigned align);
112 * Replacement function for calloc(), using huge-page memory. Memory area is
113 * initialised with zeros. In NUMA systems, the memory allocated resides on the
114 * same NUMA socket as the core that calls this function.
117 * A string identifying the type of allocated objects (useful for debug
118 * purposes, such as identifying the cause of a memory leak). Can be NULL.
120 * Number of elements to be allocated.
122 * Size (in bytes) of a single element.
124 * If 0, the return is a pointer that is suitably aligned for any kind of
125 * variable (in the same manner as malloc()).
126 * Otherwise, the return is a pointer that is a multiple of *align*. In
127 * this case, it must obviously be a power of two. (Minimum alignment is the
128 * cacheline size, i.e. 64-bytes)
130 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
131 * align is not a power of two).
132 * - Otherwise, the pointer to the allocated object.
135 rte_calloc(const char *type, size_t num, size_t size, unsigned align);
138 * Replacement function for realloc(), using huge-page memory. Reserved area
139 * memory is resized, preserving contents. In NUMA systems, the new area
140 * resides on the same NUMA socket as the old area.
143 * Pointer to already allocated memory
145 * Size (in bytes) of new area. If this is 0, memory is freed.
147 * If 0, the return is a pointer that is suitably aligned for any kind of
148 * variable (in the same manner as malloc()).
149 * Otherwise, the return is a pointer that is a multiple of *align*. In
150 * this case, it must obviously be a power of two. (Minimum alignment is the
151 * cacheline size, i.e. 64-bytes)
153 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
154 * align is not a power of two).
155 * - Otherwise, the pointer to the reallocated memory.
158 rte_realloc(void *ptr, size_t size, unsigned align);
161 * This function allocates memory from the huge-page area of memory. The memory
165 * A string identifying the type of allocated objects (useful for debug
166 * purposes, such as identifying the cause of a memory leak). Can be NULL.
168 * Size (in bytes) to be allocated.
170 * If 0, the return is a pointer that is suitably aligned for any kind of
171 * variable (in the same manner as malloc()).
172 * Otherwise, the return is a pointer that is a multiple of *align*. In
173 * this case, it must be a power of two. (Minimum alignment is the
174 * cacheline size, i.e. 64-bytes)
176 * NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
177 * will behave the same as rte_malloc().
179 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
180 * align is not a power of two).
181 * - Otherwise, the pointer to the allocated object.
184 rte_malloc_socket(const char *type, size_t size, unsigned align, int socket);
187 * Allocate zero'ed memory from the heap.
189 * Equivalent to rte_malloc() except that the memory zone is
190 * initialised with zeros.
193 * A string identifying the type of allocated objects (useful for debug
194 * purposes, such as identifying the cause of a memory leak). Can be NULL.
196 * Size (in bytes) to be allocated.
198 * If 0, the return is a pointer that is suitably aligned for any kind of
199 * variable (in the same manner as malloc()).
200 * Otherwise, the return is a pointer that is a multiple of *align*. In
201 * this case, it must obviously be a power of two. (Minimum alignment is the
202 * cacheline size, i.e. 64-bytes)
204 * NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
205 * will behave the same as rte_zmalloc().
207 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
208 * align is not a power of two).
209 * - Otherwise, the pointer to the allocated object.
212 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket);
215 * Replacement function for calloc(), using huge-page memory. Memory area is
216 * initialised with zeros.
219 * A string identifying the type of allocated objects (useful for debug
220 * purposes, such as identifying the cause of a memory leak). Can be NULL.
222 * Number of elements to be allocated.
224 * Size (in bytes) of a single element.
226 * If 0, the return is a pointer that is suitably aligned for any kind of
227 * variable (in the same manner as malloc()).
228 * Otherwise, the return is a pointer that is a multiple of *align*. In
229 * this case, it must obviously be a power of two. (Minimum alignment is the
230 * cacheline size, i.e. 64-bytes)
232 * NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
233 * will behave the same as rte_calloc().
235 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
236 * align is not a power of two).
237 * - Otherwise, the pointer to the allocated object.
240 rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket);
243 * Frees the memory space pointed to by the provided pointer.
245 * This pointer must have been returned by a previous call to
246 * rte_malloc(), rte_zmalloc(), rte_calloc() or rte_realloc(). The behaviour of
247 * rte_free() is undefined if the pointer does not match this requirement.
249 * If the pointer is NULL, the function does nothing.
252 * The pointer to memory to be freed.
258 * If malloc debug is enabled, check a memory block for header
259 * and trailer markers to indicate that all is well with the block.
260 * If size is non-null, also return the size of the block.
263 * pointer to the start of a data block, must have been returned
264 * by a previous call to rte_malloc(), rte_zmalloc(), rte_calloc()
267 * if non-null, and memory block pointer is valid, returns the size
268 * of the memory block
270 * -1 on error, invalid pointer passed or header and trailer markers
271 * are missing or corrupted
275 rte_malloc_validate(void *ptr, size_t *size);
278 * Get heap statistics for the specified heap.
281 * An unsigned integer specifying the socket to get heap statistics for
282 * @param socket_stats
283 * A structure which provides memory to store statistics
286 * Pointer to structure storing statistics on success
289 rte_malloc_get_socket_stats(int socket,
290 struct rte_malloc_socket_stats *socket_stats);
295 * Dump for the specified type to the console. If the type argument is
296 * NULL, all memory types will be dumped.
299 * A string identifying the type of objects to dump, or NULL
300 * to dump all objects.
303 rte_malloc_dump_stats(const char *type);
306 * Set the maximum amount of allocated memory for this type.
308 * This is not yet implemented
311 * A string identifying the type of allocated objects.
313 * The maximum amount of allocated bytes for this type.
319 rte_malloc_set_limit(const char *type, size_t max);
325 #endif /* _RTE_MALLOC_H_ */