… | |
… | |
46 | |
46 | |
47 | typedef std::tr1::unordered_set<const char *, hash, equal> HT; |
47 | typedef std::tr1::unordered_set<const char *, hash, equal> HT; |
48 | |
48 | |
49 | static HT ht; |
49 | static HT ht; |
50 | |
50 | |
|
|
51 | static const char *makevec (const char *s) |
|
|
52 | { |
|
|
53 | int len = strlen (s); |
|
|
54 | |
|
|
55 | const char *v = (const char *)(2 + (int *)malloc (sizeof (int) * 2 + len + 1)); |
|
|
56 | |
|
|
57 | shstr::length (v) = len; |
|
|
58 | shstr::refcnt (v) = 1; |
|
|
59 | |
|
|
60 | memcpy ((char *)v, s, len + 1); |
|
|
61 | |
|
|
62 | return v; |
|
|
63 | } |
|
|
64 | |
|
|
65 | const char *shstr::null = makevec ("<nil>"); |
|
|
66 | |
|
|
67 | // what weird misoptimisation is this again? |
|
|
68 | const shstr undead_name ("undead"); |
|
|
69 | |
51 | const char * |
70 | const char * |
52 | shstr::find (const char *s) |
71 | shstr::find (const char *s) |
53 | { |
72 | { |
|
|
73 | if (!s) |
|
|
74 | return s; |
|
|
75 | |
54 | HT::iterator i = ht.find (s); |
76 | HT::iterator i = ht.find (s); |
55 | |
77 | |
56 | return i != ht.end () |
78 | return i != ht.end () |
57 | ? (char *)*i |
79 | ? *i |
58 | : 0; |
80 | : 0; |
59 | } |
81 | } |
60 | |
82 | |
61 | const char * |
83 | const char * |
62 | shstr::intern (const char *s) |
84 | shstr::intern (const char *s) |
63 | { |
85 | { |
64 | HT::iterator i = ht.find (s); |
86 | if (!s) |
|
|
87 | return null; |
65 | |
88 | |
66 | if (i != ht.end ()) |
89 | if (const char *found = find (s)) |
67 | return (char *)*i; |
90 | { |
|
|
91 | ++refcnt (found); |
|
|
92 | return found; |
|
|
93 | } |
68 | |
94 | |
69 | int len = strlen (s); |
95 | s = makevec (s); |
70 | |
|
|
71 | int *v = (int *)malloc (sizeof (int) * 2 + len + 1); |
|
|
72 | |
|
|
73 | v [0] = len; |
|
|
74 | v [1] = 0; |
|
|
75 | |
|
|
76 | v += 2; |
|
|
77 | |
|
|
78 | memcpy (v, s, len + 1); |
|
|
79 | |
|
|
80 | ht.insert ((char *)v); |
96 | ht.insert (s); |
81 | |
97 | return s; |
82 | return (char *)v; |
|
|
83 | } |
98 | } |
84 | |
99 | |
85 | // TODO: periodically test refcounts == 0 for a few strings (e.g. one hash bucket, |
100 | // periodically test refcounts == 0 for a few strings |
86 | // exploiting the fatc that iterators stay valid for unordered_set). |
101 | // this is the ONLY thing that erases stuff from ht. keep it that way. |
87 | void |
102 | void |
88 | shstr::gc () |
103 | shstr::gc () |
89 | { |
104 | { |
|
|
105 | static const char *curpos; |
|
|
106 | |
|
|
107 | HT::iterator i = curpos ? ht.find (curpos) : ht.begin (); |
|
|
108 | |
|
|
109 | if (i == ht.end ()) |
|
|
110 | i = ht.begin (); |
|
|
111 | |
|
|
112 | // go through all strings roughly once every 4 minutes |
|
|
113 | int n = ht.size () / 256 + 16; |
|
|
114 | |
|
|
115 | for (;;) |
|
|
116 | { |
|
|
117 | if (i == ht.end ()) |
|
|
118 | { |
|
|
119 | curpos = 0; |
|
|
120 | return; |
|
|
121 | } |
|
|
122 | else if (!--n) |
|
|
123 | break; |
|
|
124 | else if (!refcnt (*i)) |
|
|
125 | { |
|
|
126 | HT::iterator o = i++; |
|
|
127 | const char *s = *o; |
|
|
128 | ht.erase (o); |
|
|
129 | |
|
|
130 | //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); |
|
|
131 | free (-2 + (int *)s); |
|
|
132 | } |
|
|
133 | else |
|
|
134 | ++i; |
|
|
135 | } |
|
|
136 | |
|
|
137 | curpos = *i; |
90 | } |
138 | } |
91 | |
139 | |
|
|
140 | //TODO: this should of course not be here |
92 | /* buf_overflow() - we don't want to exceed the buffer size of |
141 | /* buf_overflow() - we don't want to exceed the buffer size of |
93 | * buf1 by adding on buf2! Returns true if overflow will occur. |
142 | * buf1 by adding on buf2! Returns true if overflow will occur. |
94 | */ |
143 | */ |
95 | |
144 | |
96 | int |
145 | int |