]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake2/qdata_heretic2/qcommon/resourcemanager.c
uncrustify! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / tools / quake2 / qdata_heretic2 / qcommon / resourcemanager.c
1 /*
2    Copyright (C) 1999-2007 id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22
23 //
24 // ResourceManager.c
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                 // set current->next to point to next node
66                 *current = (char *)( current ) + resource->nodeSize;
67
68                 // set current node to current->next
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 ); // constructor not called; possibly due to a static object
117         // containing a static ResourceManagerFastLarge member being
118         // constructed before its own static members
119
120         toPop = resource->free;
121
122         // set unallocated to the next node and check for NULL (end of list)
123         if ( !( resource->free = (char **)( *resource->free ) ) ) { // if at end create new block
124                 ResMngr_CreateBlock( resource );
125         }
126
127         // set next to NULL
128         *toPop = NULL;
129
130         // return the resource for the node
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 ); // see same assert at top of AllocateResource
144
145         // set toPop->next to current unallocated front
146         *toPush = (char *)( resource->free );
147
148         // set unallocated to the node removed from allocated
149         resource->free = toPush;
150 }
151
152 // end