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.28 by root, Thu Nov 8 19:43:23 2007 UTC vs.
Revision 1.38 by root, Thu Jan 1 21:15:12 2009 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * 5 *
6 * Deliantra is free software: you can redistribute it and/or modify 6 * Deliantra is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or 8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version. 9 * (at your option) any later version.
30 30
31#include "global.h" 31#include "global.h"
32 32
33size_t shstr_alloc; 33size_t shstr_alloc;
34 34
35typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *>, true> HT; 35typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *> > HT;
36 36
37static HT ht; 37static HT ht;
38static int next_gc;
38 39
39static const char * 40static const char *
40makevec (const char *s) 41makevec (const char *s)
41{ 42{
42 int len = strlen (s); 43 int len = strlen (s);
44 int alloc = sizeof (uint32_t) * 2 + len + 1;
43 45
44 shstr_alloc += sizeof (int) * 2 + len + 1; 46 shstr_alloc += alloc;
45 const char *v = (const char *) (2 + (int *)g_slice_alloc (sizeof (int) * 2 + len + 1)); 47 char *v = (char *)g_slice_alloc (alloc);
48 v += sizeof (uint32_t) * 2;
46 49
47 shstr::length (v) = len; 50 shstr::length (v) = len;
48 shstr::refcnt (v) = 1; 51 shstr::refcnt (v) = 1;
49 52
50 memcpy ((char *) v, s, len + 1); 53 memcpy (v, s, len + 1);
51 54
52 return v; 55 return v;
53} 56}
54 57
55static const char * 58shstr_vec<sizeof "(null)"> shstr_tmp::nullvec = { 0, 0xffffffff, "(null)" };
56makenull ()
57{
58 const char *s = makevec ("(null)");
59 shstr::length (s) = 0;
60 return s;
61}
62
63const char *shstr::null = makenull ();
64 59
65const char * 60const char *
66shstr::find (const char *s) 61shstr::find (const char *s)
67{ 62{
68 if (!s) 63 if (!s)
75 70
76const char * 71const char *
77shstr::intern (const char *s) 72shstr::intern (const char *s)
78{ 73{
79 if (!s) 74 if (!s)
80 return null; 75 return null ();
81 76
82 if (const char *found = find (s)) 77 if (const char *found = find (s))
83 { 78 {
84 ++refcnt (found); 79 ++refcnt (found);
85 return found; 80 return found;
86 } 81 }
87 82
83 --next_gc;
88 s = makevec (s); 84 s = makevec (s);
89 ht.insert (s); 85 ht.insert (s);
90 return s; 86 return s;
91} 87}
92 88
93// periodically test refcounts == 0 for a few strings 89// periodically test refcounts == 0 for a few strings
94// this is the ONLY thing that erases stuff from ht. keep it that way. 90// this is the ONLY thing that erases stuff from ht. keep it that way.
95void 91void
96shstr::gc () 92shstr::gc ()
97{ 93{
94 if (expect_true (next_gc > 0))
95 return;
96
98 static const char *curpos; 97 static const char *curpos;
99 98
100 auto (i, curpos ? ht.find (curpos) : ht.begin ()); 99 auto (i, curpos ? ht.find (curpos) : ht.begin ());
101 100
102 if (i == ht.end ()) 101 if (i == ht.end ())
103 i = ht.begin (); 102 i = ht.begin ();
104 103
105 int n = ht.size () / 256 + 16; 104 int n = ht.size () / 256 + 16;
105 next_gc += n >> 1;
106 106
107 for (;;) 107 for (;;)
108 { 108 {
109 if (i == ht.end ()) 109 if (i == ht.end ())
110 { 110 {
119 const char *s = *o; 119 const char *s = *o;
120 120
121 ht.erase (o); 121 ht.erase (o);
122 122
123 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); 123 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s));
124 shstr_alloc -= sizeof (int) * 2 + length (s) + 1; 124 int alloc = sizeof (uint32_t) * 2 + length (s) + 1;
125 g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s); 125 shstr_alloc -= alloc;
126 g_slice_free1 (alloc, (void *)(s - sizeof (uint32_t) * 2));
126 } 127 }
127 else 128 else
128 ++i; 129 ++i;
129 } 130 }
130 131
131 curpos = *i; 132 curpos = *i;
132} 133}
133 134
134// declare these here to get correct initialisation order 135// declare these here to get correct initialisation order
135#define def(str) const shstr shstr_ ## str (# str); 136#define def2(id,str) const shstr id (str);
137#define def(id) def2(shstr_ ## id, # id)
136# include "shstrinc.h" 138# include "shstrinc.h"
137#undef def 139#undef def
140#undef def2
138 141
139shstr skill_names[NUM_SKILLS]; 142shstr skill_names[NUM_SKILLS];
140 143
141//TODO: this should of course not be here 144//TODO: this should of course not be here
142 145

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines