// Path: src/main/java/com/yahoo/memory4/UnsafeUtil.java
// public static final Unsafe unsafe;


import sun.misc.Cleaner;
import static com.yahoo.memory4.UnsafeUtil.unsafe;
/*
 * Copyright 2017, Yahoo! Inc. Licensed under the terms of the
 * Apache License 2.0. See LICENSE file at the project root for terms.
 */

package com.yahoo.memory4;



/**
 * @author Lee Rhodes
 */
final class AllocateDirect extends WritableMemoryImpl implements WritableResourceHandler {
  private final Cleaner cleaner;

  /**
   * Base Constructor for allocate native memory.
   *
   * <p>Allocates and provides access to capacityBytes directly in native (off-heap) memory
   * leveraging the Memory interface.
   * The allocated memory will be 8-byte aligned, but may not be page aligned.
   * @param state contains the capacity and optionally the MemoryRequest
   */
  private AllocateDirect(final MemoryState state) {
    super(state);
    this.cleaner = Cleaner.create(this, new Deallocator(state));
  }

  static WritableMemoryImpl allocDirect(final MemoryState state) {
// Path: src/main/java/com/yahoo/memory4/UnsafeUtil.java
// public static final Unsafe unsafe;


// Path: src/main/java/com/yahoo/memory4/AllocateDirect.java
import sun.misc.Cleaner;
import static com.yahoo.memory4.UnsafeUtil.unsafe;

/*
 * Copyright 2017, Yahoo! Inc. Licensed under the terms of the
 * Apache License 2.0. See LICENSE file at the project root for terms.
 */

package com.yahoo.memory4;



/**
 * @author Lee Rhodes
 */
final class AllocateDirect extends WritableMemoryImpl implements WritableResourceHandler {
  private final Cleaner cleaner;

  /**
   * Base Constructor for allocate native memory.
   *
   * <p>Allocates and provides access to capacityBytes directly in native (off-heap) memory
   * leveraging the Memory interface.
   * The allocated memory will be 8-byte aligned, but may not be page aligned.
   * @param state contains the capacity and optionally the MemoryRequest
   */
  private AllocateDirect(final MemoryState state) {
    super(state);
    this.cleaner = Cleaner.create(this, new Deallocator(state));
  }

  static WritableMemoryImpl allocDirect(final MemoryState state) {
    state.putNativeBaseOffset(unsafe.allocateMemory(state.getCapacity()));