1 | /* |
1 | /* |
2 | * CrossFire, A Multiplayer game |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
5 | * |
6 | * This program 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 2 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, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
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, write to the Free Software |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
|
19 | * |
18 | * |
20 | * The authors can be reached via e-mail at <crossfire@schmorp.de> |
19 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | */ |
20 | */ |
22 | |
21 | |
23 | /* |
22 | /* |
24 | * shstr.C |
23 | * shstr.C |
25 | */ |
24 | */ |
… | |
… | |
29 | #include <glib.h> |
28 | #include <glib.h> |
30 | #include <tr1/unordered_set> |
29 | #include <tr1/unordered_set> |
31 | |
30 | |
32 | #include "global.h" |
31 | #include "global.h" |
33 | |
32 | |
|
|
33 | size_t shstr_alloc; |
|
|
34 | |
34 | 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; |
35 | |
36 | |
36 | static HT ht; |
37 | static HT ht; |
|
|
38 | static int next_gc; |
37 | |
39 | |
38 | static const char * |
40 | static const char * |
39 | makevec (const char *s) |
41 | makevec (const char *s) |
40 | { |
42 | { |
41 | int len = strlen (s); |
43 | int len = strlen (s); |
42 | |
44 | |
|
|
45 | shstr_alloc += sizeof (uint32_t) * 2 + len + 1; |
43 | 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)); |
44 | |
47 | |
45 | shstr::length (v) = len; |
48 | shstr::length (v) = len; |
46 | shstr::refcnt (v) = 1; |
49 | shstr::refcnt (v) = 1; |
47 | |
50 | |
48 | memcpy ((char *) v, s, len + 1); |
51 | memcpy ((char *) v, s, len + 1); |
49 | |
52 | |
50 | return v; |
53 | return v; |
51 | } |
54 | } |
52 | |
55 | |
53 | static const char * |
56 | shstr_vec<sizeof "(null)"> shstr_tmp::nullvec = { 0, 0xffffffff, "(null)" }; |
54 | makenull () |
|
|
55 | { |
|
|
56 | const char *s = makevec ("(null)"); |
|
|
57 | shstr::length (s) = 0; |
|
|
58 | return s; |
|
|
59 | } |
|
|
60 | |
|
|
61 | const char *shstr::null = makenull (); |
|
|
62 | |
57 | |
63 | const char * |
58 | const char * |
64 | shstr::find (const char *s) |
59 | shstr::find (const char *s) |
65 | { |
60 | { |
66 | if (!s) |
61 | if (!s) |
… | |
… | |
73 | |
68 | |
74 | const char * |
69 | const char * |
75 | shstr::intern (const char *s) |
70 | shstr::intern (const char *s) |
76 | { |
71 | { |
77 | if (!s) |
72 | if (!s) |
78 | return null; |
73 | return null (); |
79 | |
74 | |
80 | if (const char *found = find (s)) |
75 | if (const char *found = find (s)) |
81 | { |
76 | { |
82 | ++refcnt (found); |
77 | ++refcnt (found); |
83 | return found; |
78 | return found; |
84 | } |
79 | } |
85 | |
80 | |
|
|
81 | --next_gc; |
86 | s = makevec (s); |
82 | s = makevec (s); |
87 | ht.insert (s); |
83 | ht.insert (s); |
88 | return s; |
84 | return s; |
89 | } |
85 | } |
90 | |
86 | |
91 | // periodically test refcounts == 0 for a few strings |
87 | // periodically test refcounts == 0 for a few strings |
92 | // 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. |
93 | void |
89 | void |
94 | shstr::gc () |
90 | shstr::gc () |
95 | { |
91 | { |
|
|
92 | if (expect_true (next_gc > 0)) |
|
|
93 | return; |
|
|
94 | |
96 | static const char *curpos; |
95 | static const char *curpos; |
97 | |
96 | |
98 | auto (i, curpos ? ht.find (curpos) : ht.begin ()); |
97 | auto (i, curpos ? ht.find (curpos) : ht.begin ()); |
99 | |
98 | |
100 | if (i == ht.end ()) |
99 | if (i == ht.end ()) |
101 | i = ht.begin (); |
100 | i = ht.begin (); |
102 | |
101 | |
103 | // go through all strings roughly once every 4 minutes |
|
|
104 | int n = ht.size () / 256 + 16; |
102 | int n = ht.size () / 256 + 16; |
|
|
103 | next_gc += n >> 1; |
105 | |
104 | |
106 | for (;;) |
105 | for (;;) |
107 | { |
106 | { |
108 | if (i == ht.end ()) |
107 | if (i == ht.end ()) |
109 | { |
108 | { |
… | |
… | |
118 | const char *s = *o; |
117 | const char *s = *o; |
119 | |
118 | |
120 | ht.erase (o); |
119 | ht.erase (o); |
121 | |
120 | |
122 | //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)); |
|
|
122 | shstr_alloc -= sizeof (uint32_t) * 2 + length (s) + 1; |
123 | 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); |
124 | } |
124 | } |
125 | else |
125 | else |
126 | ++i; |
126 | ++i; |
127 | } |
127 | } |
128 | |
128 | |
129 | curpos = *i; |
129 | curpos = *i; |
130 | } |
130 | } |
131 | |
131 | |
|
|
132 | // declare these here to get correct initialisation order |
|
|
133 | #define def(str) const shstr shstr_ ## str (# str); |
|
|
134 | # include "shstrinc.h" |
|
|
135 | #undef def |
|
|
136 | |
132 | shstr skill_names[NUM_SKILLS]; |
137 | shstr skill_names[NUM_SKILLS]; |
133 | |
|
|
134 | // what weird misoptimisation is this again? |
|
|
135 | const shstr undead_name ("undead"); |
|
|
136 | |
138 | |
137 | //TODO: this should of course not be here |
139 | //TODO: this should of course not be here |
138 | |
140 | |
139 | /* buf_overflow() - we don't want to exceed the buffer size of |
141 | /* buf_overflow() - we don't want to exceed the buffer size of |
140 | * buf1 by adding on buf2! Returns true if overflow will occur. |
142 | * buf1 by adding on buf2! Returns true if overflow will occur. |
… | |
… | |
144 | { |
146 | { |
145 | int len1 = 0, len2 = 0; |
147 | int len1 = 0, len2 = 0; |
146 | |
148 | |
147 | if (buf1) |
149 | if (buf1) |
148 | len1 = strlen (buf1); |
150 | len1 = strlen (buf1); |
|
|
151 | |
149 | if (buf2) |
152 | if (buf2) |
150 | len2 = strlen (buf2); |
153 | len2 = strlen (buf2); |
|
|
154 | |
151 | if ((len1 + len2) >= bufsize) |
155 | if ((len1 + len2) >= bufsize) |
152 | return 1; |
156 | return 1; |
153 | |
157 | |
154 | return 0; |
158 | return 0; |
155 | } |
159 | } |