… | |
… | |
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 | |
44 | |
45 | shstr_alloc += sizeof (int) * 2 + len + 1; |
45 | shstr_alloc += sizeof (unsigned int) * 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 (unsigned int) * 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); |
… | |
… | |
55 | |
55 | |
56 | static const char * |
56 | static const char * |
57 | makenull () |
57 | makenull () |
58 | { |
58 | { |
59 | const char *s = makevec ("(null)"); |
59 | const char *s = makevec ("(null)"); |
60 | shstr::length (s) = 0; |
60 | shstr_tmp::length (s) = 0; |
|
|
61 | //shstr ::refcnt (s) = 0xffffffff; // useful for debugging |
61 | return s; |
62 | return s; |
62 | } |
63 | } |
63 | |
64 | |
64 | const char *shstr::null = makenull (); |
65 | const char *shstr_tmp::null = makenull (); |
65 | |
66 | |
66 | const char * |
67 | const char * |
67 | shstr::find (const char *s) |
68 | shstr::find (const char *s) |
68 | { |
69 | { |
69 | if (!s) |
70 | if (!s) |
… | |
… | |
125 | const char *s = *o; |
126 | const char *s = *o; |
126 | |
127 | |
127 | ht.erase (o); |
128 | ht.erase (o); |
128 | |
129 | |
129 | //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |
130 | //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; |
131 | shstr_alloc -= sizeof (unsigned int) * 2 + length (s) + 1; |
131 | g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s); |
132 | g_slice_free1 (sizeof (unsigned int) * 2 + length (s) + 1, -2 + (int *) s); |
132 | } |
133 | } |
133 | else |
134 | else |
134 | ++i; |
135 | ++i; |
135 | } |
136 | } |
136 | |
137 | |
137 | curpos = *i; |
138 | curpos = *i; |
138 | } |
139 | } |
|
|
140 | |
|
|
141 | const shstr shstr_null; |
139 | |
142 | |
140 | // declare these here to get correct initialisation order |
143 | // declare these here to get correct initialisation order |
141 | #define def(str) const shstr shstr_ ## str (# str); |
144 | #define def(str) const shstr shstr_ ## str (# str); |
142 | # include "shstrinc.h" |
145 | # include "shstrinc.h" |
143 | #undef def |
146 | #undef def |