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.5 by root, Sun Sep 3 09:00:05 2006 UTC vs.
Revision 1.21 by root, Mon Apr 16 15:41:26 2007 UTC

3 */ 3 */
4 4
5#include <cstring> 5#include <cstring>
6#include <cstdlib> 6#include <cstdlib>
7 7
8#include <glib.h>
9
8#include <tr1/unordered_set> 10#include <tr1/unordered_set>
9 11
10#include "shstr.h" 12#include "global.h"
11 13
12// NOTE: even with lots of stuff loaded, we do not usually have >>20000 strings. 14typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *>, true> HT;
13// maybe refcounting is just overhead?
14
15struct hash
16{
17 std::size_t operator ()(const char *s) const
18 {
19 unsigned long hash = 0;
20 unsigned int i = 0;
21
22 /* use the one-at-a-time hash function, which supposedly is
23 * better than the djb2-like one used by perl5.005, but
24 * certainly is better then the bug used here before.
25 * see http://burtleburtle.net/bob/hash/doobs.html
26 */
27 while (*s)
28 {
29 hash += *s++;
30 hash += hash << 10;
31 hash ^= hash >> 6;
32 }
33
34 hash += hash << 3;
35 hash ^= hash >> 11;
36 hash += hash << 15;
37
38 return hash;
39 }
40};
41
42struct equal
43{
44 bool operator ()(const char *a, const char *b) const
45 {
46 return !strcmp (a, b);
47 }
48};
49
50typedef std::tr1::unordered_set<const char *, hash, equal> HT;
51 15
52static HT ht; 16static HT ht;
17
18static const char *
19makevec (const char *s)
20{
21 int len = strlen (s);
22
23 const char *v = (const char *) (2 + (int *)g_slice_alloc (sizeof (int) * 2 + len + 1));
24
25 shstr::length (v) = len;
26 shstr::refcnt (v) = 1;
27
28 memcpy ((char *) v, s, len + 1);
29
30 return v;
31}
32
33static const char *
34makenull ()
35{
36 const char *s = makevec ("(null)");
37 shstr::length (s) = 0;
38 return s;
39}
40
41const char *shstr::null = makenull ();
53 42
54const char * 43const char *
55shstr::find (const char *s) 44shstr::find (const char *s)
56{ 45{
57 if (!s) 46 if (!s)
58 return s; 47 return s;
59 48
60 HT::iterator i = ht.find (s); 49 auto (i, ht.find (s));
61 50
62 return i != ht.end () 51 return i != ht.end ()? *i : 0;
63 ? *i
64 : 0;
65} 52}
66 53
67const char * 54const char *
68shstr::intern (const char *s) 55shstr::intern (const char *s)
69{ 56{
70 if (!s) 57 if (!s)
71 return s; 58 return null;
72 59
73 if (const char *found = find (s)) 60 if (const char *found = find (s))
74 { 61 {
75 ++refcnt (found); 62 ++refcnt (found);
76 return found; 63 return found;
77 } 64 }
78 65
79 int len = strlen (s); 66 s = makevec (s);
80
81 const char *v = (const char *)(2 + (int *)malloc (sizeof (int) * 2 + len + 1));
82
83 length (v) = len;
84 refcnt (v) = 1;
85
86 memcpy ((char *)v, s, len + 1);
87
88 ht.insert (v); 67 ht.insert (s);
89
90 return v; 68 return s;
91} 69}
92 70
93// periodically test refcounts == 0 for a few strings 71// periodically test refcounts == 0 for a few strings
94// this is the ONLY thing that erases stuff from ht. keep it that way. 72// this is the ONLY thing that erases stuff from ht. keep it that way.
95void 73void
96shstr::gc () 74shstr::gc ()
97{ 75{
98 static const char *curpos; 76 static const char *curpos;
99 77
100 HT::iterator i = curpos ? ht.find (curpos) : ht.begin (); 78 auto (i, curpos ? ht.find (curpos) : ht.begin ());
101 79
102 if (i == ht.end ()) 80 if (i == ht.end ())
103 i = ht.begin (); 81 i = ht.begin ();
104 82
105 // go through all strings roughly once every 4 minutes 83 // go through all strings roughly once every 4 minutes
106 for (int n = ht.size () / 256 + 16; --n; ) 84 int n = ht.size () / 256 + 16;
85
86 for (;;)
107 { 87 {
108 if (i == ht.end ()) 88 if (i == ht.end ())
109 { 89 {
110 curpos = 0; 90 curpos = 0;
111 return; 91 return;
112 } 92 }
93 else if (!--n)
94 break;
95 else if (!refcnt (*i))
96 {
97 auto (o, i++);
98 const char *s = *o;
113 99
114 if (!refcnt (*i))
115 {
116 HT::iterator o = i++;
117 const char *s = *o;
118 ht.erase (o); 100 ht.erase (o);
119 101
120 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); 102 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s));
121 free (-2 + (int *)s); 103 g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s);
122 } 104 }
123 else 105 else
124 ++i; 106 ++i;
125 } 107 }
126 108
127 curpos = *i; 109 curpos = *i;
128} 110}
129 111
112shstr skill_names[NUM_SKILLS];
113
114// what weird misoptimisation is this again?
115const shstr undead_name ("undead");
116
130//TODO: this should of course not be here 117//TODO: this should of course not be here
118
131/* buf_overflow() - we don't want to exceed the buffer size of 119/* buf_overflow() - we don't want to exceed the buffer size of
132 * buf1 by adding on buf2! Returns true if overflow will occur. 120 * buf1 by adding on buf2! Returns true if overflow will occur.
133 */ 121 */
134 122
135int 123int
136buf_overflow (const char *buf1, const char *buf2, int bufsize) 124buf_overflow (const char *buf1, const char *buf2, int bufsize)
137{ 125{
138 int len1 = 0, len2 = 0; 126 int len1 = 0, len2 = 0;
139 127
140 if (buf1) 128 if (buf1)
141 len1 = strlen (buf1); 129 len1 = strlen (buf1);
142 if (buf2) 130 if (buf2)
143 len2 = strlen (buf2); 131 len2 = strlen (buf2);
144 if ((len1 + len2) >= bufsize) 132 if ((len1 + len2) >= bufsize)
145 return 1;
146 return 0; 133 return 1;
134 return 0;
147} 135}
148

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines