cvmx-zone.c revision 232812
13584Ssos/***********************license start***************
23584Ssos * Copyright (c) 2003-2010  Cavium Inc. (support@cavium.com). All rights
3230132Suqs * reserved.
43584Ssos *
53584Ssos *
63584Ssos * Redistribution and use in source and binary forms, with or without
73584Ssos * modification, are permitted provided that the following conditions are
83584Ssos * met:
93584Ssos *
103584Ssos *   * Redistributions of source code must retain the above copyright
113584Ssos *     notice, this list of conditions and the following disclaimer.
123584Ssos *
133584Ssos *   * Redistributions in binary form must reproduce the above
143584Ssos *     copyright notice, this list of conditions and the following
153584Ssos *     disclaimer in the documentation and/or other materials provided
1613765Smpp *     with the distribution.
173584Ssos
183584Ssos *   * Neither the name of Cavium Inc. nor the names of
193584Ssos *     its contributors may be used to endorse or promote products
203584Ssos *     derived from this software without specific prior written
213584Ssos *     permission.
223584Ssos
233584Ssos * This Software, including technical data, may be subject to U.S. export  control
243584Ssos * laws, including the U.S. Export Administration Act and its  associated
253584Ssos * regulations, and may be subject to export or import  regulations in other
263584Ssos * countries.
273584Ssos
283584Ssos * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
2950477Speter * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
303584Ssos * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
313584Ssos * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
323584Ssos * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
333584Ssos * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
343584Ssos * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
353584Ssos * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
363584Ssos * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK ARISING OUT OF USE OR
373584Ssos * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
383584Ssos ***********************license end**************************************/
393584Ssos
403584Ssos
413584Ssos
423584Ssos
433584Ssos
443584Ssos
453584Ssos
463584Ssos/**
473584Ssos * @file
483584Ssos *
493584Ssos * Support library for the Zone Allocator.
503584Ssos *
513584Ssos * <hr>$Revision: 70030 $<hr>
523584Ssos */
533584Ssos
543584Ssos
553584Ssos#include "cvmx-config.h"
563584Ssos#include "cvmx.h"
573584Ssos#include "cvmx-spinlock.h"
583584Ssos#include "cvmx-malloc.h"
593584Ssos
603584Ssos
613584Ssos
623584Ssos
633584Ssos#ifndef CVMX_BUILD_FOR_LINUX_USER
643584Ssoscvmx_zone_t cvmx_zone_create_from_addr(char *name, uint32_t elem_size, uint32_t num_elem,
653584Ssos                             void* mem_ptr, uint64_t mem_size, uint32_t flags)
663584Ssos{
673584Ssos    cvmx_zone_t zone;
683584Ssos    unsigned int i;
693584Ssos
703584Ssos    if ((unsigned long)mem_ptr & (sizeof(void *) -1))
713584Ssos    {
723584Ssos        return(NULL);  //invalid alignment
733584Ssos    }
743584Ssos    if (mem_size  < sizeof(struct cvmx_zone) + elem_size * num_elem)
753584Ssos    {
763584Ssos        return(NULL);  // not enough room
773584Ssos    }
783584Ssos
793584Ssos    zone = (cvmx_zone_t) ((char *)mem_ptr + elem_size * num_elem);
803584Ssos    zone->elem_size = elem_size;
813584Ssos    zone->num_elem = num_elem;
823584Ssos    zone->name = name;
833584Ssos    zone->align = 0;  // not used
843584Ssos    zone->baseptr = NULL;
853584Ssos    zone->freelist = NULL;
863584Ssos    zone->lock.value = CVMX_SPINLOCK_UNLOCKED_VAL;
873584Ssos
883584Ssos    zone->baseptr = (char *)mem_ptr;
893584Ssos
903584Ssos    for(i=0;i<num_elem;i++)
913584Ssos    {
923584Ssos        *(void **)(zone->baseptr + (i*elem_size)) = zone->freelist;
933584Ssos        zone->freelist = (void *)(zone->baseptr + (i*elem_size));
943584Ssos    }
953584Ssos
963584Ssos    return(zone);
973584Ssos
983584Ssos}
993584Ssos
1003584Ssoscvmx_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)
1013584Ssos{
1023584Ssos    unsigned int i;
1033584Ssos    cvmx_zone_t zone;
1043584Ssos
1053584Ssos    zone = (cvmx_zone_t)cvmx_malloc(arena_list, sizeof(struct cvmx_zone));
1063584Ssos
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