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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines