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