comparison runtime/internal/lifetime.d @ 1038:c4c6e5e9c23e

Removed inline asm for overflow checking in runtime that wasn't working.
author Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
date Tue, 03 Mar 2009 19:37:47 +0100
parents 16264a3973bf
children 21691e437c95
comparison
equal deleted inserted replaced
1037:1d8a8ecf14e1 1038:c4c6e5e9c23e
80 PAGESIZE = 4096 80 PAGESIZE = 4096
81 } 81 }
82 82
83 alias bool function(Object) CollectHandler; 83 alias bool function(Object) CollectHandler;
84 CollectHandler collectHandler = null; 84 CollectHandler collectHandler = null;
85
86 size_t length_adjust(size_t sizeelem, size_t newlength)
87 {
88 size_t newsize = sizeelem * newlength;
89 if (newsize / newlength != sizeelem)
90 onOutOfMemoryError();
91 return newsize;
92 }
85 } 93 }
86 94
87 95
88 /** 96 /**
89 * 97 *
211 219
212 debug(PRINTF) printf("_d_newarrayT(length = %u, size = %d)\n", length, size); 220 debug(PRINTF) printf("_d_newarrayT(length = %u, size = %d)\n", length, size);
213 if (length == 0 || size == 0) 221 if (length == 0 || size == 0)
214 return null; 222 return null;
215 223
216 version (D_InlineAsm_X86) 224 size = length_adjust(size, length);
217 { 225
218 asm
219 {
220 mov EAX,size ;
221 mul EAX,length ;
222 mov size,EAX ;
223 jc Loverflow ;
224 }
225 }
226 else
227 size *= length;
228 p = gc_malloc(size + 1, !(ti.next.flags() & 1) ? BlkAttr.NO_SCAN : 0); 226 p = gc_malloc(size + 1, !(ti.next.flags() & 1) ? BlkAttr.NO_SCAN : 0);
229 debug(PRINTF) printf(" p = %p\n", p); 227 debug(PRINTF) printf(" p = %p\n", p);
230 memset(p, 0, size); 228 memset(p, 0, size);
229
231 return p; 230 return p;
232
233 Loverflow:
234 onOutOfMemoryError();
235 return null;
236 } 231 }
237 232
238 /** 233 /**
239 * As _d_newarrayT, but 234 * As _d_newarrayT, but
240 * for when the array has a non-zero initializer. 235 * for when the array has a non-zero initializer.
251 else 246 else
252 { 247 {
253 auto initializer = ti.next.init(); 248 auto initializer = ti.next.init();
254 auto isize = initializer.length; 249 auto isize = initializer.length;
255 auto q = initializer.ptr; 250 auto q = initializer.ptr;
256 version (D_InlineAsm_X86) 251
257 { 252 size = length_adjust(size, length);
258 asm 253
259 {
260 mov EAX,size ;
261 mul EAX,length ;
262 mov size,EAX ;
263 jc Loverflow ;
264 }
265 }
266 else
267 size *= length;
268 auto p = gc_malloc(size + 1, !(ti.next.flags() & 1) ? BlkAttr.NO_SCAN : 0); 254 auto p = gc_malloc(size + 1, !(ti.next.flags() & 1) ? BlkAttr.NO_SCAN : 0);
269 debug(PRINTF) printf(" p = %p\n", p); 255 debug(PRINTF) printf(" p = %p\n", p);
270 if (isize == 1) 256 if (isize == 1)
271 memset(p, *cast(ubyte*)q, size); 257 memset(p, *cast(ubyte*)q, size);
272 else if (isize == int.sizeof) 258 else if (isize == int.sizeof)
286 } 272 }
287 } 273 }
288 result = p; 274 result = p;
289 } 275 }
290 return result; 276 return result;
291
292 Loverflow:
293 onOutOfMemoryError();
294 return null;
295 } 277 }
296 278
297 /** 279 /**
298 * As _d_newarrayT, but without initialization 280 * As _d_newarrayT, but without initialization
299 */ 281 */
304 286
305 debug(PRINTF) printf("_d_newarrayvT(length = %u, size = %d)\n", length, size); 287 debug(PRINTF) printf("_d_newarrayvT(length = %u, size = %d)\n", length, size);
306 if (length == 0 || size == 0) 288 if (length == 0 || size == 0)
307 return null; 289 return null;
308 290
309 version (D_InlineAsm_X86) 291 size = length_adjust(size, length);
310 { 292
311 asm
312 {
313 mov EAX,size ;
314 mul EAX,length ;
315 mov size,EAX ;
316 jc Loverflow ;
317 }
318 }
319 else
320 size *= length;
321 p = gc_malloc(size + 1, !(ti.next.flags() & 1) ? BlkAttr.NO_SCAN : 0); 293 p = gc_malloc(size + 1, !(ti.next.flags() & 1) ? BlkAttr.NO_SCAN : 0);
322 debug(PRINTF) printf(" p = %p\n", p); 294 debug(PRINTF) printf(" p = %p\n", p);
323 return p; 295 return p;
324
325 Loverflow:
326 onOutOfMemoryError();
327 return null;
328 } 296 }
329 297
330 /** 298 /**
331 * Allocate a new array of arrays of arrays of arrays ... 299 * Allocate a new array of arrays of arrays of arrays ...
332 * ti is the type of the resulting array. 300 * ti is the type of the resulting array.
624 printf("\tp.data = %p, p.length = %d\n", pdata, plength); 592 printf("\tp.data = %p, p.length = %d\n", pdata, plength);
625 } 593 }
626 594
627 if (newlength) 595 if (newlength)
628 { 596 {
629 version (D_InlineAsm_X86) 597 size_t newsize = length_adjust(sizeelem, newlength);
630 {
631 size_t newsize = void;
632
633 asm
634 {
635 mov EAX, newlength;
636 mul EAX, sizeelem;
637 mov newsize, EAX;
638 jc Loverflow;
639 }
640 }
641 else
642 {
643 size_t newsize = sizeelem * newlength;
644
645 if (newsize / newlength != sizeelem)
646 goto Loverflow;
647 }
648 598
649 debug(PRINTF) printf("newsize = %x, newlength = %x\n", newsize, newlength); 599 debug(PRINTF) printf("newsize = %x, newlength = %x\n", newsize, newlength);
650 600
651 if (pdata) 601 if (pdata)
652 { 602 {
682 { 632 {
683 newdata = pdata; 633 newdata = pdata;
684 } 634 }
685 635
686 return newdata; 636 return newdata;
687
688 Loverflow:
689 onOutOfMemoryError();
690 return null;
691 } 637 }
692 638
693 639
694 /** 640 /**
695 * Resize arrays for non-zero initializers. 641 * Resize arrays for non-zero initializers.
725 printf("\tp.data = %p, p.length = %d\n", pdata, plength); 671 printf("\tp.data = %p, p.length = %d\n", pdata, plength);
726 } 672 }
727 673
728 if (newlength) 674 if (newlength)
729 { 675 {
730 version (D_InlineAsm_X86) 676 size_t newsize = length_adjust(sizeelem, newlength);
731 {
732 size_t newsize = void;
733
734 asm
735 {
736 mov EAX,newlength ;
737 mul EAX,sizeelem ;
738 mov newsize,EAX ;
739 jc Loverflow ;
740 }
741 }
742 else
743 {
744 size_t newsize = sizeelem * newlength;
745
746 if (newsize / newlength != sizeelem)
747 goto Loverflow;
748 }
749 debug(PRINTF) printf("newsize = %x, newlength = %x\n", newsize, newlength); 677 debug(PRINTF) printf("newsize = %x, newlength = %x\n", newsize, newlength);
750 678
751 size_t size = plength * sizeelem; 679 size_t size = plength * sizeelem;
752 680
753 if (pdata) 681 if (pdata)