eal: move common header files
[dpdk.git] / lib / librte_eal / 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 __rte_experimental
157 void *
158 rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket);
159
160 /**
161  * This function allocates memory from the huge-page area of memory. The memory
162  * is not cleared.
163  *
164  * @param type
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.
167  * @param size
168  *   Size (in bytes) to be allocated.
169  * @param align
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)
175  * @param socket
176  *   NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
177  *   will behave the same as rte_malloc().
178  * @return
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.
182  */
183 void *
184 rte_malloc_socket(const char *type, size_t size, unsigned align, int socket);
185
186 /**
187  * Allocate zero'ed memory from the heap.
188  *
189  * Equivalent to rte_malloc() except that the memory zone is
190  * initialised with zeros.
191  *
192  * @param type
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.
195  * @param size
196  *   Size (in bytes) to be allocated.
197  * @param align
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)
203  * @param socket
204  *   NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
205  *   will behave the same as rte_zmalloc().
206  * @return
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.
210  */
211 void *
212 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket);
213
214 /**
215  * Replacement function for calloc(), using huge-page memory. Memory area is
216  * initialised with zeros.
217  *
218  * @param type
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.
221  * @param num
222  *   Number of elements to be allocated.
223  * @param size
224  *   Size (in bytes) of a single element.
225  * @param align
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)
231  * @param socket
232  *   NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
233  *   will behave the same as rte_calloc().
234  * @return
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.
238  */
239 void *
240 rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket);
241
242 /**
243  * Frees the memory space pointed to by the provided pointer.
244  *
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.
248  *
249  * If the pointer is NULL, the function does nothing.
250  *
251  * @param ptr
252  *   The pointer to memory to be freed.
253  */
254 void
255 rte_free(void *ptr);
256
257 /**
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.
261  *
262  * @param ptr
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()
265  *   or rte_realloc()
266  * @param size
267  *   if non-null, and memory block pointer is valid, returns the size
268  *   of the memory block
269  * @return
270  *   -1 on error, invalid pointer passed or header and trailer markers
271  *   are missing or corrupted
272  *   0 on success
273  */
274 int
275 rte_malloc_validate(const void *ptr, size_t *size);
276
277 /**
278  * Get heap statistics for the specified heap.
279  *
280  * @note This function is not thread-safe with respect to
281  *    ``rte_malloc_heap_create()``/``rte_malloc_heap_destroy()`` functions.
282  *
283  * @param socket
284  *   An unsigned integer specifying the socket to get heap statistics for
285  * @param socket_stats
286  *   A structure which provides memory to store statistics
287  * @return
288  *   Null on error
289  *   Pointer to structure storing statistics on success
290  */
291 int
292 rte_malloc_get_socket_stats(int socket,
293                 struct rte_malloc_socket_stats *socket_stats);
294
295 /**
296  * Add memory chunk to a heap with specified name.
297  *
298  * @note Multiple memory chunks can be added to the same heap
299  *
300  * @note Before accessing this memory in other processes, it needs to be
301  *   attached in each of those processes by calling
302  *   ``rte_malloc_heap_memory_attach`` in each other process.
303  *
304  * @note Memory must be previously allocated for DPDK to be able to use it as a
305  *   malloc heap. Failing to do so will result in undefined behavior, up to and
306  *   including segmentation faults.
307  *
308  * @note Calling this function will erase any contents already present at the
309  *   supplied memory address.
310  *
311  * @param heap_name
312  *   Name of the heap to add memory chunk to
313  * @param va_addr
314  *   Start of virtual area to add to the heap. Must be aligned by ``page_sz``.
315  * @param len
316  *   Length of virtual area to add to the heap. Must be aligned by ``page_sz``.
317  * @param iova_addrs
318  *   Array of page IOVA addresses corresponding to each page in this memory
319  *   area. Can be NULL, in which case page IOVA addresses will be set to
320  *   RTE_BAD_IOVA.
321  * @param n_pages
322  *   Number of elements in the iova_addrs array. Ignored if  ``iova_addrs``
323  *   is NULL.
324  * @param page_sz
325  *   Page size of the underlying memory
326  *
327  * @return
328  *   - 0 on success
329  *   - -1 in case of error, with rte_errno set to one of the following:
330  *     EINVAL - one of the parameters was invalid
331  *     EPERM  - attempted to add memory to a reserved heap
332  *     ENOSPC - no more space in internal config to store a new memory chunk
333  */
334 __rte_experimental
335 int
336 rte_malloc_heap_memory_add(const char *heap_name, void *va_addr, size_t len,
337                 rte_iova_t iova_addrs[], unsigned int n_pages, size_t page_sz);
338
339 /**
340  * Remove memory chunk from heap with specified name.
341  *
342  * @note Memory chunk being removed must be the same as one that was added;
343  *   partially removing memory chunks is not supported
344  *
345  * @note Memory area must not contain any allocated elements to allow its
346  *   removal from the heap
347  *
348  * @note All other processes must detach from the memory chunk prior to it being
349  *   removed from the heap.
350  *
351  * @param heap_name
352  *   Name of the heap to remove memory from
353  * @param va_addr
354  *   Virtual address to remove from the heap
355  * @param len
356  *   Length of virtual area to remove from the heap
357  *
358  * @return
359  *   - 0 on success
360  *   - -1 in case of error, with rte_errno set to one of the following:
361  *     EINVAL - one of the parameters was invalid
362  *     EPERM  - attempted to remove memory from a reserved heap
363  *     ENOENT - heap or memory chunk was not found
364  *     EBUSY  - memory chunk still contains data
365  */
366 __rte_experimental
367 int
368 rte_malloc_heap_memory_remove(const char *heap_name, void *va_addr, size_t len);
369
370 /**
371  * Attach to an already existing chunk of external memory in another process.
372  *
373  * @note This function must be called before any attempt is made to use an
374  *   already existing external memory chunk. This function does *not* need to
375  *   be called if a call to ``rte_malloc_heap_memory_add`` was made in the
376  *   current process.
377  *
378  * @param heap_name
379  *   Heap name to which this chunk of memory belongs
380  * @param va_addr
381  *   Start address of memory chunk to attach to
382  * @param len
383  *   Length of memory chunk to attach to
384  * @return
385  *   0 on successful attach
386  *   -1 on unsuccessful attach, with rte_errno set to indicate cause for error:
387  *     EINVAL - one of the parameters was invalid
388  *     EPERM  - attempted to attach memory to a reserved heap
389  *     ENOENT - heap or memory chunk was not found
390  */
391 __rte_experimental
392 int
393 rte_malloc_heap_memory_attach(const char *heap_name, void *va_addr, size_t len);
394
395 /**
396  * Detach from a chunk of external memory in secondary process.
397  *
398  * @note This function must be called in before any attempt is made to remove
399  *   external memory from the heap in another process. This function does *not*
400  *   need to be called if a call to ``rte_malloc_heap_memory_remove`` will be
401  *   called in current process.
402  *
403  * @param heap_name
404  *   Heap name to which this chunk of memory belongs
405  * @param va_addr
406  *   Start address of memory chunk to attach to
407  * @param len
408  *   Length of memory chunk to attach to
409  * @return
410  *   0 on successful detach
411  *   -1 on unsuccessful detach, with rte_errno set to indicate cause for error:
412  *     EINVAL - one of the parameters was invalid
413  *     EPERM  - attempted to detach memory from a reserved heap
414  *     ENOENT - heap or memory chunk was not found
415  */
416 __rte_experimental
417 int
418 rte_malloc_heap_memory_detach(const char *heap_name, void *va_addr, size_t len);
419
420 /**
421  * Creates a new empty malloc heap with a specified name.
422  *
423  * @note Heaps created via this call will automatically get assigned a unique
424  *   socket ID, which can be found using ``rte_malloc_heap_get_socket()``
425  *
426  * @param heap_name
427  *   Name of the heap to create.
428  *
429  * @return
430  *   - 0 on successful creation
431  *   - -1 in case of error, with rte_errno set to one of the following:
432  *     EINVAL - ``heap_name`` was NULL, empty or too long
433  *     EEXIST - heap by name of ``heap_name`` already exists
434  *     ENOSPC - no more space in internal config to store a new heap
435  */
436 __rte_experimental
437 int
438 rte_malloc_heap_create(const char *heap_name);
439
440 /**
441  * Destroys a previously created malloc heap with specified name.
442  *
443  * @note This function will return a failure result if not all memory allocated
444  *   from the heap has been freed back to the heap
445  *
446  * @note This function will return a failure result if not all memory segments
447  *   were removed from the heap prior to its destruction
448  *
449  * @param heap_name
450  *   Name of the heap to create.
451  *
452  * @return
453  *   - 0 on success
454  *   - -1 in case of error, with rte_errno set to one of the following:
455  *     EINVAL - ``heap_name`` was NULL, empty or too long
456  *     ENOENT - heap by the name of ``heap_name`` was not found
457  *     EPERM  - attempting to destroy reserved heap
458  *     EBUSY  - heap still contains data
459  */
460 __rte_experimental
461 int
462 rte_malloc_heap_destroy(const char *heap_name);
463
464 /**
465  * Find socket ID corresponding to a named heap.
466  *
467  * @param name
468  *   Heap name to find socket ID for
469  * @return
470  *   Socket ID in case of success (a non-negative number)
471  *   -1 in case of error, with rte_errno set to one of the following:
472  *     EINVAL - ``name`` was NULL
473  *     ENOENT - heap identified by the name ``name`` was not found
474  */
475 __rte_experimental
476 int
477 rte_malloc_heap_get_socket(const char *name);
478
479 /**
480  * Check if a given socket ID refers to externally allocated memory.
481  *
482  * @note Passing SOCKET_ID_ANY will return 0.
483  *
484  * @param socket_id
485  *   Socket ID to check
486  * @return
487  *   1 if socket ID refers to externally allocated memory
488  *   0 if socket ID refers to internal DPDK memory
489  *   -1 if socket ID is invalid
490  */
491 __rte_experimental
492 int
493 rte_malloc_heap_socket_is_external(int socket_id);
494
495 /**
496  * Dump statistics.
497  *
498  * Dump for the specified type to a file. If the type argument is
499  * NULL, all memory types will be dumped.
500  *
501  * @note This function is not thread-safe with respect to
502  *    ``rte_malloc_heap_create()``/``rte_malloc_heap_destroy()`` functions.
503  *
504  * @param f
505  *   A pointer to a file for output
506  * @param type
507  *   A string identifying the type of objects to dump, or NULL
508  *   to dump all objects.
509  */
510 void
511 rte_malloc_dump_stats(FILE *f, const char *type);
512
513 /**
514  * Dump contents of all malloc heaps to a file.
515  *
516  * @note This function is not thread-safe with respect to
517  *    ``rte_malloc_heap_create()``/``rte_malloc_heap_destroy()`` functions.
518  *
519  * @param f
520  *   A pointer to a file for output
521  */
522 __rte_experimental
523 void
524 rte_malloc_dump_heaps(FILE *f);
525
526 /**
527  * Set the maximum amount of allocated memory for this type.
528  *
529  * This is not yet implemented
530  *
531  * @param type
532  *   A string identifying the type of allocated objects.
533  * @param max
534  *   The maximum amount of allocated bytes for this type.
535  * @return
536  *   - 0: Success.
537  *   - (-1): Error.
538  */
539 __rte_deprecated
540 int
541 rte_malloc_set_limit(const char *type, size_t max);
542
543 /**
544  * Return the IO address of a virtual address obtained through
545  * rte_malloc
546  *
547  * @param addr
548  *   Address obtained from a previous rte_malloc call
549  * @return
550  *   RTE_BAD_IOVA on error
551  *   otherwise return an address suitable for IO
552  */
553 rte_iova_t
554 rte_malloc_virt2iova(const void *addr);
555
556 #ifdef __cplusplus
557 }
558 #endif
559
560 #endif /* _RTE_MALLOC_H_ */