ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/shstr.C
Revision: 1.41
Committed: Tue Nov 10 00:01:31 2009 UTC (14 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.40: +8 -5 lines
Log Message:
store hash in shstr

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.28 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 root 1.23 *
4 root 1.29 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.23 *
6 root 1.39 * 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 root 1.23 *
11 root 1.24 * 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 root 1.23 *
16 root 1.39 * 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 root 1.23 *
20 root 1.28 * The authors can be reached via e-mail to <support@deliantra.net>
21 root 1.22 */
22    
23     /*
24 root 1.3 * shstr.C
25 elmex 1.1 */
26    
27 root 1.3 #include <cstring>
28     #include <cstdlib>
29 root 1.10 #include <glib.h>
30 root 1.3 #include <tr1/unordered_set>
31 elmex 1.1
32 root 1.16 #include "global.h"
33 elmex 1.1
34 root 1.26 size_t shstr_alloc;
35    
36 root 1.31 typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *> > HT;
37 elmex 1.1
38 root 1.16 static HT ht;
39 root 1.29 static int next_gc;
40 elmex 1.1
41 root 1.41 #define NUM_INT 3
42    
43 root 1.13 static const char *
44     makevec (const char *s)
45 root 1.7 {
46 root 1.17 int len = strlen (s);
47 root 1.41 int alloc = sizeof (uint32_t) * NUM_INT + len + 1;
48 root 1.7
49 root 1.38 shstr_alloc += alloc;
50     char *v = (char *)g_slice_alloc (alloc);
51 root 1.41 v += sizeof (uint32_t) * NUM_INT;
52 root 1.7
53 root 1.41 shstr::hash (v) = strhsh (s);
54 root 1.7 shstr::length (v) = len;
55     shstr::refcnt (v) = 1;
56    
57 root 1.38 memcpy (v, s, len + 1);
58 root 1.7
59 root 1.8 return v;
60 root 1.7 }
61    
62 root 1.41 shstr_vec<sizeof "(null)"> shstr_tmp::nullvec = { STRHSH_NULL, 0, 0xffffffff, "(null)" };
63 root 1.15
64 elmex 1.1 const char *
65 root 1.3 shstr::find (const char *s)
66     {
67 root 1.4 if (!s)
68     return s;
69    
70 root 1.21 auto (i, ht.find (s));
71 elmex 1.1
72 root 1.13 return i != ht.end ()? *i : 0;
73 elmex 1.1 }
74    
75     const char *
76 root 1.3 shstr::intern (const char *s)
77     {
78 root 1.4 if (!s)
79 root 1.33 return null ();
80 elmex 1.1
81 root 1.4 if (const char *found = find (s))
82 root 1.5 {
83     ++refcnt (found);
84     return found;
85     }
86 elmex 1.1
87 root 1.29 --next_gc;
88 root 1.7 s = makevec (s);
89     ht.insert (s);
90     return s;
91 elmex 1.1 }
92    
93 root 1.5 // periodically test refcounts == 0 for a few strings
94     // this is the ONLY thing that erases stuff from ht. keep it that way.
95 elmex 1.1 void
96 root 1.3 shstr::gc ()
97     {
98 root 1.29 if (expect_true (next_gc > 0))
99     return;
100    
101 root 1.5 static const char *curpos;
102    
103 root 1.21 auto (i, curpos ? ht.find (curpos) : ht.begin ());
104 root 1.5
105     if (i == ht.end ())
106     i = ht.begin ();
107    
108 root 1.11 int n = ht.size () / 256 + 16;
109 root 1.29 next_gc += n >> 1;
110 root 1.6
111     for (;;)
112 root 1.5 {
113     if (i == ht.end ())
114     {
115     curpos = 0;
116     return;
117     }
118 root 1.6 else if (!--n)
119     break;
120     else if (!refcnt (*i))
121 root 1.5 {
122 root 1.21 auto (o, i++);
123 root 1.5 const char *s = *o;
124 root 1.13
125 root 1.5 ht.erase (o);
126    
127     //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s));
128 root 1.41 int alloc = sizeof (uint32_t) * NUM_INT + length (s) + 1;
129 root 1.38 shstr_alloc -= alloc;
130 root 1.41 g_slice_free1 (alloc, (void *)(s - sizeof (uint32_t) * NUM_INT));
131 root 1.5 }
132     else
133     ++i;
134     }
135    
136     curpos = *i;
137 elmex 1.1 }
138    
139 root 1.25 // declare these here to get correct initialisation order
140 root 1.37 #define def2(id,str) const shstr id (str);
141     #define def(id) def2(shstr_ ## id, # id)
142 root 1.25 # include "shstrinc.h"
143     #undef def
144 root 1.36 #undef def2
145 root 1.25
146 root 1.16 shstr skill_names[NUM_SKILLS];
147