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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines