ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/shstr.C
Revision: 1.18
Committed: Tue Sep 12 20:55:40 2006 UTC (17 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.17: +2 -2 lines
Log Message:
make use of slice_allocator and inline zero_initialised

File Contents

# Content
1
2 /*
3 * shstr.C
4 */
5
6 #include <cstring>
7 #include <cstdlib>
8
9 #include <glib.h>
10
11 #include <tr1/unordered_set>
12
13 #include "global.h"
14
15 typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *>, true> HT;
16
17 static HT ht;
18
19 static const char *
20 makevec (const char *s)
21 {
22 int len = strlen (s);
23
24 const char *v = (const char *) (2 + (int *)g_slice_alloc (sizeof (int) * 2 + len + 1));
25
26 shstr::length (v) = len;
27 shstr::refcnt (v) = 1;
28
29 memcpy ((char *) v, s, len + 1);
30
31 return v;
32 }
33
34 static const char *
35 makenull ()
36 {
37 const char *s = makevec ("(null)");
38 shstr::length (s) = 0;
39 return s;
40 }
41
42 const char *shstr::null = makenull ();
43
44 const char *
45 shstr::find (const char *s)
46 {
47 if (!s)
48 return s;
49
50 HT::iterator i = ht.find (s);
51
52 return i != ht.end ()? *i : 0;
53 }
54
55 const char *
56 shstr::intern (const char *s)
57 {
58 if (!s)
59 return null;
60
61 if (const char *found = find (s))
62 {
63 ++refcnt (found);
64 return found;
65 }
66
67 s = makevec (s);
68 ht.insert (s);
69 return s;
70 }
71
72 // periodically test refcounts == 0 for a few strings
73 // this is the ONLY thing that erases stuff from ht. keep it that way.
74 void
75 shstr::gc ()
76 {
77 return; //D
78 //D currently disabled: some datastructures might still store them
79 //D but their pointers will become invalidated
80 static const char *curpos;
81
82 HT::iterator i = curpos ? ht.find (curpos) : ht.begin ();
83
84 if (i == ht.end ())
85 i = ht.begin ();
86
87 // go through all strings roughly once every 4 minutes
88 int n = ht.size () / 256 + 16;
89
90 for (;;)
91 {
92 if (i == ht.end ())
93 {
94 curpos = 0;
95 return;
96 }
97 else if (!--n)
98 break;
99 else if (!refcnt (*i))
100 {
101 HT::iterator o = i++;
102 const char *s = *o;
103
104 ht.erase (o);
105
106 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s));
107 g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s);
108 }
109 else
110 ++i;
111 }
112
113 curpos = *i;
114 }
115
116 shstr skill_names[NUM_SKILLS];
117
118 // what weird misoptimisation is this again?
119 const shstr undead_name ("undead");
120
121 //TODO: this should of course not be here
122
123 /* buf_overflow() - we don't want to exceed the buffer size of
124 * buf1 by adding on buf2! Returns true if overflow will occur.
125 */
126
127 int
128 buf_overflow (const char *buf1, const char *buf2, int bufsize)
129 {
130 int len1 = 0, len2 = 0;
131
132 if (buf1)
133 len1 = strlen (buf1);
134 if (buf2)
135 len2 = strlen (buf2);
136 if ((len1 + len2) >= bufsize)
137 return 1;
138 return 0;
139 }