687ab16420bda5eb8f1861624a1aaeb4509cc3da
[dpdk.git] / lib / librte_malloc / rte_malloc.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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
35 #ifndef _RTE_MALLOC_H_
36 #define _RTE_MALLOC_H_
37
38 /**
39  * @file
40  * RTE Malloc. This library provides methods for dynamically allocating memory
41  * from hugepages.
42  */
43
44 #include <stddef.h>
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50 /**
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.
54  *
55  * @param type
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.
58  * @param size
59  *   Size (in bytes) to be allocated.
60  * @param align
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)
66  * @return
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.
70  */
71 void *
72 rte_malloc(const char *type, size_t size, unsigned align);
73
74 /**
75  * Allocate zero'ed memory from the heap.
76  *
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.
80  *
81  * @param type
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.
84  * @param size
85  *   Size (in bytes) to be allocated.
86  * @param align
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)
92  * @return
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.
96  */
97 void *
98 rte_zmalloc(const char *type, size_t size, unsigned align);
99
100 /**
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.
104  *
105  * @param type
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.
108  * @param num
109  *   Number of elements to be allocated.
110  * @param size
111  *   Size (in bytes) of a single element.
112  * @param align
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)
118  * @return
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.
122  */
123 void *
124 rte_calloc(const char *type, size_t num, size_t size, unsigned align);
125
126 /**
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.
130  *
131  * @param ptr
132  *   Pointer to already allocated memory
133  * @param size
134  *   Size (in bytes) of new area. If this is 0, memory is freed.
135  * @param align
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)
141  * @return
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.
145  */
146 void *
147 rte_realloc(void *ptr, size_t size, unsigned align);
148
149 /**
150  * This function allocates memory from the huge-page area of memory. The memory
151  * is not cleared.
152  *
153  * @param type
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.
156  * @param size
157  *   Size (in bytes) to be allocated.
158  * @param align
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)
164  * @param socket
165  *   NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
166  *   will behave the same as rte_malloc().
167  * @return
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.
171  */
172 void *
173 rte_malloc_socket(const char *type, size_t size, unsigned align, int socket);
174
175 /**
176  * Allocate zero'ed memory from the heap.
177  *
178  * Equivalent to rte_malloc() except that the memory zone is
179  * initialised with zeros.
180  *
181  * @param type
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.
184  * @param size
185  *   Size (in bytes) to be allocated.
186  * @param align
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)
192  * @param socket
193  *   NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
194  *   will behave the same as rte_zmalloc().
195  * @return
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.
199  */
200 void *
201 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket);
202
203 /**
204  * Replacement function for calloc(), using huge-page memory. Memory area is
205  * initialised with zeros.
206  *
207  * @param type
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.
210  * @param num
211  *   Number of elements to be allocated.
212  * @param size
213  *   Size (in bytes) of a single element.
214  * @param align
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)
220  * @param socket
221  *   NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
222  *   will behave the same as rte_calloc().
223  * @return
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.
227  */
228 void *
229 rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket);
230
231 /**
232  * Frees the memory space pointed to by the provided pointer.
233  *
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.
237  *
238  * If the pointer is NULL, the function does nothing.
239  *
240  * @param ptr
241  *   The pointer to memory to be freed.
242  */
243 void
244 rte_free(void *ptr);
245
246 /**
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.
250  *
251  * @param ptr
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()
254  *   or rte_realloc()
255  * @param size
256  *   if non-null, and memory block pointer is valid, returns the size
257  *   of the memory block
258  * @return
259  *   -1 on error, invalid pointer passed or header and trailer markers
260  *   are missing or corrupted
261  *   0 on success
262  */
263 int
264 rte_malloc_validate(void *ptr, size_t *size);
265
266 /**
267  * Dump statistics.
268  *
269  * Dump for the specified type to the console. If the type argument is
270  * NULL, all memory types will be dumped.
271  *
272  * @param type
273  *   A string identifying the type of objects to dump, or NULL
274  *   to dump all objects.
275  */
276 void
277 rte_malloc_dump_stats(const char *type);
278
279 /**
280  * Set the maximum amount of allocated memory for this type.
281  *
282  * This is not yet implemented
283  *
284  * @param type
285  *   A string identifying the type of allocated objects.
286  * @param max
287  *   The maximum amount of allocated bytes for this type.
288  * @return
289  *   - 0: Success.
290  *   - (-1): Error.
291  */
292 int
293 rte_malloc_set_limit(const char *type, size_t max);
294
295 #ifdef __cplusplus
296 }
297 #endif
298
299 #endif /* _RTE_MALLOC_H_ */