< prev index next >

src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/ArenaAllocator.java

Print this page

 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 
 26 package jdk.internal.foreign;
 27 
 28 import jdk.incubator.foreign.MemorySegment;
 29 import jdk.incubator.foreign.SegmentAllocator;
 30 import jdk.incubator.foreign.ResourceScope;
 31 
 32 public abstract class ArenaAllocator implements SegmentAllocator {
 33 
 34     protected MemorySegment segment;
 35 
 36     protected long sp = 0L;
 37 
 38     ArenaAllocator(MemorySegment segment) {
 39         this.segment = segment;









 40     }
 41 
 42     MemorySegment trySlice(long bytesSize, long bytesAlignment) {
 43         long min = segment.address().toRawLongValue();
 44         long start = Utils.alignUp(min + sp, bytesAlignment) - min;
 45         if (segment.byteSize() - start < bytesSize) {
 46             return null;
 47         } else {
 48             MemorySegment slice = segment.asSlice(start, bytesSize);
 49             sp = start + bytesSize;
 50             return slice;
 51         }
 52     }
 53 
 54     void checkConfinementIfNeeded() {
 55         Thread ownerThread = scope().ownerThread();
 56         if (ownerThread != null && ownerThread != Thread.currentThread()) {
 57             throw new IllegalStateException("Attempt to allocate outside confinement thread");
 58         }
 59     }
 60 
 61     ResourceScope scope() {
 62         return segment.scope();
 63     }
 64 
 65     public static class UnboundedArenaAllocator extends ArenaAllocator {
 66 
 67         private static final long DEFAULT_BLOCK_SIZE = 4 * 1024;
 68 
 69         public UnboundedArenaAllocator(ResourceScope scope) {
 70             super(MemorySegment.allocateNative(DEFAULT_BLOCK_SIZE, 1, scope));
 71         }
 72 
 73         private MemorySegment newSegment(long size, long align) {
 74             return MemorySegment.allocateNative(size, align, segment.scope());
 75         }
 76 
 77         @Override
 78         public MemorySegment allocate(long bytesSize, long bytesAlignment) {
 79             checkConfinementIfNeeded();
 80             // try to slice from current segment first...
 81             MemorySegment slice = trySlice(bytesSize, bytesAlignment);
 82             if (slice != null) {

 83                 return slice;
 84             } else {
 85                 long maxPossibleAllocationSize = bytesSize + bytesAlignment - 1;
 86                 if (maxPossibleAllocationSize > DEFAULT_BLOCK_SIZE) {
 87                     // too big




 88                     return newSegment(bytesSize, bytesAlignment);
 89                 } else {
 90                     // allocate a new segment and slice from there

 91                     sp = 0L;
 92                     segment = newSegment(DEFAULT_BLOCK_SIZE, 1L);
 93                     return trySlice(bytesSize, bytesAlignment);


 94                 }
 95             }
 96         }
 97     }
 98 
 99     public static class BoundedArenaAllocator extends ArenaAllocator {
100 
101         public BoundedArenaAllocator(ResourceScope scope, long size) {
102             super(MemorySegment.allocateNative(size, 1, scope));
103         }
104 
105         @Override
106         public MemorySegment allocate(long bytesSize, long bytesAlignment) {
107             checkConfinementIfNeeded();
108             // try to slice from current segment first...
109             MemorySegment slice = trySlice(bytesSize, bytesAlignment);
110             if (slice != null) {
111                 return slice;
112             } else {
113                 throw new OutOfMemoryError("Not enough space left to allocate");
114             }
115         }
116     }
117 
118     public static class BoundedSharedArenaAllocator extends BoundedArenaAllocator {
119         public BoundedSharedArenaAllocator(ResourceScope scope, long size) {
120             super(scope, size);
121         }
122 
123         @Override
124         public synchronized MemorySegment allocate(long bytesSize, long bytesAlignment) {
125             return super.allocate(bytesSize, bytesAlignment);
126         }
127     }
128 
129     public static class UnboundedSharedArenaAllocator implements SegmentAllocator {
130 
131         final ResourceScope scope;
132 
133         final ThreadLocal<ArenaAllocator> allocators = new ThreadLocal<>() {
134             @Override
135             protected ArenaAllocator initialValue() {
136                 return new UnboundedArenaAllocator(scope);
137             }
138         };
139 
140         public UnboundedSharedArenaAllocator(ResourceScope scope) {
141             this.scope = scope;
142         }
143 
144         @Override
145         public MemorySegment allocate(long bytesSize, long bytesAlignment) {
146             return allocators.get().allocate(bytesSize, bytesAlignment);
147         }
148     }
149 }

 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 
 26 package jdk.internal.foreign;
 27 
 28 import jdk.incubator.foreign.MemorySegment;
 29 import jdk.incubator.foreign.SegmentAllocator;
 30 import jdk.incubator.foreign.ResourceScope;
 31 
 32 public final class ArenaAllocator implements SegmentAllocator {
 33 
 34     public static final long DEFAULT_BLOCK_SIZE = 4 * 1024;
 35 
 36     MemorySegment segment;
 37 
 38     long sp = 0L;
 39     long size = 0;
 40     final long blockSize;
 41     final long arenaSize;
 42     final ResourceScope scope;
 43 
 44     public ArenaAllocator(long blockSize, long arenaSize, ResourceScope scope) {
 45         this.blockSize = blockSize;
 46         this.arenaSize = arenaSize;
 47         this.scope = scope;
 48         this.segment = newSegment(blockSize, 1);
 49     }
 50 
 51     MemorySegment trySlice(long bytesSize, long bytesAlignment) {
 52         long min = segment.address().toRawLongValue();
 53         long start = Utils.alignUp(min + sp, bytesAlignment) - min;
 54         if (segment.byteSize() - start < bytesSize) {
 55             return null;
 56         } else {
 57             MemorySegment slice = segment.asSlice(start, bytesSize);
 58             sp = start + bytesSize;
 59             return slice;
 60         }
 61     }
 62 
 63     public ResourceScope scope() {
 64         return scope;



 65     }
 66 
 67     private MemorySegment newSegment(long size, long align) {
 68         return MemorySegment.allocateNative(size, align, scope);
 69     }
 70 
 71     @Override
 72     public MemorySegment allocate(long bytesSize, long bytesAlignment) {
 73         long prevSp = sp;
 74         long allocatedSize = 0L;
 75         try {










 76             // try to slice from current segment first...
 77             MemorySegment slice = trySlice(bytesSize, bytesAlignment);
 78             if (slice != null) {
 79                 allocatedSize = sp - prevSp;
 80                 return slice;
 81             } else {
 82                 long maxPossibleAllocationSize = bytesSize + bytesAlignment - 1;
 83                 if (maxPossibleAllocationSize > blockSize) {
 84                     // too big
 85                     allocatedSize = Utils.alignUp(bytesSize, bytesAlignment);
 86                     if (size > arenaSize) {
 87                         throw new OutOfMemoryError();
 88                     }
 89                     return newSegment(bytesSize, bytesAlignment);
 90                 } else {
 91                     // allocate a new segment and slice from there
 92                     allocatedSize += segment.byteSize() - sp;
 93                     sp = 0L;
 94                     segment = newSegment(blockSize, 1L);
 95                     slice = trySlice(bytesSize, bytesAlignment);
 96                     allocatedSize += sp;
 97                     return slice;
 98                 }
 99             }
100         } finally {
101             size += allocatedSize;
102             if (size > arenaSize) {
103                 throw new OutOfMemoryError();





































104             }









105         }
106     }
107 }
< prev index next >