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.13 by root, Sun Sep 10 16:00:23 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");
53 47
54const char * 48const char *
55shstr::find (const char *s) 49shstr::find (const char *s)
56{ 50{
57 if (!s) 51 if (!s)
58 return s; 52 return s;
59 53
60 HT::iterator i = ht.find (s); 54 HT::iterator i = ht.find (s);
61 55
62 return i != ht.end () 56 return i != ht.end ()? *i : 0;
63 ? *i
64 : 0;
65} 57}
66 58
67const char * 59const char *
68shstr::intern (const char *s) 60shstr::intern (const char *s)
69{ 61{
70 if (!s) 62 if (!s)
71 return s; 63 return null;
72 64
73 if (const char *found = find (s)) 65 if (const char *found = find (s))
74 { 66 {
75 ++refcnt (found); 67 ++refcnt (found);
76 return found; 68 return found;
77 } 69 }
78 70
79 int len = strlen (s); 71 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); 72 ht.insert (s);
89
90 return v; 73 return s;
91} 74}
92 75
93// periodically test refcounts == 0 for a few strings 76// periodically test refcounts == 0 for a few strings
94// this is the ONLY thing that erases stuff from ht. keep it that way. 77// this is the ONLY thing that erases stuff from ht. keep it that way.
95void 78void
101 84
102 if (i == ht.end ()) 85 if (i == ht.end ())
103 i = ht.begin (); 86 i = ht.begin ();
104 87
105 // go through all strings roughly once every 4 minutes 88 // go through all strings roughly once every 4 minutes
106 for (int n = ht.size () / 256 + 16; --n; ) 89 int n = ht.size () / 256 + 16;
90
91 for (;;)
107 { 92 {
108 if (i == ht.end ()) 93 if (i == ht.end ())
109 { 94 {
110 curpos = 0; 95 curpos = 0;
111 return; 96 return;
112 } 97 }
113 98 else if (!--n)
99 break;
114 if (!refcnt (*i)) 100 else if (!refcnt (*i))
115 { 101 {
116 HT::iterator o = i++; 102 HT::iterator o = i++;
117 const char *s = *o; 103 const char *s = *o;
104
118 ht.erase (o); 105 ht.erase (o);
119 106
120 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); 107 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s));
121 free (-2 + (int *)s); 108 g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s);
122 } 109 }
123 else 110 else
124 ++i; 111 ++i;
125 } 112 }
126 113
127 curpos = *i; 114 curpos = *i;
128} 115}
129 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