1 |
elmex |
1.1 |
/* |
2 |
root |
1.28 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
root |
1.23 |
* |
4 |
root |
1.43 |
* Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
root |
1.23 |
* |
6 |
root |
1.39 |
* Deliantra is free software: you can redistribute it and/or modify it under |
7 |
|
|
* the terms of the Affero GNU General Public License as published by the |
8 |
|
|
* Free Software Foundation, either version 3 of the License, or (at your |
9 |
|
|
* option) any later version. |
10 |
root |
1.23 |
* |
11 |
root |
1.24 |
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
root |
1.23 |
* |
16 |
root |
1.39 |
* You should have received a copy of the Affero GNU General Public License |
17 |
|
|
* and the GNU General Public License along with this program. If not, see |
18 |
|
|
* <http://www.gnu.org/licenses/>. |
19 |
root |
1.23 |
* |
20 |
root |
1.28 |
* The authors can be reached via e-mail to <support@deliantra.net> |
21 |
root |
1.22 |
*/ |
22 |
|
|
|
23 |
|
|
/* |
24 |
root |
1.3 |
* shstr.C |
25 |
elmex |
1.1 |
*/ |
26 |
|
|
|
27 |
root |
1.3 |
#include <cstring> |
28 |
|
|
#include <cstdlib> |
29 |
root |
1.10 |
#include <glib.h> |
30 |
root |
1.3 |
#include <tr1/unordered_set> |
31 |
elmex |
1.1 |
|
32 |
root |
1.16 |
#include "global.h" |
33 |
elmex |
1.1 |
|
34 |
root |
1.26 |
size_t shstr_alloc; |
35 |
|
|
|
36 |
root |
1.31 |
typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *> > HT; |
37 |
elmex |
1.1 |
|
38 |
root |
1.16 |
static HT ht; |
39 |
root |
1.29 |
static int next_gc; |
40 |
elmex |
1.1 |
|
41 |
root |
1.41 |
#define NUM_INT 3 |
42 |
|
|
|
43 |
root |
1.13 |
static const char * |
44 |
|
|
makevec (const char *s) |
45 |
root |
1.7 |
{ |
46 |
root |
1.17 |
int len = strlen (s); |
47 |
root |
1.41 |
int alloc = sizeof (uint32_t) * NUM_INT + len + 1; |
48 |
root |
1.7 |
|
49 |
root |
1.38 |
shstr_alloc += alloc; |
50 |
|
|
char *v = (char *)g_slice_alloc (alloc); |
51 |
root |
1.41 |
v += sizeof (uint32_t) * NUM_INT; |
52 |
root |
1.7 |
|
53 |
root |
1.41 |
shstr::hash (v) = strhsh (s); |
54 |
root |
1.7 |
shstr::length (v) = len; |
55 |
|
|
shstr::refcnt (v) = 1; |
56 |
|
|
|
57 |
root |
1.38 |
memcpy (v, s, len + 1); |
58 |
root |
1.7 |
|
59 |
root |
1.8 |
return v; |
60 |
root |
1.7 |
} |
61 |
|
|
|
62 |
root |
1.41 |
shstr_vec<sizeof "(null)"> shstr_tmp::nullvec = { STRHSH_NULL, 0, 0xffffffff, "(null)" }; |
63 |
root |
1.15 |
|
64 |
root |
1.45 |
bool |
65 |
|
|
shstr_tmp::contains (const char *substring) const |
66 |
|
|
{ |
67 |
|
|
//TODO: this is supposed to check for comma-seperation... |
68 |
|
|
return s != null () |
69 |
|
|
&& substring |
70 |
|
|
&& strstr (s, substring); |
71 |
|
|
} |
72 |
|
|
|
73 |
elmex |
1.1 |
const char * |
74 |
root |
1.3 |
shstr::find (const char *s) |
75 |
|
|
{ |
76 |
root |
1.4 |
if (!s) |
77 |
|
|
return s; |
78 |
|
|
|
79 |
root |
1.21 |
auto (i, ht.find (s)); |
80 |
elmex |
1.1 |
|
81 |
root |
1.13 |
return i != ht.end ()? *i : 0; |
82 |
elmex |
1.1 |
} |
83 |
|
|
|
84 |
|
|
const char * |
85 |
root |
1.3 |
shstr::intern (const char *s) |
86 |
|
|
{ |
87 |
root |
1.4 |
if (!s) |
88 |
root |
1.33 |
return null (); |
89 |
elmex |
1.1 |
|
90 |
root |
1.4 |
if (const char *found = find (s)) |
91 |
root |
1.5 |
{ |
92 |
|
|
++refcnt (found); |
93 |
|
|
return found; |
94 |
|
|
} |
95 |
elmex |
1.1 |
|
96 |
root |
1.29 |
--next_gc; |
97 |
root |
1.7 |
s = makevec (s); |
98 |
|
|
ht.insert (s); |
99 |
|
|
return s; |
100 |
elmex |
1.1 |
} |
101 |
|
|
|
102 |
root |
1.5 |
// periodically test refcounts == 0 for a few strings |
103 |
|
|
// this is the ONLY thing that erases stuff from ht. keep it that way. |
104 |
elmex |
1.1 |
void |
105 |
root |
1.3 |
shstr::gc () |
106 |
|
|
{ |
107 |
root |
1.29 |
if (expect_true (next_gc > 0)) |
108 |
|
|
return; |
109 |
|
|
|
110 |
root |
1.5 |
static const char *curpos; |
111 |
|
|
|
112 |
root |
1.21 |
auto (i, curpos ? ht.find (curpos) : ht.begin ()); |
113 |
root |
1.5 |
|
114 |
|
|
if (i == ht.end ()) |
115 |
|
|
i = ht.begin (); |
116 |
|
|
|
117 |
root |
1.11 |
int n = ht.size () / 256 + 16; |
118 |
root |
1.29 |
next_gc += n >> 1; |
119 |
root |
1.6 |
|
120 |
|
|
for (;;) |
121 |
root |
1.5 |
{ |
122 |
|
|
if (i == ht.end ()) |
123 |
|
|
{ |
124 |
|
|
curpos = 0; |
125 |
|
|
return; |
126 |
|
|
} |
127 |
root |
1.6 |
else if (!--n) |
128 |
|
|
break; |
129 |
|
|
else if (!refcnt (*i)) |
130 |
root |
1.5 |
{ |
131 |
root |
1.21 |
auto (o, i++); |
132 |
root |
1.5 |
const char *s = *o; |
133 |
root |
1.13 |
|
134 |
root |
1.5 |
ht.erase (o); |
135 |
|
|
|
136 |
|
|
//printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |
137 |
root |
1.41 |
int alloc = sizeof (uint32_t) * NUM_INT + length (s) + 1; |
138 |
root |
1.38 |
shstr_alloc -= alloc; |
139 |
root |
1.41 |
g_slice_free1 (alloc, (void *)(s - sizeof (uint32_t) * NUM_INT)); |
140 |
root |
1.5 |
} |
141 |
|
|
else |
142 |
|
|
++i; |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
curpos = *i; |
146 |
elmex |
1.1 |
} |
147 |
|
|
|
148 |
root |
1.25 |
// declare these here to get correct initialisation order |
149 |
root |
1.37 |
#define def2(id,str) const shstr id (str); |
150 |
|
|
#define def(id) def2(shstr_ ## id, # id) |
151 |
root |
1.25 |
# include "shstrinc.h" |
152 |
|
|
#undef def |
153 |
root |
1.36 |
#undef def2 |
154 |
root |
1.25 |
|
155 |
root |
1.42 |
materialtype_t material_null; |
156 |
root |
1.16 |
|