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