// Path: src/main/java/org/lmdbjava/BufferProxy.java
// protected static final int MDB_VAL_STRUCT_SIZE = BYTES * 2;
// 
// Path: src/main/java/org/lmdbjava/BufferProxy.java
// protected static final int STRUCT_FIELD_OFFSET_SIZE = 0;
// 
// Path: src/main/java/org/lmdbjava/Library.java
// static final jnr.ffi.Runtime RUNTIME;


import static java.util.Objects.requireNonNull;
import static org.lmdbjava.BufferProxy.MDB_VAL_STRUCT_SIZE;
import static org.lmdbjava.BufferProxy.STRUCT_FIELD_OFFSET_SIZE;
import static org.lmdbjava.Library.RUNTIME;
import jnr.ffi.Pointer;
import jnr.ffi.provider.MemoryManager;
/*-
 * #%L
 * LmdbJava
 * %%
 * Copyright (C) 2016 - 2021 The LmdbJava Open Source Project
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

package org.lmdbjava;



/**
 * Represents off-heap memory holding a key and value pair.
 *
 * @param <T> buffer type
 */
final class KeyVal<T> implements AutoCloseable {

  private static final MemoryManager MEM_MGR = RUNTIME.getMemoryManager();
  private boolean closed;
  private T k;
  private final BufferProxy<T> proxy;
  private final Pointer ptrArray;
  private final Pointer ptrKey;
  private final long ptrKeyAddr;
  private final Pointer ptrVal;
  private final long ptrValAddr;
  private T v;

  KeyVal(final BufferProxy<T> proxy) {
    requireNonNull(proxy);
    this.proxy = proxy;
    this.k = proxy.allocate();
    this.v = proxy.allocate();
    ptrKey = MEM_MGR.allocateTemporary(MDB_VAL_STRUCT_SIZE, false);
    ptrKeyAddr = ptrKey.address();
    ptrArray = MEM_MGR.allocateTemporary(MDB_VAL_STRUCT_SIZE * 2, false);
// Path: src/main/java/org/lmdbjava/BufferProxy.java
// protected static final int MDB_VAL_STRUCT_SIZE = BYTES * 2;
// 
// Path: src/main/java/org/lmdbjava/BufferProxy.java
// protected static final int STRUCT_FIELD_OFFSET_SIZE = 0;
// 
// Path: src/main/java/org/lmdbjava/Library.java
// static final jnr.ffi.Runtime RUNTIME;


// Path: src/main/java/org/lmdbjava/KeyVal.java
import static java.util.Objects.requireNonNull;
import static org.lmdbjava.BufferProxy.MDB_VAL_STRUCT_SIZE;
import static org.lmdbjava.BufferProxy.STRUCT_FIELD_OFFSET_SIZE;
import static org.lmdbjava.Library.RUNTIME;
import jnr.ffi.Pointer;
import jnr.ffi.provider.MemoryManager;

/*-
 * #%L
 * LmdbJava
 * %%
 * Copyright (C) 2016 - 2021 The LmdbJava Open Source Project
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

package org.lmdbjava;



/**
 * Represents off-heap memory holding a key and value pair.
 *
 * @param <T> buffer type
 */
final class KeyVal<T> implements AutoCloseable {

  private static final MemoryManager MEM_MGR = RUNTIME.getMemoryManager();
  private boolean closed;
  private T k;
  private final BufferProxy<T> proxy;
  private final Pointer ptrArray;
  private final Pointer ptrKey;
  private final long ptrKeyAddr;
  private final Pointer ptrVal;
  private final long ptrValAddr;
  private T v;

  KeyVal(final BufferProxy<T> proxy) {
    requireNonNull(proxy);
    this.proxy = proxy;
    this.k = proxy.allocate();
    this.v = proxy.allocate();
    ptrKey = MEM_MGR.allocateTemporary(MDB_VAL_STRUCT_SIZE, false);
    ptrKeyAddr = ptrKey.address();
    ptrArray = MEM_MGR.allocateTemporary(MDB_VAL_STRUCT_SIZE * 2, false);
    ptrVal = ptrArray.slice(0, MDB_VAL_STRUCT_SIZE);