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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines