1 /***********************license start***************
2 * Copyright (c) 2003-2008 Cavium Networks (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 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
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.
35 * For any questions regarding licensing please contact marketing@caviumnetworks.com
37 ***********************license end**************************************/
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
51 * <hr>$Revision: 41586 $<hr>
54 #ifndef __CVMX_MALLOC_H__
55 #define __CVMX_MALLOC_H__
57 #include "cvmx-spinlock.h"
63 struct malloc_state; /* forward declaration */
64 typedef struct malloc_state *cvmx_arena_list_t;
68 * Creates an arena from the memory region specified and adds it
69 * to the supplied arena list.
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
75 * @param size Size of memory region available at ptr in bytes.
77 * @return -1 on Failure
80 int cvmx_add_arena(cvmx_arena_list_t *arena_list, void *ptr, size_t size);
83 * allocate buffer from an arena list
85 * @param arena_list arena list to allocate buffer from
86 * @param size size of buffer to allocate (in bytes)
88 * @return pointer to buffer or NULL if allocation failed
90 void *cvmx_malloc(cvmx_arena_list_t arena_list, size_t size);
92 * Allocate zero initialized buffer
94 * @param arena_list arena list to allocate from
95 * @param n number of elements
96 * @param elem_size size of elementes
98 * @return pointer to (n*elem_size) byte zero initialized buffer or NULL
99 * on allocation failure
101 void *cvmx_calloc(cvmx_arena_list_t arena_list, size_t n, size_t elem_size);
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.
107 * @param arena_list arena list to allocate from
108 * @param ptr pointer to buffer to extend
109 * @param size new buffer size
111 * @return pointer to expanded buffer (may differ from ptr)
114 void *cvmx_realloc(cvmx_arena_list_t arena_list, void *ptr, size_t size);
116 * allocate a buffer with a specified alignment
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
122 * @return pointer to buffer on success
125 void *cvmx_memalign(cvmx_arena_list_t arena_list, size_t alignment, size_t bytes);
127 * free a previously allocated buffer
129 * @param ptr pointer of buffer to deallocate
131 void cvmx_free(void *ptr);
136 #define CVMX_ZONE_OVERHEAD (64)
137 /** Zone allocator definitions
142 cvmx_spinlock_t lock;
150 typedef struct cvmx_zone * cvmx_zone_t;
152 static inline uint32_t cvmx_zone_size(cvmx_zone_t zone)
154 return(zone->elem_size);
156 static inline char *cvmx_zone_name(cvmx_zone_t zone)
163 * Creates a memory zone for efficient allocation/deallocation of
164 * fixed size memory blocks from a specified memory region.
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.
174 * @return pointer to zone on success or
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);
180 * Creates a memory zone for efficient allocation/deallocation of
181 * fixed size memory blocks from a previously initialized arena list.
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.
193 * @return pointer to zone on success or
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);
199 * Allocate a buffer from a memory zone
201 * @param zone zone to allocate buffer from
202 * @param flags flags (currently unused)
204 * @return pointer to buffer or NULL on failure
206 void * cvmx_zone_alloc(cvmx_zone_t zone, uint32_t flags);
208 * Free a previously allocated buffer
210 * @param zone zone that buffer was allocated from
211 * @param ptr pointer to buffer to be freed
213 void cvmx_zone_free(cvmx_zone_t zone, void *ptr);
219 #endif // __CVMX_MALLOC_H__