14a78b0b7a5235181be930959be290ad562a430d
[dpdk.git] / lib / librte_malloc / rte_malloc.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
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
16  *       distribution.
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.
20  * 
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.
32  */
33
34 #ifndef _RTE_MALLOC_H_
35 #define _RTE_MALLOC_H_
36
37 /**
38  * @file
39  * RTE Malloc. This library provides methods for dynamically allocating memory
40  * from hugepages.
41  */
42
43 #include <stddef.h>
44 #include <rte_memory.h>
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50 /**
51  *  Structure to hold heap statistics obtained from rte_malloc_get_socket_stats function.
52  */
53 struct rte_malloc_socket_stats {
54         size_t heap_totalsz_bytes; /**< Total bytes on heap */
55         size_t heap_freesz_bytes;  /**< Total free bytes on heap */
56         size_t greatest_free_size; /**< Size in bytes of largest free block */
57         unsigned free_count;       /**< Number of free elements on heap */
58         unsigned alloc_count;      /**< Number of allocated elements on heap */
59         size_t heap_allocsz_bytes; /**< Total allocated bytes on heap */
60 };
61
62 /**
63  * This function allocates memory from the huge-page area of memory. The memory
64  * is not cleared. In NUMA systems, the memory allocated resides on the same
65  * NUMA socket as the core that calls this function.
66  *
67  * @param type
68  *   A string identifying the type of allocated objects (useful for debug
69  *   purposes, such as identifying the cause of a memory leak). Can be NULL.
70  * @param size
71  *   Size (in bytes) to be allocated.
72  * @param align
73  *   If 0, the return is a pointer that is suitably aligned for any kind of
74  *   variable (in the same manner as malloc()).
75  *   Otherwise, the return is a pointer that is a multiple of *align*. In
76  *   this case, it must be a power of two. (Minimum alignment is the
77  *   cacheline size, i.e. 64-bytes)
78  * @return
79  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
80  *     align is not a power of two).
81  *   - Otherwise, the pointer to the allocated object.
82  */
83 void *
84 rte_malloc(const char *type, size_t size, unsigned align);
85
86 /**
87  * Allocate zero'ed memory from the heap.
88  *
89  * Equivalent to rte_malloc() except that the memory zone is
90  * initialised with zeros. In NUMA systems, the memory allocated resides on the
91  * same NUMA socket as the core that calls this function.
92  *
93  * @param type
94  *   A string identifying the type of allocated objects (useful for debug
95  *   purposes, such as identifying the cause of a memory leak). Can be NULL.
96  * @param size
97  *   Size (in bytes) to be allocated.
98  * @param align
99  *   If 0, the return is a pointer that is suitably aligned for any kind of
100  *   variable (in the same manner as malloc()).
101  *   Otherwise, the return is a pointer that is a multiple of *align*. In
102  *   this case, it must obviously be a power of two. (Minimum alignment is the
103  *   cacheline size, i.e. 64-bytes)
104  * @return
105  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
106  *     align is not a power of two).
107  *   - Otherwise, the pointer to the allocated object.
108  */
109 void *
110 rte_zmalloc(const char *type, size_t size, unsigned align);
111
112 /**
113  * Replacement function for calloc(), using huge-page memory. Memory area is
114  * initialised with zeros. In NUMA systems, the memory allocated resides on the
115  * same NUMA socket as the core that calls this function.
116  *
117  * @param type
118  *   A string identifying the type of allocated objects (useful for debug
119  *   purposes, such as identifying the cause of a memory leak). Can be NULL.
120  * @param num
121  *   Number of elements to be allocated.
122  * @param size
123  *   Size (in bytes) of a single element.
124  * @param align
125  *   If 0, the return is a pointer that is suitably aligned for any kind of
126  *   variable (in the same manner as malloc()).
127  *   Otherwise, the return is a pointer that is a multiple of *align*. In
128  *   this case, it must obviously be a power of two. (Minimum alignment is the
129  *   cacheline size, i.e. 64-bytes)
130  * @return
131  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
132  *     align is not a power of two).
133  *   - Otherwise, the pointer to the allocated object.
134  */
135 void *
136 rte_calloc(const char *type, size_t num, size_t size, unsigned align);
137
138 /**
139  * Replacement function for realloc(), using huge-page memory. Reserved area
140  * memory is resized, preserving contents. In NUMA systems, the new area
141  * resides on the same NUMA socket as the old area.
142  *
143  * @param ptr
144  *   Pointer to already allocated memory
145  * @param size
146  *   Size (in bytes) of new area. If this is 0, memory is freed.
147  * @param align
148  *   If 0, the return is a pointer that is suitably aligned for any kind of
149  *   variable (in the same manner as malloc()).
150  *   Otherwise, the return is a pointer that is a multiple of *align*. In
151  *   this case, it must obviously be a power of two. (Minimum alignment is the
152  *   cacheline size, i.e. 64-bytes)
153  * @return
154  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
155  *     align is not a power of two).
156  *   - Otherwise, the pointer to the reallocated memory.
157  */
158 void *
159 rte_realloc(void *ptr, size_t size, unsigned align);
160
161 /**
162  * This function allocates memory from the huge-page area of memory. The memory
163  * is not cleared.
164  *
165  * @param type
166  *   A string identifying the type of allocated objects (useful for debug
167  *   purposes, such as identifying the cause of a memory leak). Can be NULL.
168  * @param size
169  *   Size (in bytes) to be allocated.
170  * @param align
171  *   If 0, the return is a pointer that is suitably aligned for any kind of
172  *   variable (in the same manner as malloc()).
173  *   Otherwise, the return is a pointer that is a multiple of *align*. In
174  *   this case, it must be a power of two. (Minimum alignment is the
175  *   cacheline size, i.e. 64-bytes)
176  * @param socket
177  *   NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
178  *   will behave the same as rte_malloc().
179  * @return
180  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
181  *     align is not a power of two).
182  *   - Otherwise, the pointer to the allocated object.
183  */
184 void *
185 rte_malloc_socket(const char *type, size_t size, unsigned align, int socket);
186
187 /**
188  * Allocate zero'ed memory from the heap.
189  *
190  * Equivalent to rte_malloc() except that the memory zone is
191  * initialised with zeros.
192  *
193  * @param type
194  *   A string identifying the type of allocated objects (useful for debug
195  *   purposes, such as identifying the cause of a memory leak). Can be NULL.
196  * @param size
197  *   Size (in bytes) to be allocated.
198  * @param align
199  *   If 0, the return is a pointer that is suitably aligned for any kind of
200  *   variable (in the same manner as malloc()).
201  *   Otherwise, the return is a pointer that is a multiple of *align*. In
202  *   this case, it must obviously be a power of two. (Minimum alignment is the
203  *   cacheline size, i.e. 64-bytes)
204  * @param socket
205  *   NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
206  *   will behave the same as rte_zmalloc().
207  * @return
208  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
209  *     align is not a power of two).
210  *   - Otherwise, the pointer to the allocated object.
211  */
212 void *
213 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket);
214
215 /**
216  * Replacement function for calloc(), using huge-page memory. Memory area is
217  * initialised with zeros.
218  *
219  * @param type
220  *   A string identifying the type of allocated objects (useful for debug
221  *   purposes, such as identifying the cause of a memory leak). Can be NULL.
222  * @param num
223  *   Number of elements to be allocated.
224  * @param size
225  *   Size (in bytes) of a single element.
226  * @param align
227  *   If 0, the return is a pointer that is suitably aligned for any kind of
228  *   variable (in the same manner as malloc()).
229  *   Otherwise, the return is a pointer that is a multiple of *align*. In
230  *   this case, it must obviously be a power of two. (Minimum alignment is the
231  *   cacheline size, i.e. 64-bytes)
232  * @param socket
233  *   NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
234  *   will behave the same as rte_calloc().
235  * @return
236  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
237  *     align is not a power of two).
238  *   - Otherwise, the pointer to the allocated object.
239  */
240 void *
241 rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket);
242
243 /**
244  * Frees the memory space pointed to by the provided pointer.
245  *
246  * This pointer must have been returned by a previous call to
247  * rte_malloc(), rte_zmalloc(), rte_calloc() or rte_realloc(). The behaviour of
248  * rte_free() is undefined if the pointer does not match this requirement.
249  *
250  * If the pointer is NULL, the function does nothing.
251  *
252  * @param ptr
253  *   The pointer to memory to be freed.
254  */
255 void
256 rte_free(void *ptr);
257
258 /**
259  * If malloc debug is enabled, check a memory block for header
260  * and trailer markers to indicate that all is well with the block.
261  * If size is non-null, also return the size of the block.
262  *
263  * @param ptr
264  *   pointer to the start of a data block, must have been returned
265  *   by a previous call to rte_malloc(), rte_zmalloc(), rte_calloc()
266  *   or rte_realloc()
267  * @param size
268  *   if non-null, and memory block pointer is valid, returns the size
269  *   of the memory block
270  * @return
271  *   -1 on error, invalid pointer passed or header and trailer markers
272  *   are missing or corrupted
273  *   0 on success
274  */
275 int
276 rte_malloc_validate(const void *ptr, size_t *size);
277
278 /**
279  * Get heap statistics for the specified heap.
280  *
281  * @param socket
282  *   An unsigned integer specifying the socket to get heap statistics for
283  * @param socket_stats
284  *   A structure which provides memory to store statistics
285  * @return
286  *   Null on error
287  *   Pointer to structure storing statistics on success
288  */
289 int
290 rte_malloc_get_socket_stats(int socket,
291                 struct rte_malloc_socket_stats *socket_stats);
292
293 /**
294  * Dump statistics.
295  *
296  * Dump for the specified type to the console. If the type argument is
297  * NULL, all memory types will be dumped.
298  *
299  * @param type
300  *   A string identifying the type of objects to dump, or NULL
301  *   to dump all objects.
302  */
303 void
304 rte_malloc_dump_stats(const char *type);
305
306 /**
307  * Set the maximum amount of allocated memory for this type.
308  *
309  * This is not yet implemented
310  *
311  * @param type
312  *   A string identifying the type of allocated objects.
313  * @param max
314  *   The maximum amount of allocated bytes for this type.
315  * @return
316  *   - 0: Success.
317  *   - (-1): Error.
318  */
319 int
320 rte_malloc_set_limit(const char *type, size_t max);
321
322 /**
323  * Return the physical address of a virtual address obtained through
324  * rte_malloc
325  *
326  * @param addr
327  *   Adress obtained from a previous rte_malloc call
328  * @return
329  *   NULL on error
330  *   otherwise return physical address of the buffer
331  */
332 phys_addr_t
333 rte_malloc_virt2phy(const void *addr);
334
335 #ifdef __cplusplus
336 }
337 #endif
338
339 #endif /* _RTE_MALLOC_H_ */