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.29 |
* Copyright (©) 2005,2006,2007,2008 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.13 |
static const char * |
42 |
|
|
makevec (const char *s) |
43 |
root |
1.7 |
{ |
44 |
root |
1.17 |
int len = strlen (s); |
45 |
root |
1.38 |
int alloc = sizeof (uint32_t) * 2 + len + 1; |
46 |
root |
1.7 |
|
47 |
root |
1.38 |
shstr_alloc += alloc; |
48 |
|
|
char *v = (char *)g_slice_alloc (alloc); |
49 |
|
|
v += sizeof (uint32_t) * 2; |
50 |
root |
1.7 |
|
51 |
|
|
shstr::length (v) = len; |
52 |
|
|
shstr::refcnt (v) = 1; |
53 |
|
|
|
54 |
root |
1.38 |
memcpy (v, s, len + 1); |
55 |
root |
1.7 |
|
56 |
root |
1.8 |
return v; |
57 |
root |
1.7 |
} |
58 |
|
|
|
59 |
root |
1.33 |
shstr_vec<sizeof "(null)"> shstr_tmp::nullvec = { 0, 0xffffffff, "(null)" }; |
60 |
root |
1.15 |
|
61 |
elmex |
1.1 |
const char * |
62 |
root |
1.3 |
shstr::find (const char *s) |
63 |
|
|
{ |
64 |
root |
1.4 |
if (!s) |
65 |
|
|
return s; |
66 |
|
|
|
67 |
root |
1.21 |
auto (i, ht.find (s)); |
68 |
elmex |
1.1 |
|
69 |
root |
1.13 |
return i != ht.end ()? *i : 0; |
70 |
elmex |
1.1 |
} |
71 |
|
|
|
72 |
|
|
const char * |
73 |
root |
1.3 |
shstr::intern (const char *s) |
74 |
|
|
{ |
75 |
root |
1.4 |
if (!s) |
76 |
root |
1.33 |
return null (); |
77 |
elmex |
1.1 |
|
78 |
root |
1.4 |
if (const char *found = find (s)) |
79 |
root |
1.5 |
{ |
80 |
|
|
++refcnt (found); |
81 |
|
|
return found; |
82 |
|
|
} |
83 |
elmex |
1.1 |
|
84 |
root |
1.29 |
--next_gc; |
85 |
root |
1.7 |
s = makevec (s); |
86 |
|
|
ht.insert (s); |
87 |
|
|
return s; |
88 |
elmex |
1.1 |
} |
89 |
|
|
|
90 |
root |
1.5 |
// periodically test refcounts == 0 for a few strings |
91 |
|
|
// this is the ONLY thing that erases stuff from ht. keep it that way. |
92 |
elmex |
1.1 |
void |
93 |
root |
1.3 |
shstr::gc () |
94 |
|
|
{ |
95 |
root |
1.29 |
if (expect_true (next_gc > 0)) |
96 |
|
|
return; |
97 |
|
|
|
98 |
root |
1.5 |
static const char *curpos; |
99 |
|
|
|
100 |
root |
1.21 |
auto (i, curpos ? ht.find (curpos) : ht.begin ()); |
101 |
root |
1.5 |
|
102 |
|
|
if (i == ht.end ()) |
103 |
|
|
i = ht.begin (); |
104 |
|
|
|
105 |
root |
1.11 |
int n = ht.size () / 256 + 16; |
106 |
root |
1.29 |
next_gc += n >> 1; |
107 |
root |
1.6 |
|
108 |
|
|
for (;;) |
109 |
root |
1.5 |
{ |
110 |
|
|
if (i == ht.end ()) |
111 |
|
|
{ |
112 |
|
|
curpos = 0; |
113 |
|
|
return; |
114 |
|
|
} |
115 |
root |
1.6 |
else if (!--n) |
116 |
|
|
break; |
117 |
|
|
else if (!refcnt (*i)) |
118 |
root |
1.5 |
{ |
119 |
root |
1.21 |
auto (o, i++); |
120 |
root |
1.5 |
const char *s = *o; |
121 |
root |
1.13 |
|
122 |
root |
1.5 |
ht.erase (o); |
123 |
|
|
|
124 |
|
|
//printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |
125 |
root |
1.38 |
int alloc = sizeof (uint32_t) * 2 + length (s) + 1; |
126 |
|
|
shstr_alloc -= alloc; |
127 |
|
|
g_slice_free1 (alloc, (void *)(s - sizeof (uint32_t) * 2)); |
128 |
root |
1.5 |
} |
129 |
|
|
else |
130 |
|
|
++i; |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
curpos = *i; |
134 |
elmex |
1.1 |
} |
135 |
|
|
|
136 |
root |
1.25 |
// declare these here to get correct initialisation order |
137 |
root |
1.37 |
#define def2(id,str) const shstr id (str); |
138 |
|
|
#define def(id) def2(shstr_ ## id, # id) |
139 |
root |
1.25 |
# include "shstrinc.h" |
140 |
|
|
#undef def |
141 |
root |
1.36 |
#undef def2 |
142 |
root |
1.25 |
|
143 |
root |
1.16 |
shstr skill_names[NUM_SKILLS]; |
144 |
|
|
|
145 |
root |
1.5 |
//TODO: this should of course not be here |
146 |
root |
1.13 |
|
147 |
elmex |
1.1 |
/* buf_overflow() - we don't want to exceed the buffer size of |
148 |
|
|
* buf1 by adding on buf2! Returns true if overflow will occur. |
149 |
|
|
*/ |
150 |
root |
1.13 |
int |
151 |
elmex |
1.1 |
buf_overflow (const char *buf1, const char *buf2, int bufsize) |
152 |
|
|
{ |
153 |
root |
1.13 |
int len1 = 0, len2 = 0; |
154 |
elmex |
1.1 |
|
155 |
root |
1.13 |
if (buf1) |
156 |
|
|
len1 = strlen (buf1); |
157 |
root |
1.26 |
|
158 |
root |
1.13 |
if (buf2) |
159 |
|
|
len2 = strlen (buf2); |
160 |
root |
1.26 |
|
161 |
root |
1.13 |
if ((len1 + len2) >= bufsize) |
162 |
|
|
return 1; |
163 |
root |
1.22 |
|
164 |
root |
1.13 |
return 0; |
165 |
elmex |
1.1 |
} |