forked from openjdk/jdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2092836
commit fdf617c
Showing
3 changed files
with
20 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 10 additions & 42 deletions
52
src/java.base/share/classes/jdk/internal/foreign/ZeroingAllocator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,23 @@ | ||
package jdk.internal.foreign; | ||
|
||
import jdk.internal.vm.annotation.Stable; | ||
|
||
import java.lang.foreign.MemorySegment; | ||
import java.lang.foreign.SegmentAllocator; | ||
import java.lang.foreign.ValueLayout; | ||
|
||
/** | ||
* Marker interface for allocators that provide zeroing semantics. | ||
* Internal interface for allocators that provide zeroing semantics. | ||
*/ | ||
public interface ZeroingAllocator extends SegmentAllocator { | ||
|
||
static ZeroingAllocator of(SegmentAllocator allocator) { | ||
if (allocator instanceof ZeroingAllocator zeroingAllocator) { | ||
// already a zeroing allocator | ||
return zeroingAllocator; | ||
} else if (allocator instanceof MemorySegment || | ||
allocator instanceof SlicingAllocator) { | ||
// prefix and slicing allocators do not zero | ||
return (byteSize, byteAlignment) -> allocator.allocate(byteSize, byteAlignment).fill((byte)0); | ||
} else { | ||
return new ZeroingWrapper(allocator); | ||
} | ||
} | ||
|
||
class ZeroingWrapper implements ZeroingAllocator { | ||
static final MemorySegment ZERO = MemorySegment.ofArray(new long[1024]); | ||
@Stable | ||
static final ValueLayout[] ALIGNED_LAYOUTS; | ||
MemorySegment allocateRaw(long byteSize, long byteAlign); | ||
|
||
final SegmentAllocator allocator; | ||
|
||
ZeroingWrapper(SegmentAllocator allocator) { | ||
this.allocator = allocator; | ||
} | ||
|
||
static { | ||
ALIGNED_LAYOUTS = new ValueLayout[8]; | ||
ALIGNED_LAYOUTS[0] = ValueLayout.JAVA_BYTE; | ||
ALIGNED_LAYOUTS[1] = ValueLayout.JAVA_SHORT; | ||
ALIGNED_LAYOUTS[3] = ValueLayout.JAVA_INT; | ||
ALIGNED_LAYOUTS[7] = ValueLayout.JAVA_LONG; | ||
} | ||
@Override | ||
default MemorySegment allocate(long byteSize, long byteAlign) { | ||
return allocateRaw(byteSize, byteAlign).fill((byte)0); | ||
} | ||
|
||
public MemorySegment allocate(long size, long align) { | ||
if (align > 8 || size > ZERO.byteSize()) { | ||
// slow path (might perform double zeroing) | ||
return allocator.allocate(size, align).fill((byte) 0); | ||
} else { | ||
return allocator.allocateFrom(ALIGNED_LAYOUTS[(int)align - 1], ZERO, ALIGNED_LAYOUTS[(int)align - 1], 0, size); | ||
} | ||
}; | ||
static ZeroingAllocator of(SegmentAllocator allocator) { | ||
return (allocator instanceof ZeroingAllocator zeroingAllocator) ? | ||
zeroingAllocator : // already a zeroing allocator | ||
allocator::allocate; | ||
} | ||
} |