|
| 1 | +/* |
| 2 | +** SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008) |
| 3 | +** Copyright (C) [dates of first publication] Silicon Graphics, Inc. |
| 4 | +** All Rights Reserved. |
| 5 | +** |
| 6 | +** Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +** of this software and associated documentation files (the "Software"), to deal |
| 8 | +** in the Software without restriction, including without limitation the rights |
| 9 | +** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 10 | +** of the Software, and to permit persons to whom the Software is furnished to do so, |
| 11 | +** subject to the following conditions: |
| 12 | +** |
| 13 | +** The above copyright notice including the dates of first publication and either this |
| 14 | +** permission notice or a reference to http://oss.sgi.com/projects/FreeB/ shall be |
| 15 | +** included in all copies or substantial portions of the Software. |
| 16 | +** |
| 17 | +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 18 | +** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 19 | +** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL SILICON GRAPHICS, INC. |
| 20 | +** BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 21 | +** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE |
| 22 | +** OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | +** |
| 24 | +** Except as contained in this notice, the name of Silicon Graphics, Inc. shall not |
| 25 | +** be used in advertising or otherwise to promote the sale, use or other dealings in |
| 26 | +** this Software without prior written authorization from Silicon Graphics, Inc. |
| 27 | +*/ |
| 28 | +/* |
| 29 | +** Author: Mikko Mononen, July 2009. |
| 30 | +*/ |
| 31 | + |
| 32 | +#include <stdio.h> |
| 33 | +#include <stdlib.h> |
| 34 | +#include "tesselator.h" |
| 35 | + |
| 36 | +//#define CHECK_BOUNDS |
| 37 | + |
| 38 | +typedef struct BucketAlloc BucketAlloc; |
| 39 | +typedef struct Bucket Bucket; |
| 40 | + |
| 41 | +struct Bucket |
| 42 | +{ |
| 43 | + Bucket *next; |
| 44 | +}; |
| 45 | + |
| 46 | +struct BucketAlloc |
| 47 | +{ |
| 48 | + void *freelist; |
| 49 | + Bucket *buckets; |
| 50 | + unsigned int itemSize; |
| 51 | + unsigned int bucketSize; |
| 52 | + const char *name; |
| 53 | + TESSalloc* alloc; |
| 54 | +}; |
| 55 | + |
| 56 | +static int CreateBucket( struct BucketAlloc* ba ) |
| 57 | +{ |
| 58 | + size_t size; |
| 59 | + Bucket* bucket; |
| 60 | + void* freelist; |
| 61 | + unsigned char* head; |
| 62 | + unsigned char* it; |
| 63 | + |
| 64 | + // Allocate memory for the bucket |
| 65 | + size = sizeof(Bucket) + ba->itemSize * ba->bucketSize; |
| 66 | + bucket = (Bucket*)ba->alloc->memalloc( ba->alloc->userData, size ); |
| 67 | + if ( !bucket ) |
| 68 | + return 0; |
| 69 | + bucket->next = 0; |
| 70 | + |
| 71 | + // Add the bucket into the list of buckets. |
| 72 | + bucket->next = ba->buckets; |
| 73 | + ba->buckets = bucket; |
| 74 | + |
| 75 | + // Add new items to the free list. |
| 76 | + freelist = ba->freelist; |
| 77 | + head = (unsigned char*)bucket + sizeof(Bucket); |
| 78 | + it = head + ba->itemSize * ba->bucketSize; |
| 79 | + do |
| 80 | + { |
| 81 | + it -= ba->itemSize; |
| 82 | + // Store pointer to next free item. |
| 83 | + *((void**)it) = freelist; |
| 84 | + // Pointer to next location containing a free item. |
| 85 | + freelist = (void*)it; |
| 86 | + } |
| 87 | + while ( it != head ); |
| 88 | + // Update pointer to next location containing a free item. |
| 89 | + ba->freelist = (void*)it; |
| 90 | + |
| 91 | + return 1; |
| 92 | +} |
| 93 | + |
| 94 | +static void *NextFreeItem( struct BucketAlloc *ba ) |
| 95 | +{ |
| 96 | + return *(void**)ba->freelist; |
| 97 | +} |
| 98 | + |
| 99 | +struct BucketAlloc* createBucketAlloc( TESSalloc* alloc, const char* name, |
| 100 | + unsigned int itemSize, unsigned int bucketSize ) |
| 101 | +{ |
| 102 | + BucketAlloc* ba = (BucketAlloc*)alloc->memalloc( alloc->userData, sizeof(BucketAlloc) ); |
| 103 | + |
| 104 | + ba->alloc = alloc; |
| 105 | + ba->name = name; |
| 106 | + ba->itemSize = itemSize; |
| 107 | + if ( ba->itemSize < sizeof(void*) ) |
| 108 | + ba->itemSize = sizeof(void*); |
| 109 | + ba->bucketSize = bucketSize; |
| 110 | + ba->freelist = 0; |
| 111 | + ba->buckets = 0; |
| 112 | + |
| 113 | + if ( !CreateBucket( ba ) ) |
| 114 | + { |
| 115 | + alloc->memfree( alloc->userData, ba ); |
| 116 | + return 0; |
| 117 | + } |
| 118 | + |
| 119 | + return ba; |
| 120 | +} |
| 121 | + |
| 122 | +void* bucketAlloc( struct BucketAlloc *ba ) |
| 123 | +{ |
| 124 | + void *it; |
| 125 | + |
| 126 | + // If running out of memory, allocate new bucket and update the freelist. |
| 127 | + if ( !ba->freelist || !NextFreeItem( ba ) ) |
| 128 | + { |
| 129 | + if ( !CreateBucket( ba ) ) |
| 130 | + return 0; |
| 131 | + } |
| 132 | + |
| 133 | + // Pop item from in front of the free list. |
| 134 | + it = ba->freelist; |
| 135 | + ba->freelist = NextFreeItem( ba ); |
| 136 | + |
| 137 | + return it; |
| 138 | +} |
| 139 | + |
| 140 | +void bucketFree( struct BucketAlloc *ba, void *ptr ) |
| 141 | +{ |
| 142 | +#ifdef CHECK_BOUNDS |
| 143 | + int inBounds = 0; |
| 144 | + Bucket *bucket; |
| 145 | + |
| 146 | + // Check that the pointer is allocated with this allocator. |
| 147 | + bucket = ba->buckets; |
| 148 | + while ( bucket ) |
| 149 | + { |
| 150 | + void *bucketMin = (void*)((unsigned char*)bucket + sizeof(Bucket)); |
| 151 | + void *bucketMax = (void*)((unsigned char*)bucket + sizeof(Bucket) + ba->itemSize * ba->bucketSize); |
| 152 | + if ( ptr >= bucketMin && ptr < bucketMax ) |
| 153 | + { |
| 154 | + inBounds = 1; |
| 155 | + break; |
| 156 | + } |
| 157 | + bucket = bucket->next; |
| 158 | + } |
| 159 | + |
| 160 | + if ( inBounds ) |
| 161 | + { |
| 162 | + // Add the node in front of the free list. |
| 163 | + *(void**)ptr = ba->freelist; |
| 164 | + ba->freelist = ptr; |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + printf("ERROR! pointer 0x%p does not belong to allocator '%s'\n", ba->name); |
| 169 | + } |
| 170 | +#else |
| 171 | + // Add the node in front of the free list. |
| 172 | + *(void**)ptr = ba->freelist; |
| 173 | + ba->freelist = ptr; |
| 174 | +#endif |
| 175 | +} |
| 176 | + |
| 177 | +void deleteBucketAlloc( struct BucketAlloc *ba ) |
| 178 | +{ |
| 179 | + TESSalloc* alloc = ba->alloc; |
| 180 | + Bucket *bucket = ba->buckets; |
| 181 | + Bucket *next; |
| 182 | + while ( bucket ) |
| 183 | + { |
| 184 | + next = bucket->next; |
| 185 | + alloc->memfree( alloc->userData, bucket ); |
| 186 | + bucket = next; |
| 187 | + } |
| 188 | + ba->freelist = 0; |
| 189 | + ba->buckets = 0; |
| 190 | + alloc->memfree( alloc->userData, ba ); |
| 191 | +} |
0 commit comments