Пример #1
0
        /// <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 (MemoryAllotter.SegmentSize - position.SegmentOffset == 0)
            {
                position.SegmentIndex++;
                position.SegmentOffset = 0;
            }
            IMemorySegment segment           = GetSegment(position.SegmentIndex);
            int            startSegmentIndex = position.SegmentIndex;
            uint           remainingSize     = (MemoryAllotter.SegmentSize - position.SegmentOffset);

            if (remainingSize >= length)
            {
                Buffer.MemoryCopy((void *)new IntPtr(value), (void *)new IntPtr(_segments[position.SegmentIndex].GetPointer() + position.SegmentOffset), length /*protective range*/, length);
            }
            //Native.Win32API.memcpy(new IntPtr(_segments[position.SegmentIndex].GetPointer() + position.SegmentOffset), new IntPtr(value), length);
            else
            {
                uint trueRemainingSize = length;
                uint continueSize      = 0U;
                do
                {
                    Buffer.MemoryCopy(
                        (void *)new IntPtr((byte *)value + continueSize),
                        (void *)new IntPtr(segment.GetPointer() + (position.SegmentIndex - startSegmentIndex == 0 ? position.SegmentOffset : 0)),
                        MemoryAllotter.SegmentSize /*protective range*/,
                        (remainingSize < trueRemainingSize ? remainingSize : (trueRemainingSize < MemoryAllotter.SegmentSize ? trueRemainingSize : MemoryAllotter.SegmentSize)));
                    //Native.Win32API.memcpy(
                    //    new IntPtr(segment.GetPointer() +(position.SegmentIndex - startSegmentIndex == 0 ? position.SegmentOffset : 0)),
                    //    new IntPtr((byte*)value + continueSize),
                    //    (remainingSize < trueRemainingSize? remainingSize: (trueRemainingSize < MemoryAllotter.SegmentSize? trueRemainingSize: MemoryAllotter.SegmentSize)));
                    if (trueRemainingSize <= remainingSize)
                    {
                        trueRemainingSize = 0U;
                    }
                    else
                    {
                        trueRemainingSize -= remainingSize;
                    }
                    segment       = GetSegment(++position.SegmentIndex);
                    continueSize += remainingSize;
                    remainingSize = MemoryAllotter.SegmentSize;
                } while (trueRemainingSize > 0U);
            }
        }
Пример #2
0
        /// <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 = (MemoryAllotter.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)
                {
                    Buffer.MemoryCopy((void *)new IntPtr(data), (void *)new IntPtr(_segments[position.SegmentIndex].GetPointer() + position.SegmentOffset), remainingSize, remainingSize);
                    //Native.Win32API.memcpy(new IntPtr(_segments[position.SegmentIndex].GetPointer() + position.SegmentOffset), new IntPtr(data), remainingSize);
                    trueRemainingSize -= remainingSize;
                }
                //write at head.
                Buffer.MemoryCopy((void *)new IntPtr(data + remainingSize), (void *)new IntPtr(_segments[position.SegmentIndex + 1].GetPointer()), MemoryAllotter.SegmentSize, trueRemainingSize);
                //Native.Win32API.memcpy(new IntPtr(_segments[position.SegmentIndex + 1].GetPointer()), new IntPtr(data + remainingSize), trueRemainingSize);
            }
        }
Пример #3
0
 /// <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) * MemoryAllotter.SegmentSize + end.SegmentOffset) -
                  ((start.SegmentIndex) * MemoryAllotter.SegmentSize + start.SegmentOffset)));
 }