1 /***********************license start***************
2 * Copyright (c) 2003-2010 Cavium Inc. (support@cavium.com). All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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
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
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**************************************/
49 * Support library for the Zone Allocator.
51 * <hr>$Revision: 70030 $<hr>
55 #include "cvmx-config.h"
57 #include "cvmx-spinlock.h"
58 #include "cvmx-malloc.h"
63 #ifndef CVMX_BUILD_FOR_LINUX_USER
64 cvmx_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)
70 if ((unsigned long)mem_ptr & (sizeof(void *) -1))
72 return(NULL); //invalid alignment
74 if (mem_size < sizeof(struct cvmx_zone) + elem_size * num_elem)
76 return(NULL); // not enough room
79 zone = (cvmx_zone_t) ((char *)mem_ptr + elem_size * num_elem);
80 zone->elem_size = elem_size;
81 zone->num_elem = num_elem;
83 zone->align = 0; // not used
85 zone->freelist = NULL;
86 zone->lock.value = CVMX_SPINLOCK_UNLOCKED_VAL;
88 zone->baseptr = (char *)mem_ptr;
90 for(i=0;i<num_elem;i++)
92 *(void **)(zone->baseptr + (i*elem_size)) = zone->freelist;
93 zone->freelist = (void *)(zone->baseptr + (i*elem_size));
100 cvmx_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)
105 zone = (cvmx_zone_t)cvmx_malloc(arena_list, sizeof(struct cvmx_zone));
111 zone->elem_size = elem_size;
112 zone->num_elem = num_elem;
115 zone->baseptr = NULL;
116 zone->freelist = NULL;
117 zone->lock.value = CVMX_SPINLOCK_UNLOCKED_VAL;
119 zone->baseptr = (char *)cvmx_memalign(arena_list, align, num_elem * elem_size);
120 if (NULL == zone->baseptr)
125 for(i=0;i<num_elem;i++)
127 *(void **)(zone->baseptr + (i*elem_size)) = zone->freelist;
128 zone->freelist = (void *)(zone->baseptr + (i*elem_size));
138 void * cvmx_zone_alloc(cvmx_zone_t zone, uint32_t flags)
142 assert(zone != NULL);
143 assert(zone->baseptr != NULL);
144 cvmx_spinlock_lock(&zone->lock);
146 item = (cvmx_zone_t)zone->freelist;
149 zone->freelist = *(void **)item;
153 // cvmx_dprintf("No more elements in zone %s\n", zone->name);
156 cvmx_spinlock_unlock(&zone->lock);
160 void cvmx_zone_free(cvmx_zone_t zone, void *ptr)
163 assert(zone != NULL);
164 assert(zone->baseptr != NULL);
165 assert((unsigned long)ptr - (unsigned long)zone->baseptr < zone->num_elem * zone->elem_size);
167 cvmx_spinlock_lock(&zone->lock);
168 *(void **)ptr = zone->freelist;
169 zone->freelist = ptr;
170 cvmx_spinlock_unlock(&zone->lock);