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.18 by root, Tue Sep 12 20:55:40 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines