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