1 /*
 2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.  Oracle designates this
 8  * particular file as subject to the "Classpath" exception as provided
 9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package wrap;
26 
27 import java.lang.foreign.Arena;
28 import java.lang.foreign.MemorySegment;
29 
30 public interface ArenaHolder {
31     static ArenaHolder wrap(Arena arena) {
32         return ()-> arena;
33     }
34 
35     Arena arena();
36 
37     default Wrap.IntPtr intPtr(int value){
38         return Wrap.IntPtr.of(arena(), value);
39     }
40     default Wrap.ShortPtr shortPtr(short value){
41         return Wrap.ShortPtr.of(arena(), value);
42     }
43 
44     default Wrap.LongPtr longPtr(long value){
45         return Wrap.LongPtr.of(arena(), value);
46     }
47     default Wrap.DoublePtr doublePtr(double value){
48         return Wrap.DoublePtr.of(arena(), value);
49     }
50     default Wrap.FloatPtr floatPtr(float value){
51         return Wrap.FloatPtr.of(arena(), value);
52     }
53     default Wrap.IntArr ofInts(int ...values){
54         return  Wrap.IntArr.of(arena(), values);
55     }
56     default Wrap.FloatArr ofFloats(float ...values){
57         return  Wrap.FloatArr.of(arena(), values);
58     }
59     default Wrap.CStrPtr cstr(MemorySegment segment){
60         return Wrap.CStrPtr.of( segment);
61     }
62 
63     default Wrap.CStrPtr cstr(String s){
64         return Wrap.CStrPtr.of(arena(), s);
65     }
66     default Wrap.CStrPtr cstr(long size){
67         return Wrap.CStrPtr.of(arena(), (int)size);
68     }
69     default Wrap.CStrPtr cstr(int size){
70         return Wrap.CStrPtr.of(arena(), size);
71     }
72     default Wrap.PtrArr ptrArr(MemorySegment ... memorySegments) {
73         return Wrap.PtrArr.of(arena(), memorySegments);
74     }
75     default Wrap.PtrArr ptrArr(Wrap.Ptr ...ptrs) {
76         return Wrap.PtrArr.of(arena(), ptrs);
77     }
78     default Wrap.PtrArr ptrArr(int len) {
79         return Wrap.PtrArr.of(arena(), len);
80     }
81 
82     default Wrap.PtrArr ptrArr(String ...strings) {
83         return Wrap.PtrArr.of(arena(), strings);
84     }
85 }