|
|
1 | |
1 | /* |
2 | /* |
2 | * shstr.C |
3 | * shstr.C |
3 | */ |
4 | */ |
4 | |
5 | |
5 | #include <cstring> |
6 | #include <cstring> |
… | |
… | |
7 | |
8 | |
8 | #include <glib.h> |
9 | #include <glib.h> |
9 | |
10 | |
10 | #include <tr1/unordered_set> |
11 | #include <tr1/unordered_set> |
11 | |
12 | |
12 | #include "shstr.h" |
|
|
13 | #include "util.h" |
13 | #include "global.h" |
14 | |
14 | |
15 | typedef std::tr1::unordered_set<const char *, str_hash, str_equal> HT; |
15 | typedef std::tr1::unordered_set<const char *, str_hash, str_equal> HT; |
16 | |
16 | |
17 | static HT ht; |
17 | static HT ht; |
18 | |
18 | |
19 | static const char *makevec (const char *s) |
19 | static const char * |
|
|
20 | makevec (const char *s) |
20 | { |
21 | { |
|
|
22 | int |
21 | int len = strlen (s); |
23 | len = strlen (s); |
22 | |
24 | |
|
|
25 | const char * |
23 | const char *v = (const char *)(2 + (int *)g_slice_alloc (sizeof (int) * 2 + len + 1)); |
26 | v = (const char *) (2 + (int *) g_slice_alloc (sizeof (int) * 2 + len + 1)); |
24 | |
27 | |
25 | shstr::length (v) = len; |
28 | shstr::length (v) = len; |
26 | shstr::refcnt (v) = 1; |
29 | shstr::refcnt (v) = 1; |
27 | |
30 | |
28 | memcpy ((char *)v, s, len + 1); |
31 | memcpy ((char *) v, s, len + 1); |
29 | |
32 | |
30 | return v; |
33 | return v; |
31 | } |
34 | } |
32 | |
35 | |
33 | const char *shstr::null = makevec ("<nil>"); |
36 | const char *shstr::null = makevec ("<nil>"); |
34 | |
|
|
35 | // what weird misoptimisation is this again? |
|
|
36 | const shstr undead_name ("undead"); |
|
|
37 | |
37 | |
38 | const char * |
38 | const char * |
39 | shstr::find (const char *s) |
39 | shstr::find (const char *s) |
40 | { |
40 | { |
41 | if (!s) |
41 | if (!s) |
42 | return s; |
42 | return s; |
43 | |
43 | |
44 | HT::iterator i = ht.find (s); |
44 | HT::iterator i = ht.find (s); |
45 | |
45 | |
46 | return i != ht.end () |
46 | return i != ht.end ()? *i : 0; |
47 | ? *i |
|
|
48 | : 0; |
|
|
49 | } |
47 | } |
50 | |
48 | |
51 | const char * |
49 | const char * |
52 | shstr::intern (const char *s) |
50 | shstr::intern (const char *s) |
53 | { |
51 | { |
… | |
… | |
68 | // periodically test refcounts == 0 for a few strings |
66 | // periodically test refcounts == 0 for a few strings |
69 | // this is the ONLY thing that erases stuff from ht. keep it that way. |
67 | // this is the ONLY thing that erases stuff from ht. keep it that way. |
70 | void |
68 | void |
71 | shstr::gc () |
69 | shstr::gc () |
72 | { |
70 | { |
|
|
71 | return; //D |
|
|
72 | //D currently disabled: some datastructures might still store them |
|
|
73 | //D but their pointers will become invalidated |
73 | static const char *curpos; |
74 | static const char *curpos; |
74 | |
75 | |
75 | HT::iterator i = curpos ? ht.find (curpos) : ht.begin (); |
76 | HT::iterator i = curpos ? ht.find (curpos) : ht.begin (); |
76 | |
77 | |
77 | if (i == ht.end ()) |
78 | if (i == ht.end ()) |
… | |
… | |
91 | break; |
92 | break; |
92 | else if (!refcnt (*i)) |
93 | else if (!refcnt (*i)) |
93 | { |
94 | { |
94 | HT::iterator o = i++; |
95 | HT::iterator o = i++; |
95 | const char *s = *o; |
96 | const char *s = *o; |
|
|
97 | |
96 | ht.erase (o); |
98 | ht.erase (o); |
97 | |
99 | |
98 | //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |
100 | //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |
99 | g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *)s); |
101 | g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s); |
100 | } |
102 | } |
101 | else |
103 | else |
102 | ++i; |
104 | ++i; |
103 | } |
105 | } |
104 | |
106 | |
105 | curpos = *i; |
107 | curpos = *i; |
106 | } |
108 | } |
107 | |
109 | |
|
|
110 | shstr skill_names[NUM_SKILLS]; |
|
|
111 | |
|
|
112 | // what weird misoptimisation is this again? |
|
|
113 | const shstr undead_name ("undead"); |
|
|
114 | |
108 | //TODO: this should of course not be here |
115 | //TODO: this should of course not be here |
|
|
116 | |
109 | /* buf_overflow() - we don't want to exceed the buffer size of |
117 | /* buf_overflow() - we don't want to exceed the buffer size of |
110 | * buf1 by adding on buf2! Returns true if overflow will occur. |
118 | * buf1 by adding on buf2! Returns true if overflow will occur. |
111 | */ |
119 | */ |
112 | |
120 | |
113 | int |
121 | int |
114 | buf_overflow (const char *buf1, const char *buf2, int bufsize) |
122 | buf_overflow (const char *buf1, const char *buf2, int bufsize) |
115 | { |
123 | { |
116 | int len1 = 0, len2 = 0; |
124 | int len1 = 0, len2 = 0; |
117 | |
125 | |
118 | if (buf1) |
126 | if (buf1) |
119 | len1 = strlen (buf1); |
127 | len1 = strlen (buf1); |
120 | if (buf2) |
128 | if (buf2) |
121 | len2 = strlen (buf2); |
129 | len2 = strlen (buf2); |
122 | if ((len1 + len2) >= bufsize) |
130 | if ((len1 + len2) >= bufsize) |
123 | return 1; |
|
|
124 | return 0; |
131 | return 1; |
|
|
132 | return 0; |
125 | } |
133 | } |
126 | |
|
|