ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/shstr.C
Revision: 1.23
Committed: Mon May 28 21:21:40 2007 UTC (17 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.22: +19 -19 lines
Log Message:
update copyrights in common/*.C and util/*.C

File Contents

# Content
1 /*
2 * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game.
3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
5 *
6 * Crossfire TRT is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * The authors can be reached via e-mail to <crossfire@schmorp.de>
21 */
22
23 /*
24 * shstr.C
25 */
26
27 #include <cstring>
28 #include <cstdlib>
29 #include <glib.h>
30 #include <tr1/unordered_set>
31
32 #include "global.h"
33
34 typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *>, true> HT;
35
36 static HT ht;
37
38 static const char *
39 makevec (const char *s)
40 {
41 int len = strlen (s);
42
43 const char *v = (const char *) (2 + (int *)g_slice_alloc (sizeof (int) * 2 + len + 1));
44
45 shstr::length (v) = len;
46 shstr::refcnt (v) = 1;
47
48 memcpy ((char *) v, s, len + 1);
49
50 return v;
51 }
52
53 static const char *
54 makenull ()
55 {
56 const char *s = makevec ("(null)");
57 shstr::length (s) = 0;
58 return s;
59 }
60
61 const char *shstr::null = makenull ();
62
63 const char *
64 shstr::find (const char *s)
65 {
66 if (!s)
67 return s;
68
69 auto (i, ht.find (s));
70
71 return i != ht.end ()? *i : 0;
72 }
73
74 const char *
75 shstr::intern (const char *s)
76 {
77 if (!s)
78 return null;
79
80 if (const char *found = find (s))
81 {
82 ++refcnt (found);
83 return found;
84 }
85
86 s = makevec (s);
87 ht.insert (s);
88 return s;
89 }
90
91 // periodically test refcounts == 0 for a few strings
92 // this is the ONLY thing that erases stuff from ht. keep it that way.
93 void
94 shstr::gc ()
95 {
96 static const char *curpos;
97
98 auto (i, curpos ? ht.find (curpos) : ht.begin ());
99
100 if (i == ht.end ())
101 i = ht.begin ();
102
103 // go through all strings roughly once every 4 minutes
104 int n = ht.size () / 256 + 16;
105
106 for (;;)
107 {
108 if (i == ht.end ())
109 {
110 curpos = 0;
111 return;
112 }
113 else if (!--n)
114 break;
115 else if (!refcnt (*i))
116 {
117 auto (o, i++);
118 const char *s = *o;
119
120 ht.erase (o);
121
122 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s));
123 g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s);
124 }
125 else
126 ++i;
127 }
128
129 curpos = *i;
130 }
131
132 shstr skill_names[NUM_SKILLS];
133
134 // what weird misoptimisation is this again?
135 const shstr undead_name ("undead");
136
137 //TODO: this should of course not be here
138
139 /* buf_overflow() - we don't want to exceed the buffer size of
140 * buf1 by adding on buf2! Returns true if overflow will occur.
141 */
142 int
143 buf_overflow (const char *buf1, const char *buf2, int bufsize)
144 {
145 int len1 = 0, len2 = 0;
146
147 if (buf1)
148 len1 = strlen (buf1);
149 if (buf2)
150 len2 = strlen (buf2);
151 if ((len1 + len2) >= bufsize)
152 return 1;
153
154 return 0;
155 }