]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/contrib/octeon-sdk/cvmx-malloc.h
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.git] / sys / contrib / octeon-sdk / cvmx-malloc.h
1 /***********************license start***************
2  *  Copyright (c) 2003-2008 Cavium Networks (support@cavium.com). All rights
3  *  reserved.
4  *
5  *
6  *  Redistribution and use in source and binary forms, with or without
7  *  modification, are permitted provided that the following conditions are
8  *  met:
9  *
10  *      * Redistributions of source code must retain the above copyright
11  *        notice, this list of conditions and the following disclaimer.
12  *
13  *      * Redistributions in binary form must reproduce the above
14  *        copyright notice, this list of conditions and the following
15  *        disclaimer in the documentation and/or other materials provided
16  *        with the distribution.
17  *
18  *      * Neither the name of Cavium Networks nor the names of
19  *        its contributors may be used to endorse or promote products
20  *        derived from this software without specific prior written
21  *        permission.
22  *
23  *  TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
24  *  AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS
25  *  OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
26  *  RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
27  *  REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
28  *  DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
29  *  OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
30  *  PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET
31  *  POSSESSION OR CORRESPONDENCE TO DESCRIPTION.  THE ENTIRE RISK ARISING OUT
32  *  OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
33  *
34  *
35  *  For any questions regarding licensing please contact marketing@caviumnetworks.com
36  *
37  ***********************license end**************************************/
38
39
40
41
42
43 /**
44  * @file
45  *
46  * This file provides prototypes for the memory management library functions.
47  * Two different allocators are provided: an arena based allocator that is derived from a
48  * modified version of ptmalloc2 (used in glibc), and a zone allocator for allocating fixed
49  * size memory blocks.
50  *
51  * <hr>$Revision: 41586 $<hr>
52  */
53
54 #ifndef __CVMX_MALLOC_H__
55 #define __CVMX_MALLOC_H__
56
57 #include "cvmx-spinlock.h"
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61
62
63 struct malloc_state; /* forward declaration */
64 typedef struct malloc_state *cvmx_arena_list_t;
65
66
67 /**
68  * Creates an arena from the memory region specified and adds it
69  * to the supplied arena list.
70  *
71  * @param arena_list Pointer to an arena list to add new arena to.
72  *                   If NULL, new list is created.
73  * @param ptr        pointer to memory region to create arena from
74  *
75  * @param size       Size of memory region available at ptr in bytes.
76  *
77  * @return -1 on Failure
78  *         0 on success
79  */
80 int cvmx_add_arena(cvmx_arena_list_t *arena_list, void *ptr, size_t size);
81
82 /**
83  * allocate buffer from an arena list
84  *
85  * @param arena_list arena list to allocate buffer from
86  * @param size       size of buffer to allocate (in bytes)
87  *
88  * @return pointer to buffer or NULL if allocation failed
89  */
90 void *cvmx_malloc(cvmx_arena_list_t arena_list, size_t size);
91 /**
92  * Allocate zero initialized buffer
93  *
94  * @param arena_list arena list to allocate from
95  * @param n          number of elements
96  * @param elem_size  size of elementes
97  *
98  * @return pointer to (n*elem_size) byte zero initialized buffer or NULL
99  *         on allocation failure
100  */
101 void *cvmx_calloc(cvmx_arena_list_t arena_list, size_t n, size_t elem_size);
102 /**
103  * attempt to increase the size of an already allocated buffer
104  * This function may allocate a new buffer and copy
105  * the data if current buffer can't be extended.
106  *
107  * @param arena_list arena list to allocate from
108  * @param ptr        pointer to buffer to extend
109  * @param size       new buffer size
110  *
111  * @return pointer to expanded buffer (may differ from ptr)
112  *         or NULL on failure
113  */
114 void *cvmx_realloc(cvmx_arena_list_t arena_list, void *ptr, size_t size);
115 /**
116  * allocate a buffer with a specified alignment
117  *
118  * @param arena_list arena list to allocate from
119  * @param alignment  alignment of buffer.  Must be a power of 2
120  * @param bytes      size of buffer in bytes
121  *
122  * @return pointer to buffer on success
123  *         NULL on failure
124  */
125 void *cvmx_memalign(cvmx_arena_list_t arena_list, size_t alignment, size_t bytes);
126 /**
127  * free a previously allocated buffer
128  *
129  * @param ptr    pointer of buffer to deallocate
130  */
131 void cvmx_free(void *ptr);
132
133
134
135
136 #define CVMX_ZONE_OVERHEAD  (64)
137 /** Zone allocator definitions
138  *
139  */
140 struct cvmx_zone
141 {
142         cvmx_spinlock_t lock;
143         char *baseptr;
144         char *name;
145         void *freelist;
146         uint32_t num_elem;
147         uint32_t elem_size;
148         uint32_t align;
149 };
150 typedef struct cvmx_zone * cvmx_zone_t;
151
152 static inline uint32_t cvmx_zone_size(cvmx_zone_t zone)
153 {
154     return(zone->elem_size);
155 }
156 static inline char *cvmx_zone_name(cvmx_zone_t zone)
157 {
158     return(zone->name);
159 }
160
161
162 /**
163  * Creates a memory zone for efficient allocation/deallocation of
164  * fixed size memory blocks from a specified memory region.
165  *
166  * @param name      name of zone.
167  * @param elem_size size of blocks that will be requested from zone
168  * @param num_elem  number of elements to allocate
169  * @param mem_ptr   pointer to memory to allocate zone from
170  * @param mem_size  size of memory region available
171  *                  (must be at least elem_size * num_elem + CVMX_ZONE_OVERHEAD bytes)
172  * @param flags     flags for zone.  Currently unused.
173  *
174  * @return pointer to zone on success or
175  *         NULL on failure
176  */
177 cvmx_zone_t cvmx_zone_create_from_addr(char *name, uint32_t elem_size, uint32_t num_elem,
178                              void* mem_ptr, uint64_t mem_size, uint32_t flags);
179 /**
180  * Creates a memory zone for efficient allocation/deallocation of
181  * fixed size memory blocks from a previously initialized arena list.
182  *
183  * @param name       name of zone.
184  * @param elem_size  size of blocks that will be requested from zone
185  * @param num_elem   number of elements to allocate
186  * @param align      alignment of buffers (must be power of 2)
187  *                   Elements are allocated contiguously, so the buffer size
188  *                   must be a multiple of the requested alignment for all
189  *                   buffers to have the requested alignment.
190  * @param arena_list arena list to allocate memory from
191  * @param flags      flags for zone.  Currently unused.
192  *
193  * @return pointer to zone on success or
194  *         NULL on failure
195  */
196 cvmx_zone_t cvmx_zone_create_from_arena(char *name, uint32_t elem_size, uint32_t num_elem, uint32_t align,
197                              cvmx_arena_list_t arena_list, uint32_t flags);
198 /**
199  * Allocate a buffer from a memory zone
200  *
201  * @param zone   zone to allocate buffer from
202  * @param flags  flags (currently unused)
203  *
204  * @return pointer to buffer or NULL on failure
205  */
206 void * cvmx_zone_alloc(cvmx_zone_t zone, uint32_t flags);
207 /**
208  * Free a previously allocated buffer
209  *
210  * @param zone   zone that buffer was allocated from
211  * @param ptr    pointer to buffer to be freed
212  */
213 void cvmx_zone_free(cvmx_zone_t zone, void *ptr);
214
215 #ifdef __cplusplus
216 }
217 #endif
218
219 #endif // __CVMX_MALLOC_H__