cvmx-malloc.h revision 210284
1168515Sgshapiro/***********************license start*************** 264562Sgshapiro * Copyright (c) 2003-2008 Cavium Networks (support@cavium.com). All rights 338032Speter * reserved. 490792Sgshapiro * 5168515Sgshapiro * 666494Sgshapiro * Redistribution and use in source and binary forms, with or without 766494Sgshapiro * modification, are permitted provided that the following conditions are 864562Sgshapiro * met: 966494Sgshapiro * 1038032Speter * * Redistributions of source code must retain the above copyright 1164562Sgshapiro * notice, this list of conditions and the following disclaimer. 1290792Sgshapiro * 1364562Sgshapiro * * Redistributions in binary form must reproduce the above 1490792Sgshapiro * copyright notice, this list of conditions and the following 1564562Sgshapiro * disclaimer in the documentation and/or other materials provided 1638032Speter * with the distribution. 1764562Sgshapiro * 1864562Sgshapiro * * Neither the name of Cavium Networks nor the names of 1964562Sgshapiro * its contributors may be used to endorse or promote products 2038032Speter * derived from this software without specific prior written 2164562Sgshapiro * permission. 2238032Speter * 2364562Sgshapiro * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" 2464562Sgshapiro * AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS 2538032Speter * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH 2664562Sgshapiro * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY 2738032Speter * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT 2838032Speter * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES 2964562Sgshapiro * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR 3038032Speter * PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET 3164562Sgshapiro * POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT 3238032Speter * OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU. 3338032Speter * 3464562Sgshapiro * 3564562Sgshapiro * For any questions regarding licensing please contact marketing@caviumnetworks.com 3638032Speter * 3764562Sgshapiro ***********************license end**************************************/ 3838032Speter 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 59extern "C" { 60#endif 61 62 63struct malloc_state; /* forward declaration */ 64typedef 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 */ 80int 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 */ 90void *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 */ 101void *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 */ 114void *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 */ 125void *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 */ 131void cvmx_free(void *ptr); 132 133 134 135 136#define CVMX_ZONE_OVERHEAD (64) 137/** Zone allocator definitions 138 * 139 */ 140struct 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}; 150typedef struct cvmx_zone * cvmx_zone_t; 151 152static inline uint32_t cvmx_zone_size(cvmx_zone_t zone) 153{ 154 return(zone->elem_size); 155} 156static 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 */ 177cvmx_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 */ 196cvmx_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 */ 206void * 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 */ 213void cvmx_zone_free(cvmx_zone_t zone, void *ptr); 214 215#ifdef __cplusplus 216} 217#endif 218 219#endif // __CVMX_MALLOC_H__ 220