Skip to content

Commit

Permalink
static final MH
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisHegarty committed Mar 21, 2024
1 parent 4d9a442 commit d311b97
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ abstract class NativeAccess {
public static NativeAccess getImplementation() {
if (Constants.LINUX || Constants.MAC_OS_X) {
try {
return new PosixNativeAccess();
return PosixNativeAccess.getInstance();
} catch (UnsupportedOperationException uoe) {
LOG.warning(uoe.getMessage());
} catch (IllegalCallerException ice) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,20 @@ final class PosixNativeAccess extends NativeAccess {

private static final Logger LOG = Logger.getLogger(PosixNativeAccess.class.getName());

private final MethodHandle mh$posix_madvise;
private static final MethodHandle mh$posix_madvise = lookupmadvise();

public PosixNativeAccess() {
private static final PosixNativeAccess INSTANCE = new PosixNativeAccess();

private PosixNativeAccess() {}

static PosixNativeAccess getInstance() {
return INSTANCE;
}

private static MethodHandle lookupmadvise() {
final Linker linker = Linker.nativeLinker();
final SymbolLookup stdlib = linker.defaultLookup();
this.mh$posix_madvise =
final MethodHandle mh =
findFunction(
linker,
stdlib,
Expand All @@ -47,6 +55,7 @@ public PosixNativeAccess() {
ValueLayout.JAVA_LONG,
ValueLayout.JAVA_INT));
LOG.info("posix_madvise() available on this platform");
return mh;
}

private static MethodHandle findFunction(
Expand Down

0 comments on commit d311b97

Please sign in to comment.