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.9 by root, Mon Sep 4 17:16:19 2006 UTC vs.
Revision 1.21 by root, Mon Apr 16 15:41:26 2007 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"
11#include "util.h" 12#include "global.h"
12 13
13typedef std::tr1::unordered_set<const char *, str_hash, str_equal> HT; 14typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *>, true> HT;
14 15
15static HT ht; 16static HT ht;
16 17
17static const char *makevec (const char *s) 18static const char *
19makevec (const char *s)
18{ 20{
19 int len = strlen (s); 21 int len = strlen (s);
20 22
21 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));
22 24
23 shstr::length (v) = len; 25 shstr::length (v) = len;
24 shstr::refcnt (v) = 1; 26 shstr::refcnt (v) = 1;
25 27
26 memcpy ((char *)v, s, len + 1); 28 memcpy ((char *) v, s, len + 1);
27 29
28 return v; 30 return v;
29} 31}
30 32
33static const char *
34makenull ()
35{
31const char *shstr::null = makevec ("<nil>"); 36 const char *s = makevec ("(null)");
37 shstr::length (s) = 0;
38 return s;
39}
32 40
33// what weird misoptimisation is this again? 41const char *shstr::null = makenull ();
34const shstr undead_name ("undead");
35 42
36const char * 43const char *
37shstr::find (const char *s) 44shstr::find (const char *s)
38{ 45{
39 if (!s) 46 if (!s)
40 return s; 47 return s;
41 48
42 HT::iterator i = ht.find (s); 49 auto (i, ht.find (s));
43 50
44 return i != ht.end () 51 return i != ht.end ()? *i : 0;
45 ? *i
46 : 0;
47} 52}
48 53
49const char * 54const char *
50shstr::intern (const char *s) 55shstr::intern (const char *s)
51{ 56{
68void 73void
69shstr::gc () 74shstr::gc ()
70{ 75{
71 static const char *curpos; 76 static const char *curpos;
72 77
73 HT::iterator i = curpos ? ht.find (curpos) : ht.begin (); 78 auto (i, curpos ? ht.find (curpos) : ht.begin ());
74 79
75 if (i == ht.end ()) 80 if (i == ht.end ())
76 i = ht.begin (); 81 i = ht.begin ();
77 82
78 // go through all strings roughly once every 4 minutes 83 // go through all strings roughly once every 4 minutes
87 } 92 }
88 else if (!--n) 93 else if (!--n)
89 break; 94 break;
90 else if (!refcnt (*i)) 95 else if (!refcnt (*i))
91 { 96 {
92 HT::iterator o = i++; 97 auto (o, i++);
93 const char *s = *o; 98 const char *s = *o;
99
94 ht.erase (o); 100 ht.erase (o);
95 101
96 //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));
97 free (-2 + (int *)s); 103 g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s);
98 } 104 }
99 else 105 else
100 ++i; 106 ++i;
101 } 107 }
102 108
103 curpos = *i; 109 curpos = *i;
104} 110}
105 111
112shstr skill_names[NUM_SKILLS];
113
114// what weird misoptimisation is this again?
115const shstr undead_name ("undead");
116
106//TODO: this should of course not be here 117//TODO: this should of course not be here
118
107/* 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
108 * buf1 by adding on buf2! Returns true if overflow will occur. 120 * buf1 by adding on buf2! Returns true if overflow will occur.
109 */ 121 */
110 122
111int 123int
112buf_overflow (const char *buf1, const char *buf2, int bufsize) 124buf_overflow (const char *buf1, const char *buf2, int bufsize)
113{ 125{
114 int len1 = 0, len2 = 0; 126 int len1 = 0, len2 = 0;
115 127
116 if (buf1) 128 if (buf1)
117 len1 = strlen (buf1); 129 len1 = strlen (buf1);
118 if (buf2) 130 if (buf2)
119 len2 = strlen (buf2); 131 len2 = strlen (buf2);
120 if ((len1 + len2) >= bufsize) 132 if ((len1 + len2) >= bufsize)
121 return 1;
122 return 0; 133 return 1;
134 return 0;
123} 135}
124

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines