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.8 by root, Mon Sep 4 11:07:59 2006 UTC

46 46
47typedef std::tr1::unordered_set<const char *, hash, equal> HT; 47typedef std::tr1::unordered_set<const char *, hash, equal> HT;
48 48
49static HT ht; 49static HT ht;
50 50
51static const char *makevec (const char *s)
52{
53 int len = strlen (s);
54
55 const char *v = (const char *)(2 + (int *)malloc (sizeof (int) * 2 + len + 1));
56
57 shstr::length (v) = len;
58 shstr::refcnt (v) = 1;
59
60 memcpy ((char *)v, s, len + 1);
61
62 return v;
63}
64
65const char *shstr::null = makevec ("<nil>");
66
67// what weird misoptimisation is this again?
68const shstr undead_name ("undead");
69
51const char * 70const char *
52shstr::find (const char *s) 71shstr::find (const char *s)
53{ 72{
54 if (!s) 73 if (!s)
55 return s; 74 return s;
56 75
57 HT::iterator i = ht.find (s); 76 HT::iterator i = ht.find (s);
58 77
59 return i != ht.end () 78 return i != ht.end ()
60 ? (char *)*i 79 ? *i
61 : 0; 80 : 0;
62} 81}
63 82
64const char * 83const char *
65shstr::intern (const char *s) 84shstr::intern (const char *s)
66{ 85{
67 if (!s) 86 if (!s)
68 return s; 87 return null;
69 88
70 if (const char *found = find (s)) 89 if (const char *found = find (s))
90 {
91 ++refcnt (found);
71 return found; 92 return found;
93 }
72 94
73 int len = strlen (s); 95 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); 96 ht.insert (s);
85 97 return s;
86 return (char *)v;
87} 98}
88 99
89// TODO: periodically test refcounts == 0 for a few strings (e.g. one hash bucket, 100// periodically test refcounts == 0 for a few strings
90// exploiting the fatc that iterators stay valid for unordered_set). 101// this is the ONLY thing that erases stuff from ht. keep it that way.
91void 102void
92shstr::gc () 103shstr::gc ()
93{ 104{
105 static const char *curpos;
106
107 HT::iterator i = curpos ? ht.find (curpos) : ht.begin ();
108
109 if (i == ht.end ())
110 i = ht.begin ();
111
112 // go through all strings roughly once every 4 minutes
113 int n = ht.size () / 256 + 16;
114
115 for (;;)
116 {
117 if (i == ht.end ())
118 {
119 curpos = 0;
120 return;
121 }
122 else if (!--n)
123 break;
124 else if (!refcnt (*i))
125 {
126 HT::iterator o = i++;
127 const char *s = *o;
128 ht.erase (o);
129
130 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s));
131 free (-2 + (int *)s);
132 }
133 else
134 ++i;
135 }
136
137 curpos = *i;
94} 138}
95 139
140//TODO: this should of course not be here
96/* buf_overflow() - we don't want to exceed the buffer size of 141/* buf_overflow() - we don't want to exceed the buffer size of
97 * buf1 by adding on buf2! Returns true if overflow will occur. 142 * buf1 by adding on buf2! Returns true if overflow will occur.
98 */ 143 */
99 144
100int 145int

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines