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.14 by root, Mon Sep 11 23:33:28 2006 UTC vs.
Revision 1.39 by root, Mon Oct 12 14:00:57 2009 UTC

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
16typedef 34size_t shstr_alloc;
17std::tr1::unordered_set < const char *,
18 str_hash,
19 str_equal >
20 HT;
21 35
36typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *> > HT;
37
22static HT 38static HT ht;
23 ht; 39static int next_gc;
24 40
25static const char * 41static const char *
26makevec (const char *s) 42makevec (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
42const char * 59shstr_vec<sizeof "(null)"> shstr_tmp::nullvec = { 0, 0xffffffff, "(null)" };
43 shstr::null = makevec ("<nil>");
44
45// what weird misoptimisation is this again?
46const shstr undead_name ("undead");
47 60
48const char * 61const char *
49shstr::find (const char *s) 62shstr::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
59const char * 72const char *
60shstr::intern (const char *s) 73shstr::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.
78void 92void
79shstr::gc () 93shstr::gc ()
80{ 94{
81return; //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
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
143shstr skill_names[NUM_SKILLS];
144
120//TODO: this should of course not be here 145//TODO: this should of course not be here
121 146
122/* 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
123 * buf1 by adding on buf2! Returns true if overflow will occur. 148 * buf1 by adding on buf2! Returns true if overflow will occur.
124 */ 149 */
125
126int 150int
127buf_overflow (const char *buf1, const char *buf2, int bufsize) 151buf_overflow (const char *buf1, const char *buf2, int bufsize)
128{ 152{
129 int len1 = 0, len2 = 0; 153 int len1 = 0, len2 = 0;
130 154
131 if (buf1) 155 if (buf1)
132 len1 = strlen (buf1); 156 len1 = strlen (buf1);
157
133 if (buf2) 158 if (buf2)
134 len2 = strlen (buf2); 159 len2 = strlen (buf2);
160
135 if ((len1 + len2) >= bufsize) 161 if ((len1 + len2) >= bufsize)
136 return 1; 162 return 1;
163
137 return 0; 164 return 0;
138} 165}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines