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 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 }