/// <summary> /// 根据期望大小获得一个buffer. /// autoSetValidLength为true则默认设置validLength为期望大小,否则设置为0. /// </summary> /// <param name="size">期望大小</param> /// <param name="autoSetValidLength">是否validLength会自动标记为size</param> /// <returns></returns> public ByteBuffer GetBuffer(long size, bool autoSetValidLength = false) { ByteBuffer bbf = null; if (size > maxBlockSize) { ByteBufferPool.countNew++; DxDebug.LogConsole("ByteBufferPools.GetBuffer():申请了一块过大的内存,size=" + size); //这个内存块太了,所以就不作缓存了 bbf = new ByteBuffer(size); } else { ByteBufferPool bbPool = ChoosePool(size); bbf = bbPool.GetBuffer((int)size); } if (autoSetValidLength) { bbf.validLength = (int)size; } else { bbf.validLength = 0; } return(bbf); }
/// <summary> /// 构造 /// </summary> public ByteBufferPools() { long curBlockSize = mixBlockSize; while (curBlockSize <= maxBlockSize) { //统一使用4M好了 ByteBufferPool bfPool = new ByteBufferPool(curBlockSize, 4 * 1024 * 1024); curBlockSize = 2 * curBlockSize; _listPool.Add(bfPool); } }