| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#include "pa_allocation.h" |
| 39 |
#include "pa_util.h" |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
#define PA_INITIAL_LINK_COUNT_ 16 |
| 53 |
|
| 54 |
struct PaUtilAllocationGroupLink |
| 55 |
{ |
| 56 |
struct PaUtilAllocationGroupLink *next; |
| 57 |
void *buffer; |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
static struct PaUtilAllocationGroupLink *AllocateLinks( long count, |
| 67 |
struct PaUtilAllocationGroupLink *nextBlock, |
| 68 |
struct PaUtilAllocationGroupLink *nextSpare ) |
| 69 |
{ |
| 70 |
struct PaUtilAllocationGroupLink *result; |
| 71 |
int i; |
| 72 |
|
| 73 |
result = (struct PaUtilAllocationGroupLink *)PaUtil_AllocateMemory( |
| 74 |
sizeof(struct PaUtilAllocationGroupLink) * count ); |
| 75 |
if( result ) |
| 76 |
{ |
| 77 |
|
| 78 |
result[0].buffer = result; |
| 79 |
result[0].next = nextBlock; |
| 80 |
|
| 81 |
|
| 82 |
for( i=1; i<count; ++i ) |
| 83 |
{ |
| 84 |
result[i].buffer = 0; |
| 85 |
result[i].next = &result[i+1]; |
| 86 |
} |
| 87 |
result[count-1].next = nextSpare; |
| 88 |
} |
| 89 |
|
| 90 |
return result; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
PaUtilAllocationGroup* PaUtil_CreateAllocationGroup( void ) |
| 95 |
{ |
| 96 |
PaUtilAllocationGroup* result = 0; |
| 97 |
struct PaUtilAllocationGroupLink *links; |
| 98 |
|
| 99 |
|
| 100 |
links = AllocateLinks( PA_INITIAL_LINK_COUNT_, 0, 0 ); |
| 101 |
if( links != 0 ) |
| 102 |
{ |
| 103 |
result = (PaUtilAllocationGroup*)PaUtil_AllocateMemory( sizeof(PaUtilAllocationGroup) ); |
| 104 |
if( result ) |
| 105 |
{ |
| 106 |
result->linkCount = PA_INITIAL_LINK_COUNT_; |
| 107 |
result->linkBlocks = &links[0]; |
| 108 |
result->spareLinks = &links[1]; |
| 109 |
result->allocations = 0; |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
PaUtil_FreeMemory( links ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return result; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
void PaUtil_DestroyAllocationGroup( PaUtilAllocationGroup* group ) |
| 122 |
{ |
| 123 |
struct PaUtilAllocationGroupLink *current = group->linkBlocks; |
| 124 |
struct PaUtilAllocationGroupLink *next; |
| 125 |
|
| 126 |
while( current ) |
| 127 |
{ |
| 128 |
next = current->next; |
| 129 |
PaUtil_FreeMemory( current->buffer ); |
| 130 |
current = next; |
| 131 |
} |
| 132 |
|
| 133 |
PaUtil_FreeMemory( group ); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
void* PaUtil_GroupAllocateMemory( PaUtilAllocationGroup* group, long size ) |
| 138 |
{ |
| 139 |
struct PaUtilAllocationGroupLink *links, *link; |
| 140 |
void *result = 0; |
| 141 |
|
| 142 |
|
| 143 |
if( !group->spareLinks ) |
| 144 |
{ |
| 145 |
|
| 146 |
links = AllocateLinks( group->linkCount, group->linkBlocks, group->spareLinks ); |
| 147 |
if( links ) |
| 148 |
{ |
| 149 |
group->linkCount += group->linkCount; |
| 150 |
group->linkBlocks = &links[0]; |
| 151 |
group->spareLinks = &links[1]; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
if( group->spareLinks ) |
| 156 |
{ |
| 157 |
result = PaUtil_AllocateMemory( size ); |
| 158 |
if( result ) |
| 159 |
{ |
| 160 |
link = group->spareLinks; |
| 161 |
group->spareLinks = link->next; |
| 162 |
|
| 163 |
link->buffer = result; |
| 164 |
link->next = group->allocations; |
| 165 |
|
| 166 |
group->allocations = link; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
return result; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
void PaUtil_GroupFreeMemory( PaUtilAllocationGroup* group, void *buffer ) |
| 175 |
{ |
| 176 |
struct PaUtilAllocationGroupLink *current = group->allocations; |
| 177 |
struct PaUtilAllocationGroupLink *previous = 0; |
| 178 |
|
| 179 |
if( buffer == 0 ) |
| 180 |
return; |
| 181 |
|
| 182 |
|
| 183 |
while( current ) |
| 184 |
{ |
| 185 |
if( current->buffer == buffer ) |
| 186 |
{ |
| 187 |
if( previous ) |
| 188 |
{ |
| 189 |
previous->next = current->next; |
| 190 |
} |
| 191 |
else |
| 192 |
{ |
| 193 |
group->allocations = current->next; |
| 194 |
} |
| 195 |
|
| 196 |
current->buffer = 0; |
| 197 |
current->next = group->spareLinks; |
| 198 |
group->spareLinks = current; |
| 199 |
|
| 200 |
break; |
| 201 |
} |
| 202 |
|
| 203 |
previous = current; |
| 204 |
current = current->next; |
| 205 |
} |
| 206 |
|
| 207 |
PaUtil_FreeMemory( buffer ); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
void PaUtil_FreeAllAllocations( PaUtilAllocationGroup* group ) |
| 212 |
{ |
| 213 |
struct PaUtilAllocationGroupLink *current = group->allocations; |
| 214 |
struct PaUtilAllocationGroupLink *previous = 0; |
| 215 |
|
| 216 |
|
| 217 |
while( current ) |
| 218 |
{ |
| 219 |
PaUtil_FreeMemory( current->buffer ); |
| 220 |
current->buffer = 0; |
| 221 |
|
| 222 |
previous = current; |
| 223 |
current = current->next; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
if( previous ) |
| 228 |
{ |
| 229 |
previous->next = group->spareLinks; |
| 230 |
group->spareLinks = group->allocations; |
| 231 |
group->allocations = 0; |
| 232 |
} |
| 233 |
} |
| 234 |
|