Arena/Segment allocator and zero initialized MemorySegment

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Wed Jun 5 10:18:47 UTC 2024


On 05/06/2024 11:11, forax at univ-mlv.fr wrote:
> Here is an example
> https://urldefense.com/v3/__https://github.com/forax/memory-mapper/blob/master/src/main/java/com/github/forax/memorymapper/MemoryCollections.java*L772__;Iw!!ACWV5N9M2RV99hQ!PUJIR9R4tNJ_QKk01hd0VFW5mMO0KsBeJk0WxuQsq4MzmNZ0c5WvuGhrAHSJQJGDJog9qIZghlkqNDjgBnxZ8JXAYA$
>   
>
> I want to have a List of records (or primitive type) implemented as an
> array of flatten values.
> I need a way to allocate a zeroed MemorySegment, and I need a way to
> grow the storage using the Array.copyOf() semantics.
>
> For the history, I've an existing non-public library that uses Unsafe +
> ASM for the code generation and i'm trying to replace it with a new
> publicly available library that uses the foreign memory API + the method
>   handle API + Hidden classes.

As you are allocating only one segment, the use of SegmentAllocator 
seems fine here. But... if you later on need to grow storage, it's not a 
one-shot allocation anymore, and I believe you need an Arena (as you 
might need to start managing different segments with same lifetime).

You can also go a different route, and use calloc/realloc directly (via 
Linker), and avoid the allocator parameter (not sure how your old API 
dealt with deallocation safely).

Maurizio

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20240605/34bbbf58/attachment.htm>


More information about the panama-dev mailing list