|
|
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 | |
60 | |
48 | const char * |
61 | const char * |
49 | shstr::find (const char *s) |
62 | shstr::find (const char *s) |
50 | { |
63 | { |
51 | if (!s) |
64 | if (!s) |
52 | return s; |
65 | return s; |
53 | |
66 | |
54 | HT::iterator i = ht.find (s); |
67 | auto (i, ht.find (s)); |
55 | |
68 | |
56 | return i != ht.end ()? *i : 0; |
69 | return i != ht.end ()? *i : 0; |
57 | } |
70 | } |
58 | |
71 | |
59 | const char * |
72 | const char * |
60 | shstr::intern (const char *s) |
73 | shstr::intern (const char *s) |
61 | { |
74 | { |
62 | if (!s) |
75 | if (!s) |
63 | return null; |
76 | return null (); |
64 | |
77 | |
65 | if (const char *found = find (s)) |
78 | if (const char *found = find (s)) |
66 | { |
79 | { |
67 | ++refcnt (found); |
80 | ++refcnt (found); |
68 | return found; |
81 | return found; |
69 | } |
82 | } |
70 | |
83 | |
|
|
84 | --next_gc; |
71 | s = makevec (s); |
85 | s = makevec (s); |
72 | ht.insert (s); |
86 | ht.insert (s); |
73 | return s; |
87 | return s; |
74 | } |
88 | } |
75 | |
89 | |
76 | // periodically test refcounts == 0 for a few strings |
90 | // periodically test refcounts == 0 for a few strings |
77 | // 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. |
78 | void |
92 | void |
79 | shstr::gc () |
93 | shstr::gc () |
80 | { |
94 | { |
81 | return; //D |
95 | if (expect_true (next_gc > 0)) |
82 | //D currently disabled: some datastructures might still store them |
96 | return; |
83 | //D but their pointers will become invalidated |
97 | |
84 | static const char *curpos; |
98 | static const char *curpos; |
85 | |
99 | |
86 | HT::iterator i = curpos ? ht.find (curpos) : ht.begin (); |
100 | auto (i, curpos ? ht.find (curpos) : ht.begin ()); |
87 | |
101 | |
88 | if (i == ht.end ()) |
102 | if (i == ht.end ()) |
89 | i = ht.begin (); |
103 | i = ht.begin (); |
90 | |
104 | |
91 | // go through all strings roughly once every 4 minutes |
|
|
92 | int n = ht.size () / 256 + 16; |
105 | int n = ht.size () / 256 + 16; |
|
|
106 | next_gc += n >> 1; |
93 | |
107 | |
94 | for (;;) |
108 | for (;;) |
95 | { |
109 | { |
96 | if (i == ht.end ()) |
110 | if (i == ht.end ()) |
97 | { |
111 | { |
… | |
… | |
100 | } |
114 | } |
101 | else if (!--n) |
115 | else if (!--n) |
102 | break; |
116 | break; |
103 | else if (!refcnt (*i)) |
117 | else if (!refcnt (*i)) |
104 | { |
118 | { |
105 | HT::iterator o = i++; |
119 | auto (o, i++); |
106 | const char *s = *o; |
120 | const char *s = *o; |
107 | |
121 | |
108 | ht.erase (o); |
122 | ht.erase (o); |
109 | |
123 | |
110 | //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)); |
111 | 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)); |
112 | } |
128 | } |
113 | else |
129 | else |
114 | ++i; |
130 | ++i; |
115 | } |
131 | } |
116 | |
132 | |
117 | curpos = *i; |
133 | curpos = *i; |
118 | } |
134 | } |
119 | |
135 | |
120 | //TODO: this should of course not be here |
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 |
121 | |
142 | |
122 | /* buf_overflow() - we don't want to exceed the buffer size of |
143 | shstr skill_names[NUM_SKILLS]; |
123 | * buf1 by adding on buf2! Returns true if overflow will occur. |
|
|
124 | */ |
|
|
125 | |
144 | |
126 | int |
|
|
127 | buf_overflow (const char *buf1, const char *buf2, int bufsize) |
|
|
128 | { |
|
|
129 | int len1 = 0, len2 = 0; |
|
|
130 | |
|
|
131 | if (buf1) |
|
|
132 | len1 = strlen (buf1); |
|
|
133 | if (buf2) |
|
|
134 | len2 = strlen (buf2); |
|
|
135 | if ((len1 + len2) >= bufsize) |
|
|
136 | return 1; |
|
|
137 | return 0; |
|
|
138 | } |
|
|