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 | |
33 | size_t shstr_alloc; |
33 | size_t shstr_alloc; |
34 | |
34 | |
35 | typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *>, true> HT; |
35 | typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *> > HT; |
36 | |
36 | |
37 | static HT ht; |
37 | static HT ht; |
|
|
38 | static int next_gc; |
38 | |
39 | |
39 | static const char * |
40 | static const char * |
40 | makevec (const char *s) |
41 | makevec (const char *s) |
41 | { |
42 | { |
42 | int len = strlen (s); |
43 | int len = strlen (s); |
43 | |
44 | |
44 | shstr_alloc += sizeof (int) * 2 + len + 1; |
45 | shstr_alloc += sizeof (unsigned int) * 2 + len + 1; |
45 | const char *v = (const char *) (2 + (int *)g_slice_alloc (sizeof (int) * 2 + len + 1)); |
46 | const char *v = (const char *) (2 + (int *)g_slice_alloc (sizeof (unsigned int) * 2 + len + 1)); |
46 | |
47 | |
47 | shstr::length (v) = len; |
48 | shstr::length (v) = len; |
48 | shstr::refcnt (v) = 1; |
49 | shstr::refcnt (v) = 1; |
49 | |
50 | |
50 | memcpy ((char *) v, s, len + 1); |
51 | memcpy ((char *) v, s, len + 1); |
… | |
… | |
54 | |
55 | |
55 | static const char * |
56 | static const char * |
56 | makenull () |
57 | makenull () |
57 | { |
58 | { |
58 | const char *s = makevec ("(null)"); |
59 | const char *s = makevec ("(null)"); |
59 | shstr::length (s) = 0; |
60 | shstr_tmp::length (s) = 0; |
|
|
61 | //shstr ::refcnt (s) = 0xffffffff; // useful for debugging |
60 | return s; |
62 | return s; |
61 | } |
63 | } |
62 | |
64 | |
63 | const char *shstr::null = makenull (); |
65 | const char *shstr_tmp::null = makenull (); |
64 | |
66 | |
65 | const char * |
67 | const char * |
66 | shstr::find (const char *s) |
68 | shstr::find (const char *s) |
67 | { |
69 | { |
68 | if (!s) |
70 | if (!s) |
… | |
… | |
83 | { |
85 | { |
84 | ++refcnt (found); |
86 | ++refcnt (found); |
85 | return found; |
87 | return found; |
86 | } |
88 | } |
87 | |
89 | |
|
|
90 | --next_gc; |
88 | s = makevec (s); |
91 | s = makevec (s); |
89 | ht.insert (s); |
92 | ht.insert (s); |
90 | return s; |
93 | return s; |
91 | } |
94 | } |
92 | |
95 | |
93 | // periodically test refcounts == 0 for a few strings |
96 | // periodically test refcounts == 0 for a few strings |
94 | // this is the ONLY thing that erases stuff from ht. keep it that way. |
97 | // this is the ONLY thing that erases stuff from ht. keep it that way. |
95 | void |
98 | void |
96 | shstr::gc () |
99 | shstr::gc () |
97 | { |
100 | { |
|
|
101 | if (expect_true (next_gc > 0)) |
|
|
102 | return; |
|
|
103 | |
98 | static const char *curpos; |
104 | static const char *curpos; |
99 | |
105 | |
100 | auto (i, curpos ? ht.find (curpos) : ht.begin ()); |
106 | auto (i, curpos ? ht.find (curpos) : ht.begin ()); |
101 | |
107 | |
102 | if (i == ht.end ()) |
108 | if (i == ht.end ()) |
103 | i = ht.begin (); |
109 | i = ht.begin (); |
104 | |
110 | |
105 | int n = ht.size () / 256 + 16; |
111 | int n = ht.size () / 256 + 16; |
|
|
112 | next_gc += n >> 1; |
106 | |
113 | |
107 | for (;;) |
114 | for (;;) |
108 | { |
115 | { |
109 | if (i == ht.end ()) |
116 | if (i == ht.end ()) |
110 | { |
117 | { |
… | |
… | |
119 | const char *s = *o; |
126 | const char *s = *o; |
120 | |
127 | |
121 | ht.erase (o); |
128 | ht.erase (o); |
122 | |
129 | |
123 | //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |
130 | //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; |
131 | shstr_alloc -= sizeof (unsigned int) * 2 + length (s) + 1; |
125 | g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s); |
132 | g_slice_free1 (sizeof (unsigned int) * 2 + length (s) + 1, -2 + (int *) s); |
126 | } |
133 | } |
127 | else |
134 | else |
128 | ++i; |
135 | ++i; |
129 | } |
136 | } |
130 | |
137 | |
131 | curpos = *i; |
138 | curpos = *i; |
132 | } |
139 | } |
|
|
140 | |
|
|
141 | const shstr shstr_null; |
133 | |
142 | |
134 | // declare these here to get correct initialisation order |
143 | // declare these here to get correct initialisation order |
135 | #define def(str) const shstr shstr_ ## str (# str); |
144 | #define def(str) const shstr shstr_ ## str (# str); |
136 | # include "shstrinc.h" |
145 | # include "shstrinc.h" |
137 | #undef def |
146 | #undef def |