PoolSubpage boolean free(PoolSubpage<T> head, int bitmapIdx)
//subpage在使用中(在链表中)则返回true,否则返回false boolean free(PoolSubpage<T> head, int bitmapIdx) { if (elemSize == 0) { return true; } //计算bitmap下标 int q = bitmapIdx >>> 6; //计算在位图中的下标 int r = bitmapIdx & 63; assert (bitmap[q] >>> r & 1) != 0; //将位图中对应的位置为0 bitmap[q] ^= 1L << r; //设置下一个可用 setNextAvail(bitmapIdx); //可用数量为0的时候subpage已经不在链表中,需要将其加入链表中 if (numAvail ++ == 0) { addToPool(head); return true; } if (numAvail != maxNumElems) { return true; } else { // Subpage 未被使用 (numAvail == maxNumElems) if (prev == next) { // 如果链表中只有一个subpage,那么不要删除它 return true; } // 如果链表中还有其它subpage,那么从链表中删除当前subpage doNotDestroy = false; removeFromPool(); return false; } } private void addToPool(PoolSubpage<T> head) { assert prev == null && next == null; prev = head; next = head.next; next.prev = this; head.next = this; } private void removeFromPool() { assert prev != null && next != null; prev.next = next; next.prev = prev; next = null; prev = null; }
PoolChunk free(long handle, ByteBuffer nioBuffer)
void free(long handle, ByteBuffer nioBuffer) { //handle的低32位是memoryMapIdx int memoryMapIdx = memoryMapIdx(handle); //handle的高32们是bitmapIdx int bitmapIdx = bitmapIdx(handle); // bitmapIdx !=0 表明分配的是subpage if (bitmapIdx != 0) { //根据 memoryMapIdx 获取subpages中的某个subpage PoolSubpage<T> subpage = subpages[subpageIdx(memoryMapIdx)]; assert subpage != null && subpage.doNotDestroy;