cvmx-zone.c revision 210284
1139826Simp/***********************license start*************** 253541Sshin * Copyright (c) 2003-2008 Cavium Networks (support@cavium.com). All rights 353541Sshin * reserved. 453541Sshin * 553541Sshin * 653541Sshin * Redistribution and use in source and binary forms, with or without 753541Sshin * modification, are permitted provided that the following conditions are 853541Sshin * met: 953541Sshin * 1053541Sshin * * Redistributions of source code must retain the above copyright 1153541Sshin * notice, this list of conditions and the following disclaimer. 1253541Sshin * 1353541Sshin * * Redistributions in binary form must reproduce the above 1453541Sshin * copyright notice, this list of conditions and the following 1553541Sshin * disclaimer in the documentation and/or other materials provided 1653541Sshin * with the distribution. 1753541Sshin * 1853541Sshin * * Neither the name of Cavium Networks nor the names of 1953541Sshin * its contributors may be used to endorse or promote products 2053541Sshin * derived from this software without specific prior written 2153541Sshin * permission. 2253541Sshin * 2353541Sshin * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" 2453541Sshin * AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS 2553541Sshin * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH 2653541Sshin * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY 2753541Sshin * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT 28174510Sobrien * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES 29174510Sobrien * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR 30174510Sobrien * PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET 3153541Sshin * POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT 3253541Sshin * OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU. 3353541Sshin * 3453541Sshin * 3553541Sshin * For any questions regarding licensing please contact marketing@caviumnetworks.com 3653541Sshin * 3753541Sshin ***********************license end**************************************/ 3853541Sshin 3953541Sshin 4053541Sshin 41174510Sobrien 42174510Sobrien 43174510Sobrien 4453541Sshin/** 45253081Sae * @file 46253081Sae * 47253081Sae * Support library for the Zone Allocator. 48253081Sae * 49253081Sae * <hr>$Revision: 41586 $<hr> 50253081Sae */ 51253081Sae 5253541Sshin 5353541Sshin#include "cvmx-config.h" 5462587Sitojun#include "cvmx.h" 55241916Sdelphij#include "cvmx-spinlock.h" 5662587Sitojun#include "cvmx-malloc.h" 5753541Sshin 5853541Sshin 5953541Sshin 6053541Sshin 6162587Sitojuncvmx_zone_t cvmx_zone_create_from_addr(char *name, uint32_t elem_size, uint32_t num_elem, 6262587Sitojun void* mem_ptr, uint64_t mem_size, uint32_t flags) 6353541Sshin{ 6453541Sshin cvmx_zone_t zone; 65 unsigned int i; 66 67 if ((unsigned long)mem_ptr & (sizeof(void *) -1)) 68 { 69 return(NULL); //invalid alignment 70 } 71 if (mem_size < sizeof(struct cvmx_zone) + elem_size * num_elem) 72 { 73 return(NULL); // not enough room 74 } 75 76 zone = (cvmx_zone_t) ((char *)mem_ptr + elem_size * num_elem); 77 zone->elem_size = elem_size; 78 zone->num_elem = num_elem; 79 zone->name = name; 80 zone->align = 0; // not used 81 zone->baseptr = NULL; 82 zone->freelist = NULL; 83 zone->lock.value = CVMX_SPINLOCK_UNLOCKED_VAL; 84 85 zone->baseptr = (char *)mem_ptr; 86 87 for(i=0;i<num_elem;i++) 88 { 89 *(void **)(zone->baseptr + (i*elem_size)) = zone->freelist; 90 zone->freelist = (void *)(zone->baseptr + (i*elem_size)); 91 } 92 93 return(zone); 94 95} 96 97cvmx_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) 98{ 99 unsigned int i; 100 cvmx_zone_t zone; 101 102 zone = (cvmx_zone_t)cvmx_malloc(arena_list, sizeof(struct cvmx_zone)); 103 104 if (NULL == zone) 105 { 106 return(NULL); 107 } 108 zone->elem_size = elem_size; 109 zone->num_elem = num_elem; 110 zone->name = name; 111 zone->align = align; 112 zone->baseptr = NULL; 113 zone->freelist = NULL; 114 zone->lock.value = CVMX_SPINLOCK_UNLOCKED_VAL; 115 116 zone->baseptr = (char *)cvmx_memalign(arena_list, align, num_elem * elem_size); 117 if (NULL == zone->baseptr) 118 { 119 return(NULL); 120 } 121 122 for(i=0;i<num_elem;i++) 123 { 124 *(void **)(zone->baseptr + (i*elem_size)) = zone->freelist; 125 zone->freelist = (void *)(zone->baseptr + (i*elem_size)); 126 } 127 128 return(zone); 129 130} 131 132 133 134void * cvmx_zone_alloc(cvmx_zone_t zone, uint32_t flags) 135{ 136 cvmx_zone_t item; 137 138 assert(zone != NULL); 139 assert(zone->baseptr != NULL); 140 cvmx_spinlock_lock(&zone->lock); 141 142 item = (cvmx_zone_t)zone->freelist; 143 if(item != NULL) 144 { 145 zone->freelist = *(void **)item; 146 } 147 else 148 { 149// cvmx_dprintf("No more elements in zone %s\n", zone->name); 150 } 151 152 cvmx_spinlock_unlock(&zone->lock); 153 return(item); 154} 155 156void cvmx_zone_free(cvmx_zone_t zone, void *ptr) 157{ 158 159 assert(zone != NULL); 160 assert(zone->baseptr != NULL); 161 assert((unsigned long)ptr - (unsigned long)zone->baseptr < zone->num_elem * zone->elem_size); 162 163 cvmx_spinlock_lock(&zone->lock); 164 *(void **)ptr = zone->freelist; 165 zone->freelist = ptr; 166 cvmx_spinlock_unlock(&zone->lock); 167} 168 169 170