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.5 by root, Sun Sep 3 09:00:05 2006 UTC vs.
Revision 1.8 by root, Mon Sep 4 11:07:59 2006 UTC

6#include <cstdlib> 6#include <cstdlib>
7 7
8#include <tr1/unordered_set> 8#include <tr1/unordered_set>
9 9
10#include "shstr.h" 10#include "shstr.h"
11
12// NOTE: even with lots of stuff loaded, we do not usually have >>20000 strings.
13// maybe refcounting is just overhead?
14 11
15struct hash 12struct hash
16{ 13{
17 std::size_t operator ()(const char *s) const 14 std::size_t operator ()(const char *s) const
18 { 15 {
49 46
50typedef std::tr1::unordered_set<const char *, hash, equal> HT; 47typedef std::tr1::unordered_set<const char *, hash, equal> HT;
51 48
52static HT ht; 49static HT ht;
53 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));
56
57 shstr::length (v) = len;
58 shstr::refcnt (v) = 1;
59
60 memcpy ((char *)v, s, len + 1);
61
62 return v;
63}
64
65const char *shstr::null = makevec ("<nil>");
66
67// what weird misoptimisation is this again?
68const shstr undead_name ("undead");
69
54const char * 70const char *
55shstr::find (const char *s) 71shstr::find (const char *s)
56{ 72{
57 if (!s) 73 if (!s)
58 return s; 74 return s;
66 82
67const char * 83const char *
68shstr::intern (const char *s) 84shstr::intern (const char *s)
69{ 85{
70 if (!s) 86 if (!s)
71 return s; 87 return null;
72 88
73 if (const char *found = find (s)) 89 if (const char *found = find (s))
74 { 90 {
75 ++refcnt (found); 91 ++refcnt (found);
76 return found; 92 return found;
77 } 93 }
78 94
79 int len = strlen (s); 95 s = makevec (s);
80
81 const char *v = (const char *)(2 + (int *)malloc (sizeof (int) * 2 + len + 1));
82
83 length (v) = len;
84 refcnt (v) = 1;
85
86 memcpy ((char *)v, s, len + 1);
87
88 ht.insert (v); 96 ht.insert (s);
89
90 return v; 97 return s;
91} 98}
92 99
93// periodically test refcounts == 0 for a few strings 100// periodically test refcounts == 0 for a few strings
94// this is the ONLY thing that erases stuff from ht. keep it that way. 101// this is the ONLY thing that erases stuff from ht. keep it that way.
95void 102void
101 108
102 if (i == ht.end ()) 109 if (i == ht.end ())
103 i = ht.begin (); 110 i = ht.begin ();
104 111
105 // go through all strings roughly once every 4 minutes 112 // go through all strings roughly once every 4 minutes
106 for (int n = ht.size () / 256 + 16; --n; ) 113 int n = ht.size () / 256 + 16;
114
115 for (;;)
107 { 116 {
108 if (i == ht.end ()) 117 if (i == ht.end ())
109 { 118 {
110 curpos = 0; 119 curpos = 0;
111 return; 120 return;
112 } 121 }
113 122 else if (!--n)
123 break;
114 if (!refcnt (*i)) 124 else if (!refcnt (*i))
115 { 125 {
116 HT::iterator o = i++; 126 HT::iterator o = i++;
117 const char *s = *o; 127 const char *s = *o;
118 ht.erase (o); 128 ht.erase (o);
119 129

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines