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); |
43 | |
44 | |
44 | shstr_alloc += sizeof (int) * 2 + len + 1; |
45 | shstr_alloc += sizeof (uint32_t) * 2 + len + 1; |
45 | const char *v = (const char *) (2 + (int *)g_slice_alloc (sizeof (int) * 2 + len + 1)); |
46 | const char *v = (const char *) (2 + (int *)g_slice_alloc (sizeof (uint32_t) * 2 + len + 1)); |
46 | |
47 | |
47 | shstr::length (v) = len; |
48 | shstr::length (v) = len; |
48 | shstr::refcnt (v) = 1; |
49 | shstr::refcnt (v) = 1; |
49 | |
50 | |
50 | memcpy ((char *) v, s, len + 1); |
51 | memcpy ((char *) v, s, len + 1); |
51 | |
52 | |
52 | return v; |
53 | return v; |
53 | } |
54 | } |
54 | |
55 | |
55 | static const char * |
56 | shstr_vec<sizeof "(null)"> shstr_tmp::nullvec = { 0, 0xffffffff, "(null)" }; |
56 | makenull () |
|
|
57 | { |
|
|
58 | const char *s = makevec ("(null)"); |
|
|
59 | shstr::length (s) = 0; |
|
|
60 | return s; |
|
|
61 | } |
|
|
62 | |
|
|
63 | const char *shstr::null = makenull (); |
|
|
64 | |
57 | |
65 | const char * |
58 | const char * |
66 | shstr::find (const char *s) |
59 | shstr::find (const char *s) |
67 | { |
60 | { |
68 | if (!s) |
61 | if (!s) |
… | |
… | |
75 | |
68 | |
76 | const char * |
69 | const char * |
77 | shstr::intern (const char *s) |
70 | shstr::intern (const char *s) |
78 | { |
71 | { |
79 | if (!s) |
72 | if (!s) |
80 | return null; |
73 | return null (); |
81 | |
74 | |
82 | if (const char *found = find (s)) |
75 | if (const char *found = find (s)) |
83 | { |
76 | { |
84 | ++refcnt (found); |
77 | ++refcnt (found); |
85 | return found; |
78 | return found; |
86 | } |
79 | } |
87 | |
80 | |
|
|
81 | --next_gc; |
88 | s = makevec (s); |
82 | s = makevec (s); |
89 | ht.insert (s); |
83 | ht.insert (s); |
90 | return s; |
84 | return s; |
91 | } |
85 | } |
92 | |
86 | |
93 | // periodically test refcounts == 0 for a few strings |
87 | // periodically test refcounts == 0 for a few strings |
94 | // this is the ONLY thing that erases stuff from ht. keep it that way. |
88 | // this is the ONLY thing that erases stuff from ht. keep it that way. |
95 | void |
89 | void |
96 | shstr::gc () |
90 | shstr::gc () |
97 | { |
91 | { |
|
|
92 | if (expect_true (next_gc > 0)) |
|
|
93 | return; |
|
|
94 | |
98 | static const char *curpos; |
95 | static const char *curpos; |
99 | |
96 | |
100 | auto (i, curpos ? ht.find (curpos) : ht.begin ()); |
97 | auto (i, curpos ? ht.find (curpos) : ht.begin ()); |
101 | |
98 | |
102 | if (i == ht.end ()) |
99 | if (i == ht.end ()) |
103 | i = ht.begin (); |
100 | i = ht.begin (); |
104 | |
101 | |
105 | // go through all strings roughly once every 4 minutes |
|
|
106 | int n = ht.size () / 256 + 16; |
102 | int n = ht.size () / 256 + 16; |
|
|
103 | next_gc += n >> 1; |
107 | |
104 | |
108 | for (;;) |
105 | for (;;) |
109 | { |
106 | { |
110 | if (i == ht.end ()) |
107 | if (i == ht.end ()) |
111 | { |
108 | { |
… | |
… | |
120 | const char *s = *o; |
117 | const char *s = *o; |
121 | |
118 | |
122 | ht.erase (o); |
119 | ht.erase (o); |
123 | |
120 | |
124 | //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |
121 | //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |
125 | shstr_alloc -= sizeof (int) * 2 + length (s) + 1; |
122 | shstr_alloc -= sizeof (uint32_t) * 2 + length (s) + 1; |
126 | g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s); |
123 | g_slice_free1 (sizeof (uint32_t) * 2 + length (s) + 1, -2 + (int *) s); |
127 | } |
124 | } |
128 | else |
125 | else |
129 | ++i; |
126 | ++i; |
130 | } |
127 | } |
131 | |
128 | |
132 | curpos = *i; |
129 | curpos = *i; |
133 | } |
130 | } |
134 | |
131 | |
135 | // declare these here to get correct initialisation order |
132 | // declare these here to get correct initialisation order |
136 | #define def(str) const shstr shstr_ ## str (# str); |
133 | #define def2(id,str) const shstr shstr_ ## id (str); |
|
|
134 | #define def(id) def2(id, # id) |
137 | # include "shstrinc.h" |
135 | # include "shstrinc.h" |
138 | #undef def |
136 | #undef def |
|
|
137 | #undef def2 |
139 | |
138 | |
140 | shstr skill_names[NUM_SKILLS]; |
139 | shstr skill_names[NUM_SKILLS]; |
141 | |
140 | |
142 | //TODO: this should of course not be here |
141 | //TODO: this should of course not be here |
143 | |
142 | |