… | |
… | |
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 (unsigned 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 (unsigned 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_tmp::length (s) = 0; |
|
|
61 | //shstr ::refcnt (s) = 0xffffffff; // useful for debugging |
|
|
62 | return s; |
|
|
63 | } |
|
|
64 | |
|
|
65 | const char *shstr_tmp::null = makenull (); |
|
|
66 | |
57 | |
67 | const char * |
58 | const char * |
68 | shstr::find (const char *s) |
59 | shstr::find (const char *s) |
69 | { |
60 | { |
70 | if (!s) |
61 | if (!s) |
… | |
… | |
77 | |
68 | |
78 | const char * |
69 | const char * |
79 | shstr::intern (const char *s) |
70 | shstr::intern (const char *s) |
80 | { |
71 | { |
81 | if (!s) |
72 | if (!s) |
82 | return null; |
73 | return null (); |
83 | |
74 | |
84 | if (const char *found = find (s)) |
75 | if (const char *found = find (s)) |
85 | { |
76 | { |
86 | ++refcnt (found); |
77 | ++refcnt (found); |
87 | return found; |
78 | return found; |
… | |
… | |
126 | const char *s = *o; |
117 | const char *s = *o; |
127 | |
118 | |
128 | ht.erase (o); |
119 | ht.erase (o); |
129 | |
120 | |
130 | //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)); |
131 | shstr_alloc -= sizeof (unsigned int) * 2 + length (s) + 1; |
122 | shstr_alloc -= sizeof (uint32_t) * 2 + length (s) + 1; |
132 | g_slice_free1 (sizeof (unsigned int) * 2 + length (s) + 1, -2 + (int *) s); |
123 | g_slice_free1 (sizeof (uint32_t) * 2 + length (s) + 1, -2 + (int *) s); |
133 | } |
124 | } |
134 | else |
125 | else |
135 | ++i; |
126 | ++i; |
136 | } |
127 | } |
137 | |
128 | |
138 | curpos = *i; |
129 | curpos = *i; |
139 | } |
130 | } |
140 | |
|
|
141 | const shstr shstr_null; |
|
|
142 | |
131 | |
143 | // declare these here to get correct initialisation order |
132 | // declare these here to get correct initialisation order |
144 | #define def(str) const shstr shstr_ ## str (# str); |
133 | #define def(str) const shstr shstr_ ## str (# str); |
145 | # include "shstrinc.h" |
134 | # include "shstrinc.h" |
146 | #undef def |
135 | #undef def |