… | |
… | |
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 | |
44 | |
45 | shstr_alloc += sizeof (int) * 2 + len + 1; |
45 | shstr_alloc += sizeof (uint32_t) * 2 + len + 1; |
46 | const char *v = (const char *) (2 + (int *)g_slice_alloc (sizeof (int) * 2 + len + 1)); |
46 | const char *v = (const char *) (2 + (int *)g_slice_alloc (sizeof (uint32_t) * 2 + len + 1)); |
47 | |
47 | |
48 | shstr::length (v) = len; |
48 | shstr::length (v) = len; |
49 | shstr::refcnt (v) = 1; |
49 | shstr::refcnt (v) = 1; |
50 | |
50 | |
51 | memcpy ((char *) v, s, len + 1); |
51 | memcpy ((char *) v, s, len + 1); |
52 | |
52 | |
53 | return v; |
53 | return v; |
54 | } |
54 | } |
55 | |
55 | |
56 | static const char * |
56 | 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 | |
57 | |
66 | const char * |
58 | const char * |
67 | shstr::find (const char *s) |
59 | shstr::find (const char *s) |
68 | { |
60 | { |
69 | if (!s) |
61 | if (!s) |
… | |
… | |
76 | |
68 | |
77 | const char * |
69 | const char * |
78 | shstr::intern (const char *s) |
70 | shstr::intern (const char *s) |
79 | { |
71 | { |
80 | if (!s) |
72 | if (!s) |
81 | return null; |
73 | return null (); |
82 | |
74 | |
83 | if (const char *found = find (s)) |
75 | if (const char *found = find (s)) |
84 | { |
76 | { |
85 | ++refcnt (found); |
77 | ++refcnt (found); |
86 | return found; |
78 | return found; |
… | |
… | |
125 | const char *s = *o; |
117 | const char *s = *o; |
126 | |
118 | |
127 | ht.erase (o); |
119 | ht.erase (o); |
128 | |
120 | |
129 | //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |
121 | //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; |
122 | shstr_alloc -= sizeof (uint32_t) * 2 + length (s) + 1; |
131 | g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s); |
123 | g_slice_free1 (sizeof (uint32_t) * 2 + length (s) + 1, -2 + (int *) s); |
132 | } |
124 | } |
133 | else |
125 | else |
134 | ++i; |
126 | ++i; |
135 | } |
127 | } |
136 | |
128 | |
137 | curpos = *i; |
129 | curpos = *i; |
138 | } |
130 | } |
139 | |
131 | |
140 | const shstr shstr_null; |
|
|
141 | |
|
|
142 | // declare these here to get correct initialisation order |
132 | // declare these here to get correct initialisation order |
143 | #define def(str) const shstr shstr_ ## str (# str); |
133 | #define def2(id,str) const shstr shstr_ ## id (str); |
|
|
134 | #define def(id) def2(id, # id) |
144 | # include "shstrinc.h" |
135 | # include "shstrinc.h" |
145 | #undef def |
136 | #undef def |
|
|
137 | #undef def2 |
146 | |
138 | |
147 | shstr skill_names[NUM_SKILLS]; |
139 | shstr skill_names[NUM_SKILLS]; |
148 | |
140 | |
149 | //TODO: this should of course not be here |
141 | //TODO: this should of course not be here |
150 | |
142 | |