4 * Copyright(c) 2010-2012 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.
35 #ifndef _RTE_MALLOC_H_
36 #define _RTE_MALLOC_H_
40 * RTE Malloc. This library provides methods for dynamically allocating memory
51 * This function allocates memory from the huge-page area of memory. The memory
52 * is not cleared. In NUMA systems, the memory allocated resides on the same
53 * NUMA socket as the core that calls this function.
56 * A string identifying the type of allocated objects (useful for debug
57 * purposes, such as identifying the cause of a memory leak). Can be NULL.
59 * Size (in bytes) to be allocated.
61 * If 0, the return is a pointer that is suitably aligned for any kind of
62 * variable (in the same manner as malloc()).
63 * Otherwise, the return is a pointer that is a multiple of *align*. In
64 * this case, it must be a power of two. (Minimum alignment is the
65 * cacheline size, i.e. 64-bytes)
67 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
68 * align is not a power of two).
69 * - Otherwise, the pointer to the allocated object.
72 rte_malloc(const char *type, size_t size, unsigned align);
75 * Allocate zero'ed memory from the heap.
77 * Equivalent to rte_malloc() except that the memory zone is
78 * initialised with zeros. In NUMA systems, the memory allocated resides on the
79 * same NUMA socket as the core that calls this function.
82 * A string identifying the type of allocated objects (useful for debug
83 * purposes, such as identifying the cause of a memory leak). Can be NULL.
85 * Size (in bytes) to be allocated.
87 * If 0, the return is a pointer that is suitably aligned for any kind of
88 * variable (in the same manner as malloc()).
89 * Otherwise, the return is a pointer that is a multiple of *align*. In
90 * this case, it must obviously be a power of two. (Minimum alignment is the
91 * cacheline size, i.e. 64-bytes)
93 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
94 * align is not a power of two).
95 * - Otherwise, the pointer to the allocated object.
98 rte_zmalloc(const char *type, size_t size, unsigned align);
101 * Replacement function for calloc(), using huge-page memory. Memory area is
102 * initialised with zeros. In NUMA systems, the memory allocated resides on the
103 * same NUMA socket as the core that calls this function.
106 * A string identifying the type of allocated objects (useful for debug
107 * purposes, such as identifying the cause of a memory leak). Can be NULL.
109 * Number of elements to be allocated.
111 * Size (in bytes) of a single element.
113 * If 0, the return is a pointer that is suitably aligned for any kind of
114 * variable (in the same manner as malloc()).
115 * Otherwise, the return is a pointer that is a multiple of *align*. In
116 * this case, it must obviously be a power of two. (Minimum alignment is the
117 * cacheline size, i.e. 64-bytes)
119 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
120 * align is not a power of two).
121 * - Otherwise, the pointer to the allocated object.
124 rte_calloc(const char *type, size_t num, size_t size, unsigned align);
127 * Replacement function for realloc(), using huge-page memory. Reserved area
128 * memory is resized, preserving contents. In NUMA systems, the new area
129 * resides on the same NUMA socket as the old area.
132 * Pointer to already allocated memory
134 * Size (in bytes) of new area. If this is 0, memory is freed.
136 * If 0, the return is a pointer that is suitably aligned for any kind of
137 * variable (in the same manner as malloc()).
138 * Otherwise, the return is a pointer that is a multiple of *align*. In
139 * this case, it must obviously be a power of two. (Minimum alignment is the
140 * cacheline size, i.e. 64-bytes)
142 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
143 * align is not a power of two).
144 * - Otherwise, the pointer to the reallocated memory.
147 rte_realloc(void *ptr, size_t size, unsigned align);
150 * This function allocates memory from the huge-page area of memory. The memory
154 * A string identifying the type of allocated objects (useful for debug
155 * purposes, such as identifying the cause of a memory leak). Can be NULL.
157 * Size (in bytes) to be allocated.
159 * If 0, the return is a pointer that is suitably aligned for any kind of
160 * variable (in the same manner as malloc()).
161 * Otherwise, the return is a pointer that is a multiple of *align*. In
162 * this case, it must be a power of two. (Minimum alignment is the
163 * cacheline size, i.e. 64-bytes)
165 * NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
166 * will behave the same as rte_malloc().
168 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
169 * align is not a power of two).
170 * - Otherwise, the pointer to the allocated object.
173 rte_malloc_socket(const char *type, size_t size, unsigned align, int socket);
176 * Allocate zero'ed memory from the heap.
178 * Equivalent to rte_malloc() except that the memory zone is
179 * initialised with zeros.
182 * A string identifying the type of allocated objects (useful for debug
183 * purposes, such as identifying the cause of a memory leak). Can be NULL.
185 * Size (in bytes) to be allocated.
187 * If 0, the return is a pointer that is suitably aligned for any kind of
188 * variable (in the same manner as malloc()).
189 * Otherwise, the return is a pointer that is a multiple of *align*. In
190 * this case, it must obviously be a power of two. (Minimum alignment is the
191 * cacheline size, i.e. 64-bytes)
193 * NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
194 * will behave the same as rte_zmalloc().
196 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
197 * align is not a power of two).
198 * - Otherwise, the pointer to the allocated object.
201 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket);
204 * Replacement function for calloc(), using huge-page memory. Memory area is
205 * initialised with zeros.
208 * A string identifying the type of allocated objects (useful for debug
209 * purposes, such as identifying the cause of a memory leak). Can be NULL.
211 * Number of elements to be allocated.
213 * Size (in bytes) of a single element.
215 * If 0, the return is a pointer that is suitably aligned for any kind of
216 * variable (in the same manner as malloc()).
217 * Otherwise, the return is a pointer that is a multiple of *align*. In
218 * this case, it must obviously be a power of two. (Minimum alignment is the
219 * cacheline size, i.e. 64-bytes)
221 * NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
222 * will behave the same as rte_calloc().
224 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
225 * align is not a power of two).
226 * - Otherwise, the pointer to the allocated object.
229 rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket);
232 * Frees the memory space pointed to by the provided pointer.
234 * This pointer must have been returned by a previous call to
235 * rte_malloc(), rte_zmalloc(), rte_calloc() or rte_realloc(). The behaviour of
236 * rte_free() is undefined if the pointer does not match this requirement.
238 * If the pointer is NULL, the function does nothing.
241 * The pointer to memory to be freed.
247 * If malloc debug is enabled, check a memory block for header
248 * and trailer markers to indicate that all is well with the block.
249 * If size is non-null, also return the size of the block.
252 * pointer to the start of a data block, must have been returned
253 * by a previous call to rte_malloc(), rte_zmalloc(), rte_calloc()
256 * if non-null, and memory block pointer is valid, returns the size
257 * of the memory block
259 * -1 on error, invalid pointer passed or header and trailer markers
260 * are missing or corrupted
264 rte_malloc_validate(void *ptr, size_t *size);
269 * Dump for the specified type to the console. If the type argument is
270 * NULL, all memory types will be dumped.
273 * A string identifying the type of objects to dump, or NULL
274 * to dump all objects.
277 rte_malloc_dump_stats(const char *type);
280 * Set the maximum amount of allocated memory for this type.
282 * This is not yet implemented
285 * A string identifying the type of allocated objects.
287 * The maximum amount of allocated bytes for this type.
293 rte_malloc_set_limit(const char *type, size_t max);
299 #endif /* _RTE_MALLOC_H_ */