1 /***********************license start***************
2 * Copyright (c) 2003-2010 Cavium Inc. (support@cavium.com). All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
18 * * Neither the name of Cavium Inc. nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written
23 * This Software, including technical data, may be subject to U.S. export control
24 * laws, including the U.S. Export Administration Act and its associated
25 * regulations, and may be subject to export or import regulations in other
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR
37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
48 * This file provides prototypes for the memory management library functions.
49 * Two different allocators are provided: an arena based allocator that is derived from a
50 * modified version of ptmalloc2 (used in glibc), and a zone allocator for allocating fixed
53 * <hr>$Revision: 70030 $<hr>
56 #ifndef __CVMX_MALLOC_H__
57 #define __CVMX_MALLOC_H__
59 #include "cvmx-spinlock.h"
65 struct malloc_state; /* forward declaration */
66 typedef struct malloc_state *cvmx_arena_list_t;
69 #ifndef CVMX_BUILD_FOR_LINUX_USER
71 * Creates an arena from the memory region specified and adds it
72 * to the supplied arena list.
74 * @param arena_list Pointer to an arena list to add new arena to.
75 * If NULL, new list is created.
76 * @param ptr pointer to memory region to create arena from
78 * @param size Size of memory region available at ptr in bytes.
80 * @return -1 on Failure
83 int cvmx_add_arena(cvmx_arena_list_t *arena_list, void *ptr, size_t size);
86 * allocate buffer from an arena list
88 * @param arena_list arena list to allocate buffer from
89 * @param size size of buffer to allocate (in bytes)
91 * @return pointer to buffer or NULL if allocation failed
93 void *cvmx_malloc(cvmx_arena_list_t arena_list, size_t size);
95 * Allocate zero initialized buffer
97 * @param arena_list arena list to allocate from
98 * @param n number of elements
99 * @param elem_size size of elementes
101 * @return pointer to (n*elem_size) byte zero initialized buffer or NULL
102 * on allocation failure
104 void *cvmx_calloc(cvmx_arena_list_t arena_list, size_t n, size_t elem_size);
106 * attempt to increase the size of an already allocated buffer
107 * This function may allocate a new buffer and copy
108 * the data if current buffer can't be extended.
110 * @param arena_list arena list to allocate from
111 * @param ptr pointer to buffer to extend
112 * @param size new buffer size
114 * @return pointer to expanded buffer (may differ from ptr)
117 void *cvmx_realloc(cvmx_arena_list_t arena_list, void *ptr, size_t size);
119 * allocate a buffer with a specified alignment
121 * @param arena_list arena list to allocate from
122 * @param alignment alignment of buffer. Must be a power of 2
123 * @param bytes size of buffer in bytes
125 * @return pointer to buffer on success
128 void *cvmx_memalign(cvmx_arena_list_t arena_list, size_t alignment, size_t bytes);
130 * free a previously allocated buffer
132 * @param ptr pointer of buffer to deallocate
134 void cvmx_free(void *ptr);
140 #define CVMX_ZONE_OVERHEAD (64)
141 /** Zone allocator definitions
146 cvmx_spinlock_t lock;
154 typedef struct cvmx_zone * cvmx_zone_t;
156 static inline uint32_t cvmx_zone_size(cvmx_zone_t zone)
158 return(zone->elem_size);
160 static inline char *cvmx_zone_name(cvmx_zone_t zone)
166 #ifndef CVMX_BUILD_FOR_LINUX_USER
168 * Creates a memory zone for efficient allocation/deallocation of
169 * fixed size memory blocks from a specified memory region.
171 * @param name name of zone.
172 * @param elem_size size of blocks that will be requested from zone
173 * @param num_elem number of elements to allocate
174 * @param mem_ptr pointer to memory to allocate zone from
175 * @param mem_size size of memory region available
176 * (must be at least elem_size * num_elem + CVMX_ZONE_OVERHEAD bytes)
177 * @param flags flags for zone. Currently unused.
179 * @return pointer to zone on success or
182 cvmx_zone_t cvmx_zone_create_from_addr(char *name, uint32_t elem_size, uint32_t num_elem,
183 void* mem_ptr, uint64_t mem_size, uint32_t flags);
185 * Creates a memory zone for efficient allocation/deallocation of
186 * fixed size memory blocks from a previously initialized arena list.
188 * @param name name of zone.
189 * @param elem_size size of blocks that will be requested from zone
190 * @param num_elem number of elements to allocate
191 * @param align alignment of buffers (must be power of 2)
192 * Elements are allocated contiguously, so the buffer size
193 * must be a multiple of the requested alignment for all
194 * buffers to have the requested alignment.
195 * @param arena_list arena list to allocate memory from
196 * @param flags flags for zone. Currently unused.
198 * @return pointer to zone on success or
201 cvmx_zone_t cvmx_zone_create_from_arena(char *name, uint32_t elem_size, uint32_t num_elem, uint32_t align,
202 cvmx_arena_list_t arena_list, uint32_t flags);
205 * Allocate a buffer from a memory zone
207 * @param zone zone to allocate buffer from
208 * @param flags flags (currently unused)
210 * @return pointer to buffer or NULL on failure
212 void * cvmx_zone_alloc(cvmx_zone_t zone, uint32_t flags);
214 * Free a previously allocated buffer
216 * @param zone zone that buffer was allocated from
217 * @param ptr pointer to buffer to be freed
219 void cvmx_zone_free(cvmx_zone_t zone, void *ptr);
225 #endif // __CVMX_MALLOC_H__