1 | |
|
|
2 | /* |
1 | /* |
3 | * shstr.C |
2 | * shstr.C |
4 | */ |
3 | */ |
5 | |
4 | |
6 | #include <cstring> |
5 | #include <cstring> |
… | |
… | |
10 | |
9 | |
11 | #include <tr1/unordered_set> |
10 | #include <tr1/unordered_set> |
12 | |
11 | |
13 | #include "global.h" |
12 | #include "global.h" |
14 | |
13 | |
15 | typedef std::tr1::unordered_set<const char *, str_hash, str_equal> HT; |
14 | typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *>, true> HT; |
16 | |
15 | |
17 | static HT ht; |
16 | static HT ht; |
18 | |
17 | |
19 | static const char * |
18 | static const char * |
20 | makevec (const char *s) |
19 | makevec (const char *s) |
21 | { |
20 | { |
22 | int len = strlen (s); |
21 | int len = strlen (s); |
23 | |
22 | |
24 | const char *v = (const char *) (2 + (int *) g_slice_alloc (sizeof (int) * 2 + len + 1)); |
23 | const char *v = (const char *) (2 + (int *)g_slice_alloc (sizeof (int) * 2 + len + 1)); |
25 | |
24 | |
26 | shstr::length (v) = len; |
25 | shstr::length (v) = len; |
27 | shstr::refcnt (v) = 1; |
26 | shstr::refcnt (v) = 1; |
28 | |
27 | |
29 | memcpy ((char *) v, s, len + 1); |
28 | memcpy ((char *) v, s, len + 1); |
… | |
… | |
45 | shstr::find (const char *s) |
44 | shstr::find (const char *s) |
46 | { |
45 | { |
47 | if (!s) |
46 | if (!s) |
48 | return s; |
47 | return s; |
49 | |
48 | |
50 | HT::iterator i = ht.find (s); |
49 | AUTODECL (i, ht.find (s)); |
51 | |
50 | |
52 | return i != ht.end ()? *i : 0; |
51 | return i != ht.end ()? *i : 0; |
53 | } |
52 | } |
54 | |
53 | |
55 | const char * |
54 | const char * |
… | |
… | |
72 | // periodically test refcounts == 0 for a few strings |
71 | // periodically test refcounts == 0 for a few strings |
73 | // this is the ONLY thing that erases stuff from ht. keep it that way. |
72 | // this is the ONLY thing that erases stuff from ht. keep it that way. |
74 | void |
73 | void |
75 | shstr::gc () |
74 | shstr::gc () |
76 | { |
75 | { |
77 | return; //D |
|
|
78 | //D currently disabled: some datastructures might still store them |
|
|
79 | //D but their pointers will become invalidated |
|
|
80 | static const char *curpos; |
76 | static const char *curpos; |
81 | |
77 | |
82 | HT::iterator i = curpos ? ht.find (curpos) : ht.begin (); |
78 | AUTODECL (i, curpos ? ht.find (curpos) : ht.begin ()); |
83 | |
79 | |
84 | if (i == ht.end ()) |
80 | if (i == ht.end ()) |
85 | i = ht.begin (); |
81 | i = ht.begin (); |
86 | |
82 | |
87 | // go through all strings roughly once every 4 minutes |
83 | // go through all strings roughly once every 4 minutes |
… | |
… | |
96 | } |
92 | } |
97 | else if (!--n) |
93 | else if (!--n) |
98 | break; |
94 | break; |
99 | else if (!refcnt (*i)) |
95 | else if (!refcnt (*i)) |
100 | { |
96 | { |
101 | HT::iterator o = i++; |
97 | AUTODECL (o, i++); |
102 | const char *s = *o; |
98 | const char *s = *o; |
103 | |
99 | |
104 | ht.erase (o); |
100 | ht.erase (o); |
105 | |
101 | |
106 | //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |
102 | //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |