// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import jdk.incubator.foreign.MemoryAccess;
import jdk.incubator.foreign.MemorySegment;

public class Mem16 {
    public static void test(MemorySegment ms) {
        System.out.println(ms.byteSize());
        for(int i = 0; i < 3; i++) {
            MemoryAccess.setIntAtIndex(ms, i, i + 1);
        }
        for(int i = 0; i < 3; i++) {
            System.out.println(MemoryAccess.getIntAtIndex(ms, i));
        }
    }
    public static void main(String[] args) {
        try(MemorySegment ms = MemorySegment.allocateNative(12)) {
            test(ms);
        }
        byte[] b = new byte[12];
        try(MemorySegment ms = MemorySegment.ofArray(b)) {
            test(ms);
        }
        try(MemorySegment ms = MemorySegment.allocateNative(20)) {
            test(ms.asSlice(4, 12));
        }
    }
}
