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.7 by root, Sun Sep 3 23:33:00 2006 UTC vs.
Revision 1.19 by root, Wed Sep 13 23:39:27 2006 UTC

3 */ 3 */
4 4
5#include <cstring> 5#include <cstring>
6#include <cstdlib> 6#include <cstdlib>
7 7
8#include <glib.h>
9
8#include <tr1/unordered_set> 10#include <tr1/unordered_set>
9 11
10#include "shstr.h" 12#include "global.h"
11 13
12struct hash 14typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *>, true> HT;
13{
14 std::size_t operator ()(const char *s) const
15 {
16 unsigned long hash = 0;
17 unsigned int i = 0;
18
19 /* use the one-at-a-time hash function, which supposedly is
20 * better than the djb2-like one used by perl5.005, but
21 * certainly is better then the bug used here before.
22 * see http://burtleburtle.net/bob/hash/doobs.html
23 */
24 while (*s)
25 {
26 hash += *s++;
27 hash += hash << 10;
28 hash ^= hash >> 6;
29 }
30
31 hash += hash << 3;
32 hash ^= hash >> 11;
33 hash += hash << 15;
34
35 return hash;
36 }
37};
38
39struct equal
40{
41 bool operator ()(const char *a, const char *b) const
42 {
43 return !strcmp (a, b);
44 }
45};
46
47typedef std::tr1::unordered_set<const char *, hash, equal> HT;
48 15
49static HT ht; 16static HT ht;
50 17
51static const char *makevec (const char *s) 18static const char *
19makevec (const char *s)
52{ 20{
53 int len = strlen (s); 21 int len = strlen (s);
54 22
55 const char *v = (const char *)(2 + (int *)malloc (sizeof (int) * 2 + len + 1)); 23 const char *v = (const char *) (2 + (int *)g_slice_alloc (sizeof (int) * 2 + len + 1));
56 24
57 shstr::length (v) = len; 25 shstr::length (v) = len;
58 shstr::refcnt (v) = 1; 26 shstr::refcnt (v) = 1;
59 27
60 memcpy ((char *)v, s, len + 1); 28 memcpy ((char *) v, s, len + 1);
61 29
30 return v;
31}
32
33static const char *
34makenull ()
35{
36 const char *s = makevec ("(null)");
37 shstr::length (s) = 0;
62 return s; 38 return s;
63} 39}
64 40
65const char *shstr::null = makevec ("<nil>"); 41const char *shstr::null = makenull ();
66 42
67const char * 43const char *
68shstr::find (const char *s) 44shstr::find (const char *s)
69{ 45{
70 if (!s) 46 if (!s)
71 return s; 47 return s;
72 48
73 HT::iterator i = ht.find (s); 49 HT::iterator i = ht.find (s);
74 50
75 return i != ht.end () 51 return i != ht.end ()? *i : 0;
76 ? *i
77 : 0;
78} 52}
79 53
80const char * 54const char *
81shstr::intern (const char *s) 55shstr::intern (const char *s)
82{ 56{
120 break; 94 break;
121 else if (!refcnt (*i)) 95 else if (!refcnt (*i))
122 { 96 {
123 HT::iterator o = i++; 97 HT::iterator o = i++;
124 const char *s = *o; 98 const char *s = *o;
99
125 ht.erase (o); 100 ht.erase (o);
126 101
127 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); 102 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s));
128 free (-2 + (int *)s); 103 g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s);
129 } 104 }
130 else 105 else
131 ++i; 106 ++i;
132 } 107 }
133 108
134 curpos = *i; 109 curpos = *i;
135} 110}
136 111
112shstr skill_names[NUM_SKILLS];
113
114// what weird misoptimisation is this again?
115const shstr undead_name ("undead");
116
137//TODO: this should of course not be here 117//TODO: this should of course not be here
118
138/* buf_overflow() - we don't want to exceed the buffer size of 119/* buf_overflow() - we don't want to exceed the buffer size of
139 * buf1 by adding on buf2! Returns true if overflow will occur. 120 * buf1 by adding on buf2! Returns true if overflow will occur.
140 */ 121 */
141 122
142int 123int
143buf_overflow (const char *buf1, const char *buf2, int bufsize) 124buf_overflow (const char *buf1, const char *buf2, int bufsize)
144{ 125{
145 int len1 = 0, len2 = 0; 126 int len1 = 0, len2 = 0;
146 127
147 if (buf1) 128 if (buf1)
148 len1 = strlen (buf1); 129 len1 = strlen (buf1);
149 if (buf2) 130 if (buf2)
150 len2 = strlen (buf2); 131 len2 = strlen (buf2);
151 if ((len1 + len2) >= bufsize) 132 if ((len1 + len2) >= bufsize)
152 return 1;
153 return 0; 133 return 1;
134 return 0;
154} 135}
155

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines