We do not need the Memory interface, remove it.
JIRA: CONTROLLER-2071
Change-Id: I0bd43f2a01e58a23fc6c2a4d2a9b93af2ba8b41a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
+++ /dev/null
-/*
- * Copyright 2017-present Open Networking Foundation
- *
- * 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.
- */
-package io.atomix.utils.memory;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.File;
-import java.nio.MappedByteBuffer;
-import java.nio.channels.FileChannel;
-
-/**
- * Mapped memory.
- * <p>
- * This is a special memory descriptor that handles management of {@link MappedByteBuffer} based memory. The
- * mapped memory descriptor simply points to the memory address of the underlying byte buffer. When memory is reallocated,
- * the parent {@link MappedMemoryAllocator} is used to create a new {@link MappedByteBuffer}
- * and free the existing buffer.
- *
- * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
- */
-public class MappedMemory implements Memory {
- private static final long MAX_SIZE = Integer.MAX_VALUE - 5;
-
- private static final Logger LOGGER = LoggerFactory.getLogger(MappedMemory.class);
-
- /**
- * Allocates memory mapped to a file on disk.
- *
- * @param file The file to which to map memory.
- * @param size The count of the memory to map.
- * @return The mapped memory.
- * @throws IllegalArgumentException If {@code count} is greater than {@link MappedMemory#MAX_SIZE}
- */
- public static MappedMemory allocate(File file, int size) {
- return new MappedMemoryAllocator(file).allocate(size);
- }
-
- /**
- * Allocates memory mapped to a file on disk.
- *
- * @param file The file to which to map memory.
- * @param mode The mode with which to map memory.
- * @param size The count of the memory to map.
- * @return The mapped memory.
- * @throws IllegalArgumentException If {@code count} is greater than {@link MappedMemory#MAX_SIZE}
- */
- public static MappedMemory allocate(File file, FileChannel.MapMode mode, int size) {
- if (size > MAX_SIZE) {
- throw new IllegalArgumentException("size cannot be greater than " + MAX_SIZE);
- }
- return new MappedMemoryAllocator(file, mode).allocate(size);
- }
-
- private final MappedByteBuffer buffer;
- private final MappedMemoryAllocator allocator;
- private final int size;
-
- public MappedMemory(MappedByteBuffer buffer, MappedMemoryAllocator allocator) {
- this.buffer = buffer;
- this.allocator = allocator;
- this.size = buffer.capacity();
- }
-
- /**
- * Flushes the mapped buffer to disk.
- */
- public void flush() {
- buffer.force();
- }
-
- @Override
- public int size() {
- return size;
- }
-
- @Override
- public void free() {
- try {
- BufferCleaner.freeBuffer(buffer);
- } catch (Exception e) {
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Failed to unmap direct buffer", e);
- }
- }
- allocator.release();
- }
-
- public void close() {
- free();
- allocator.close();
- }
-}
+++ /dev/null
-/*
- * Copyright 2017-present Open Networking Foundation
- *
- * 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.
- */
-package io.atomix.utils.memory;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.RandomAccessFile;
-import java.nio.channels.FileChannel;
-import java.util.concurrent.atomic.AtomicInteger;
-
-/**
- * Mapped memory allocator.
- * <p>
- * The mapped memory allocator provides direct memory access to memory mapped from a file on disk. The mapped allocator
- * supports allocating memory in any {@link FileChannel.MapMode}. Once the file is mapped and the
- * memory has been allocated, the mapped allocator provides the memory address of the underlying
- * {@link java.nio.MappedByteBuffer} for access via {@link sun.misc.Unsafe}.
- *
- * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
- */
-public class MappedMemoryAllocator implements MemoryAllocator<MappedMemory> {
- public static final FileChannel.MapMode DEFAULT_MAP_MODE = FileChannel.MapMode.READ_WRITE;
-
- private final AtomicInteger referenceCount = new AtomicInteger();
- private final RandomAccessFile file;
- private final FileChannel channel;
- private final FileChannel.MapMode mode;
- private final long offset;
-
- public MappedMemoryAllocator(File file) {
- this(file, DEFAULT_MAP_MODE, 0);
- }
-
- public MappedMemoryAllocator(File file, FileChannel.MapMode mode) {
- this(file, mode, 0);
- }
-
- public MappedMemoryAllocator(File file, FileChannel.MapMode mode, long offset) {
- this(createFile(file, mode), mode, offset);
- }
-
- public MappedMemoryAllocator(RandomAccessFile file, FileChannel.MapMode mode, long offset) {
- if (file == null) {
- throw new NullPointerException("file cannot be null");
- }
- if (mode == null) {
- throw new NullPointerException("mode cannot be null");
- }
- if (offset < 0) {
- throw new IllegalArgumentException("offset cannot be negative");
- }
- this.file = file;
- this.channel = this.file.getChannel();
- this.mode = mode;
- this.offset = offset;
- }
-
- private static RandomAccessFile createFile(File file, FileChannel.MapMode mode) {
- if (file == null) {
- throw new NullPointerException("file cannot be null");
- }
- if (mode == null) {
- mode = DEFAULT_MAP_MODE;
- }
- try {
- return new RandomAccessFile(file, parseMode(mode));
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- private static String parseMode(FileChannel.MapMode mode) {
- if (mode == FileChannel.MapMode.READ_ONLY) {
- return "r";
- } else if (mode == FileChannel.MapMode.READ_WRITE) {
- return "rw";
- }
- throw new IllegalArgumentException("unsupported map mode");
- }
-
- @Override
- public MappedMemory allocate(int size) {
- try {
- if (file.length() < size) {
- file.setLength(size);
- }
- referenceCount.incrementAndGet();
- return new MappedMemory(channel.map(mode, offset, size), this);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Override
- public MappedMemory reallocate(MappedMemory memory, int size) {
- MappedMemory newMemory = allocate(size);
- memory.free();
- return newMemory;
- }
-
- public void close() {
- try {
- file.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- /**
- * Releases a reference from the allocator.
- */
- void release() {
- if (referenceCount.decrementAndGet() == 0) {
- close();
- }
- }
-
-}
+++ /dev/null
-/*
- * Copyright 2017-present Open Networking Foundation
- *
- * 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.
- */
-package io.atomix.utils.memory;
-
-/**
- * Memory allocator.
- *
- * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
- */
-public interface Memory {
-
- /**
- * Returns the memory count.
- *
- * @return The memory count.
- */
- int size();
-
- /**
- * Frees the memory.
- */
- void free();
-
- /**
- * Memory utilities.
- */
- class Util {
-
- /**
- * Returns a boolean indicating whether the given count is a power of 2.
- */
- public static boolean isPow2(int size) {
- return size > 0 & (size & (size - 1)) == 0;
- }
-
- /**
- * Rounds the count to the nearest power of two.
- */
- public static long toPow2(int size) {
- if ((size & (size - 1)) == 0) {
- return size;
- }
- int i = 128;
- while (i < size) {
- i *= 2;
- if (i <= 0) {
- return 1L << 62;
- }
- }
- return i;
- }
- }
-
-}
+++ /dev/null
-/*
- * Copyright 2017-present Open Networking Foundation
- *
- * 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.
- */
-package io.atomix.utils.memory;
-
-/**
- * Memory allocator.
- * <p>
- * Memory allocators handle allocation of memory for {@link Memory} objects.
- *
- * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
- */
-public interface MemoryAllocator<T extends Memory> {
-
- /**
- * Allocates memory.
- *
- * @param size The count of the memory to allocate.
- * @return The allocated memory.
- */
- T allocate(int size);
-
- /**
- * Reallocates the given memory.
- * <p>
- * When the memory is reallocated, the memory address for the given {@link Memory} instance may change. The returned
- * {@link Memory} instance will contain the updated address and count.
- *
- * @param memory The memory to reallocate.
- * @param size The count to which to reallocate the given memory.
- * @return The reallocated memory.
- */
- T reallocate(T memory, int size);
-
-}
+++ /dev/null
-/*
- * Copyright 2018-present Open Networking Foundation
- *
- * 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.
- */
-package io.atomix.utils.memory;
-
-import static com.google.common.base.MoreObjects.toStringHelper;
-
-/**
- * Memory size.
- */
-public class MemorySize {
-
- /**
- * Creates a memory size from the given bytes.
- *
- * @param bytes the number of bytes
- * @return the memory size
- */
- public static MemorySize from(long bytes) {
- return new MemorySize(bytes);
- }
-
- private final long bytes;
-
- public MemorySize(long bytes) {
- this.bytes = bytes;
- }
-
- /**
- * Returns the number of bytes.
- *
- * @return the number of bytes
- */
- public long bytes() {
- return bytes;
- }
-
- @Override
- public int hashCode() {
- return Long.valueOf(bytes).hashCode();
- }
-
- @Override
- public boolean equals(Object object) {
- return object instanceof MemorySize && ((MemorySize) object).bytes == bytes;
- }
-
- @Override
- public String toString() {
- return toStringHelper(this)
- .addValue(bytes)
- .toString();
- }
-}