ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/shstr.C
(Generate patch)

Comparing deliantra/server/common/shstr.C (file contents):
Revision 1.23 by root, Mon May 28 21:21:40 2007 UTC vs.
Revision 1.32 by root, Wed Dec 31 17:35:37 2008 UTC

1/* 1/*
2 * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. 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 it 6 * Deliantra is free software: you can redistribute it and/or modify
7 * under the terms of the GNU General Public License as published by the Free 7 * it under the terms of the GNU General Public License as published by
8 * Software Foundation; either version 2 of the License, or (at your option) 8 * the Free Software Foundation, either version 3 of the License, or
9 * 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, but 11 * This program is distributed in the hope that it will be useful,
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * 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 along 16 * You should have received a copy of the GNU General Public License
17 * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * 18 *
20 * 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>
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
33size_t shstr_alloc;
34
34typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *>, true> HT; 35typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *> > HT;
35 36
36static HT ht; 37static HT ht;
38static int next_gc;
37 39
38static const char * 40static const char *
39makevec (const char *s) 41makevec (const char *s)
40{ 42{
41 int len = strlen (s); 43 int len = strlen (s);
42 44
45 shstr_alloc += sizeof (unsigned int) * 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 (unsigned int) * 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);
52 55
53static const char * 56static const char *
54makenull () 57makenull ()
55{ 58{
56 const char *s = makevec ("(null)"); 59 const char *s = makevec ("(null)");
57 shstr::length (s) = 0; 60 shstr_tmp::length (s) = 0;
61 //shstr ::refcnt (s) = 0xffffffff; // useful for debugging
58 return s; 62 return s;
59} 63}
60 64
61const char *shstr::null = makenull (); 65const char *shstr_tmp::null = makenull ();
62 66
63const char * 67const char *
64shstr::find (const char *s) 68shstr::find (const char *s)
65{ 69{
66 if (!s) 70 if (!s)
81 { 85 {
82 ++refcnt (found); 86 ++refcnt (found);
83 return found; 87 return found;
84 } 88 }
85 89
90 --next_gc;
86 s = makevec (s); 91 s = makevec (s);
87 ht.insert (s); 92 ht.insert (s);
88 return s; 93 return s;
89} 94}
90 95
91// periodically test refcounts == 0 for a few strings 96// periodically test refcounts == 0 for a few strings
92// this is the ONLY thing that erases stuff from ht. keep it that way. 97// this is the ONLY thing that erases stuff from ht. keep it that way.
93void 98void
94shstr::gc () 99shstr::gc ()
95{ 100{
101 if (expect_true (next_gc > 0))
102 return;
103
96 static const char *curpos; 104 static const char *curpos;
97 105
98 auto (i, curpos ? ht.find (curpos) : ht.begin ()); 106 auto (i, curpos ? ht.find (curpos) : ht.begin ());
99 107
100 if (i == ht.end ()) 108 if (i == ht.end ())
101 i = ht.begin (); 109 i = ht.begin ();
102 110
103 // go through all strings roughly once every 4 minutes
104 int n = ht.size () / 256 + 16; 111 int n = ht.size () / 256 + 16;
112 next_gc += n >> 1;
105 113
106 for (;;) 114 for (;;)
107 { 115 {
108 if (i == ht.end ()) 116 if (i == ht.end ())
109 { 117 {
118 const char *s = *o; 126 const char *s = *o;
119 127
120 ht.erase (o); 128 ht.erase (o);
121 129
122 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); 130 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s));
131 shstr_alloc -= sizeof (unsigned int) * 2 + length (s) + 1;
123 g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s); 132 g_slice_free1 (sizeof (unsigned int) * 2 + length (s) + 1, -2 + (int *) s);
124 } 133 }
125 else 134 else
126 ++i; 135 ++i;
127 } 136 }
128 137
129 curpos = *i; 138 curpos = *i;
130} 139}
131 140
141const shstr shstr_null;
142
143// declare these here to get correct initialisation order
144#define def(str) const shstr shstr_ ## str (# str);
145# include "shstrinc.h"
146#undef def
147
132shstr skill_names[NUM_SKILLS]; 148shstr skill_names[NUM_SKILLS];
133
134// what weird misoptimisation is this again?
135const shstr undead_name ("undead");
136 149
137//TODO: this should of course not be here 150//TODO: this should of course not be here
138 151
139/* buf_overflow() - we don't want to exceed the buffer size of 152/* buf_overflow() - we don't want to exceed the buffer size of
140 * buf1 by adding on buf2! Returns true if overflow will occur. 153 * buf1 by adding on buf2! Returns true if overflow will occur.
144{ 157{
145 int len1 = 0, len2 = 0; 158 int len1 = 0, len2 = 0;
146 159
147 if (buf1) 160 if (buf1)
148 len1 = strlen (buf1); 161 len1 = strlen (buf1);
162
149 if (buf2) 163 if (buf2)
150 len2 = strlen (buf2); 164 len2 = strlen (buf2);
165
151 if ((len1 + len2) >= bufsize) 166 if ((len1 + len2) >= bufsize)
152 return 1; 167 return 1;
153 168
154 return 0; 169 return 0;
155} 170}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines