Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,17 @@ public abstract class AllocationManager {
// see JIRA for details
private final LowCostIdentityHashMap<BaseAllocator, BufferLedger> map = new LowCostIdentityHashMap<>();
private final long amCreationTime = System.nanoTime();
private final long size;

// The ReferenceManager created at the time of creation of this AllocationManager
// is treated as the owning reference manager for the underlying chunk of memory
// managed by this allocation manager
private volatile BufferLedger owningLedger;
private volatile long amDestructionTime = 0;

protected AllocationManager(BaseAllocator accountingAllocator, long size) {
protected AllocationManager(BaseAllocator accountingAllocator) {
Preconditions.checkNotNull(accountingAllocator);
accountingAllocator.assertOpen();

this.size = size;
this.root = accountingAllocator.root;

// we do a no retain association since our creator will want to retrieve the newly created
Expand Down Expand Up @@ -181,11 +179,11 @@ void release(final BufferLedger ledger) {
/**
* Return the size of underlying chunk of memory managed by this Allocation Manager.
*
* <p>The underlying memory chunk managed can be different from the original requested size.
*
* @return size of underlying memory chunk
*/
public long getSize() {
return size;
}
public abstract long getSize();

/**
* Return the absolute memory address pointing to the fist byte of underling memory chunk.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public class NettyAllocationManager extends AllocationManager {
static final UnsafeDirectLittleEndian EMPTY = INNER_ALLOCATOR.empty;
static final long CHUNK_SIZE = INNER_ALLOCATOR.getChunkSize();

private final int size;
private final int allocatedSize;
private final UnsafeDirectLittleEndian memoryChunk;

NettyAllocationManager(BaseAllocator accountingAllocator, int size) {
super(accountingAllocator, size);
this.memoryChunk = INNER_ALLOCATOR.allocate(size);
this.size = memoryChunk.capacity();
NettyAllocationManager(BaseAllocator accountingAllocator, int requestedSize) {
super(accountingAllocator);
this.memoryChunk = INNER_ALLOCATOR.allocate(requestedSize);
this.allocatedSize = memoryChunk.capacity();
}

/**
Expand All @@ -61,9 +61,14 @@ protected void release0() {
memoryChunk.release();
}

/**
* Returns the underlying memory chunk size managed.
*
* <p>NettyAllocationManager rounds requested size up to the next power of two.
*/
@Override
public long getSize() {
return size;
return allocatedSize;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,9 @@ public void testCustomizedAllocationManager() {
private BaseAllocator createAllocatorWithCustomizedAllocationManager() {
return new RootAllocator(BaseAllocator.configBuilder()
.maxAllocation(MAX_ALLOCATION)
.allocationManagerFactory((accountingAllocator, size) -> new AllocationManager(accountingAllocator, size) {
.allocationManagerFactory((accountingAllocator, requestedSize) -> new AllocationManager(accountingAllocator) {
private final Unsafe unsafe = getUnsafe();
private final long address = unsafe.allocateMemory(size);
private final long address = unsafe.allocateMemory(requestedSize);

@Override
protected long memoryAddress() {
Expand All @@ -402,10 +402,15 @@ protected long memoryAddress() {

@Override
protected void release0() {
unsafe.setMemory(address, size, (byte) 0);
unsafe.setMemory(address, requestedSize, (byte) 0);
unsafe.freeMemory(address);
}

@Override
public long getSize() {
return requestedSize;
}

private Unsafe getUnsafe() {
Field f = null;
try {
Expand Down