std.expreimantal.allocator deallocate
vitamin
vit at vit.vit
Sun Jan 24 11:00:17 UTC 2021
Allocators from std.expreimantal.allocator allocate memory and
return slice void[] to allocated memory.
Method deallocate has as parameter void[] slice to allocated
memory.
It is Ok when I call deallocate with smaller slice or I need
track exact lengtht?
Example:
import std.experimental.allocator : theAllocator;
import core.lifetime : emplace;
import std.stdio;
class Base{
int i;
}
class Derived : Base{
int j;
}
Base make(){
void[] x = theAllocator.allocate(__traits(classInstanceSize,
Derived));
writeln("allocate: ", x.length);
emplace!Derived(x);
return cast(Derived)x.ptr;
}
void destruct(Base base){
void[] x = (cast(void*)base)[0 .. __traits(classInstanceSize,
Base)];
writeln("deallocate: ", x.length);
theAllocator.deallocate(x);
}
void main(){
Base x = make;
scope(exit)destruct(x);
///some code...
}
//print:
//allocate: 24
//deallocate: 20
More information about the Digitalmars-d-learn
mailing list