| 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 |
#include <stdio.h> |
| 28 |
#include "resourcemanager.h" |
| 29 |
#include <assert.h> |
| 30 |
|
| 31 |
typedef struct ResMngr_Block_s |
| 32 |
{ |
| 33 |
char *start; |
| 34 |
unsigned int size; |
| 35 |
struct ResMngr_Block_s *next; |
| 36 |
} ResMngr_Block_t; |
| 37 |
|
| 38 |
static void ResMngr_CreateBlock( ResourceManager_t *resource ){ |
| 39 |
unsigned int _blockSize; |
| 40 |
char *block; |
| 41 |
char **current; |
| 42 |
ResMngr_Block_t *temp; |
| 43 |
unsigned int i; |
| 44 |
|
| 45 |
_blockSize = resource->nodeSize * resource->resPerBlock; |
| 46 |
|
| 47 |
block = malloc( _blockSize ); |
| 48 |
|
| 49 |
assert( block ); |
| 50 |
|
| 51 |
temp = malloc( sizeof( *temp ) ); |
| 52 |
|
| 53 |
temp->start = block; |
| 54 |
temp->size = _blockSize; |
| 55 |
temp->next = resource->blockList; |
| 56 |
|
| 57 |
resource->blockList = temp; |
| 58 |
|
| 59 |
resource->free = (char **)( block ); |
| 60 |
|
| 61 |
current = resource->free; |
| 62 |
|
| 63 |
for ( i = 1; i < resource->resPerBlock; ++i ) |
| 64 |
{ |
| 65 |
|
| 66 |
*current = (char *)( current ) + resource->nodeSize; |
| 67 |
|
| 68 |
|
| 69 |
current = (char **)( *current ); |
| 70 |
} |
| 71 |
|
| 72 |
*current = NULL; |
| 73 |
} |
| 74 |
|
| 75 |
H2COMMON_API void ResMngr_Con( ResourceManager_t *resource, size_t init_resSize, unsigned int init_resPerBlock, char *resman_name ){ |
| 76 |
resource->resSize = init_resSize; |
| 77 |
|
| 78 |
resource->resPerBlock = init_resPerBlock; |
| 79 |
|
| 80 |
resource->nodeSize = resource->resSize + sizeof( *resource->free ); |
| 81 |
|
| 82 |
resource->blockList = NULL; |
| 83 |
|
| 84 |
resource->numResourcesAllocated = 0; |
| 85 |
|
| 86 |
ResMngr_CreateBlock( resource ); |
| 87 |
} |
| 88 |
|
| 89 |
H2COMMON_API void ResMngr_Des( ResourceManager_t *resource ){ |
| 90 |
ResMngr_Block_t *toDelete; |
| 91 |
|
| 92 |
#if 0 |
| 93 |
if ( resource->numResourcesAllocated ) { |
| 94 |
char mess[100]; |
| 95 |
sprintf( mess,"Potential memory leak %d bytes unfreed\n",resource->resSize * resource->numResourcesAllocated ); |
| 96 |
OutputDebugString( mess ); |
| 97 |
} |
| 98 |
#endif |
| 99 |
|
| 100 |
while ( resource->blockList ) |
| 101 |
{ |
| 102 |
toDelete = resource->blockList; |
| 103 |
resource->blockList = resource->blockList->next; |
| 104 |
free( toDelete->start ); |
| 105 |
free( toDelete ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
H2COMMON_API void *ResMngr_AllocateResource( ResourceManager_t *resource, size_t size ){ |
| 110 |
char **toPop; |
| 111 |
|
| 112 |
assert( size == resource->resSize ); |
| 113 |
|
| 114 |
++resource->numResourcesAllocated; |
| 115 |
|
| 116 |
assert( resource->free ); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
toPop = resource->free; |
| 121 |
|
| 122 |
|
| 123 |
if ( !( resource->free = (char **)( *resource->free ) ) ) { |
| 124 |
ResMngr_CreateBlock( resource ); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
*toPop = NULL; |
| 129 |
|
| 130 |
|
| 131 |
return (void *)( toPop + 1 ); |
| 132 |
} |
| 133 |
|
| 134 |
H2COMMON_API void ResMngr_DeallocateResource( ResourceManager_t *resource, void *toDeallocate, size_t size ){ |
| 135 |
char **toPush; |
| 136 |
|
| 137 |
assert( size == resource->resSize ); |
| 138 |
|
| 139 |
--resource->numResourcesAllocated; |
| 140 |
|
| 141 |
toPush = (char **)( toDeallocate ) - 1; |
| 142 |
|
| 143 |
assert( resource->free ); |
| 144 |
|
| 145 |
|
| 146 |
*toPush = (char *)( resource->free ); |
| 147 |
|
| 148 |
|
| 149 |
resource->free = toPush; |
| 150 |
} |
| 151 |
|
| 152 |
|