|
|
1 | /* |
|
|
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
|
|
3 | * |
|
|
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
5 | * |
|
|
6 | * 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 | * |
|
|
11 | * 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 | * |
|
|
16 | * 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 | * |
|
|
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
|
|
21 | */ |
1 | |
22 | |
2 | /* |
23 | /* |
3 | * shstr.C |
24 | * shstr.C |
4 | */ |
25 | */ |
5 | |
26 | |
6 | #include <cstring> |
27 | #include <cstring> |
7 | #include <cstdlib> |
28 | #include <cstdlib> |
8 | |
|
|
9 | #include <glib.h> |
29 | #include <glib.h> |
10 | |
|
|
11 | #include <tr1/unordered_set> |
30 | #include <tr1/unordered_set> |
12 | |
31 | |
13 | #include "shstr.h" |
|
|
14 | #include "util.h" |
32 | #include "global.h" |
15 | |
33 | |
16 | typedef |
34 | size_t shstr_alloc; |
17 | std::tr1::unordered_set < const char *, |
|
|
18 | str_hash, |
|
|
19 | str_equal > |
|
|
20 | HT; |
|
|
21 | |
35 | |
|
|
36 | typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *> > HT; |
|
|
37 | |
22 | static HT |
38 | static HT ht; |
23 | ht; |
39 | static int next_gc; |
24 | |
40 | |
25 | static const char * |
41 | static const char * |
26 | makevec (const char *s) |
42 | makevec (const char *s) |
27 | { |
43 | { |
28 | int |
|
|
29 | len = strlen (s); |
44 | int len = strlen (s); |
|
|
45 | int alloc = sizeof (uint32_t) * 2 + len + 1; |
30 | |
46 | |
31 | const char * |
47 | shstr_alloc += alloc; |
32 | v = (const char *) (2 + (int *) g_slice_alloc (sizeof (int) * 2 + len + 1)); |
48 | char *v = (char *)g_slice_alloc (alloc); |
|
|
49 | v += sizeof (uint32_t) * 2; |
33 | |
50 | |
34 | shstr::length (v) = len; |
51 | shstr::length (v) = len; |
35 | shstr::refcnt (v) = 1; |
52 | shstr::refcnt (v) = 1; |
36 | |
53 | |
37 | memcpy ((char *) v, s, len + 1); |
54 | memcpy (v, s, len + 1); |
38 | |
55 | |
39 | return v; |
56 | return v; |
40 | } |
57 | } |
41 | |
58 | |
42 | const char * |
59 | shstr_vec<sizeof "(null)"> shstr_tmp::nullvec = { 0, 0xffffffff, "(null)" }; |
43 | shstr::null = makevec ("<nil>"); |
|
|
44 | |
|
|
45 | // what weird misoptimisation is this again? |
|
|
46 | const shstr undead_name ("undead"); |
|
|
47 | |
|
|
48 | shstr skill_names[NUM_SKILLS]; |
|
|
49 | |
60 | |
50 | const char * |
61 | const char * |
51 | shstr::find (const char *s) |
62 | shstr::find (const char *s) |
52 | { |
63 | { |
53 | if (!s) |
64 | if (!s) |
54 | return s; |
65 | return s; |
55 | |
66 | |
56 | HT::iterator i = ht.find (s); |
67 | auto (i, ht.find (s)); |
57 | |
68 | |
58 | return i != ht.end ()? *i : 0; |
69 | return i != ht.end ()? *i : 0; |
59 | } |
70 | } |
60 | |
71 | |
61 | const char * |
72 | const char * |
62 | shstr::intern (const char *s) |
73 | shstr::intern (const char *s) |
63 | { |
74 | { |
64 | if (!s) |
75 | if (!s) |
65 | return null; |
76 | return null (); |
66 | |
77 | |
67 | if (const char *found = find (s)) |
78 | if (const char *found = find (s)) |
68 | { |
79 | { |
69 | ++refcnt (found); |
80 | ++refcnt (found); |
70 | return found; |
81 | return found; |
71 | } |
82 | } |
72 | |
83 | |
|
|
84 | --next_gc; |
73 | s = makevec (s); |
85 | s = makevec (s); |
74 | ht.insert (s); |
86 | ht.insert (s); |
75 | return s; |
87 | return s; |
76 | } |
88 | } |
77 | |
89 | |
78 | // periodically test refcounts == 0 for a few strings |
90 | // periodically test refcounts == 0 for a few strings |
79 | // this is the ONLY thing that erases stuff from ht. keep it that way. |
91 | // this is the ONLY thing that erases stuff from ht. keep it that way. |
80 | void |
92 | void |
81 | shstr::gc () |
93 | shstr::gc () |
82 | { |
94 | { |
83 | return; //D |
95 | if (expect_true (next_gc > 0)) |
84 | //D currently disabled: some datastructures might still store them |
96 | return; |
85 | //D but their pointers will become invalidated |
97 | |
86 | static const char *curpos; |
98 | static const char *curpos; |
87 | |
99 | |
88 | HT::iterator i = curpos ? ht.find (curpos) : ht.begin (); |
100 | auto (i, curpos ? ht.find (curpos) : ht.begin ()); |
89 | |
101 | |
90 | if (i == ht.end ()) |
102 | if (i == ht.end ()) |
91 | i = ht.begin (); |
103 | i = ht.begin (); |
92 | |
104 | |
93 | // go through all strings roughly once every 4 minutes |
|
|
94 | int n = ht.size () / 256 + 16; |
105 | int n = ht.size () / 256 + 16; |
|
|
106 | next_gc += n >> 1; |
95 | |
107 | |
96 | for (;;) |
108 | for (;;) |
97 | { |
109 | { |
98 | if (i == ht.end ()) |
110 | if (i == ht.end ()) |
99 | { |
111 | { |
… | |
… | |
102 | } |
114 | } |
103 | else if (!--n) |
115 | else if (!--n) |
104 | break; |
116 | break; |
105 | else if (!refcnt (*i)) |
117 | else if (!refcnt (*i)) |
106 | { |
118 | { |
107 | HT::iterator o = i++; |
119 | auto (o, i++); |
108 | const char *s = *o; |
120 | const char *s = *o; |
109 | |
121 | |
110 | ht.erase (o); |
122 | ht.erase (o); |
111 | |
123 | |
112 | //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |
124 | //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |
113 | g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s); |
125 | int alloc = sizeof (uint32_t) * 2 + length (s) + 1; |
|
|
126 | shstr_alloc -= alloc; |
|
|
127 | g_slice_free1 (alloc, (void *)(s - sizeof (uint32_t) * 2)); |
114 | } |
128 | } |
115 | else |
129 | else |
116 | ++i; |
130 | ++i; |
117 | } |
131 | } |
118 | |
132 | |
119 | curpos = *i; |
133 | curpos = *i; |
120 | } |
134 | } |
121 | |
135 | |
|
|
136 | // declare these here to get correct initialisation order |
|
|
137 | #define def2(id,str) const shstr id (str); |
|
|
138 | #define def(id) def2(shstr_ ## id, # id) |
|
|
139 | # include "shstrinc.h" |
|
|
140 | #undef def |
|
|
141 | #undef def2 |
|
|
142 | |
|
|
143 | shstr skill_names[NUM_SKILLS]; |
|
|
144 | |
122 | //TODO: this should of course not be here |
145 | //TODO: this should of course not be here |
123 | |
146 | |
124 | /* buf_overflow() - we don't want to exceed the buffer size of |
147 | /* buf_overflow() - we don't want to exceed the buffer size of |
125 | * buf1 by adding on buf2! Returns true if overflow will occur. |
148 | * buf1 by adding on buf2! Returns true if overflow will occur. |
126 | */ |
149 | */ |
127 | |
|
|
128 | int |
150 | int |
129 | buf_overflow (const char *buf1, const char *buf2, int bufsize) |
151 | buf_overflow (const char *buf1, const char *buf2, int bufsize) |
130 | { |
152 | { |
131 | int len1 = 0, len2 = 0; |
153 | int len1 = 0, len2 = 0; |
132 | |
154 | |
133 | if (buf1) |
155 | if (buf1) |
134 | len1 = strlen (buf1); |
156 | len1 = strlen (buf1); |
|
|
157 | |
135 | if (buf2) |
158 | if (buf2) |
136 | len2 = strlen (buf2); |
159 | len2 = strlen (buf2); |
|
|
160 | |
137 | if ((len1 + len2) >= bufsize) |
161 | if ((len1 + len2) >= bufsize) |
138 | return 1; |
162 | return 1; |
|
|
163 | |
139 | return 0; |
164 | return 0; |
140 | } |
165 | } |