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.14 by root, Mon Sep 11 23:33:28 2006 UTC vs.
Revision 1.20 by root, Thu Sep 14 18:13:01 2006 UTC

1
2/* 1/*
3 * shstr.C 2 * shstr.C
4 */ 3 */
5 4
6#include <cstring> 5#include <cstring>
8 7
9#include <glib.h> 8#include <glib.h>
10 9
11#include <tr1/unordered_set> 10#include <tr1/unordered_set>
12 11
13#include "shstr.h"
14#include "util.h" 12#include "global.h"
15 13
16typedef 14typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *>, true> HT;
17std::tr1::unordered_set < const char *,
18 str_hash,
19 str_equal >
20 HT;
21 15
22static HT 16static HT ht;
23 ht;
24 17
25static const char * 18static const char *
26makevec (const char *s) 19makevec (const char *s)
27{ 20{
28 int
29 len = strlen (s); 21 int len = strlen (s);
30 22
31 const char *
32 v = (const char *) (2 + (int *) g_slice_alloc (sizeof (int) * 2 + len + 1)); 23 const char *v = (const char *) (2 + (int *)g_slice_alloc (sizeof (int) * 2 + len + 1));
33 24
34 shstr::length (v) = len; 25 shstr::length (v) = len;
35 shstr::refcnt (v) = 1; 26 shstr::refcnt (v) = 1;
36 27
37 memcpy ((char *) v, s, len + 1); 28 memcpy ((char *) v, s, len + 1);
38 29
39 return v; 30 return v;
40} 31}
41 32
42const char * 33static const char *
43 shstr::null = makevec ("<nil>"); 34makenull ()
35{
36 const char *s = makevec ("(null)");
37 shstr::length (s) = 0;
38 return s;
39}
44 40
45// what weird misoptimisation is this again? 41const char *shstr::null = makenull ();
46const shstr undead_name ("undead");
47 42
48const char * 43const char *
49shstr::find (const char *s) 44shstr::find (const char *s)
50{ 45{
51 if (!s) 46 if (!s)
52 return s; 47 return s;
53 48
54 HT::iterator i = ht.find (s); 49 AUTODECL (i, ht.find (s));
55 50
56 return i != ht.end ()? *i : 0; 51 return i != ht.end ()? *i : 0;
57} 52}
58 53
59const char * 54const char *
76// periodically test refcounts == 0 for a few strings 71// periodically test refcounts == 0 for a few strings
77// this is the ONLY thing that erases stuff from ht. keep it that way. 72// this is the ONLY thing that erases stuff from ht. keep it that way.
78void 73void
79shstr::gc () 74shstr::gc ()
80{ 75{
81return; //D
82//D currently disabled: some datastructures might still store them
83//D but their pointers will become invalidated
84 static const char *curpos; 76 static const char *curpos;
85 77
86 HT::iterator i = curpos ? ht.find (curpos) : ht.begin (); 78 AUTODECL (i, curpos ? ht.find (curpos) : ht.begin ());
87 79
88 if (i == ht.end ()) 80 if (i == ht.end ())
89 i = ht.begin (); 81 i = ht.begin ();
90 82
91 // go through all strings roughly once every 4 minutes 83 // go through all strings roughly once every 4 minutes
100 } 92 }
101 else if (!--n) 93 else if (!--n)
102 break; 94 break;
103 else if (!refcnt (*i)) 95 else if (!refcnt (*i))
104 { 96 {
105 HT::iterator o = i++; 97 AUTODECL (o, i++);
106 const char *s = *o; 98 const char *s = *o;
107 99
108 ht.erase (o); 100 ht.erase (o);
109 101
110 //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));
114 ++i; 106 ++i;
115 } 107 }
116 108
117 curpos = *i; 109 curpos = *i;
118} 110}
111
112shstr skill_names[NUM_SKILLS];
113
114// what weird misoptimisation is this again?
115const shstr undead_name ("undead");
119 116
120//TODO: this should of course not be here 117//TODO: this should of course not be here
121 118
122/* 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
123 * buf1 by adding on buf2! Returns true if overflow will occur. 120 * buf1 by adding on buf2! Returns true if overflow will occur.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines