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) |
52 | return s; |
47 | return s; |
53 | |
48 | |
54 | HT::iterator i = ht.find (s); |
49 | auto (i, ht.find (s)); |
55 | |
50 | |
56 | return i != ht.end ()? *i : 0; |
51 | return i != ht.end ()? *i : 0; |
57 | } |
52 | } |
58 | |
53 | |
59 | const char * |
54 | const char * |
… | |
… | |
78 | void |
73 | void |
79 | shstr::gc () |
74 | shstr::gc () |
80 | { |
75 | { |
81 | static const char *curpos; |
76 | static const char *curpos; |
82 | |
77 | |
83 | HT::iterator i = curpos ? ht.find (curpos) : ht.begin (); |
78 | auto (i, curpos ? ht.find (curpos) : ht.begin ()); |
84 | |
79 | |
85 | if (i == ht.end ()) |
80 | if (i == ht.end ()) |
86 | i = ht.begin (); |
81 | i = ht.begin (); |
87 | |
82 | |
88 | // go through all strings roughly once every 4 minutes |
83 | // go through all strings roughly once every 4 minutes |
… | |
… | |
97 | } |
92 | } |
98 | else if (!--n) |
93 | else if (!--n) |
99 | break; |
94 | break; |
100 | else if (!refcnt (*i)) |
95 | else if (!refcnt (*i)) |
101 | { |
96 | { |
102 | HT::iterator o = i++; |
97 | auto (o, i++); |
103 | const char *s = *o; |
98 | const char *s = *o; |
104 | |
99 | |
105 | ht.erase (o); |
100 | ht.erase (o); |
106 | |
101 | |
107 | //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)); |
… | |
… | |
111 | ++i; |
106 | ++i; |
112 | } |
107 | } |
113 | |
108 | |
114 | curpos = *i; |
109 | curpos = *i; |
115 | } |
110 | } |
|
|
111 | |
|
|
112 | shstr skill_names[NUM_SKILLS]; |
|
|
113 | |
|
|
114 | // what weird misoptimisation is this again? |
|
|
115 | const shstr undead_name ("undead"); |
116 | |
116 | |
117 | //TODO: this should of course not be here |
117 | //TODO: this should of course not be here |
118 | |
118 | |
119 | /* 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 |
120 | * buf1 by adding on buf2! Returns true if overflow will occur. |
120 | * buf1 by adding on buf2! Returns true if overflow will occur. |