[go: up one dir, main page]

Menu

[904618]: / src / z_util.c  Maximize  Restore  History

Download this file

327 lines (261 with data), 8.3 kB

  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
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-
// ----------------------------------------------------------------------------
// :oCCCCOCoc.
// .cCO8OOOOOOOOO8Oo:
// .oOO8OOOOOOOOOOOOOOOCc
// cO8888: .:oOOOOC. TM
// :888888: :CCCc .oOOOOC. ### ### #########
// C888888: .ooo: .C######## ##### ##### ###### ###### ##########
// O888888: .oO### ### ##### ##### ######## ######## #### ###
// C888888: :8O. .C########## ### #### ### ## ## ## ## #### ###
// :8@@@@8: :888c o### ### #### ### ######## ######## ##########
// :8@@@@C C@@@@ oo######## ### ## ### ###### ###### #########
// cO@@@@@@@@@@@@@@@@@Oc0
// :oO8@@@@@@@@@@Oo.
// .oCOOOOOCc. http://remood.org/
// ----------------------------------------------------------------------------
// Copyright (C) 2011-2013 GhostlyDeath <ghostlydeath@remood.org>
// <ghostlydeath@gmail.com>
// ----------------------------------------------------------------------------
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// ----------------------------------------------------------------------------
// DESCRIPTION: New New Memory Manager Written by GhostlyDeath
// Stolen from my proprietary game and stripped
/***************
*** INCLUDES ***
***************/
#include "z_zone.h"
#include "i_system.h"
/***********************
*** COMMON FUNCTIONS ***
***********************/
/* Z_ResizeArrayWrappee() -- Resizes an array */
void Z_ResizeArrayWrappee(void** const PtrPtr, const size_t ElemSize, const size_t OldSize, const size_t NewSize _ZMGD_WRAPPEE)
{
void* Temp;
Z_MemoryTag_t OldTag;
/* Check */
if (!PtrPtr || !ElemSize || OldSize == NewSize)
return;
/* Free? */
// Only if the newsize is zero
if (!NewSize)
{
Z_Free(*PtrPtr);
*PtrPtr = NULL;
return;
}
/* Change info from PtrPtr */
// Find the old tag
if (*PtrPtr)
{
OldTag = Z_GetTagFromPtr(*PtrPtr);
// Force tag to be static
Z_ChangeTag(*PtrPtr, PU_STATIC);
}
else
OldTag = PU_STATIC;
/* Allocate temp for new size */
Temp = Z_MallocExWrappee(ElemSize * NewSize, PU_STATIC, NULL, 0
#if defined(_DEBUG)
, File, Line
#endif
);
/* If *PtrPtr is set then manage it */
if (*PtrPtr)
{
// Copy file:line
#if defined(_DEBUG)
// Z_DupFileLine(Temp, *PtrPtr);
#endif
// Copy all the old data
memmove(Temp, *PtrPtr, ElemSize * (NewSize < OldSize ? NewSize : OldSize));
// Free it
Z_Free(*PtrPtr);
}
/* Set *PtrPtr to temp */
*PtrPtr = Temp;
// Restore the old tag
Z_ChangeTag(*PtrPtr, OldTag);
}
/* Z_StrDupWrappee() -- Duplicate String */
char* Z_StrDupWrappee(const char* const String, const Z_MemoryTag_t Tag, void** Ref _ZMGD_WRAPPEE)
{
size_t n;
char* Ptr;
/* Check */
if (!String)
return NULL;
/* Copy */
n = strlen(String);
#if defined(_DEBUG)
Ptr = Z_MallocExWrappee(sizeof(char) * (n + 1), Tag, Ref, 0, File, -Line);
#else
Ptr = Z_Malloc(sizeof(char) * (n + 1), Tag, Ref);
#endif
strcpy(Ptr, String);
/* Return */
return Ptr;
}
/* Z_StrDup() -- Duplicate a string */
char* Z_Strdup(const char* const String, const Z_MemoryTag_t Tag, void** Ref)
{
return Z_StrDup(String, Tag, Ref);
}
/*******************
*** HASH UTILITY ***
*******************/
/*** STRUCTURES ***/
/* Z_HashKey_t -- A single key in a hash table */
typedef struct Z_HashKey_s
{
uint32_t Key;
void* Data;
} Z_HashKey_t;
/* Z_HashTable_s -- A hash table */
struct Z_HashTable_s
{
Z_HashKey_t* KeyList[256]; // Key list
size_t KeySize[256]; // Keys in list
bool_t (*CompareFunc) (void* const a_A, void* const a_B);
};
/*** FUNCTIONS ***/
/* Z_Hash() -- Hashes a string */
// The input string is case insensitive
uint32_t Z_Hash(const char* const a_Str)
{
register int i;
uint32_t Val;
uint32_t Ret = 0;
static const uint32_t c_Primes[16] =
{
0, 1, 2, 3, 5, 7, 11, 13, 17, 19, // Base
17, 13, 11, 7, 5, 3 // For Masking
};
/* Check */
if (!a_Str)
return 0;
/* Hash loop */
// A = 0x41 Z = 0x5A
// a = 0x61 z = 0x7A
// Diff is 32, or 0x20
for (i = 0; a_Str[i]; i++)
{
Val = toupper(a_Str[i]) & 0x7F;
if ((i & 1) == 0)
Val = ~Val;
// Modify return value
Ret ^= (Val << c_Primes[i & 15]);// | (Val << c_Primes[31 - (Val & 7)]);
}
/* Return */
return Ret;
}
/* Z_HashCreateTable() -- Creates a new hashing table */
Z_HashTable_t* Z_HashCreateTable(bool_t (*a_CompareFunc) (void* const a_A, void* const a_B))
{
Z_HashTable_t* New;
/* Allocate and function */
New = Z_Malloc(sizeof(*New), PU_STATIC, NULL);
New->CompareFunc = a_CompareFunc;
/* Return new table */
return New;
}
/* Z_HashDeleteTable() -- Deletes a hash table */
void Z_HashDeleteTable(Z_HashTable_t* const a_HashTable)
{
size_t i;
/* Check */
if (!a_HashTable)
return;
/* Free it */
for (i = 0; i < 256; i++)
if (a_HashTable->KeyList[i])
Z_Free(a_HashTable->KeyList[i]);
Z_Free(a_HashTable);
}
/* Z_HashAddEntry() -- Adds a single entry to the hash table */
bool_t Z_HashAddEntry(Z_HashTable_t* const a_HashTable, const uint32_t a_Key, void* const a_Data)
{
uint32_t Nub;
size_t i;
/* Check */
if (!a_HashTable || !a_Data)
return false;
/* Get nub of key */
Nub = a_Key & 0xFF;
/* Resize list */
Z_ResizeArray((void**)&a_HashTable->KeyList[Nub], sizeof(Z_HashKey_t), a_HashTable->KeySize[Nub], a_HashTable->KeySize[Nub] + 1);
// Slap at end
i = a_HashTable->KeySize[Nub]++;
a_HashTable->KeyList[Nub][i].Key = a_Key;
a_HashTable->KeyList[Nub][i].Data = a_Data;
/* Success! */
return true;
}
/* Z_HashFindEntry() -- Finds an entry in the hash table */
void* Z_HashFindEntry(Z_HashTable_t* const a_HashTable, const uint32_t a_Key, void* const a_DataSim, const bool_t a_BackRun)
{
uint32_t Nub;
size_t i;
/* Check */
if (!a_HashTable)
return NULL;
/* Get nub */
Nub = a_Key & 0xFF;
/* Seek based on direction */
for (i = (a_BackRun ? a_HashTable->KeySize[Nub] - 1 : 0); i != (a_BackRun ? (size_t) - 1 : a_HashTable->KeySize[Nub]); i = (a_BackRun ? i - 1 : i + 1))
{
// Compare for key equality
if (a_Key == a_HashTable->KeyList[Nub][i].Key)
{
// If there is a compare function and data is being passed
if (a_HashTable->CompareFunc && a_DataSim)
if (!a_HashTable->CompareFunc(a_DataSim, a_HashTable->KeyList[Nub][i].Data))
continue;
// Found it!
return a_HashTable->KeyList[Nub][i].Data;
}
}
/* Found nothing */
return NULL;
}
/* Z_HashDeleteEntry() -- Deletes the found entry from the hash table */
bool_t Z_HashDeleteEntry(Z_HashTable_t* const a_HashTable, const uint32_t a_Key, void* const a_DataSim, const bool_t a_BackRun)
{
uint32_t Nub;
size_t i;
/* Check */
if (!a_HashTable)
return true;
/* Get nub */
Nub = a_Key & 0xFF;
/* Seek based on direction */
for (i = (a_BackRun ? a_HashTable->KeySize[Nub] - 1 : 0); i != (a_BackRun ? (size_t) - 1 : a_HashTable->KeySize[Nub]); i = (a_BackRun ? i - 1 : i + 1))
{
// Compare for key equality
if (a_Key == a_HashTable->KeyList[Nub][i].Key)
{
// If there is a compare function and data is being passed
if (a_HashTable->CompareFunc && a_DataSim)
if (!a_HashTable->CompareFunc(a_DataSim, a_HashTable->KeyList[Nub][i].Data))
continue;
// Smash the table
memmove(&a_HashTable->KeyList[Nub][i], &a_HashTable->KeyList[Nub][i + 1], a_HashTable->KeySize[Nub] - i - 1);
// Shrink array
Z_ResizeArray((void**)&a_HashTable->KeyList[Nub], sizeof(Z_HashKey_t), a_HashTable->KeySize[Nub], a_HashTable->KeySize[Nub] - 1);
// Shrink count
a_HashTable->KeySize[Nub]--;
return true;
}
}
/* Found nothing */
return false;
}