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.14 by root, Mon Sep 11 23:33:28 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
96shstr::gc () 79shstr::gc ()
97{ 80{
81return; //D
82//D currently disabled: some datastructures might still store them
83//D but their pointers will become invalidated
98 static const char *curpos; 84 static const char *curpos;
99 85
100 HT::iterator i = curpos ? ht.find (curpos) : ht.begin (); 86 HT::iterator i = curpos ? ht.find (curpos) : ht.begin ();
101 87
102 if (i == ht.end ()) 88 if (i == ht.end ())
103 i = ht.begin (); 89 i = ht.begin ();
104 90
105 // go through all strings roughly once every 4 minutes 91 // go through all strings roughly once every 4 minutes
106 for (int n = ht.size () / 256 + 16; --n; ) 92 int n = ht.size () / 256 + 16;
93
94 for (;;)
107 { 95 {
108 if (i == ht.end ()) 96 if (i == ht.end ())
109 { 97 {
110 curpos = 0; 98 curpos = 0;
111 return; 99 return;
112 } 100 }
113 101 else if (!--n)
102 break;
114 if (!refcnt (*i)) 103 else if (!refcnt (*i))
115 { 104 {
116 HT::iterator o = i++; 105 HT::iterator o = i++;
117 const char *s = *o; 106 const char *s = *o;
107
118 ht.erase (o); 108 ht.erase (o);
119 109
120 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); 110 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s));
121 free (-2 + (int *)s); 111 g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s);
122 } 112 }
123 else 113 else
124 ++i; 114 ++i;
125 } 115 }
126 116
127 curpos = *i; 117 curpos = *i;
128} 118}
129 119
130//TODO: this should of course not be here 120//TODO: this should of course not be here
121
131/* buf_overflow() - we don't want to exceed the buffer size of 122/* buf_overflow() - we don't want to exceed the buffer size of
132 * buf1 by adding on buf2! Returns true if overflow will occur. 123 * buf1 by adding on buf2! Returns true if overflow will occur.
133 */ 124 */
134 125
135int 126int
136buf_overflow (const char *buf1, const char *buf2, int bufsize) 127buf_overflow (const char *buf1, const char *buf2, int bufsize)
137{ 128{
138 int len1 = 0, len2 = 0; 129 int len1 = 0, len2 = 0;
139 130
140 if (buf1) 131 if (buf1)
141 len1 = strlen (buf1); 132 len1 = strlen (buf1);
142 if (buf2) 133 if (buf2)
143 len2 = strlen (buf2); 134 len2 = strlen (buf2);
144 if ((len1 + len2) >= bufsize) 135 if ((len1 + len2) >= bufsize)
145 return 1;
146 return 0; 136 return 1;
137 return 0;
147} 138}
148

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines