1/***********************license start*************** 2 * Copyright (c) 2003-2010 Cavium Inc. (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 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 21 * permission. 22 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 26 * countries. 27 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**************************************/ 39 40 41 42 43 44 45/** 46 * @file 47 * 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 51 * size memory blocks. 52 * 53 * <hr>$Revision: 70030 $<hr> 54 */ 55 56#ifndef __CVMX_MALLOC_H__ 57#define __CVMX_MALLOC_H__ 58 59#include "cvmx-spinlock.h" 60#ifdef __cplusplus 61extern "C" { 62#endif 63 64 65struct malloc_state; /* forward declaration */ 66typedef struct malloc_state *cvmx_arena_list_t; 67 68 69#ifndef CVMX_BUILD_FOR_LINUX_USER 70/** 71 * Creates an arena from the memory region specified and adds it 72 * to the supplied arena list. 73 * 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 77 * 78 * @param size Size of memory region available at ptr in bytes. 79 * 80 * @return -1 on Failure 81 * 0 on success 82 */ 83int cvmx_add_arena(cvmx_arena_list_t *arena_list, void *ptr, size_t size); 84 85/** 86 * allocate buffer from an arena list 87 * 88 * @param arena_list arena list to allocate buffer from 89 * @param size size of buffer to allocate (in bytes) 90 * 91 * @return pointer to buffer or NULL if allocation failed 92 */ 93void *cvmx_malloc(cvmx_arena_list_t arena_list, size_t size); 94/** 95 * Allocate zero initialized buffer 96 * 97 * @param arena_list arena list to allocate from 98 * @param n number of elements 99 * @param elem_size size of elementes 100 * 101 * @return pointer to (n*elem_size) byte zero initialized buffer or NULL 102 * on allocation failure 103 */ 104void *cvmx_calloc(cvmx_arena_list_t arena_list, size_t n, size_t elem_size); 105/** 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. 109 * 110 * @param arena_list arena list to allocate from 111 * @param ptr pointer to buffer to extend 112 * @param size new buffer size 113 * 114 * @return pointer to expanded buffer (may differ from ptr) 115 * or NULL on failure 116 */ 117void *cvmx_realloc(cvmx_arena_list_t arena_list, void *ptr, size_t size); 118/** 119 * allocate a buffer with a specified alignment 120 * 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 124 * 125 * @return pointer to buffer on success 126 * NULL on failure 127 */ 128void *cvmx_memalign(cvmx_arena_list_t arena_list, size_t alignment, size_t bytes); 129/** 130 * free a previously allocated buffer 131 * 132 * @param ptr pointer of buffer to deallocate 133 */ 134void cvmx_free(void *ptr); 135#endif 136 137 138 139 140#define CVMX_ZONE_OVERHEAD (64) 141/** Zone allocator definitions 142 * 143 */ 144struct cvmx_zone 145{ 146 cvmx_spinlock_t lock; 147 char *baseptr; 148 char *name; 149 void *freelist; 150 uint32_t num_elem; 151 uint32_t elem_size; 152 uint32_t align; 153}; 154typedef struct cvmx_zone * cvmx_zone_t; 155 156static inline uint32_t cvmx_zone_size(cvmx_zone_t zone) 157{ 158 return(zone->elem_size); 159} 160static inline char *cvmx_zone_name(cvmx_zone_t zone) 161{ 162 return(zone->name); 163} 164 165 166#ifndef CVMX_BUILD_FOR_LINUX_USER 167/** 168 * Creates a memory zone for efficient allocation/deallocation of 169 * fixed size memory blocks from a specified memory region. 170 * 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. 178 * 179 * @return pointer to zone on success or 180 * NULL on failure 181 */ 182cvmx_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); 184/** 185 * Creates a memory zone for efficient allocation/deallocation of 186 * fixed size memory blocks from a previously initialized arena list. 187 * 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. 197 * 198 * @return pointer to zone on success or 199 * NULL on failure 200 */ 201cvmx_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); 203#endif 204/** 205 * Allocate a buffer from a memory zone 206 * 207 * @param zone zone to allocate buffer from 208 * @param flags flags (currently unused) 209 * 210 * @return pointer to buffer or NULL on failure 211 */ 212void * cvmx_zone_alloc(cvmx_zone_t zone, uint32_t flags); 213/** 214 * Free a previously allocated buffer 215 * 216 * @param zone zone that buffer was allocated from 217 * @param ptr pointer to buffer to be freed 218 */ 219void cvmx_zone_free(cvmx_zone_t zone, void *ptr); 220 221#ifdef __cplusplus 222} 223#endif 224 225#endif // __CVMX_MALLOC_H__ 226