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 |
|
|
23 | len = strlen (s); |
21 | int len = strlen (s); |
24 | |
22 | |
25 | const char * |
|
|
26 | 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)); |
27 | |
24 | |
28 | shstr::length (v) = len; |
25 | shstr::length (v) = len; |
29 | shstr::refcnt (v) = 1; |
26 | shstr::refcnt (v) = 1; |
30 | |
27 | |
31 | memcpy ((char *) v, s, len + 1); |
28 | memcpy ((char *) v, s, len + 1); |
32 | |
29 | |
33 | return v; |
30 | return v; |
34 | } |
31 | } |
35 | |
32 | |
|
|
33 | static const char * |
|
|
34 | makenull () |
|
|
35 | { |
|
|
36 | const char *s = makevec ("(null)"); |
|
|
37 | shstr::length (s) = 0; |
|
|
38 | return s; |
|
|
39 | } |
|
|
40 | |
36 | const char *shstr::null = makevec ("<nil>"); |
41 | const char *shstr::null = makenull (); |
37 | |
42 | |
38 | const char * |
43 | const char * |
39 | shstr::find (const char *s) |
44 | shstr::find (const char *s) |
40 | { |
45 | { |
41 | if (!s) |
46 | if (!s) |
42 | return s; |
47 | return s; |
43 | |
48 | |
44 | HT::iterator i = ht.find (s); |
49 | auto (i, ht.find (s)); |
45 | |
50 | |
46 | return i != ht.end ()? *i : 0; |
51 | return i != ht.end ()? *i : 0; |
47 | } |
52 | } |
48 | |
53 | |
49 | const char * |
54 | const char * |
… | |
… | |
66 | // periodically test refcounts == 0 for a few strings |
71 | // periodically test refcounts == 0 for a few strings |
67 | // 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. |
68 | void |
73 | void |
69 | shstr::gc () |
74 | shstr::gc () |
70 | { |
75 | { |
71 | return; //D |
|
|
72 | //D currently disabled: some datastructures might still store them |
|
|
73 | //D but their pointers will become invalidated |
|
|
74 | static const char *curpos; |
76 | static const char *curpos; |
75 | |
77 | |
76 | HT::iterator i = curpos ? ht.find (curpos) : ht.begin (); |
78 | auto (i, curpos ? ht.find (curpos) : ht.begin ()); |
77 | |
79 | |
78 | if (i == ht.end ()) |
80 | if (i == ht.end ()) |
79 | i = ht.begin (); |
81 | i = ht.begin (); |
80 | |
82 | |
81 | // go through all strings roughly once every 4 minutes |
83 | // go through all strings roughly once every 4 minutes |
… | |
… | |
90 | } |
92 | } |
91 | else if (!--n) |
93 | else if (!--n) |
92 | break; |
94 | break; |
93 | else if (!refcnt (*i)) |
95 | else if (!refcnt (*i)) |
94 | { |
96 | { |
95 | HT::iterator o = i++; |
97 | auto (o, i++); |
96 | const char *s = *o; |
98 | const char *s = *o; |
97 | |
99 | |
98 | ht.erase (o); |
100 | ht.erase (o); |
99 | |
101 | |
100 | //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)); |