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.14 by root, Mon Sep 11 23:33:28 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines