1 | /* |
1 | /* |
2 | * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
5 | * |
6 | * Crossfire TRT is free software: you can redistribute it and/or modify |
6 | * Deliantra is free software: you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation, either version 3 of the License, or |
8 | * the Free Software Foundation, either version 3 of the License, or |
9 | * (at your option) any later version. |
9 | * (at your option) any later version. |
10 | * |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
11 | * This program is distributed in the hope that it will be useful, |
… | |
… | |
14 | * GNU General Public License for more details. |
14 | * GNU General Public License for more details. |
15 | * |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | * |
18 | * |
19 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
19 | * The authors can be reached via e-mail to <support@deliantra.net> |
20 | */ |
20 | */ |
21 | |
21 | |
22 | /* |
22 | /* |
23 | * shstr.C |
23 | * shstr.C |
24 | */ |
24 | */ |
… | |
… | |
30 | |
30 | |
31 | #include "global.h" |
31 | #include "global.h" |
32 | |
32 | |
33 | size_t shstr_alloc; |
33 | size_t shstr_alloc; |
34 | |
34 | |
35 | typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *>, true> HT; |
35 | typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *> > HT; |
36 | |
36 | |
37 | static HT ht; |
37 | static HT ht; |
|
|
38 | static int next_gc; |
38 | |
39 | |
39 | static const char * |
40 | static const char * |
40 | makevec (const char *s) |
41 | makevec (const char *s) |
41 | { |
42 | { |
42 | int len = strlen (s); |
43 | int len = strlen (s); |
… | |
… | |
83 | { |
84 | { |
84 | ++refcnt (found); |
85 | ++refcnt (found); |
85 | return found; |
86 | return found; |
86 | } |
87 | } |
87 | |
88 | |
|
|
89 | --next_gc; |
88 | s = makevec (s); |
90 | s = makevec (s); |
89 | ht.insert (s); |
91 | ht.insert (s); |
90 | return s; |
92 | return s; |
91 | } |
93 | } |
92 | |
94 | |
93 | // periodically test refcounts == 0 for a few strings |
95 | // periodically test refcounts == 0 for a few strings |
94 | // this is the ONLY thing that erases stuff from ht. keep it that way. |
96 | // this is the ONLY thing that erases stuff from ht. keep it that way. |
95 | void |
97 | void |
96 | shstr::gc () |
98 | shstr::gc () |
97 | { |
99 | { |
|
|
100 | if (expect_true (next_gc > 0)) |
|
|
101 | return; |
|
|
102 | |
98 | static const char *curpos; |
103 | static const char *curpos; |
99 | |
104 | |
100 | auto (i, curpos ? ht.find (curpos) : ht.begin ()); |
105 | auto (i, curpos ? ht.find (curpos) : ht.begin ()); |
101 | |
106 | |
102 | if (i == ht.end ()) |
107 | if (i == ht.end ()) |
103 | i = ht.begin (); |
108 | i = ht.begin (); |
104 | |
109 | |
105 | // go through all strings roughly once every 4 minutes |
|
|
106 | int n = ht.size () / 256 + 16; |
110 | int n = ht.size () / 256 + 16; |
|
|
111 | next_gc += n >> 1; |
107 | |
112 | |
108 | for (;;) |
113 | for (;;) |
109 | { |
114 | { |
110 | if (i == ht.end ()) |
115 | if (i == ht.end ()) |
111 | { |
116 | { |
… | |
… | |
129 | ++i; |
134 | ++i; |
130 | } |
135 | } |
131 | |
136 | |
132 | curpos = *i; |
137 | curpos = *i; |
133 | } |
138 | } |
|
|
139 | |
|
|
140 | const shstr shstr_null; |
134 | |
141 | |
135 | // declare these here to get correct initialisation order |
142 | // declare these here to get correct initialisation order |
136 | #define def(str) const shstr shstr_ ## str (# str); |
143 | #define def(str) const shstr shstr_ ## str (# str); |
137 | # include "shstrinc.h" |
144 | # include "shstrinc.h" |
138 | #undef def |
145 | #undef def |