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.8 by root, Mon Sep 4 11:07:59 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
62 return v; 30 return v;
63} 31}
64 32
33static const char *
34makenull ()
35{
65const char *shstr::null = makevec ("<nil>"); 36 const char *s = makevec ("(null)");
37 shstr::length (s) = 0;
38 return s;
39}
66 40
67// what weird misoptimisation is this again? 41const char *shstr::null = makenull ();
68const shstr undead_name ("undead");
69 42
70const char * 43const char *
71shstr::find (const char *s) 44shstr::find (const char *s)
72{ 45{
73 if (!s) 46 if (!s)
74 return s; 47 return s;
75 48
76 HT::iterator i = ht.find (s); 49 HT::iterator i = ht.find (s);
77 50
78 return i != ht.end () 51 return i != ht.end ()? *i : 0;
79 ? *i
80 : 0;
81} 52}
82 53
83const char * 54const char *
84shstr::intern (const char *s) 55shstr::intern (const char *s)
85{ 56{
123 break; 94 break;
124 else if (!refcnt (*i)) 95 else if (!refcnt (*i))
125 { 96 {
126 HT::iterator o = i++; 97 HT::iterator o = i++;
127 const char *s = *o; 98 const char *s = *o;
99
128 ht.erase (o); 100 ht.erase (o);
129 101
130 //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));
131 free (-2 + (int *)s); 103 g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s);
132 } 104 }
133 else 105 else
134 ++i; 106 ++i;
135 } 107 }
136 108
137 curpos = *i; 109 curpos = *i;
138} 110}
139 111
112shstr skill_names[NUM_SKILLS];
113
114// what weird misoptimisation is this again?
115const shstr undead_name ("undead");
116
140//TODO: this should of course not be here 117//TODO: this should of course not be here
118
141/* 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
142 * buf1 by adding on buf2! Returns true if overflow will occur. 120 * buf1 by adding on buf2! Returns true if overflow will occur.
143 */ 121 */
144 122
145int 123int
146buf_overflow (const char *buf1, const char *buf2, int bufsize) 124buf_overflow (const char *buf1, const char *buf2, int bufsize)
147{ 125{
148 int len1 = 0, len2 = 0; 126 int len1 = 0, len2 = 0;
149 127
150 if (buf1) 128 if (buf1)
151 len1 = strlen (buf1); 129 len1 = strlen (buf1);
152 if (buf2) 130 if (buf2)
153 len2 = strlen (buf2); 131 len2 = strlen (buf2);
154 if ((len1 + len2) >= bufsize) 132 if ((len1 + len2) >= bufsize)
155 return 1;
156 return 0; 133 return 1;
134 return 0;
157} 135}
158

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines