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