1 /*
  2  * Copyright (c) 2021, 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 
 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 }