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 | |
42 | |
48 | const char * |
43 | const char * |
49 | shstr::find (const char *s) |
44 | shstr::find (const char *s) |
50 | { |
45 | { |
51 | if (!s) |
46 | if (!s) |
… | |
… | |
76 | // periodically test refcounts == 0 for a few strings |
71 | // periodically test refcounts == 0 for a few strings |
77 | // 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. |
78 | void |
73 | void |
79 | shstr::gc () |
74 | shstr::gc () |
80 | { |
75 | { |
81 | return; //D |
|
|
82 | //D currently disabled: some datastructures might still store them |
|
|
83 | //D but their pointers will become invalidated |
|
|
84 | static const char *curpos; |
76 | static const char *curpos; |
85 | |
77 | |
86 | HT::iterator i = curpos ? ht.find (curpos) : ht.begin (); |
78 | HT::iterator i = curpos ? ht.find (curpos) : ht.begin (); |
87 | |
79 | |
88 | if (i == ht.end ()) |
80 | if (i == ht.end ()) |
… | |
… | |
115 | } |
107 | } |
116 | |
108 | |
117 | curpos = *i; |
109 | curpos = *i; |
118 | } |
110 | } |
119 | |
111 | |
|
|
112 | shstr skill_names[NUM_SKILLS]; |
|
|
113 | |
|
|
114 | // what weird misoptimisation is this again? |
|
|
115 | const shstr undead_name ("undead"); |
|
|
116 | |
120 | //TODO: this should of course not be here |
117 | //TODO: this should of course not be here |
121 | |
118 | |
122 | /* 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 |
123 | * buf1 by adding on buf2! Returns true if overflow will occur. |
120 | * buf1 by adding on buf2! Returns true if overflow will occur. |
124 | */ |
121 | */ |