… | |
… | |
30 | |
30 | |
31 | #include "global.h" |
31 | #include "global.h" |
32 | |
32 | |
33 | size_t shstr_alloc; |
33 | size_t shstr_alloc; |
34 | |
34 | |
35 | typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *>, true> HT; |
35 | typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *> > HT; |
36 | |
36 | |
37 | static HT ht; |
37 | static HT ht; |
38 | static int next_gc; |
38 | static int next_gc; |
39 | |
39 | |
40 | static const char * |
40 | static const char * |
41 | makevec (const char *s) |
41 | makevec (const char *s) |
42 | { |
42 | { |
43 | int len = strlen (s); |
43 | int len = strlen (s); |
|
|
44 | int alloc = sizeof (uint32_t) * 2 + len + 1; |
44 | |
45 | |
45 | shstr_alloc += sizeof (int) * 2 + len + 1; |
46 | shstr_alloc += alloc; |
46 | const char *v = (const char *) (2 + (int *)g_slice_alloc (sizeof (int) * 2 + len + 1)); |
47 | char *v = (char *)g_slice_alloc (alloc); |
|
|
48 | v += sizeof (uint32_t) * 2; |
47 | |
49 | |
48 | shstr::length (v) = len; |
50 | shstr::length (v) = len; |
49 | shstr::refcnt (v) = 1; |
51 | shstr::refcnt (v) = 1; |
50 | |
52 | |
51 | memcpy ((char *) v, s, len + 1); |
53 | memcpy (v, s, len + 1); |
52 | |
54 | |
53 | return v; |
55 | return v; |
54 | } |
56 | } |
55 | |
57 | |
56 | static const char * |
58 | shstr_vec<sizeof "(null)"> shstr_tmp::nullvec = { 0, 0xffffffff, "(null)" }; |
57 | makenull () |
|
|
58 | { |
|
|
59 | const char *s = makevec ("(null)"); |
|
|
60 | shstr::length (s) = 0; |
|
|
61 | return s; |
|
|
62 | } |
|
|
63 | |
|
|
64 | const char *shstr::null = makenull (); |
|
|
65 | |
59 | |
66 | const char * |
60 | const char * |
67 | shstr::find (const char *s) |
61 | shstr::find (const char *s) |
68 | { |
62 | { |
69 | if (!s) |
63 | if (!s) |
… | |
… | |
76 | |
70 | |
77 | const char * |
71 | const char * |
78 | shstr::intern (const char *s) |
72 | shstr::intern (const char *s) |
79 | { |
73 | { |
80 | if (!s) |
74 | if (!s) |
81 | return null; |
75 | return null (); |
82 | |
76 | |
83 | if (const char *found = find (s)) |
77 | if (const char *found = find (s)) |
84 | { |
78 | { |
85 | ++refcnt (found); |
79 | ++refcnt (found); |
86 | return found; |
80 | return found; |
… | |
… | |
125 | const char *s = *o; |
119 | const char *s = *o; |
126 | |
120 | |
127 | ht.erase (o); |
121 | ht.erase (o); |
128 | |
122 | |
129 | //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |
123 | //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |
130 | shstr_alloc -= sizeof (int) * 2 + length (s) + 1; |
124 | int alloc = sizeof (uint32_t) * 2 + length (s) + 1; |
131 | g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s); |
125 | shstr_alloc -= alloc; |
|
|
126 | g_slice_free1 (alloc, (void *)(s - sizeof (uint32_t) * 2)); |
132 | } |
127 | } |
133 | else |
128 | else |
134 | ++i; |
129 | ++i; |
135 | } |
130 | } |
136 | |
131 | |
137 | curpos = *i; |
132 | curpos = *i; |
138 | } |
133 | } |
139 | |
134 | |
140 | // declare these here to get correct initialisation order |
135 | // declare these here to get correct initialisation order |
141 | #define def(str) const shstr shstr_ ## str (# str); |
136 | #define def2(id,str) const shstr id (str); |
|
|
137 | #define def(id) def2(shstr_ ## id, # id) |
142 | # include "shstrinc.h" |
138 | # include "shstrinc.h" |
143 | #undef def |
139 | #undef def |
|
|
140 | #undef def2 |
144 | |
141 | |
145 | shstr skill_names[NUM_SKILLS]; |
142 | shstr skill_names[NUM_SKILLS]; |
146 | |
143 | |
147 | //TODO: this should of course not be here |
144 | //TODO: this should of course not be here |
148 | |
145 | |