doc: whitespace changes in licenses
[dpdk.git] / lib / librte_malloc / rte_malloc.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 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
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 /**
50  *  Structure to hold heap statistics obtained from rte_malloc_get_socket_stats function.
51  */
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 */
59 };
60
61 /**
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.
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 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_malloc(const char *type, size_t size, unsigned align);
84
85 /**
86  * Allocate zero'ed memory from the heap.
87  *
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.
91  *
92  * @param type
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.
95  * @param size
96  *   Size (in bytes) to be allocated.
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_zmalloc(const char *type, size_t size, unsigned align);
110
111 /**
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.
115  *
116  * @param type
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.
119  * @param num
120  *   Number of elements to be allocated.
121  * @param size
122  *   Size (in bytes) of a single element.
123  * @param align
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)
129  * @return
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.
133  */
134 void *
135 rte_calloc(const char *type, size_t num, size_t size, unsigned align);
136
137 /**
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.
141  *
142  * @param ptr
143  *   Pointer to already allocated memory
144  * @param size
145  *   Size (in bytes) of new area. If this is 0, memory is freed.
146  * @param align
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)
152  * @return
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.
156  */
157 void *
158 rte_realloc(void *ptr, size_t size, unsigned align);
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(void *ptr, size_t *size);
276
277 /**
278  * Get heap statistics for the specified heap.
279  *
280  * @param socket
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
284  * @return
285  *   Null on error
286  *   Pointer to structure storing statistics on success
287  */
288 int
289 rte_malloc_get_socket_stats(int socket,
290                 struct rte_malloc_socket_stats *socket_stats);
291
292 /**
293  * Dump statistics.
294  *
295  * Dump for the specified type to the console. If the type argument is
296  * NULL, all memory types will be dumped.
297  *
298  * @param type
299  *   A string identifying the type of objects to dump, or NULL
300  *   to dump all objects.
301  */
302 void
303 rte_malloc_dump_stats(const char *type);
304
305 /**
306  * Set the maximum amount of allocated memory for this type.
307  *
308  * This is not yet implemented
309  *
310  * @param type
311  *   A string identifying the type of allocated objects.
312  * @param max
313  *   The maximum amount of allocated bytes for this type.
314  * @return
315  *   - 0: Success.
316  *   - (-1): Error.
317  */
318 int
319 rte_malloc_set_limit(const char *type, size_t max);
320
321 #ifdef __cplusplus
322 }
323 #endif
324
325 #endif /* _RTE_MALLOC_H_ */