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.6 by root, Sun Sep 3 11:37:25 2006 UTC vs.
Revision 1.7 by root, Sun Sep 3 23:33:00 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 s;
63}
64
65const char *shstr::null = makevec ("<nil>");
66
54const char * 67const char *
55shstr::find (const char *s) 68shstr::find (const char *s)
56{ 69{
57 if (!s) 70 if (!s)
58 return s; 71 return s;
66 79
67const char * 80const char *
68shstr::intern (const char *s) 81shstr::intern (const char *s)
69{ 82{
70 if (!s) 83 if (!s)
71 return s; 84 return null;
72 85
73 if (const char *found = find (s)) 86 if (const char *found = find (s))
74 { 87 {
75 ++refcnt (found); 88 ++refcnt (found);
76 return found; 89 return found;
77 } 90 }
78 91
79 int len = strlen (s); 92 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); 93 ht.insert (s);
89
90 return v; 94 return s;
91} 95}
92 96
93// periodically test refcounts == 0 for a few strings 97// periodically test refcounts == 0 for a few strings
94// this is the ONLY thing that erases stuff from ht. keep it that way. 98// this is the ONLY thing that erases stuff from ht. keep it that way.
95void 99void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines