/** * Allocates some unpooled memory and creates a new memory segment that represents * that memory. * * <p>This method is similar to {@link #allocateUnpooledSegment(int, Object)}, but the * memory segment will have null as the owner. * * @param size The size of the memory segment to allocate. * @return A new memory segment, backed by unpooled heap memory. */ public static MemorySegment allocateUnpooledSegment(int size) { return allocateUnpooledSegment(size, null); }
private static BufferOrEvent createBuffer(int channel) { return new BufferOrEvent( new NetworkBuffer(MemorySegmentFactory.wrap(new byte[]{1, 2}), FreeingBufferRecycler.INSTANCE), channel); }
/** * Allocates some unpooled off-heap memory and creates a new memory segment that * represents that memory. * * @param size The size of the off-heap memory segment to allocate. * @param owner The owner to associate with the off-heap memory segment. * @return A new memory segment, backed by unpooled off-heap memory. */ public static MemorySegment allocateUnpooledOffHeapMemory(int size, Object owner) { ByteBuffer memory = ByteBuffer.allocateDirect(size); return wrapPooledOffHeapMemory(memory, owner); }
public FixedLengthBufferPool(int numberOfBuffers, int memorySegmentSize, MemoryType memoryType) { checkArgument(numberOfBuffers > 0, "There should be at least one buffer."); this.numberOfBuffers = numberOfBuffers; this.memorySegmentSize = memorySegmentSize; this.freeOnDestroy = true; this.memorySegments = new ArrayDeque<>(numberOfBuffers); synchronized (memorySegments) { if (memoryType == MemoryType.HEAP) { for (int i = 0; i < numberOfBuffers; i++) { memorySegments.add(MemorySegmentFactory.allocateUnpooledSegment(memorySegmentSize)); } } else if (memoryType == MemoryType.OFF_HEAP) { for (int i = 0; i < numberOfBuffers; i++) { ByteBuffer memory = ByteBuffer.allocateDirect(memorySegmentSize); memorySegments.add(MemorySegmentFactory.wrapPooledOffHeapMemory(memory, null)); } } else { throw new IllegalArgumentException("Unknown memory type " + memoryType); } } }
for (int i = 0; i < numberOfSegmentsToAllocate; i++) { byte[] memory = new byte[segmentSize]; availableMemorySegments.add(MemorySegmentFactory.wrapPooledHeapMemory(memory, null)); for (int i = 0; i < numberOfSegmentsToAllocate; i++) { ByteBuffer memory = ByteBuffer.allocateDirect(segmentSize); availableMemorySegments.add(MemorySegmentFactory.wrapPooledOffHeapMemory(memory, null));
@Override MemorySegment requestSegmentFromPool(Object owner) { byte[] buf = availableMemory.remove(); return MemorySegmentFactory.wrapPooledHeapMemory(buf, owner); }
@Override MemorySegment allocateNewSegment(Object owner) { return MemorySegmentFactory.allocateUnpooledOffHeapMemory(segmentSize, owner); }
@Override MemorySegment requestSegmentFromPool(Object owner) { byte[] buf = availableMemory.remove(); return MemorySegmentFactory.wrapPooledHeapMemory(buf, owner); }
@Override MemorySegment allocateNewSegment(Object owner) { return MemorySegmentFactory.allocateUnpooledOffHeapMemory(segmentSize, owner); }
@SuppressWarnings("unchecked") private <T extends Comparable<T>> void assertNormalizableKey(NormalizableKey<T> key1, NormalizableKey<T> key2, int len) { byte[] normalizedKeys = new byte[32]; MemorySegment wrapper = MemorySegmentFactory.wrap(normalizedKeys); key1.copyNormalizedKey(wrapper, 0, len); key2.copyNormalizedKey(wrapper, len, len); for (int i = 0; i < len; i++) { int comp; int normKey1 = normalizedKeys[i] & 0xFF; int normKey2 = normalizedKeys[len + i] & 0xFF; if ((comp = (normKey1 - normKey2)) != 0) { if (Math.signum(key1.compareTo((T) key2)) != Math.signum(comp)) { Assert.fail("Normalized key comparison differs from actual key comparision"); } return; } } if (key1.compareTo((T) key2) != 0 && key1.getMaxNormalizedKeyLen() <= len) { Assert.fail("Normalized key was not able to distinguish keys, " + "although it should as the length of it sufficies to uniquely identify them"); } }
private static List<MemorySegment> getMemory(int numSegments, int segmentSize) { ArrayList<MemorySegment> list = new ArrayList<MemorySegment>(numSegments); for (int i = 0; i < numSegments; i++) { list.add(MemorySegmentFactory.allocateUnpooledSegment(segmentSize)); } return list; }
/** * Allocates some unpooled off-heap memory and creates a new memory segment that * represents that memory. * * @param size The size of the off-heap memory segment to allocate. * @param owner The owner to associate with the off-heap memory segment. * @return A new memory segment, backed by unpooled off-heap memory. */ public static MemorySegment allocateUnpooledOffHeapMemory(int size, Object owner) { ByteBuffer memory = ByteBuffer.allocateDirect(size); return wrapPooledOffHeapMemory(memory, owner); }
@Override MemorySegment requestSegmentFromPool(Object owner) { byte[] buf = availableMemory.remove(); return MemorySegmentFactory.wrapPooledHeapMemory(buf, owner); }
SpillReadBufferPool(int numberOfBuffers, int memorySegmentSize) { this.buffers = new ArrayDeque<>(numberOfBuffers); synchronized (buffers) { for (int i = 0; i < numberOfBuffers; i++) { buffers.add(new NetworkBuffer(MemorySegmentFactory.allocateUnpooledOffHeapMemory( memorySegmentSize, null), this)); } } }
@Override public Buffer allocateUnPooledNetworkBuffer(int size) { byte[] byteArray = new byte[size]; MemorySegment memSeg = MemorySegmentFactory.wrap(byteArray); return new NetworkBuffer(memSeg, FreeingBufferRecycler.INSTANCE, false); } }
public MemorySegment setupNormalizedKeysMemSegment(T[] data, int normKeyLen, TypeComparator<T> comparator) { MemorySegment memSeg = MemorySegmentFactory.allocateUnpooledSegment(2048); // Setup normalized Keys in the memory segment int offset = 0; for (T e : data) { comparator.putNormalizedKey(e, memSeg, offset, normKeyLen); offset += normKeyLen; } return memSeg; }
@Override MemorySegment allocateNewSegment(Object owner) { ByteBuffer memory = ByteBuffer.allocateDirect(segmentSize); return MemorySegmentFactory.wrapPooledOffHeapMemory(memory, owner); }
SpillReadBufferPool(int numberOfBuffers, int memorySegmentSize) { this.buffers = new ArrayDeque<>(numberOfBuffers); synchronized (buffers) { for (int i = 0; i < numberOfBuffers; i++) { buffers.add(new NetworkBuffer(MemorySegmentFactory.allocateUnpooledOffHeapMemory( memorySegmentSize, null), this)); } } }
@Override public MemorySegment nextSegment() { allocated++; if (allocated <= segments.size()) { return segments.get(allocated - 1); } else { return MemorySegmentFactory.wrap(new byte[pageSize()]); } }
MemorySegment seg = MemorySegmentFactory.allocateUnpooledSegment(pageSize);