Hello,
I'd like to be able to send dataviewtypes as messages in a low-latency ipc library I've written.
In this scenario, any use of malloc is unacceptable. Everything must be allocated from a memory pool or the stack, but datavtype constructors always allocate on the heap.
What is the best way to access to the size and flat memory of the datavtype's tagged union, preferably in a typesafe manner? You can do this by a hack like
dataviewtype message =
| Bar of (int, double)
| Baz of (double, double)
#define BAR 0
#define BAZ 1
typedef BAR_ = @{
contag = int,
atslab__0 = int,
atslab__1 = double
}
typedef BAZ_ = @{
contag = int,
atslab__0 = double,
atslab__1 = double
}
and then using $UNSAFE.cast{Foo} on a pointer to stack or pool allocated memory of size FOO_SIZE = max(sizeof<BAR_>, sizeof<BAZ_>), which has had its contag set to BAR or BAZ via another unsafe cast.
Unfortunately there is really a lot of boilerplate here and it will be easy for things to get out of sync with datatype definitions, especially with a lot of constructors. Moreover, FOO_SIZE is not a static constant, so you can't just write something like var buf : @[char][FOO_SIZE*N]
Also, is there an equivalent of the placement new operator in C++?
Thanks,
Andrew