Mercurial > projects > ddmd
annotate dmd/OutBuffer.d @ 168:ceed63f310fb
stringtable, stringbuffer and freelist moved to Global
author | korDen |
---|---|
date | Thu, 30 Sep 2010 12:57:13 +0400 |
parents | e28b18c23469 |
children | 94b6033c07f3 |
rev | line source |
---|---|
0 | 1 module dmd.OutBuffer; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import std.stdarg; |
34 | 5 import std.exception; |
0 | 6 |
7 import core.stdc.stdlib; | |
8 import core.stdc.string; | |
9 | |
4 | 10 import core.memory; |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
11 import core.stdc.stdlib; |
2 | 12 |
0 | 13 class OutBuffer |
14 { | |
15 ubyte* data; | |
16 uint offset; | |
17 uint size; | |
18 | |
19 this() | |
20 { | |
21 // do nothing | |
22 } | |
23 | |
24 final void* extractData() | |
25 { | |
26 void* p = cast(void*)data; | |
27 | |
28 data = null; | |
29 offset = 0; | |
30 size = 0; | |
31 | |
32 return p; | |
33 } | |
34 | |
35 void mark() | |
36 { | |
37 assert(false); | |
38 } | |
39 | |
40 final void reserve(uint nbytes) | |
41 { | |
42 //printf("OutBuffer::reserve: size = %d, offset = %d, nbytes = %d\n", size, offset, nbytes); | |
43 if (size - offset < nbytes) | |
44 { | |
45 size = (offset + nbytes) * 2; | |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
46 data = cast(ubyte*)realloc(data, size); |
0 | 47 } |
48 } | |
49 | |
50 final void setsize(uint size) | |
51 { | |
52 assert(false); | |
53 } | |
54 | |
55 final void reset() | |
56 { | |
57 offset = 0; | |
58 } | |
59 | |
60 final void write(const(void)* data, uint nbytes) | |
61 { | |
62 reserve(nbytes); | |
63 memcpy(this.data + offset, data, nbytes); | |
64 offset += nbytes; | |
65 } | |
66 | |
67 final void writebstring(ubyte* string_) | |
68 { | |
69 assert(false); | |
70 } | |
71 | |
72 final void writestring(const(char)[] string_) | |
73 { | |
74 write(string_.ptr , string_.length); | |
75 } | |
76 | |
77 final void writedstring(const(char)* string_) | |
78 { | |
79 assert(false); | |
80 } | |
81 | |
82 final void writedstring(const(wchar)* string_) | |
83 { | |
84 assert(false); | |
85 } | |
86 | |
87 final void prependstring(const(char)[] string_) | |
88 { | |
89 uint len = string_.length; | |
90 reserve(len); | |
91 memmove(data + len, data, offset); | |
92 memcpy(data, string_.ptr, len); | |
93 offset += len; | |
94 } | |
95 | |
96 final void writenl() // write newline | |
97 { | |
114 | 98 version (Windows) |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
99 { |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
100 version (M_UNICODE) |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
101 { |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
102 write4(0x000A000D); // newline is CR,LF on Microsoft OS's |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
103 } |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
104 else |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
105 { |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
106 writeword(0x0A0D); // newline is CR,LF on Microsoft OS's |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
107 } |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
108 } |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
109 else |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
110 { |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
111 version (M_UNICODE) |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
112 { |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
113 writeword('\n'); |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
114 } |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
115 else |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
116 { |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
117 writeByte('\n'); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
118 } |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
119 } |
0 | 120 } |
121 | |
122 final void writeByte(uint b) | |
123 { | |
124 reserve(1); | |
125 this.data[offset] = cast(ubyte)b; | |
126 offset++; | |
127 } | |
128 | |
129 final void writebyte(uint b) { writeByte(b); } | |
130 | |
131 final void writeUTF8(uint b) | |
132 { | |
133 reserve(6); | |
134 if (b <= 0x7F) | |
135 { | |
136 this.data[offset] = cast(ubyte)b; | |
137 offset++; | |
138 } | |
139 else if (b <= 0x7FF) | |
140 { | |
141 this.data[offset + 0] = cast(ubyte)((b >> 6) | 0xC0); | |
142 this.data[offset + 1] = cast(ubyte)((b & 0x3F) | 0x80); | |
143 offset += 2; | |
144 } | |
145 else if (b <= 0xFFFF) | |
146 { | |
147 this.data[offset + 0] = cast(ubyte)((b >> 12) | 0xE0); | |
148 this.data[offset + 1] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); | |
149 this.data[offset + 2] = cast(ubyte)((b & 0x3F) | 0x80); | |
150 offset += 3; | |
151 } | |
152 else if (b <= 0x1FFFFF) | |
153 { | |
154 this.data[offset + 0] = cast(ubyte)((b >> 18) | 0xF0); | |
155 this.data[offset + 1] = cast(ubyte)(((b >> 12) & 0x3F) | 0x80); | |
156 this.data[offset + 2] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); | |
157 this.data[offset + 3] = cast(ubyte)((b & 0x3F) | 0x80); | |
158 offset += 4; | |
159 } | |
160 else if (b <= 0x3FFFFFF) | |
161 { | |
162 this.data[offset + 0] = cast(ubyte)((b >> 24) | 0xF8); | |
163 this.data[offset + 1] = cast(ubyte)(((b >> 18) & 0x3F) | 0x80); | |
164 this.data[offset + 2] = cast(ubyte)(((b >> 12) & 0x3F) | 0x80); | |
165 this.data[offset + 3] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); | |
166 this.data[offset + 4] = cast(ubyte)((b & 0x3F) | 0x80); | |
167 offset += 5; | |
168 } | |
169 else if (b <= 0x7FFFFFFF) | |
170 { | |
171 this.data[offset + 0] = cast(ubyte)((b >> 30) | 0xFC); | |
172 this.data[offset + 1] = cast(ubyte)(((b >> 24) & 0x3F) | 0x80); | |
173 this.data[offset + 2] = cast(ubyte)(((b >> 18) & 0x3F) | 0x80); | |
174 this.data[offset + 3] = cast(ubyte)(((b >> 12) & 0x3F) | 0x80); | |
175 this.data[offset + 4] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); | |
176 this.data[offset + 5] = cast(ubyte)((b & 0x3F) | 0x80); | |
177 offset += 6; | |
178 } | |
179 else | |
180 assert(0); | |
181 } | |
182 | |
183 final void writedchar(uint b) | |
184 { | |
185 assert(false); | |
186 } | |
187 | |
188 final void prependbyte(uint b) | |
189 { | |
190 assert(false); | |
191 } | |
192 | |
193 final void writeword(uint w) | |
194 { | |
195 reserve(2); | |
196 *cast(ushort*)(this.data + offset) = cast(ushort)w; | |
197 offset += 2; | |
198 } | |
199 | |
200 final void writeUTF16(uint w) | |
201 { | |
202 reserve(4); | |
203 if (w <= 0xFFFF) | |
204 { | |
205 *cast(ushort*)(this.data + offset) = cast(ushort)w; | |
206 offset += 2; | |
207 } | |
208 else if (w <= 0x10FFFF) | |
209 { | |
210 *cast(ushort*)(this.data + offset) = cast(ushort)((w >> 10) + 0xD7C0); | |
211 *cast(ushort*)(this.data + offset + 2) = cast(ushort)((w & 0x3FF) | 0xDC00); | |
212 offset += 4; | |
213 } | |
214 else | |
215 assert(0); | |
216 } | |
217 | |
218 final void write4(uint w) | |
219 { | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
5
diff
changeset
|
220 reserve(4); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
5
diff
changeset
|
221 *cast(uint*)(this.data + offset) = w; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
5
diff
changeset
|
222 offset += 4; |
0 | 223 } |
224 | |
225 final void write(OutBuffer buf) | |
226 { | |
227 if (buf) | |
228 { | |
229 reserve(buf.offset); | |
230 memcpy(data + offset, buf.data, buf.offset); | |
231 offset += buf.offset; | |
232 } | |
233 } | |
234 | |
235 final void write(Object obj) | |
236 { | |
237 assert(false); | |
238 } | |
239 | |
240 final void fill0(uint nbytes) | |
241 { | |
242 reserve(nbytes); | |
243 memset(data + offset, 0, nbytes); | |
244 offset += nbytes; | |
245 } | |
246 | |
247 final void align_(uint size) | |
248 { | |
249 assert(false); | |
250 } | |
251 | |
252 void vprintf(const(char)* format, va_list args) | |
253 { | |
254 assert(false); | |
255 } | |
256 | |
257 void printf(T...)(string format, T t) | |
258 { | |
259 string s = std.string.format(format, t); | |
260 writestring(s); | |
261 } | |
262 | |
263 version (M_UNICODE) { | |
264 /// void vprintf(const uint short *format, va_list args); | |
265 /// void printf(const uint short *format, ...); | |
266 } | |
267 final void bracket(char left, char right) | |
268 { | |
269 assert(false); | |
270 } | |
271 | |
272 final uint bracket(uint i, const(char)* left, uint j, const(char)* right) | |
273 { | |
274 assert(false); | |
275 } | |
276 | |
277 final void spread(uint offset, uint nbytes) | |
278 { | |
279 assert(false); | |
280 } | |
281 | |
282 final uint insert(uint offset, const(void)* data, uint nbytes) | |
283 { | |
284 assert(false); | |
285 } | |
286 | |
287 final void remove(uint offset, uint nbytes) | |
288 { | |
289 assert(false); | |
290 } | |
291 | |
292 string toChars() | |
293 { | |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
294 char[] s = getString(); |
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
295 char* copy = cast(char*)malloc(s.length); |
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
296 memcpy(copy, s.ptr, s.length); |
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
297 return assumeUnique(copy[0..s.length]); |
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
298 |
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
299 //return getString().idup; |
0 | 300 } |
301 | |
302 final string extractString() | |
303 { | |
304 char[] s = getString(); | |
305 data = null; | |
306 offset = 0; | |
307 size = 0; | |
308 | |
309 return assumeUnique(s); | |
310 } | |
311 | |
312 final char[] getString() | |
313 { | |
314 char* s = cast(char*)data; | |
315 return s[0..offset]; | |
316 } | |
317 } |