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

# User Rev Content
1 elmex 1.1 /*
2 root 1.23 * 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 root 1.22 */
22    
23     /*
24 root 1.3 * shstr.C
25 elmex 1.1 */
26    
27 root 1.3 #include <cstring>
28     #include <cstdlib>
29 root 1.10 #include <glib.h>
30 root 1.3 #include <tr1/unordered_set>
31 elmex 1.1
32 root 1.16 #include "global.h"
33 elmex 1.1
34 root 1.18 typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *>, true> HT;
35 elmex 1.1
36 root 1.16 static HT ht;
37 elmex 1.1
38 root 1.13 static const char *
39     makevec (const char *s)
40 root 1.7 {
41 root 1.17 int len = strlen (s);
42 root 1.7
43 root 1.18 const char *v = (const char *) (2 + (int *)g_slice_alloc (sizeof (int) * 2 + len + 1));
44 root 1.7
45     shstr::length (v) = len;
46     shstr::refcnt (v) = 1;
47    
48 root 1.13 memcpy ((char *) v, s, len + 1);
49 root 1.7
50 root 1.8 return v;
51 root 1.7 }
52    
53 root 1.17 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 root 1.15
63 elmex 1.1 const char *
64 root 1.3 shstr::find (const char *s)
65     {
66 root 1.4 if (!s)
67     return s;
68    
69 root 1.21 auto (i, ht.find (s));
70 elmex 1.1
71 root 1.13 return i != ht.end ()? *i : 0;
72 elmex 1.1 }
73    
74     const char *
75 root 1.3 shstr::intern (const char *s)
76     {
77 root 1.4 if (!s)
78 root 1.7 return null;
79 elmex 1.1
80 root 1.4 if (const char *found = find (s))
81 root 1.5 {
82     ++refcnt (found);
83     return found;
84     }
85 elmex 1.1
86 root 1.7 s = makevec (s);
87     ht.insert (s);
88     return s;
89 elmex 1.1 }
90    
91 root 1.5 // periodically test refcounts == 0 for a few strings
92     // this is the ONLY thing that erases stuff from ht. keep it that way.
93 elmex 1.1 void
94 root 1.3 shstr::gc ()
95     {
96 root 1.5 static const char *curpos;
97    
98 root 1.21 auto (i, curpos ? ht.find (curpos) : ht.begin ());
99 root 1.5
100     if (i == ht.end ())
101     i = ht.begin ();
102    
103     // go through all strings roughly once every 4 minutes
104 root 1.11 int n = ht.size () / 256 + 16;
105 root 1.6
106     for (;;)
107 root 1.5 {
108     if (i == ht.end ())
109     {
110     curpos = 0;
111     return;
112     }
113 root 1.6 else if (!--n)
114     break;
115     else if (!refcnt (*i))
116 root 1.5 {
117 root 1.21 auto (o, i++);
118 root 1.5 const char *s = *o;
119 root 1.13
120 root 1.5 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 root 1.13 g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s);
124 root 1.5 }
125     else
126     ++i;
127     }
128    
129     curpos = *i;
130 elmex 1.1 }
131    
132 root 1.16 shstr skill_names[NUM_SKILLS];
133    
134     // what weird misoptimisation is this again?
135     const shstr undead_name ("undead");
136    
137 root 1.5 //TODO: this should of course not be here
138 root 1.13
139 elmex 1.1 /* 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 root 1.13 int
143 elmex 1.1 buf_overflow (const char *buf1, const char *buf2, int bufsize)
144     {
145 root 1.13 int len1 = 0, len2 = 0;
146 elmex 1.1
147 root 1.13 if (buf1)
148     len1 = strlen (buf1);
149     if (buf2)
150     len2 = strlen (buf2);
151     if ((len1 + len2) >= bufsize)
152     return 1;
153 root 1.22
154 root 1.13 return 0;
155 elmex 1.1 }