forked from cotillion/cd-gamedriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mstring.c
424 lines (371 loc) · 11.1 KB
/
mstring.c
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*-
* Copyright (c) 1996 Dave Richards <[email protected]>
* Copyright (c) 1996 Thorsten Lockert <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* and exceptions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* 4. The code can not be used by Gary Random, Random Communications, Inc.,
* the employees of Random Communications, Inc. or its subsidiaries,
* including Defiance MUD, without prior written permission from the
* authors.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <sys/types.h>
#include "config.h"
#include "lint.h"
#include "mstring.h"
#include "hash.h"
#include "simulate.h"
int num_distinct_strings_shared;
long bytes_distinct_strings_shared;
long overhead_bytes_shared;
int num_distinct_strings_malloced;
long bytes_distinct_strings_malloced;
long overhead_bytes_malloced;
static long long search_len;
static long long num_str_searches;
static long allocd_bytes_malloced;
static int allocd_strings_malloced;
static long allocd_bytes_shared;
static int allocd_strings_shared;
static char *sstring_table[HTABLE_SIZE];
/* 1 G characters should be big enough */
#define MAX_STRING_SIZE (1<<30)
char *
allocate_mstring(size_t len)
{
char *cp;
if (len < 0 || len > MAX_STRING_SIZE)
error("Illegal string size.\n");
cp = (char *)xalloc(mstring_header + len + 1) + mstring_header;
#ifdef DEBUG
mstring_magic(cp) = MSTRING_MAGIC;
#endif
mstring_count(cp) = 1;
mstring_len(cp) = len;
allocd_strings_malloced++;
allocd_bytes_malloced += mstring_header + len + 1;
num_distinct_strings_malloced++;
bytes_distinct_strings_malloced += len + 1;
overhead_bytes_malloced += mstring_header;
return cp;
}
char *
make_mstring(const char *cp)
{
size_t len;
char *xp;
len = strlen(cp);
xp = allocate_mstring(len);
(void)memcpy(xp, cp, len + 1);
return xp;
}
char *
reference_mstring(char *cp)
{
#ifdef DEBUG
if (mstring_magic(cp) != MSTRING_MAGIC)
fatal("Bad m-magic: %lx %x\n", mstring_magic(cp), MSTRING_MAGIC);
#endif
if (mstring_count(cp) != 0)
mstring_count(cp)++;
allocd_strings_malloced++;
allocd_bytes_malloced += mstring_header + mstring_len(cp) + 1;
return cp;
}
void
free_mstring(char *cp)
{
#ifdef DEBUG
if (mstring_magic(cp) != MSTRING_MAGIC)
fatal("Bad m-magic: %lx %x\n", mstring_magic(cp), MSTRING_MAGIC);
#endif
if (mstring_count(cp) != 0) {
allocd_strings_malloced--;
allocd_bytes_malloced -= mstring_header + mstring_len(cp) + 1;
if (--mstring_count(cp) == 0)
{
#ifdef DEBUG
mstring_magic(cp) = 0;
#endif
num_distinct_strings_malloced--;
bytes_distinct_strings_malloced -= mstring_len(cp) + 1;
overhead_bytes_malloced -= mstring_header;
free(cp - mstring_header);
}
}
}
char *
reference_sstring(char *cp)
{
#ifdef DEBUG
if (sstring_magic(cp) != SSTRING_MAGIC)
fatal("Bad s-magic: %lx %x\n", sstring_magic(cp), SSTRING_MAGIC);
#endif
if (sstring_count(cp) != 0)
sstring_count(cp)++;
allocd_strings_shared++;
allocd_bytes_shared += sstring_header + sstring_len(cp) + 1;
return cp;
}
char *
find_sstring(char *cp)
{
unsigned short hash;
char *xp;
hash = HASH_SSTRING(cp);
xp = sstring_table[hash];
num_str_searches++;
while (xp != NULL)
{
search_len++;
#ifdef DEBUG
if (sstring_magic(xp) != SSTRING_MAGIC)
fatal("Bad s-magic: %lx %x\n", sstring_magic(xp), SSTRING_MAGIC);
#endif
if (xp == cp || (*xp == *cp && strcmp(xp, cp) == 0))
{
if (sstring_prev(xp) != NULL) {
sstring_next(sstring_prev(xp)) = sstring_next(xp);
if (sstring_next(xp) != NULL)
sstring_prev(sstring_next(xp)) = sstring_prev(xp);
sstring_prev(xp) = NULL;
sstring_next(xp) = sstring_table[hash];
sstring_prev(sstring_table[hash]) = xp;
sstring_table[hash] = xp;
}
return xp;
}
xp = sstring_next(xp);
}
return NULL;
}
char *
make_sstring(const char *cp)
{
size_t len;
unsigned short hash;
char *xp;
hash = HASH_SSTRING(cp);
xp = sstring_table[hash];
num_str_searches++;
while (xp != NULL)
{
search_len++;
#ifdef DEBUG
if (sstring_magic(xp) != SSTRING_MAGIC)
fatal("Bad s-magic: %lx %x\n", sstring_magic(xp), SSTRING_MAGIC);
#endif
if (xp == cp || (*xp == *cp && strcmp(xp, cp) == 0))
{
if (sstring_prev(xp) != NULL) {
sstring_next(sstring_prev(xp)) = sstring_next(xp);
if (sstring_next(xp) != NULL)
sstring_prev(sstring_next(xp)) = sstring_prev(xp);
sstring_prev(xp) = NULL;
sstring_next(xp) = sstring_table[hash];
sstring_prev(sstring_table[hash]) = xp;
sstring_table[hash] = xp;
}
return reference_sstring(xp);
}
xp = sstring_next(xp);
}
len = strlen(cp);
if (len < 0 || len > MAX_STRING_SIZE)
error("Illegal string size.\n");
xp = (char *)xalloc(sstring_header + len + 1) + sstring_header;
#ifdef DEBUG
sstring_magic(xp) = SSTRING_MAGIC;
#endif
sstring_prev(xp) = NULL;
sstring_next(xp) = sstring_table[hash];
if (sstring_next(xp) != NULL)
sstring_prev(sstring_next(xp)) = xp;
sstring_hash(xp) = hash;
sstring_count(xp) = 1;
sstring_len(xp) = len;
(void)memcpy(xp, cp, len + 1);
sstring_table[hash] = xp;
num_distinct_strings_shared++;
bytes_distinct_strings_shared += len + 1;
overhead_bytes_shared += sstring_header;
allocd_strings_shared++;
allocd_bytes_shared += sstring_header + sstring_len(xp) + 1;
return xp;
}
void
free_sstring(char *cp)
{
#ifdef DEBUG
if (sstring_magic(cp) != SSTRING_MAGIC)
fatal("Bad s-magic: %lx %x\n", sstring_magic(cp), SSTRING_MAGIC);
#endif
if (sstring_count(cp) != 0) {
allocd_strings_shared--;
allocd_bytes_shared -= sstring_header + sstring_len(cp) + 1;
if (--sstring_count(cp) == 0)
{
if (sstring_prev(cp) != NULL)
sstring_next(sstring_prev(cp)) = sstring_next(cp);
else
sstring_table[sstring_hash(cp)] = sstring_next(cp);
if (sstring_next(cp) != NULL)
sstring_prev(sstring_next(cp)) = sstring_prev(cp);
#ifdef DEBUG
sstring_magic(cp) = 0;
#endif
num_distinct_strings_shared--;
bytes_distinct_strings_shared -= sstring_len(cp) + 1;
overhead_bytes_shared -= sstring_header;
free(cp - sstring_header);
}
}
}
char *
multiply_string(char *str, long long factor)
{
char *result;
long long size, newsize, offset;
if (factor <= 0 || (size = strlen(str)) == 0) {
return make_mstring("");
}
if (factor > MAX_STRING_SIZE)
error("Illegal string size.\n");
newsize = size * factor;
result = allocate_mstring(newsize);
for (offset = 0; offset < newsize; offset += size) {
strcpy(result + offset, str);
}
return result;
}
void
add_string_status(char *debinf)
{
int i, n;
char *cp;
int min = INT_MAX;
int max = INT_MIN;
long sum1 = 0;
long sum2 = 0;
double mean, stddev;
/*
* Compute the min, max, sum of n and sum of n^2.
*/
for (i = 0; i < HTABLE_SIZE; i++)
{
n = 0;
for (cp = sstring_table[i]; cp != NULL; cp = sstring_next(cp))
n++;
if (min > n)
min = n;
if (max < n)
max = n;
sum1 += n;
sum2 += n * n;
}
if (sum1 == 0)
{
min = 0;
max = 0;
}
/*
* Compute the mean.
*/
mean = (double)sum1 / HTABLE_SIZE;
/*
* Compute the standard deviation.
*/
stddev = sqrt(((double)sum2 -
(((double)sum1 * (double)sum1) / HTABLE_SIZE)) / HTABLE_SIZE);
(void)strcat(debinf, "\nShared string hash table:\n");
(void)strcat(debinf, "------------------------- Strings Bytes\n");
(void)sprintf(debinf + strlen(debinf), "Total asked for\t\t%12d %12ld\n",
allocd_strings_shared, allocd_bytes_shared);
(void)sprintf(debinf + strlen(debinf), "Actually used\t\t%12d %12ld\n",
num_distinct_strings_shared,
bytes_distinct_strings_shared + overhead_bytes_shared);
(void)sprintf(debinf + strlen(debinf),
"Space actually required/total string bytes %6.2f%%\n",
(bytes_distinct_strings_shared + overhead_bytes_shared)*100.0 /
(double)allocd_bytes_shared);
(void)sprintf(debinf + strlen(debinf),
"Searches : %12lld Average search: %7.3f\n",
num_str_searches,
(double)search_len / (double)num_str_searches);
(void)sprintf(debinf + strlen(debinf),
"Hash size : %12d\n", HTABLE_SIZE);
(void)sprintf(debinf + strlen(debinf),
"Minimum depth: %12d Average depth : %7.3f\n",
min, mean);
(void)sprintf(debinf + strlen(debinf),
"Maximum depth: %12d Std. deviation: %7.3f\n",
max, stddev);
(void)strcat(debinf, "\nMalloced string table:\n");
(void)strcat(debinf, "----------------------\t Strings Bytes\n");
(void)sprintf(debinf + strlen(debinf), "Total asked for\t\t%12d %12ld\n",
allocd_strings_malloced, allocd_bytes_malloced);
(void)sprintf(debinf + strlen(debinf), "Actually used\t\t%12d %12ld\n",
num_distinct_strings_malloced,
bytes_distinct_strings_malloced + overhead_bytes_malloced);
(void)sprintf(debinf + strlen(debinf),
"Space actually required/total string bytes %6.2f%%\n",
(bytes_distinct_strings_malloced + overhead_bytes_malloced)*100.0 /
(double)allocd_bytes_malloced);
}
#ifdef DEBUG
void
dump_sstrings(void)
{
char *str;
short len;
FILE *fp;
int i;
if ((fp = fopen("SSTRING_DUMP", "w")) == NULL)
return;
for (i = 0; i < HTABLE_SIZE; i++) {
str = sstring_table[i];
while (str) {
if (sstring_magic(str) != SSTRING_MAGIC)
fatal("Bad s-magic: %lx %x\n", sstring_magic(str), SSTRING_MAGIC);
len = sstring_len(str);
(void)fwrite(&len, sizeof(len), 1, fp);
(void)fwrite(str, sizeof(char), strlen(str) + 1, fp);
str = sstring_next(str);
}
}
(void)fclose(fp);
}
#endif /* DEBUG */
#ifdef DEALLOCATE_MEMORY_AT_SHUTDOWN
void
remove_string_hash()
{
int i;
for (i = 0 ; i < HTABLE_SIZE ; i++)
sstring_table[i] = NULL;
}
#endif /* DEALLOCATE_MEMORY_AT_SHUTDOWN */