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.3 by root, Sun Sep 3 00:18:40 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{
73 if (!s)
74 return s;
75
54 HT::iterator i = ht.find (s); 76 HT::iterator i = ht.find (s);
55 77
56 return i != ht.end () 78 return i != ht.end ()
57 ? (char *)*i 79 ? *i
58 : 0; 80 : 0;
59} 81}
60 82
61const char * 83const char *
62shstr::intern (const char *s) 84shstr::intern (const char *s)
63{ 85{
64 HT::iterator i = ht.find (s); 86 if (!s)
87 return null;
65 88
66 if (i != ht.end ()) 89 if (const char *found = find (s))
67 return (char *)*i; 90 {
91 ++refcnt (found);
92 return found;
93 }
68 94
69 int len = strlen (s); 95 s = makevec (s);
70
71 int *v = (int *)malloc (sizeof (int) * 2 + len + 1);
72
73 v [0] = len;
74 v [1] = 0;
75
76 v += 2;
77
78 memcpy (v, s, len + 1);
79
80 ht.insert ((char *)v); 96 ht.insert (s);
81 97 return s;
82 return (char *)v;
83} 98}
84 99
85// TODO: periodically test refcounts == 0 for a few strings (e.g. one hash bucket, 100// periodically test refcounts == 0 for a few strings
86// 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.
87void 102void
88shstr::gc () 103shstr::gc ()
89{ 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;
90} 138}
91 139
140//TODO: this should of course not be here
92/* 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
93 * buf1 by adding on buf2! Returns true if overflow will occur. 142 * buf1 by adding on buf2! Returns true if overflow will occur.
94 */ 143 */
95 144
96int 145int

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines