malloc: fix documentation of realloc function
[dpdk.git] / lib / librte_eal / common / include / rte_malloc.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2019 Intel Corporation
3  */
4
5 #ifndef _RTE_MALLOC_H_
6 #define _RTE_MALLOC_H_
7
8 /**
9  * @file
10  * RTE Malloc. This library provides methods for dynamically allocating memory
11  * from hugepages.
12  */
13
14 #include <stdio.h>
15 #include <stddef.h>
16 #include <rte_compat.h>
17 #include <rte_memory.h>
18
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22
23 /**
24  *  Structure to hold heap statistics obtained from rte_malloc_get_socket_stats function.
25  */
26 struct rte_malloc_socket_stats {
27         size_t heap_totalsz_bytes; /**< Total bytes on heap */
28         size_t heap_freesz_bytes;  /**< Total free bytes on heap */
29         size_t greatest_free_size; /**< Size in bytes of largest free block */
30         unsigned free_count;       /**< Number of free elements on heap */
31         unsigned alloc_count;      /**< Number of allocated elements on heap */
32         size_t heap_allocsz_bytes; /**< Total allocated bytes on heap */
33 };
34
35 /**
36  * This function allocates memory from the huge-page area of memory. The memory
37  * is not cleared. In NUMA systems, the memory allocated resides on the same
38  * NUMA socket as the core that calls this function.
39  *
40  * @param type
41  *   A string identifying the type of allocated objects (useful for debug
42  *   purposes, such as identifying the cause of a memory leak). Can be NULL.
43  * @param size
44  *   Size (in bytes) to be allocated.
45  * @param align
46  *   If 0, the return is a pointer that is suitably aligned for any kind of
47  *   variable (in the same manner as malloc()).
48  *   Otherwise, the return is a pointer that is a multiple of *align*. In
49  *   this case, it must be a power of two. (Minimum alignment is the
50  *   cacheline size, i.e. 64-bytes)
51  * @return
52  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
53  *     align is not a power of two).
54  *   - Otherwise, the pointer to the allocated object.
55  */
56 void *
57 rte_malloc(const char *type, size_t size, unsigned align);
58
59 /**
60  * Allocate zero'ed memory from the heap.
61  *
62  * Equivalent to rte_malloc() except that the memory zone is
63  * initialised with zeros. In NUMA systems, the memory allocated resides on the
64  * same NUMA socket as the core that calls this function.
65  *
66  * @param type
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.
69  * @param size
70  *   Size (in bytes) to be allocated.
71  * @param align
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 obviously be a power of two. (Minimum alignment is the
76  *   cacheline size, i.e. 64-bytes)
77  * @return
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.
81  */
82 void *
83 rte_zmalloc(const char *type, size_t size, unsigned align);
84
85 /**
86  * Replacement function for calloc(), using huge-page memory. Memory area is
87  * initialised with zeros. In NUMA systems, the memory allocated resides on the
88  * same NUMA socket as the core that calls this function.
89  *
90  * @param type
91  *   A string identifying the type of allocated objects (useful for debug
92  *   purposes, such as identifying the cause of a memory leak). Can be NULL.
93  * @param num
94  *   Number of elements to be allocated.
95  * @param size
96  *   Size (in bytes) of a single element.
97  * @param align
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)
103  * @return
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.
107  */
108 void *
109 rte_calloc(const char *type, size_t num, size_t size, unsigned align);
110
111 /**
112  * Replacement function for realloc(), using huge-page memory. Reserved area
113  * memory is resized, preserving contents. In NUMA systems, the new area
114  * may not reside on the same NUMA node as the old one.
115  *
116  * @param ptr
117  *   Pointer to already allocated memory
118  * @param size
119  *   Size (in bytes) of new area. If this is 0, memory is freed.
120  * @param align
121  *   If 0, the return is a pointer that is suitably aligned for any kind of
122  *   variable (in the same manner as malloc()).
123  *   Otherwise, the return is a pointer that is a multiple of *align*. In
124  *   this case, it must obviously be a power of two. (Minimum alignment is the
125  *   cacheline size, i.e. 64-bytes)
126  * @return
127  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
128  *     align is not a power of two).
129  *   - Otherwise, the pointer to the reallocated memory.
130  */
131 void *
132 rte_realloc(void *ptr, size_t size, unsigned int align);
133
134 /**
135  * Replacement function for realloc(), using huge-page memory. Reserved area
136  * memory is resized, preserving contents. In NUMA systems, the new area
137  * resides on requested NUMA socket.
138  *
139  * @param ptr
140  *   Pointer to already allocated memory
141  * @param size
142  *   Size (in bytes) of new area. If this is 0, memory is freed.
143  * @param align
144  *   If 0, the return is a pointer that is suitably aligned for any kind of
145  *   variable (in the same manner as malloc()).
146  *   Otherwise, the return is a pointer that is a multiple of *align*. In
147  *   this case, it must obviously be a power of two. (Minimum alignment is the
148  *   cacheline size, i.e. 64-bytes)
149  * @param socket
150  *   NUMA socket to allocate memory on.
151  * @return
152  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
153  *     align is not a power of two).
154  *   - Otherwise, the pointer to the reallocated memory.
155  */
156 void * __rte_experimental
157 rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket);
158
159 /**
160  * This function allocates memory from the huge-page area of memory. The memory
161  * is not cleared.
162  *
163  * @param type
164  *   A string identifying the type of allocated objects (useful for debug
165  *   purposes, such as identifying the cause of a memory leak). Can be NULL.
166  * @param size
167  *   Size (in bytes) to be allocated.
168  * @param align
169  *   If 0, the return is a pointer that is suitably aligned for any kind of
170  *   variable (in the same manner as malloc()).
171  *   Otherwise, the return is a pointer that is a multiple of *align*. In
172  *   this case, it must be a power of two. (Minimum alignment is the
173  *   cacheline size, i.e. 64-bytes)
174  * @param socket
175  *   NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
176  *   will behave the same as rte_malloc().
177  * @return
178  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
179  *     align is not a power of two).
180  *   - Otherwise, the pointer to the allocated object.
181  */
182 void *
183 rte_malloc_socket(const char *type, size_t size, unsigned align, int socket);
184
185 /**
186  * Allocate zero'ed memory from the heap.
187  *
188  * Equivalent to rte_malloc() except that the memory zone is
189  * initialised with zeros.
190  *
191  * @param type
192  *   A string identifying the type of allocated objects (useful for debug
193  *   purposes, such as identifying the cause of a memory leak). Can be NULL.
194  * @param size
195  *   Size (in bytes) to be allocated.
196  * @param align
197  *   If 0, the return is a pointer that is suitably aligned for any kind of
198  *   variable (in the same manner as malloc()).
199  *   Otherwise, the return is a pointer that is a multiple of *align*. In
200  *   this case, it must obviously be a power of two. (Minimum alignment is the
201  *   cacheline size, i.e. 64-bytes)
202  * @param socket
203  *   NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
204  *   will behave the same as rte_zmalloc().
205  * @return
206  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
207  *     align is not a power of two).
208  *   - Otherwise, the pointer to the allocated object.
209  */
210 void *
211 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket);
212
213 /**
214  * Replacement function for calloc(), using huge-page memory. Memory area is
215  * initialised with zeros.
216  *
217  * @param type
218  *   A string identifying the type of allocated objects (useful for debug
219  *   purposes, such as identifying the cause of a memory leak). Can be NULL.
220  * @param num
221  *   Number of elements to be allocated.
222  * @param size
223  *   Size (in bytes) of a single element.
224  * @param align
225  *   If 0, the return is a pointer that is suitably aligned for any kind of
226  *   variable (in the same manner as malloc()).
227  *   Otherwise, the return is a pointer that is a multiple of *align*. In
228  *   this case, it must obviously be a power of two. (Minimum alignment is the
229  *   cacheline size, i.e. 64-bytes)
230  * @param socket
231  *   NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
232  *   will behave the same as rte_calloc().
233  * @return
234  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
235  *     align is not a power of two).
236  *   - Otherwise, the pointer to the allocated object.
237  */
238 void *
239 rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket);
240
241 /**
242  * Frees the memory space pointed to by the provided pointer.
243  *
244  * This pointer must have been returned by a previous call to
245  * rte_malloc(), rte_zmalloc(), rte_calloc() or rte_realloc(). The behaviour of
246  * rte_free() is undefined if the pointer does not match this requirement.
247  *
248  * If the pointer is NULL, the function does nothing.
249  *
250  * @param ptr
251  *   The pointer to memory to be freed.
252  */
253 void
254 rte_free(void *ptr);
255
256 /**
257  * If malloc debug is enabled, check a memory block for header
258  * and trailer markers to indicate that all is well with the block.
259  * If size is non-null, also return the size of the block.
260  *
261  * @param ptr
262  *   pointer to the start of a data block, must have been returned
263  *   by a previous call to rte_malloc(), rte_zmalloc(), rte_calloc()
264  *   or rte_realloc()
265  * @param size
266  *   if non-null, and memory block pointer is valid, returns the size
267  *   of the memory block
268  * @return
269  *   -1 on error, invalid pointer passed or header and trailer markers
270  *   are missing or corrupted
271  *   0 on success
272  */
273 int
274 rte_malloc_validate(const void *ptr, size_t *size);
275
276 /**
277  * Get heap statistics for the specified heap.
278  *
279  * @note This function is not thread-safe with respect to
280  *    ``rte_malloc_heap_create()``/``rte_malloc_heap_destroy()`` functions.
281  *
282  * @param socket
283  *   An unsigned integer specifying the socket to get heap statistics for
284  * @param socket_stats
285  *   A structure which provides memory to store statistics
286  * @return
287  *   Null on error
288  *   Pointer to structure storing statistics on success
289  */
290 int
291 rte_malloc_get_socket_stats(int socket,
292                 struct rte_malloc_socket_stats *socket_stats);
293
294 /**
295  * Add memory chunk to a heap with specified name.
296  *
297  * @note Multiple memory chunks can be added to the same heap
298  *
299  * @note Before accessing this memory in other processes, it needs to be
300  *   attached in each of those processes by calling
301  *   ``rte_malloc_heap_memory_attach`` in each other process.
302  *
303  * @note Memory must be previously allocated for DPDK to be able to use it as a
304  *   malloc heap. Failing to do so will result in undefined behavior, up to and
305  *   including segmentation faults.
306  *
307  * @note Calling this function will erase any contents already present at the
308  *   supplied memory address.
309  *
310  * @param heap_name
311  *   Name of the heap to add memory chunk to
312  * @param va_addr
313  *   Start of virtual area to add to the heap. Must be aligned by ``page_sz``.
314  * @param len
315  *   Length of virtual area to add to the heap. Must be aligned by ``page_sz``.
316  * @param iova_addrs
317  *   Array of page IOVA addresses corresponding to each page in this memory
318  *   area. Can be NULL, in which case page IOVA addresses will be set to
319  *   RTE_BAD_IOVA.
320  * @param n_pages
321  *   Number of elements in the iova_addrs array. Ignored if  ``iova_addrs``
322  *   is NULL.
323  * @param page_sz
324  *   Page size of the underlying memory
325  *
326  * @return
327  *   - 0 on success
328  *   - -1 in case of error, with rte_errno set to one of the following:
329  *     EINVAL - one of the parameters was invalid
330  *     EPERM  - attempted to add memory to a reserved heap
331  *     ENOSPC - no more space in internal config to store a new memory chunk
332  */
333 int __rte_experimental
334 rte_malloc_heap_memory_add(const char *heap_name, void *va_addr, size_t len,
335                 rte_iova_t iova_addrs[], unsigned int n_pages, size_t page_sz);
336
337 /**
338  * Remove memory chunk from heap with specified name.
339  *
340  * @note Memory chunk being removed must be the same as one that was added;
341  *   partially removing memory chunks is not supported
342  *
343  * @note Memory area must not contain any allocated elements to allow its
344  *   removal from the heap
345  *
346  * @note All other processes must detach from the memory chunk prior to it being
347  *   removed from the heap.
348  *
349  * @param heap_name
350  *   Name of the heap to remove memory from
351  * @param va_addr
352  *   Virtual address to remove from the heap
353  * @param len
354  *   Length of virtual area to remove from the heap
355  *
356  * @return
357  *   - 0 on success
358  *   - -1 in case of error, with rte_errno set to one of the following:
359  *     EINVAL - one of the parameters was invalid
360  *     EPERM  - attempted to remove memory from a reserved heap
361  *     ENOENT - heap or memory chunk was not found
362  *     EBUSY  - memory chunk still contains data
363  */
364 int __rte_experimental
365 rte_malloc_heap_memory_remove(const char *heap_name, void *va_addr, size_t len);
366
367 /**
368  * Attach to an already existing chunk of external memory in another process.
369  *
370  * @note This function must be called before any attempt is made to use an
371  *   already existing external memory chunk. This function does *not* need to
372  *   be called if a call to ``rte_malloc_heap_memory_add`` was made in the
373  *   current process.
374  *
375  * @param heap_name
376  *   Heap name to which this chunk of memory belongs
377  * @param va_addr
378  *   Start address of memory chunk to attach to
379  * @param len
380  *   Length of memory chunk to attach to
381  * @return
382  *   0 on successful attach
383  *   -1 on unsuccessful attach, with rte_errno set to indicate cause for error:
384  *     EINVAL - one of the parameters was invalid
385  *     EPERM  - attempted to attach memory to a reserved heap
386  *     ENOENT - heap or memory chunk was not found
387  */
388 int __rte_experimental
389 rte_malloc_heap_memory_attach(const char *heap_name, void *va_addr, size_t len);
390
391 /**
392  * Detach from a chunk of external memory in secondary process.
393  *
394  * @note This function must be called in before any attempt is made to remove
395  *   external memory from the heap in another process. This function does *not*
396  *   need to be called if a call to ``rte_malloc_heap_memory_remove`` will be
397  *   called in current process.
398  *
399  * @param heap_name
400  *   Heap name to which this chunk of memory belongs
401  * @param va_addr
402  *   Start address of memory chunk to attach to
403  * @param len
404  *   Length of memory chunk to attach to
405  * @return
406  *   0 on successful detach
407  *   -1 on unsuccessful detach, with rte_errno set to indicate cause for error:
408  *     EINVAL - one of the parameters was invalid
409  *     EPERM  - attempted to detach memory from a reserved heap
410  *     ENOENT - heap or memory chunk was not found
411  */
412 int __rte_experimental
413 rte_malloc_heap_memory_detach(const char *heap_name, void *va_addr, size_t len);
414
415 /**
416  * Creates a new empty malloc heap with a specified name.
417  *
418  * @note Heaps created via this call will automatically get assigned a unique
419  *   socket ID, which can be found using ``rte_malloc_heap_get_socket()``
420  *
421  * @param heap_name
422  *   Name of the heap to create.
423  *
424  * @return
425  *   - 0 on successful creation
426  *   - -1 in case of error, with rte_errno set to one of the following:
427  *     EINVAL - ``heap_name`` was NULL, empty or too long
428  *     EEXIST - heap by name of ``heap_name`` already exists
429  *     ENOSPC - no more space in internal config to store a new heap
430  */
431 int __rte_experimental
432 rte_malloc_heap_create(const char *heap_name);
433
434 /**
435  * Destroys a previously created malloc heap with specified name.
436  *
437  * @note This function will return a failure result if not all memory allocated
438  *   from the heap has been freed back to the heap
439  *
440  * @note This function will return a failure result if not all memory segments
441  *   were removed from the heap prior to its destruction
442  *
443  * @param heap_name
444  *   Name of the heap to create.
445  *
446  * @return
447  *   - 0 on success
448  *   - -1 in case of error, with rte_errno set to one of the following:
449  *     EINVAL - ``heap_name`` was NULL, empty or too long
450  *     ENOENT - heap by the name of ``heap_name`` was not found
451  *     EPERM  - attempting to destroy reserved heap
452  *     EBUSY  - heap still contains data
453  */
454 int __rte_experimental
455 rte_malloc_heap_destroy(const char *heap_name);
456
457 /**
458  * Find socket ID corresponding to a named heap.
459  *
460  * @param name
461  *   Heap name to find socket ID for
462  * @return
463  *   Socket ID in case of success (a non-negative number)
464  *   -1 in case of error, with rte_errno set to one of the following:
465  *     EINVAL - ``name`` was NULL
466  *     ENOENT - heap identified by the name ``name`` was not found
467  */
468 int __rte_experimental
469 rte_malloc_heap_get_socket(const char *name);
470
471 /**
472  * Check if a given socket ID refers to externally allocated memory.
473  *
474  * @note Passing SOCKET_ID_ANY will return 0.
475  *
476  * @param socket_id
477  *   Socket ID to check
478  * @return
479  *   1 if socket ID refers to externally allocated memory
480  *   0 if socket ID refers to internal DPDK memory
481  *   -1 if socket ID is invalid
482  */
483 int __rte_experimental
484 rte_malloc_heap_socket_is_external(int socket_id);
485
486 /**
487  * Dump statistics.
488  *
489  * Dump for the specified type to a file. If the type argument is
490  * NULL, all memory types will be dumped.
491  *
492  * @note This function is not thread-safe with respect to
493  *    ``rte_malloc_heap_create()``/``rte_malloc_heap_destroy()`` functions.
494  *
495  * @param f
496  *   A pointer to a file for output
497  * @param type
498  *   A string identifying the type of objects to dump, or NULL
499  *   to dump all objects.
500  */
501 void
502 rte_malloc_dump_stats(FILE *f, const char *type);
503
504 /**
505  * Dump contents of all malloc heaps to a file.
506  *
507  * @note This function is not thread-safe with respect to
508  *    ``rte_malloc_heap_create()``/``rte_malloc_heap_destroy()`` functions.
509  *
510  * @param f
511  *   A pointer to a file for output
512  */
513 void __rte_experimental
514 rte_malloc_dump_heaps(FILE *f);
515
516 /**
517  * Set the maximum amount of allocated memory for this type.
518  *
519  * This is not yet implemented
520  *
521  * @param type
522  *   A string identifying the type of allocated objects.
523  * @param max
524  *   The maximum amount of allocated bytes for this type.
525  * @return
526  *   - 0: Success.
527  *   - (-1): Error.
528  */
529 int
530 rte_malloc_set_limit(const char *type, size_t max);
531
532 /**
533  * Return the IO address of a virtual address obtained through
534  * rte_malloc
535  *
536  * @param addr
537  *   Address obtained from a previous rte_malloc call
538  * @return
539  *   RTE_BAD_IOVA on error
540  *   otherwise return an address suitable for IO
541  */
542 rte_iova_t
543 rte_malloc_virt2iova(const void *addr);
544
545 __rte_deprecated
546 static inline phys_addr_t
547 rte_malloc_virt2phy(const void *addr)
548 {
549         return rte_malloc_virt2iova(addr);
550 }
551
552 #ifdef __cplusplus
553 }
554 #endif
555
556 #endif /* _RTE_MALLOC_H_ */