156 lines
4.2 KiB
Java
156 lines
4.2 KiB
Java
|
/*
|
||
|
* Copyright 2012 The Netty Project
|
||
|
*
|
||
|
* The Netty Project licenses this file to you 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.netty.buffer;
|
||
|
|
||
|
final class PoolSubpage<T> {
|
||
|
|
||
|
final PoolChunk<T> chunk;
|
||
|
final int memoryMapIdx;
|
||
|
final int runOffset;
|
||
|
final int pageSize;
|
||
|
final long[] bitmap;
|
||
|
|
||
|
boolean doNotDestroy;
|
||
|
int elemSize;
|
||
|
int maxNumElems;
|
||
|
int nextAvail;
|
||
|
int bitmapLength;
|
||
|
int numAvail;
|
||
|
|
||
|
// TODO: Test if adding padding helps under contention
|
||
|
//private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7;
|
||
|
|
||
|
PoolSubpage(PoolChunk<T> chunk, int memoryMapIdx, int runOffset, int pageSize, int elemSize) {
|
||
|
this.chunk = chunk;
|
||
|
this.memoryMapIdx = memoryMapIdx;
|
||
|
this.runOffset = runOffset;
|
||
|
this.pageSize = pageSize;
|
||
|
bitmap = new long[pageSize >>> 10]; // pageSize / 16 / 64
|
||
|
init(elemSize);
|
||
|
}
|
||
|
|
||
|
void init(int elemSize) {
|
||
|
doNotDestroy = true;
|
||
|
this.elemSize = elemSize;
|
||
|
if (elemSize == 0) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
maxNumElems = numAvail = pageSize / elemSize;
|
||
|
nextAvail = 0;
|
||
|
bitmapLength = maxNumElems >>> 6;
|
||
|
if ((maxNumElems & 63) != 0) {
|
||
|
bitmapLength ++;
|
||
|
}
|
||
|
|
||
|
for (int i = 0; i < bitmapLength; i ++) {
|
||
|
bitmap[i] = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the bitmap index of the subpage allocation.
|
||
|
*/
|
||
|
long allocate() {
|
||
|
if (elemSize == 0) {
|
||
|
return toHandle(0);
|
||
|
}
|
||
|
|
||
|
if (numAvail == 0 || !doNotDestroy) {
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
final int bitmapIdx = nextAvail;
|
||
|
int q = bitmapIdx >>> 6;
|
||
|
int r = bitmapIdx & 63;
|
||
|
assert (bitmap[q] >>> r & 1) == 0;
|
||
|
bitmap[q] |= 1L << r;
|
||
|
|
||
|
if (-- numAvail == 0) {
|
||
|
nextAvail = -1;
|
||
|
} else {
|
||
|
nextAvail = findNextAvailable();
|
||
|
}
|
||
|
|
||
|
return toHandle(bitmapIdx);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return {@code true} if this subpage is in use.
|
||
|
* {@code false} if this subpage is not used by its chunk and thus it's OK to be released.
|
||
|
*/
|
||
|
boolean free(int bitmapIdx) {
|
||
|
if (elemSize == 0) {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
int q = bitmapIdx >>> 6;
|
||
|
int r = bitmapIdx & 63;
|
||
|
assert (bitmap[q] >>> r & 1) != 0;
|
||
|
bitmap[q] ^= 1L << r;
|
||
|
|
||
|
if (numAvail ++ == 0) {
|
||
|
nextAvail = bitmapIdx;
|
||
|
chunk.arena.addSubpage(this);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
if (numAvail < maxNumElems) {
|
||
|
return true;
|
||
|
} else {
|
||
|
doNotDestroy = false;
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private int findNextAvailable() {
|
||
|
int newNextAvail = -1;
|
||
|
loop:
|
||
|
for (int i = 0; i < bitmapLength; i ++) {
|
||
|
long bits = bitmap[i];
|
||
|
if (~bits != 0) {
|
||
|
for (int j = 0; j < 64; j ++) {
|
||
|
if ((bits & 1) == 0) {
|
||
|
newNextAvail = i << 6 | j;
|
||
|
break loop;
|
||
|
}
|
||
|
bits >>>= 1;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (newNextAvail < maxNumElems) {
|
||
|
return newNextAvail;
|
||
|
} else {
|
||
|
return -1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private long toHandle(int bitmapIdx) {
|
||
|
return 0x4000000000000000L | (long) bitmapIdx << 32 | memoryMapIdx;
|
||
|
}
|
||
|
|
||
|
public String toString() {
|
||
|
if (!doNotDestroy) {
|
||
|
return "(" + memoryMapIdx + ": not in use)";
|
||
|
}
|
||
|
|
||
|
return String.valueOf('(') + memoryMapIdx + ": " + (maxNumElems - numAvail) + '/' + maxNumElems +
|
||
|
", offset: " + runOffset + ", length: " + pageSize + ", elemSize: " + elemSize + ')';
|
||
|
}
|
||
|
}
|