… | |
… | |
36 | typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *> > HT; |
36 | typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *> > HT; |
37 | |
37 | |
38 | static HT ht; |
38 | static HT ht; |
39 | static int next_gc; |
39 | static int next_gc; |
40 | |
40 | |
|
|
41 | #define NUM_INT 3 |
|
|
42 | |
41 | static const char * |
43 | static const char * |
42 | makevec (const char *s) |
44 | makevec (const char *s) |
43 | { |
45 | { |
44 | int len = strlen (s); |
46 | int len = strlen (s); |
45 | int alloc = sizeof (uint32_t) * 2 + len + 1; |
47 | int alloc = sizeof (uint32_t) * NUM_INT + len + 1; |
46 | |
48 | |
47 | shstr_alloc += alloc; |
49 | shstr_alloc += alloc; |
48 | char *v = (char *)g_slice_alloc (alloc); |
50 | char *v = (char *)g_slice_alloc (alloc); |
49 | v += sizeof (uint32_t) * 2; |
51 | v += sizeof (uint32_t) * NUM_INT; |
50 | |
52 | |
|
|
53 | shstr::hash (v) = strhsh (s); |
51 | shstr::length (v) = len; |
54 | shstr::length (v) = len; |
52 | shstr::refcnt (v) = 1; |
55 | shstr::refcnt (v) = 1; |
53 | |
56 | |
54 | memcpy (v, s, len + 1); |
57 | memcpy (v, s, len + 1); |
55 | |
58 | |
56 | return v; |
59 | return v; |
57 | } |
60 | } |
58 | |
61 | |
59 | shstr_vec<sizeof "(null)"> shstr_tmp::nullvec = { 0, 0xffffffff, "(null)" }; |
62 | shstr_vec<sizeof "(null)"> shstr_tmp::nullvec = { STRHSH_NULL, 0, 0xffffffff, "(null)" }; |
60 | |
63 | |
61 | const char * |
64 | const char * |
62 | shstr::find (const char *s) |
65 | shstr::find (const char *s) |
63 | { |
66 | { |
64 | if (!s) |
67 | if (!s) |
… | |
… | |
120 | const char *s = *o; |
123 | const char *s = *o; |
121 | |
124 | |
122 | ht.erase (o); |
125 | ht.erase (o); |
123 | |
126 | |
124 | //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |
127 | //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |
125 | int alloc = sizeof (uint32_t) * 2 + length (s) + 1; |
128 | int alloc = sizeof (uint32_t) * NUM_INT + length (s) + 1; |
126 | shstr_alloc -= alloc; |
129 | shstr_alloc -= alloc; |
127 | g_slice_free1 (alloc, (void *)(s - sizeof (uint32_t) * 2)); |
130 | g_slice_free1 (alloc, (void *)(s - sizeof (uint32_t) * NUM_INT)); |
128 | } |
131 | } |
129 | else |
132 | else |
130 | ++i; |
133 | ++i; |
131 | } |
134 | } |
132 | |
135 | |
… | |
… | |
140 | #undef def |
143 | #undef def |
141 | #undef def2 |
144 | #undef def2 |
142 | |
145 | |
143 | shstr skill_names[NUM_SKILLS]; |
146 | shstr skill_names[NUM_SKILLS]; |
144 | |
147 | |
145 | //TODO: this should of course not be here |
|
|
146 | |
|
|
147 | /* buf_overflow() - we don't want to exceed the buffer size of |
|
|
148 | * buf1 by adding on buf2! Returns true if overflow will occur. |
|
|
149 | */ |
|
|
150 | int |
|
|
151 | buf_overflow (const char *buf1, const char *buf2, int bufsize) |
|
|
152 | { |
|
|
153 | int len1 = 0, len2 = 0; |
|
|
154 | |
|
|
155 | if (buf1) |
|
|
156 | len1 = strlen (buf1); |
|
|
157 | |
|
|
158 | if (buf2) |
|
|
159 | len2 = strlen (buf2); |
|
|
160 | |
|
|
161 | if ((len1 + len2) >= bufsize) |
|
|
162 | return 1; |
|
|
163 | |
|
|
164 | return 0; |
|
|
165 | } |
|
|