Arena.addCloseAction and the implementation of a custom allocator
Quân Anh Mai
anhmdq at gmail.com
Sat Mar 4 10:10:08 UTC 2023
Hi,
I noticed that the method Arena::addCloseAction has been dropped from the
API. May I ask in that case what is the way to create a custom allocator
that manages its allocated memory? With the method, I believe it can be
achieved with something like this:
public MemorySegment allocate(long byteSize, long byteAlignment) {
if (byteAlignment > MAX_ALIGNMENT) {
throw new IllegalArgumentException();
}
try {
var arena = Arena.ofAuto();
long address = (long)MALLOC.invokeExact(byteSize); // I assume this
should work since other reinterpret casts like unsigned <-> int is legal
here
arena.addCloseAction(() -> {
try {
FREE.invokeExact(address);
} catch(Throwable e) {
throw new RuntimeException(e);
}
});
return MemorySegment.ofAddress(address, byteSize, arena);
} catch(Throwable e) {
throw new RuntimeException(e);
}
}
Thanks a lot,
Quan Anh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20230304/ecce62ff/attachment.htm>
More information about the panama-dev
mailing list