… | |
… | |
76 | shstr::find (const char *s) |
76 | shstr::find (const char *s) |
77 | { |
77 | { |
78 | if (!s) |
78 | if (!s) |
79 | return s; |
79 | return s; |
80 | |
80 | |
81 | auto (i, ht.find (s)); |
81 | auto i = ht.find (s); |
82 | |
82 | |
83 | return i != ht.end ()? *i : 0; |
83 | return i != ht.end ()? *i : 0; |
84 | } |
84 | } |
85 | |
85 | |
86 | const char * |
86 | const char * |
… | |
… | |
104 | // periodically test refcounts == 0 for a few strings |
104 | // periodically test refcounts == 0 for a few strings |
105 | // this is the ONLY thing that erases stuff from ht. keep it that way. |
105 | // this is the ONLY thing that erases stuff from ht. keep it that way. |
106 | void |
106 | void |
107 | shstr::gc () |
107 | shstr::gc () |
108 | { |
108 | { |
109 | if (expect_true (next_gc > 0)) |
109 | if (ecb_expect_true (--next_gc > 0)) |
110 | return; |
110 | return; |
111 | |
111 | |
112 | static const char *curpos; |
112 | static const char *curpos; |
113 | |
113 | |
114 | auto (i, curpos ? ht.find (curpos) : ht.begin ()); |
114 | auto i = curpos ? ht.find (curpos) : ht.begin (); |
115 | |
115 | |
116 | if (i == ht.end ()) |
116 | if (i == ht.end ()) |
117 | i = ht.begin (); |
117 | i = ht.begin (); |
118 | |
118 | |
119 | int n = ht.size () / 256 + 16; |
119 | int n = ht.size () / 256 + 16; |
… | |
… | |
128 | } |
128 | } |
129 | else if (!--n) |
129 | else if (!--n) |
130 | break; |
130 | break; |
131 | else if (!refcnt (*i)) |
131 | else if (!refcnt (*i)) |
132 | { |
132 | { |
133 | auto (o, i++); |
|
|
134 | const char *s = *o; |
133 | const char *s = *i; |
135 | |
134 | |
136 | ht.erase (o); |
135 | i = ht.erase (i); |
137 | |
136 | |
138 | //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |
137 | //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |
139 | int alloc = sizeof (uint32_t) * NUM_INT + length (s) + 1; |
138 | int alloc = sizeof (uint32_t) * NUM_INT + length (s) + 1; |
140 | shstr_alloc -= alloc; |
139 | shstr_alloc -= alloc; |
141 | g_slice_free1 (alloc, (void *)(s - sizeof (uint32_t) * NUM_INT)); |
140 | g_slice_free1 (alloc, (void *)(s - sizeof (uint32_t) * NUM_INT)); |