/// <summary> /// 向指定内存段的指定偏移处回写一个void* /// </summary> /// <param name="position">回写位置</param> /// <param name="value">回写值</param> /// <param name="length">回写长度</param> public void WriteBackMemory(MemoryPosition position, void *value, uint length) { //well-done, needn't cross memory segments. if (ThriftProtocolMemoryAllotter.SegmentSize - position.SegmentOffset == 0) { position.SegmentIndex++; position.SegmentOffset = 0; } IMemorySegment segment = GetSegment(position.SegmentIndex); int startSegmentIndex = position.SegmentIndex; uint remainingSize = (ThriftProtocolMemoryAllotter.SegmentSize - position.SegmentOffset); if (remainingSize >= length) { Native.Win32API.memcpy(new IntPtr(_segments[position.SegmentIndex].GetPointer() + position.SegmentOffset), new IntPtr(value), length); } else { uint trueRemainingSize = length; uint continueSize = 0U; do { Native.Win32API.memcpy( new IntPtr(segment.GetPointer() + (position.SegmentIndex - startSegmentIndex == 0 ? position.SegmentOffset : 0)), new IntPtr((byte *)value + continueSize), (remainingSize < trueRemainingSize ? remainingSize : (trueRemainingSize < ThriftProtocolMemoryAllotter.SegmentSize ? trueRemainingSize : ThriftProtocolMemoryAllotter.SegmentSize))); if (trueRemainingSize <= remainingSize) { trueRemainingSize = 0U; } else { trueRemainingSize -= remainingSize; } segment = GetSegment(++position.SegmentIndex); continueSize += remainingSize; remainingSize = ThriftProtocolMemoryAllotter.SegmentSize; } while (trueRemainingSize > 0U); } }
/// <summary> /// 向指定内存段的指定偏移处回写一个uint32数值 /// </summary> /// <param name="position">回写位置</param> /// <param name="value">回写值</param> public void WriteBackUInt32(MemoryPosition position, uint value) { //well-done, needn't cross memory segments. uint remainingSize = (ThriftProtocolMemoryAllotter.SegmentSize - position.SegmentOffset); if (remainingSize >= Size.UInt32) { *(uint *)(_segments[position.SegmentIndex].GetPointer() + position.SegmentOffset) = value; } else { uint trueRemainingSize = Size.UInt32; byte *data = (byte *)&value; if (remainingSize != 0U) { Native.Win32API.memcpy(new IntPtr(_segments[position.SegmentIndex].GetPointer() + position.SegmentOffset), new IntPtr(data), remainingSize); trueRemainingSize -= remainingSize; } //write at head. Native.Win32API.memcpy(new IntPtr(_segments[position.SegmentIndex + 1].GetPointer()), new IntPtr(data + remainingSize), trueRemainingSize); } }
/// <summary> /// ��ָ���ڴ�ε�ָ��ƫ�ƴ���дһ��uint32��ֵ /// </summary> /// <param name="position">��дλ��</param> /// <param name="value">��дֵ</param> public void WriteBackUInt32(MemoryPosition position, uint value) { //well-done, needn't cross memory segments. uint remainingSize = (ThriftProtocolMemoryAllotter.SegmentSize - position.SegmentOffset); if (remainingSize >= Size.UInt32) *(uint*)(_segments[position.SegmentIndex].GetPointer() + position.SegmentOffset) = value; else { uint trueRemainingSize = Size.UInt32; byte* data = (byte*)&value; if (remainingSize != 0U) { Native.Win32API.memcpy(new IntPtr(_segments[position.SegmentIndex].GetPointer() + position.SegmentOffset), new IntPtr(data), remainingSize); trueRemainingSize -= remainingSize; } //write at head. Native.Win32API.memcpy(new IntPtr(_segments[position.SegmentIndex + 1].GetPointer()), new IntPtr(data + remainingSize), trueRemainingSize); } }
/// <summary> /// ��ָ���ڴ�ε�ָ��ƫ�ƴ���дһ��void* /// </summary> /// <param name="position">��дλ��</param> /// <param name="value">��дֵ</param> /// <param name="length">��д����</param> public void WriteBackMemory(MemoryPosition position, void* value, uint length) { //well-done, needn't cross memory segments. if (ThriftProtocolMemoryAllotter.SegmentSize - position.SegmentOffset == 0) { position.SegmentIndex++; position.SegmentOffset = 0; } IMemorySegment segment = GetSegment(position.SegmentIndex); int startSegmentIndex = position.SegmentIndex; uint remainingSize = (ThriftProtocolMemoryAllotter.SegmentSize - position.SegmentOffset); if (remainingSize >= length) Native.Win32API.memcpy(new IntPtr(_segments[position.SegmentIndex].GetPointer() + position.SegmentOffset), new IntPtr(value), length); else { uint trueRemainingSize = length; uint continueSize = 0U; do { Native.Win32API.memcpy( new IntPtr(segment.GetPointer() + (position.SegmentIndex - startSegmentIndex == 0 ? position.SegmentOffset : 0)), new IntPtr((byte*)value + continueSize), (remainingSize < trueRemainingSize ? remainingSize : (trueRemainingSize < ThriftProtocolMemoryAllotter.SegmentSize ? trueRemainingSize : ThriftProtocolMemoryAllotter.SegmentSize))); if (trueRemainingSize <= remainingSize) trueRemainingSize = 0U; else trueRemainingSize -= remainingSize; segment = GetSegment(++position.SegmentIndex); continueSize += remainingSize; remainingSize = ThriftProtocolMemoryAllotter.SegmentSize; } while (trueRemainingSize > 0U); } }
/// <summary> /// 计算截止位置与开始位置之间的距离 /// </summary> /// <param name="segmentCount">内存段数量</param> /// <param name="start">起始位置</param> /// <param name="end">截止位置</param> /// <returns>返回它们之间所差的距离</returns> public static int CalcLength(int segmentCount, MemoryPosition start, MemoryPosition end) { return (int) (((end.SegmentIndex)*ThriftProtocolMemoryAllotter.SegmentSize + end.SegmentOffset) - ((start.SegmentIndex)*ThriftProtocolMemoryAllotter.SegmentSize + start.SegmentOffset)); }
/// <summary> /// 计算截止位置与开始位置之间的距离 /// </summary> /// <param name="segmentCount">内存段数量</param> /// <param name="start">起始位置</param> /// <param name="end">截止位置</param> /// <returns>返回它们之间所差的距离</returns> public static int CalcLength(int segmentCount, MemoryPosition start, MemoryPosition end) { return((int)(((end.SegmentIndex) * ThriftProtocolMemoryAllotter.SegmentSize + end.SegmentOffset) - ((start.SegmentIndex) * ThriftProtocolMemoryAllotter.SegmentSize + start.SegmentOffset))); }