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 | |
16 | typedef |
14 | typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *>, true> HT; |
17 | std::tr1::unordered_set < const char *, |
|
|
18 | str_hash, |
|
|
19 | str_equal > |
|
|
20 | HT; |
|
|
21 | |
15 | |
22 | static HT |
16 | static HT ht; |
23 | ht; |
|
|
24 | |
17 | |
25 | static const char * |
18 | static const char * |
26 | makevec (const char *s) |
19 | makevec (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 | |
42 | const char * |
33 | static const char * |
43 | shstr::null = makevec ("<nil>"); |
34 | makenull () |
|
|
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? |
41 | const char *shstr::null = makenull (); |
46 | const shstr undead_name ("undead"); |
|
|
47 | |
|
|
48 | shstr skill_names[NUM_SKILLS]; |
|
|
49 | |
42 | |
50 | const char * |
43 | const char * |
51 | shstr::find (const char *s) |
44 | shstr::find (const char *s) |
52 | { |
45 | { |
53 | if (!s) |
46 | if (!s) |
54 | return s; |
47 | return s; |
55 | |
48 | |
56 | HT::iterator i = ht.find (s); |
49 | auto (i, ht.find (s)); |
57 | |
50 | |
58 | return i != ht.end ()? *i : 0; |
51 | return i != ht.end ()? *i : 0; |
59 | } |
52 | } |
60 | |
53 | |
61 | const char * |
54 | const char * |
… | |
… | |
78 | // periodically test refcounts == 0 for a few strings |
71 | // periodically test refcounts == 0 for a few strings |
79 | // 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. |
80 | void |
73 | void |
81 | shstr::gc () |
74 | shstr::gc () |
82 | { |
75 | { |
83 | return; //D |
|
|
84 | //D currently disabled: some datastructures might still store them |
|
|
85 | //D but their pointers will become invalidated |
|
|
86 | static const char *curpos; |
76 | static const char *curpos; |
87 | |
77 | |
88 | HT::iterator i = curpos ? ht.find (curpos) : ht.begin (); |
78 | auto (i, curpos ? ht.find (curpos) : ht.begin ()); |
89 | |
79 | |
90 | if (i == ht.end ()) |
80 | if (i == ht.end ()) |
91 | i = ht.begin (); |
81 | i = ht.begin (); |
92 | |
82 | |
93 | // go through all strings roughly once every 4 minutes |
83 | // go through all strings roughly once every 4 minutes |
… | |
… | |
102 | } |
92 | } |
103 | else if (!--n) |
93 | else if (!--n) |
104 | break; |
94 | break; |
105 | else if (!refcnt (*i)) |
95 | else if (!refcnt (*i)) |
106 | { |
96 | { |
107 | HT::iterator o = i++; |
97 | auto (o, i++); |
108 | const char *s = *o; |
98 | const char *s = *o; |
109 | |
99 | |
110 | ht.erase (o); |
100 | ht.erase (o); |
111 | |
101 | |
112 | //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)); |
… | |
… | |
116 | ++i; |
106 | ++i; |
117 | } |
107 | } |
118 | |
108 | |
119 | curpos = *i; |
109 | curpos = *i; |
120 | } |
110 | } |
|
|
111 | |
|
|
112 | shstr skill_names[NUM_SKILLS]; |
|
|
113 | |
|
|
114 | // what weird misoptimisation is this again? |
|
|
115 | const shstr undead_name ("undead"); |
121 | |
116 | |
122 | //TODO: this should of course not be here |
117 | //TODO: this should of course not be here |
123 | |
118 | |
124 | /* 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 |
125 | * buf1 by adding on buf2! Returns true if overflow will occur. |
120 | * buf1 by adding on buf2! Returns true if overflow will occur. |