add Placement New

This commit is contained in:
Walter Bright 2024-11-09 22:07:37 -08:00 committed by Manu Evans
parent cdf4f5b072
commit 73158d8a78
24 changed files with 407 additions and 46 deletions

View file

@ -0,0 +1,4 @@
void f(int* p) @nogc
{
new(*p) int;
}

View file

@ -0,0 +1,80 @@
/* TEST_OUTPUT:
---
fail_compilation/placenew.d(23): Error: PlacementExpression `3` is an rvalue, but must be an lvalue
fail_compilation/placenew.d(28): Error: undefined identifier `x`
fail_compilation/placenew.d(36): Error: `new ( i )` PlacementExpression cannot be evaluated at compile time
fail_compilation/placenew.d(39): called from here: `xxx()`
fail_compilation/placenew.d(39): while evaluating: `static assert(xxx() == 1)`
fail_compilation/placenew.d(48): Error: new placement size 24 must be >= object size 40
fail_compilation/placenew.d(54): Error: placement new cannot be used with associative arrays
fail_compilation/placenew.d(67): Error: new placement size 4 must be >= class object size $?:32=16|64=24$
fail_compilation/placenew.d(77): Error: `@safe` function `test7` cannot use placement `new` is not allowed in a `@safe` function
---
*/
void test0()
{
int i;
int* pi = new (i) int;
}
void test1()
{
int* pi = new (3) int;
}
void test2()
{
int* px = new (x) int;
}
void test3()
{
int xxx()
{
int i;
int* pi = new (i) int(1);
return 1;
}
static assert(xxx() == 1);
}
struct S { int[6] a; }
struct T { int[10] a; }
void test4()
{
S s;
new (s) T();
}
void test5()
{
T p;
auto aa = new(p) int[int*];
}
/*************************************************/
class C6
{
int i, j;
}
int test6()
{
int k;
C6 c = new(k) C6;
return c.j;
}
/*************************************************/
@safe
void test7()
{
int i;
int* p = new(i) int;
}
/*************************************************/

View file

@ -0,0 +1,106 @@
import core.stdc.stdio;
import core.stdc.stdlib;
/*************************************************/
struct S
{
float d;
int i;
char c;
}
void test1()
{
S s;
S* p = new (s) S();
assert(p.i == 0 && p.c == 0xFF);
}
void test2()
{
S s;
S* p = new (s) S(i:3);
assert(p.i == 3 && p.c == 0xFF);
}
/*************************************************/
struct S3
{
int i;
this(int i) { this.i = i + 3; }
}
void test3()
{
S3 s;
s.i = 20;
S3* p = new (s) S3(4);
assert(p.i == 7);
}
/*************************************************/
void test4()
{
int i = 3;
int* p = new(i) int;
*p = 4;
assert(i == 4);
p = new(i) int(7);
assert(i == 7);
}
/*************************************************/
class C5
{
int i, j = 4;
}
int test5()
{
int[10] k;
C5 c = new(k) C5;
//printf("c.j: %d\n", c.j);
assert(c.j == 4);
assert(cast(void*)c == cast(void*)k.ptr);
return c.j;
}
/*************************************************/
struct S6
{
int i = 1, j = 4, k = 9;
}
ref void[T.sizeof] mallocate(T)()
{
return *(cast(void[T.sizeof]*) malloc(T.sizeof));
}
void test6()
{
S6* ps = new(mallocate!S6()) S6;
assert(ps.i == 1);
assert(ps.j == 4);
assert(ps.k == 9);
}
/*************************************************/
int main()
{
test1();
test2();
test3();
test4();
test5();
test6();
return 0;
}