comparison runtime/internal/lifetime.d @ 591:e6bcc4d9e5ff

Add _d_newarrayvT and _d_newarraymvT to create arrays without initialization. Adjust DtoNewDynArray to use DtoArrayInit for initialization of new arrays. Make Type::tvoid->defaultInit() not error.
author Christian Kamm <kamm incasoftware de>
date Sun, 14 Sep 2008 10:13:50 +0200
parents f8c979770af3
children 6aaa3d3c1183
comparison
equal deleted inserted replaced
590:1ecb43102d12 591:e6bcc4d9e5ff
200 +/ 200 +/
201 201
202 /** 202 /**
203 * Allocate a new array of length elements. 203 * Allocate a new array of length elements.
204 * ti is the type of the resulting array, or pointer to element. 204 * ti is the type of the resulting array, or pointer to element.
205 * (For when the array is initialized to 0) 205 * The resulting array is initialized to 0
206 */ 206 */
207 extern (C) void* _d_newarrayT(TypeInfo ti, size_t length) 207 extern (C) void* _d_newarrayT(TypeInfo ti, size_t length)
208 { 208 {
209 void* p; 209 void* p;
210 auto size = ti.next.tsize(); // array element size 210 auto size = ti.next.tsize(); // array element size
234 onOutOfMemoryError(); 234 onOutOfMemoryError();
235 return null; 235 return null;
236 } 236 }
237 237
238 /** 238 /**
239 * For when the array has a non-zero initializer. 239 * As _d_newarrayT, but
240 * for when the array has a non-zero initializer.
240 */ 241 */
241 extern (C) void* _d_newarrayiT(TypeInfo ti, size_t length) 242 extern (C) void* _d_newarrayiT(TypeInfo ti, size_t length)
242 { 243 {
243 void* result; 244 void* result;
244 auto size = ti.next.tsize(); // array element size 245 auto size = ti.next.tsize(); // array element size
292 onOutOfMemoryError(); 293 onOutOfMemoryError();
293 return null; 294 return null;
294 } 295 }
295 296
296 /** 297 /**
297 * 298 * As _d_newarrayT, but without initialization
299 */
300 extern (C) void* _d_newarrayvT(TypeInfo ti, size_t length)
301 {
302 void* p;
303 auto size = ti.next.tsize(); // array element size
304
305 debug(PRINTF) printf("_d_newarrayvT(length = %u, size = %d)\n", length, size);
306 if (length == 0 || size == 0)
307 return null;
308
309 version (D_InlineAsm_X86)
310 {
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);
322 debug(PRINTF) printf(" p = %p\n", p);
323 return p;
324
325 Loverflow:
326 onOutOfMemoryError();
327 return null;
328 }
329
330 /**
331 * Allocate a new array of arrays of arrays of arrays ...
332 * ti is the type of the resulting array.
333 * ndims is the number of nested arrays.
334 * dims it the array of dimensions, its size is ndims.
335 * The resulting array is initialized to 0
298 */ 336 */
299 extern (C) void* _d_newarraymT(TypeInfo ti, int ndims, size_t* dims) 337 extern (C) void* _d_newarraymT(TypeInfo ti, int ndims, size_t* dims)
300 { 338 {
301 void* result; 339 void* result;
302 340
341 return result; 379 return result;
342 } 380 }
343 381
344 382
345 /** 383 /**
346 * 384 * As _d_newarraymT, but
385 * for when the array has a non-zero initializer.
347 */ 386 */
348 extern (C) void* _d_newarraymiT(TypeInfo ti, int ndims, size_t* dims) 387 extern (C) void* _d_newarraymiT(TypeInfo ti, int ndims, size_t* dims)
349 { 388 {
350 void* result; 389 void* result;
351 390
382 { 421 {
383 for (int i = 0; i < ndims; i++) 422 for (int i = 0; i < ndims; i++)
384 { 423 {
385 printf("index %d: %d\n", i, *dims++); 424 printf("index %d: %d\n", i, *dims++);
386 printf("init = %d\n", *dims++); 425 printf("init = %d\n", *dims++);
426 }
427 }
428 }
429 return result;
430 }
431
432 /**
433 * As _d_newarraymT, but without initialization
434 */
435 extern (C) void* _d_newarraymvT(TypeInfo ti, int ndims, size_t* dims)
436 {
437 void* result;
438
439 debug(PRINTF) printf("_d_newarraymvT(ndims = %d)\n", ndims);
440 if (ndims == 0)
441 result = null;
442 else
443 {
444 static void[] foo(TypeInfo ti, size_t* pdim, int ndims)
445 {
446 size_t dim = *pdim;
447 void[] p;
448
449 debug(PRINTF) printf("foo(ti = %p, ti.next = %p, dim = %d, ndims = %d\n", ti, ti.next, dim, ndims);
450 if (ndims == 1)
451 {
452 auto r = _d_newarrayvT(ti, dim);
453 return r[0 .. dim];
454 }
455 else
456 {
457 p = gc_malloc(dim * (void[]).sizeof + 1)[0 .. dim];
458 for (int i = 0; i < dim; i++)
459 {
460 (cast(void[]*)p.ptr)[i] = foo(ti.next, pdim + 1, ndims - 1);
461 }
462 }
463 return p;
464 }
465
466 result = foo(ti, dims, ndims).ptr;
467 debug(PRINTF) printf("result = %p\n", result);
468
469 version (none)
470 {
471 for (int i = 0; i < ndims; i++)
472 {
473 printf("index %d: %d\n", i, *dims++);
387 } 474 }
388 } 475 }
389 } 476 }
390 return result; 477 return result;
391 } 478 }