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/** 47 * @file 48 * 49 * Support library for the Zone Allocator. 50 * 51 * <hr>$Revision: 70030 $<hr> 52 */ 53 54 55#include "cvmx-config.h" 56#include "cvmx.h" 57#include "cvmx-spinlock.h" 58#include "cvmx-malloc.h" 59 60 61 62 63#ifndef CVMX_BUILD_FOR_LINUX_USER 64cvmx_zone_t cvmx_zone_create_from_addr(char *name, uint32_t elem_size, uint32_t num_elem, 65 void* mem_ptr, uint64_t mem_size, uint32_t flags) 66{ 67 cvmx_zone_t zone; 68 unsigned int i; 69 70 if ((unsigned long)mem_ptr & (sizeof(void *) -1)) 71 { 72 return(NULL); //invalid alignment 73 } 74 if (mem_size < sizeof(struct cvmx_zone) + elem_size * num_elem) 75 { 76 return(NULL); // not enough room 77 } 78 79 zone = (cvmx_zone_t) ((char *)mem_ptr + elem_size * num_elem); 80 zone->elem_size = elem_size; 81 zone->num_elem = num_elem; 82 zone->name = name; 83 zone->align = 0; // not used 84 zone->baseptr = NULL; 85 zone->freelist = NULL; 86 zone->lock.value = CVMX_SPINLOCK_UNLOCKED_VAL; 87 88 zone->baseptr = (char *)mem_ptr; 89 90 for(i=0;i<num_elem;i++) 91 { 92 *(void **)(zone->baseptr + (i*elem_size)) = zone->freelist; 93 zone->freelist = (void *)(zone->baseptr + (i*elem_size)); 94 } 95 96 return(zone); 97 98} 99 100cvmx_zone_t cvmx_zone_create_from_arena(char *name, uint32_t elem_size, uint32_t num_elem, uint32_t align, cvmx_arena_list_t arena_list, uint32_t flags) 101{ 102 unsigned int i; 103 cvmx_zone_t zone; 104 105 zone = (cvmx_zone_t)cvmx_malloc(arena_list, sizeof(struct cvmx_zone)); 106 107 if (NULL == zone) 108 { 109 return(NULL); 110 } 111 zone->elem_size = elem_size; 112 zone->num_elem = num_elem; 113 zone->name = name; 114 zone->align = align; 115 zone->baseptr = NULL; 116 zone->freelist = NULL; 117 zone->lock.value = CVMX_SPINLOCK_UNLOCKED_VAL; 118 119 zone->baseptr = (char *)cvmx_memalign(arena_list, align, num_elem * elem_size); 120 if (NULL == zone->baseptr) 121 { 122 return(NULL); 123 } 124 125 for(i=0;i<num_elem;i++) 126 { 127 *(void **)(zone->baseptr + (i*elem_size)) = zone->freelist; 128 zone->freelist = (void *)(zone->baseptr + (i*elem_size)); 129 } 130 131 return(zone); 132 133} 134#endif 135 136 137 138void * cvmx_zone_alloc(cvmx_zone_t zone, uint32_t flags) 139{ 140 cvmx_zone_t item; 141 142 assert(zone != NULL); 143 assert(zone->baseptr != NULL); 144 cvmx_spinlock_lock(&zone->lock); 145 146 item = (cvmx_zone_t)zone->freelist; 147 if(item != NULL) 148 { 149 zone->freelist = *(void **)item; 150 } 151 else 152 { 153// cvmx_dprintf("No more elements in zone %s\n", zone->name); 154 } 155 156 cvmx_spinlock_unlock(&zone->lock); 157 return(item); 158} 159 160void cvmx_zone_free(cvmx_zone_t zone, void *ptr) 161{ 162 163 assert(zone != NULL); 164 assert(zone->baseptr != NULL); 165 assert((unsigned long)ptr - (unsigned long)zone->baseptr < zone->num_elem * zone->elem_size); 166 167 cvmx_spinlock_lock(&zone->lock); 168 *(void **)ptr = zone->freelist; 169 zone->freelist = ptr; 170 cvmx_spinlock_unlock(&zone->lock); 171} 172 173 174