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