first public release
[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  *  version: DPDK.L.1.2.3-3
34  */
35
36 #ifndef _RTE_MALLOC_H_
37 #define _RTE_MALLOC_H_
38
39 /**
40  * @file
41  * RTE Malloc. This library provides methods for dynamically allocating memory
42  * from hugepages.
43  */
44
45 #include <stddef.h>
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 /**
52  * This function allocates memory from the huge-page area of memory. The memory
53  * is not cleared.
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.
79  *
80  * @param type
81  *   A string identifying the type of allocated objects (useful for debug
82  *   purposes, such as identifying the cause of a memory leak). Can be NULL.
83  * @param size
84  *   Size (in bytes) to be allocated.
85  * @param align
86  *   If 0, the return is a pointer that is suitably aligned for any kind of
87  *   variable (in the same manner as malloc()).
88  *   Otherwise, the return is a pointer that is a multiple of *align*. In
89  *   this case, it must obviously be a power of two. (Minimum alignment is the
90  *   cacheline size, i.e. 64-bytes)
91  * @return
92  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
93  *     align is not a power of two).
94  *   - Otherwise, the pointer to the allocated object.
95  */
96 void *
97 rte_zmalloc(const char *type, size_t size, unsigned align);
98
99 /**
100  * Replacement function for calloc(), using huge-page memory. Memory area is
101  * initialised with zeros.
102  *
103  * @param type
104  *   A string identifying the type of allocated objects (useful for debug
105  *   purposes, such as identifying the cause of a memory leak). Can be NULL.
106  * @param num
107  *   Number of elements to be allocated.
108  * @param size
109  *   Size (in bytes) of a single element.
110  * @param align
111  *   If 0, the return is a pointer that is suitably aligned for any kind of
112  *   variable (in the same manner as malloc()).
113  *   Otherwise, the return is a pointer that is a multiple of *align*. In
114  *   this case, it must obviously be a power of two. (Minimum alignment is the
115  *   cacheline size, i.e. 64-bytes)
116  * @return
117  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
118  *     align is not a power of two).
119  *   - Otherwise, the pointer to the allocated object.
120  */
121 void *
122 rte_calloc(const char *type, size_t num, size_t size, unsigned align);
123
124 /**
125  * Replacement function for realloc(), using huge-page memory. Reserved area
126  * memory is resized, preserving contents.
127  *
128  * @param ptr
129  *   Pointer to already allocated memory
130  * @param size
131  *   Size (in bytes) of new area. If this is 0, memory is freed.
132  * @param align
133  *   If 0, the return is a pointer that is suitably aligned for any kind of
134  *   variable (in the same manner as malloc()).
135  *   Otherwise, the return is a pointer that is a multiple of *align*. In
136  *   this case, it must obviously be a power of two. (Minimum alignment is the
137  *   cacheline size, i.e. 64-bytes)
138  * @return
139  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
140  *     align is not a power of two).
141  *   - Otherwise, the pointer to the reallocated memory.
142  */
143 void *
144 rte_realloc(void *ptr, size_t size, unsigned align);
145
146 /**
147  * Frees the memory space pointed to by the provided pointer.
148  *
149  * This pointer must have been returned by a previous call to
150  * rte_malloc(), rte_zmalloc(), rte_calloc() or rte_realloc(). The behaviour of
151  * rte_free() is undefined if the pointer does not match this requirement.
152  *
153  * If the pointer is NULL, the function does nothing.
154  *
155  * @param ptr
156  *   The pointer to memory to be freed.
157  */
158 void
159 rte_free(void *ptr);
160
161 /**
162  * If malloc debug is enabled, check a memory block for header
163  * and trailer markers to indicate that all is well with the block.
164  * If size is non-null, also return the size of the block.
165  *
166  * @param ptr
167  *   pointer to the start of a data block, must have been returned
168  *   by a previous call to rte_malloc(), rte_zmalloc(), rte_calloc()
169  *   or rte_realloc()
170  * @param size
171  *   if non-null, and memory block pointer is valid, returns the size
172  *   of the memory block
173  * @return
174  *   -1 on error, invalid pointer passed or header and trailer markers
175  *   are missing or corrupted
176  *   0 on success
177  */
178 int
179 rte_malloc_validate(void *ptr, size_t *size);
180
181 /**
182  * Dump statistics.
183  *
184  * Dump for the specified type to the console. If the type argument is
185  * NULL, all memory types will be dumped.
186  *
187  * @param type
188  *   A string identifying the type of objects to dump, or NULL
189  *   to dump all objects.
190  */
191 void
192 rte_malloc_dump_stats(const char *type);
193
194 /**
195  * Set the maximum amount of allocated memory for this type.
196  *
197  * @param type
198  *   A string identifying the type of allocated objects.
199  * @param max
200  *   The maximum amount of allocated bytes for this type.
201  * @return
202  *   - 0: Success.
203  *   - (-1): Error.
204  */
205 int
206 rte_malloc_set_limit(const char *type, size_t max);
207
208 #ifdef __cplusplus
209 }
210 #endif
211
212 #endif /* _RTE_MALLOC_H_ */