MemoryBlock.cs 51.6 KB
Newer Older
cdy816's avatar
cdy816 已提交
1 2 3 4 5 6 7 8 9
//==============================================================
//  Copyright (C) 2019  Inc. All rights reserved.
//
//==============================================================
//  Create by 种道洋 at 2019/12/27 18:45:02.
//  Version 1.0
//  种道洋
//==============================================================
using System;
cdy816's avatar
cdy816 已提交
10 11
using System.Collections.Generic;
using System.IO;
cdy816's avatar
cdy816 已提交
12
using System.Runtime.InteropServices;
cdy816's avatar
cdy816 已提交
13
using System.Text;
cdy816's avatar
cdy816 已提交
14
using System.Threading;
cdy816's avatar
cdy816 已提交
15 16 17 18 19 20

namespace Cdy.Tag
{
    /// <summary>
    /// 划分内存
    /// </summary>
cdy816's avatar
cdy816 已提交
21
    public unsafe class MemoryBlock : IDisposable
cdy816's avatar
cdy816 已提交
22 23 24
    {

        #region ... Variables  ...
cdy816's avatar
cdy816 已提交
25

26 27 28 29
        ///// <summary>
        ///// 
        ///// </summary>
        //private byte[] mDataBuffer;
cdy816's avatar
cdy816 已提交
30

cdy816's avatar
tt  
cdy816 已提交
31 32
        private List<byte[]> mBuffers;

cdy816's avatar
cdy816 已提交
33 34 35
        /// <summary>
        /// 
        /// </summary>
cdy816's avatar
tt  
cdy816 已提交
36
        private List<IntPtr> mHandles;
cdy816's avatar
cdy816 已提交
37

cdy816's avatar
cdy816 已提交
38 39 40 41 42
        /// <summary>
        /// 
        /// </summary>
        private long mPosition = 0;

43
        //private long mUsedSize = 0;
cdy816's avatar
cdy816 已提交
44 45 46 47 48 49

        private long mAllocSize = 0;

        private object mUserSizeLock = new object();


50
        public int BufferItemSize = 1024 * 1024 * 4;
cdy816's avatar
cdy816 已提交
51 52

        public static byte[] zoreData = new byte[1024 * 10];
cdy816's avatar
tt  
cdy816 已提交
53

cdy816's avatar
cdy816 已提交
54 55 56 57 58 59 60 61 62 63 64 65
        #endregion ...Variables...

        #region ... Events     ...

        #endregion ...Events...

        #region ... Constructor...


        /// <summary>
        /// 
        /// </summary>
66
        public MemoryBlock()
cdy816's avatar
cdy816 已提交
67 68 69 70 71 72 73 74
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="size"></param>
cdy816's avatar
cdy816 已提交
75
        /// <param name="blockSize"></param>
cdy816's avatar
cdy816 已提交
76
        public MemoryBlock(long size, int blockSize)
cdy816's avatar
cdy816 已提交
77
        {
cdy816's avatar
cdy816 已提交
78 79 80
            BufferItemSize = blockSize;
            Init(size);
        }
cdy816's avatar
tt  
cdy816 已提交
81

cdy816's avatar
cdy816 已提交
82 83 84 85 86 87 88
        /// <summary>
        /// 
        /// </summary>
        /// <param name="size"></param>
        public MemoryBlock(long size)
        {
            Init(size);
cdy816's avatar
cdy816 已提交
89 90 91 92 93 94
        }

        #endregion ...Constructor...

        #region ... Properties ...

cdy816's avatar
cdy816 已提交
95 96 97 98 99
        /// <summary>
        /// 
        /// </summary>
        public string Name { get; set; }

100 101 102 103 104 105 106 107 108
        /// <summary>
        /// 
        /// </summary>
        public List<byte[]> Buffers
        {
            get
            {
                return mBuffers;
            }
cdy816's avatar
cdy816 已提交
109 110 111 112
            internal set
            {
                mBuffers = value;
            }
113 114 115
        }


cdy816's avatar
cdy816 已提交
116 117 118
        /// <summary>
        /// 
        /// </summary>
cdy816's avatar
tt  
cdy816 已提交
119 120 121 122 123 124 125 126
        public byte[] StartMemory
        {
            get
            {
                return mBuffers[0];
            }
        }

127 128 129
        /// <summary>
        /// 
        /// </summary>
cdy816's avatar
tt  
cdy816 已提交
130
        public byte[] EndMemory
cdy816's avatar
cdy816 已提交
131 132 133
        {
            get
            {
cdy816's avatar
tt  
cdy816 已提交
134
                return mBuffers[0];
cdy816's avatar
cdy816 已提交
135 136 137 138 139 140 141 142 143 144 145 146
            }
        }

        /// <summary>
        /// 是否繁忙
        /// </summary>
        public bool IsBusy { get; set; }


        /// <summary>
        /// 
        /// </summary>
cdy816's avatar
cdy816 已提交
147
        public long Length
cdy816's avatar
cdy816 已提交
148 149 150
        {
            get
            {
cdy816's avatar
cdy816 已提交
151
                return mBuffers.Count * (long)BufferItemSize;
cdy816's avatar
cdy816 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public long Position
        {
            get
            {
                return mPosition;
            }
            set
            {
                mPosition = value;
167 168
                //lock (mUserSizeLock)
                //    mUsedSize = mUsedSize < value ? value : mUsedSize;
cdy816's avatar
cdy816 已提交
169 170 171 172
            }
        }


173 174 175 176 177 178 179 180 181 182
        ///// <summary>
        ///// 
        ///// </summary>
        //public long UsedSize
        //{
        //    get
        //    {
        //        return mUsedSize;
        //    }
        //}
cdy816's avatar
cdy816 已提交
183

cdy816's avatar
cdy816 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
        /// <summary>
        /// 
        /// </summary>
        public long AllocSize
        {
            get
            {
                return mAllocSize;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        internal List<IntPtr> Handles
        {
            get
            {
                return mHandles;
            }
            set
            {
                mHandles = value;
            }

        }

cdy816's avatar
cdy816 已提交
212 213 214 215 216

        #endregion ...Properties...

        #region ... Methods    ...

cdy816's avatar
cdy816 已提交
217
        /// <summary>
cdy816's avatar
cdy816 已提交
218
        /// 
cdy816's avatar
cdy816 已提交
219 220
        /// </summary>
        /// <param name="size"></param>
cdy816's avatar
cdy816 已提交
221
        private void Init(long size)
cdy816's avatar
cdy816 已提交
222
        {
cdy816's avatar
cdy816 已提交
223 224 225 226 227
            int count = (int)(size / BufferItemSize);

            count = size % BufferItemSize > 0 ? count + 1 : count;


cdy816's avatar
tt  
cdy816 已提交
228 229 230 231 232
            mBuffers = new List<byte[]>(count);
            for (int i = 0; i < count; i++)
            {
                mBuffers.Add(new byte[BufferItemSize]);
            }
cdy816's avatar
cdy816 已提交
233 234

            mHandles = new List<IntPtr>();
cdy816's avatar
tt  
cdy816 已提交
235 236 237 238
            for (int i = 0; i < count; i++)
            {
                mHandles.Add(System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(mBuffers[i], 0));
            }
cdy816's avatar
cdy816 已提交
239
            mAllocSize = size;
240
            //mDataBuffer = mBuffers[0];
cdy816's avatar
cdy816 已提交
241 242
        }

cdy816's avatar
cdy816 已提交
243 244 245 246 247 248 249 250 251 252 253
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <returns></returns>
        //public  IEnumerable<MemoryStream> GetStream()
        //{
        //    foreach(var vv in mBuffers)
        //    {
        //        yield return new MemoryStream(vv);
        //    }
        //}
cdy816's avatar
cdy816 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

        /// <summary>
        /// 重新分配对象
        /// </summary>
        /// <param name="size"></param>
        public void ReAlloc(long size)
        {
            //int count = (int)(size / BufferItemSize) + 1;
            //mBuffers = new List<byte[]>(count);
            //for (int i = 0; i < count; i++)
            //{
            //    mBuffers.Add(new byte[BufferItemSize]);
            //}
            //for (int i = 0; i < count; i++)
            //{
            //    mHandles.Add(System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(mBuffers[i], 0));
            //}
            Init(size);
cdy816's avatar
cdy816 已提交
272 273 274 275 276 277 278 279 280
            GC.Collect();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="size"></param>
        public void Resize(long size)
        {
cdy816's avatar
tt  
cdy816 已提交
281 282 283 284 285 286 287 288
            int count = (int)(size / BufferItemSize) + 1;
            List<byte[]> nBuffers = new List<byte[]>(count);

            if (count > mBuffers.Count)
            {
                int i = 0;
                for (i = 0; i < mBuffers.Count; i++)
                {
cdy816's avatar
cdy816 已提交
289
                    nBuffers.Add(mBuffers[i]);
cdy816's avatar
tt  
cdy816 已提交
290 291 292
                }
                for(int j=i;j<count;j++)
                {
cdy816's avatar
cdy816 已提交
293
                    mBuffers.Add(new byte[BufferItemSize]);
cdy816's avatar
tt  
cdy816 已提交
294 295 296 297 298 299
                }
            }
            else if (count < mBuffers.Count)
            {
                for(int i=0;i<count;i++)
                {
cdy816's avatar
cdy816 已提交
300
                    nBuffers.Add(mBuffers[i]);
cdy816's avatar
tt  
cdy816 已提交
301 302
                }
            }
cdy816's avatar
cdy816 已提交
303
            
cdy816's avatar
tt  
cdy816 已提交
304 305 306 307 308 309
            mBuffers = nBuffers;
            mHandles = new List<IntPtr>();
            for (int i = 0; i < count; i++)
            {
                mHandles.Add(System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(mBuffers[i], 0));
            }
cdy816's avatar
cdy816 已提交
310 311 312
            GC.Collect();
        }

cdy816's avatar
cdy816 已提交
313 314 315 316
        /// <summary>
        /// 
        /// </summary>
        /// <param name="size"></param>
cdy816's avatar
cdy816 已提交
317
        public void CheckAndResize(long size)
cdy816's avatar
cdy816 已提交
318 319 320
        {
            if(size>mAllocSize)
            {
cdy816's avatar
cdy816 已提交
321 322 323 324
                if (size > this.Length)
                {
                    int count = (int)(size / BufferItemSize);
                    count = size % BufferItemSize > 0 ? count + 1 : count;
cdy816's avatar
cdy816 已提交
325

cdy816's avatar
cdy816 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
                    if (mBuffers.Count < count)
                    {
                        List<byte[]> nBuffers = new List<byte[]>(count);
                        int i = 0;
                        for (i = 0; i < mBuffers.Count; i++)
                        {
                            nBuffers.Add(mBuffers[i]);
                            //nBuffers[i] = mBuffers[i];
                        }

                        for (int j = i; j < count; j++)
                        {
                            nBuffers.Add(new byte[BufferItemSize]);
                           // nBuffers[j] = new byte[BufferItemSize];
                        }

                        mBuffers = nBuffers;
                        mHandles = new List<IntPtr>();
                        for (i = 0; i < count; i++)
                        {
                            mHandles.Add(System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(mBuffers[i], 0));
                        }
348
                        //GC.Collect();
cdy816's avatar
cdy816 已提交
349 350
                    }
                    mAllocSize = size;
351
                    //GC.Collect();
cdy816's avatar
cdy816 已提交
352

353
                    LoggerService.Service.Info("MemoryBlock", "CheckAndResize " + this.Name + " " + size, ConsoleColor.Red);
cdy816's avatar
cdy816 已提交
354 355
                }
                else
cdy816's avatar
cdy816 已提交
356
                {
cdy816's avatar
cdy816 已提交
357
                    mAllocSize = size;
cdy816's avatar
cdy816 已提交
358
                }
cdy816's avatar
cdy816 已提交
359

cdy816's avatar
cdy816 已提交
360 361 362
            }
        }

cdy816's avatar
cdy816 已提交
363 364 365 366 367
        /// <summary>
        /// 清空内存
        /// </summary>
        public void Clear()
        {
cdy816's avatar
cdy816 已提交
368 369
            //foreach (var vv in mBuffers)
            //    Array.Clear(vv, 0, vv.Length);
cdy816's avatar
tt  
cdy816 已提交
370
            foreach (var vv in mBuffers)
cdy816's avatar
cdy816 已提交
371 372 373 374 375 376
            {
                for (int i = 0; i < BufferItemSize / zoreData.Length; i++)
                {
                    Buffer.BlockCopy(zoreData, 0, vv,i * zoreData.Length, zoreData.Length);
                }
            }
377
            //mUsedSize = 0;
cdy816's avatar
cdy816 已提交
378
            mPosition = 0;
cdy816's avatar
cdy816 已提交
379 380

            LoggerService.Service.Info("MemoryBlock", Name + " is clear !");
cdy816's avatar
cdy816 已提交
381 382
        }

cdy816's avatar
cdy816 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
        /// <summary>
        /// 
        /// </summary>
        /// <param name="target"></param>
        /// <param name="start"></param>
        /// <param name="len"></param>
        private void Clear(byte[] target,int start,int len)
        {
            int i = 0;
            for (i = 0; i < len / zoreData.Length; i++)
            {
                Buffer.BlockCopy(zoreData, 0, target, start + i * zoreData.Length, zoreData.Length);
            }
            int zz = len % zoreData.Length;
            if (zz > 0)
            {
                Buffer.BlockCopy(zoreData, 0, target, start+ i * zoreData.Length, zz);
            }
        }

cdy816's avatar
cdy816 已提交
403 404
        #region ReadAndWrite

cdy816's avatar
tt  
cdy816 已提交
405 406 407 408 409 410 411
        /// <summary>
        /// 
        /// </summary>
        /// <param name="position"></param>
        /// <param name="size"></param>
        /// <param name="innerAddr"></param>
        /// <param name="offset"></param>
412
        private IntPtr RelocationAddress(long position, out long offset)
cdy816's avatar
tt  
cdy816 已提交
413 414 415
        {
            offset = position % BufferItemSize;
            int id = (int)(position / BufferItemSize);
416
            return mHandles[id];
cdy816's avatar
tt  
cdy816 已提交
417 418 419 420 421 422 423
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="position"></param>
        /// <param name="offset"></param>
424 425
        /// <returns></returns>
        private int RelocationAddressToArrayIndex(long position,out long offset)
cdy816's avatar
tt  
cdy816 已提交
426 427
        {
            offset = position % BufferItemSize;
428
            return (int)(position / BufferItemSize);
cdy816's avatar
tt  
cdy816 已提交
429 430 431 432 433 434 435 436
        }

        /// <summary>
        /// 数据是否跨数据块
        /// </summary>
        /// <param name="position"></param>
        /// <param name="size"></param>
        /// <returns></returns>
437
        private bool IsCrossDataBuffer(long position, int size)
cdy816's avatar
tt  
cdy816 已提交
438 439 440 441
        {
            return (position % BufferItemSize) + size >= BufferItemSize;
        }

cdy816's avatar
cdy816 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(DateTime value)
        {
            WriteDatetime(mPosition, value);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(long value)
        {
            WriteLong(mPosition, value);
        }

460 461 462 463 464 465 466 467 468 469
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>

        public void Write(ulong value)
        {
            WriteULong(mPosition, value);
        }

cdy816's avatar
cdy816 已提交
470 471 472 473 474 475 476 477 478
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(int value)
        {
            WriteInt(mPosition, value);
        }

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(uint value)
        {
            WriteUInt(mPosition, value);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(short value)
        {
            WriteShort(mPosition, value);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(ushort value)
        {
            WriteUShort(mPosition, value);
        }

cdy816's avatar
cdy816 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(float value)
        {
            WriteFloat(mPosition, value);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(double value)
        {
            WriteDouble(mPosition, value);
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="encoding"></param>
        public void Write(string value,Encoding encoding)
        {
            WriteString(mPosition, value, encoding);
        }

536 537 538 539 540 541 542 543 544
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(string value)
        {
            WriteString(mPosition, value, Encoding.Unicode);
        }

cdy816's avatar
cdy816 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(byte value)
        {
            WriteByte(mPosition, value);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="values"></param>
        public void Write(byte[] values)
        {
            WriteBytes(mPosition, values);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="values"></param>
        /// <param name="offset"></param>
        /// <param name="len"></param>
        public void Write(byte[] values,int offset,int len)
        {
            WriteBytes(mPosition, values,offset,len);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="value"></param>
        public void WriteLong(long offset, long value)
        {
cdy816's avatar
tt  
cdy816 已提交
581 582
            IntPtr hd;
            long ost;
583
            if (IsCrossDataBuffer(offset,8))
cdy816's avatar
tt  
cdy816 已提交
584 585 586 587 588
            {
                WriteBytes(offset, BitConverter.GetBytes(value));
            }
            else
            {
cdy816's avatar
cdy816 已提交
589
                CheckAndResize(offset + 8);
590
                hd = RelocationAddress(offset, out ost);
cdy816's avatar
tt  
cdy816 已提交
591
                MemoryHelper.WriteInt64((void*)hd, ost, value);
cdy816's avatar
cdy816 已提交
592
                Position = offset + 8;
cdy816's avatar
tt  
cdy816 已提交
593
            }
594
           
cdy816's avatar
cdy816 已提交
595 596
        }

cdy816's avatar
cdy816 已提交
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="value"></param>
        public void WriteLongDirect(long offset, long value)
        {
            IntPtr hd;
            long ost;
            if (IsCrossDataBuffer(offset, 8))
            {
                WriteBytesDirect(offset, BitConverter.GetBytes(value));
            }
            else
            {
                hd = RelocationAddress(offset, out ost);
                MemoryHelper.WriteInt64((void*)hd, ost, value);
            }

        }

618 619 620 621 622 623 624
        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="value"></param>
        public void WriteULong(long offset, ulong value)
        {
cdy816's avatar
tt  
cdy816 已提交
625 626
            IntPtr hd;
            long ost;
627
            if (IsCrossDataBuffer(offset, 8))
cdy816's avatar
tt  
cdy816 已提交
628 629
            {
                WriteBytes(offset, BitConverter.GetBytes(value));
630
               
cdy816's avatar
tt  
cdy816 已提交
631 632 633
            }
            else
            {
cdy816's avatar
cdy816 已提交
634
                CheckAndResize(offset + 8);
635
                hd = RelocationAddress(offset, out ost);
cdy816's avatar
tt  
cdy816 已提交
636
                MemoryHelper.WriteUInt64((void*)hd, ost, value);
cdy816's avatar
cdy816 已提交
637
                Position = offset + 8;
cdy816's avatar
tt  
cdy816 已提交
638
            }
639
           
640 641
        }

cdy816's avatar
cdy816 已提交
642 643 644 645 646 647 648
        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="value"></param>
        public void WriteFloat(long offset, float value)
        {
cdy816's avatar
tt  
cdy816 已提交
649 650
            IntPtr hd;
            long ost;
651
            if (IsCrossDataBuffer(offset, 4))
cdy816's avatar
tt  
cdy816 已提交
652 653 654 655 656
            {
                WriteBytes(offset, BitConverter.GetBytes(value));
            }
            else
            {
cdy816's avatar
cdy816 已提交
657
                CheckAndResize(offset + 4);
658
                hd = RelocationAddress(offset, out ost);
cdy816's avatar
tt  
cdy816 已提交
659
                MemoryHelper.WriteFloat((void*)hd, ost, value);
cdy816's avatar
cdy816 已提交
660
                Position = offset + 4;
cdy816's avatar
tt  
cdy816 已提交
661
            }
662
            
cdy816's avatar
cdy816 已提交
663 664 665 666 667 668 669 670 671
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="value"></param>
        public void WriteDouble(long offset, double value)
        {
cdy816's avatar
tt  
cdy816 已提交
672 673
            IntPtr hd;
            long ost;
674
            if (IsCrossDataBuffer(offset, 8))
cdy816's avatar
tt  
cdy816 已提交
675 676 677 678 679
            {
                WriteBytes(offset, BitConverter.GetBytes(value));
            }
            else
            {
cdy816's avatar
cdy816 已提交
680
                CheckAndResize(offset + 8);
681
                hd = RelocationAddress(offset, out ost);
cdy816's avatar
tt  
cdy816 已提交
682
                MemoryHelper.WriteDouble((void*)hd, ost, value);
cdy816's avatar
cdy816 已提交
683
                Position = offset + 8;
cdy816's avatar
tt  
cdy816 已提交
684
            }
685
            
cdy816's avatar
cdy816 已提交
686 687 688 689 690 691 692 693 694
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="values"></param>
        public void WriteBytes(long offset,byte[] values)
        {
cdy816's avatar
tt  
cdy816 已提交
695
            WriteBytes(offset, values, 0, values.Length);
cdy816's avatar
cdy816 已提交
696 697
        }

cdy816's avatar
cdy816 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="values"></param>
        public void WriteBytesDirect(long offset, byte[] values)
        {
            WriteBytesDirect(offset, values, 0, values.Length);
        }

        /// <summary>
        /// 清空值
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="len"></param>
        public void Clear(long offset,long len)
        {
            int id = (int)(offset / BufferItemSize);

            long ost = offset % BufferItemSize;

            if (len + ost < BufferItemSize)
            {
cdy816's avatar
cdy816 已提交
721 722
                Clear(mBuffers[id], (int)ost, (int)len);
               // System.Array.Clear(mBuffers[id], (int)ost, (int)len);
cdy816's avatar
cdy816 已提交
723 724 725 726 727
            }
            else
            {
                int ll = BufferItemSize - (int)ost;

cdy816's avatar
cdy816 已提交
728 729
                //System.Array.Clear(mBuffers[id], (int)ost, (int)ll);
                Clear(mBuffers[id], (int)ost, (int)ll);
cdy816's avatar
cdy816 已提交
730 731 732
                if (len - ll < BufferItemSize)
                {
                    id++;
cdy816's avatar
cdy816 已提交
733 734
                    // System.Array.Clear(mBuffers[id], 0, (int)(len - ll));
                    Clear(mBuffers[id], 0, (int)(len - ll));
cdy816's avatar
cdy816 已提交
735 736 737 738 739 740 741 742 743
                }
                else
                {
                    long ltmp = len - ll;
                    int bcount = ll / BufferItemSize;
                    int i = 0;
                    for (i = 0; i < bcount; i++)
                    {
                        id++;
cdy816's avatar
cdy816 已提交
744 745
                        Clear(mBuffers[id], 0, (int)BufferItemSize);
                        // System.Array.Clear(mBuffers[id], 0, BufferItemSize);
cdy816's avatar
cdy816 已提交
746 747 748 749 750
                    }
                    int otmp = ll % BufferItemSize;
                    if (otmp > 0)
                    {
                        id++;
cdy816's avatar
cdy816 已提交
751 752
                        Clear(mBuffers[id],0, (int)otmp);
                        //System.Array.Clear(mBuffers[id], 0, otmp);
cdy816's avatar
cdy816 已提交
753 754 755 756 757
                    }
                }
            }
        }

cdy816's avatar
cdy816 已提交
758 759 760 761
        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
cdy816's avatar
tt  
cdy816 已提交
762 763 764
        /// <param name="values"></param>
        /// <param name="valueoffset"></param>
        /// <param name="len"></param>
cdy816's avatar
cdy816 已提交
765
        public void WriteBytes(long offset, byte[] values, int valueoffset, int len)
cdy816's avatar
cdy816 已提交
766
        {
cdy816's avatar
cdy816 已提交
767 768 769

            CheckAndResize(offset + len);

cdy816's avatar
tt  
cdy816 已提交
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
            int id = (int)(offset / BufferItemSize);

            long ost = offset % BufferItemSize;

            if (len + ost < BufferItemSize)
            {
                Buffer.BlockCopy(values, valueoffset, mBuffers[id], (int)ost, len);
            }
            else
            {
                int ll = BufferItemSize - (int)ost;

                Buffer.BlockCopy(values, valueoffset, mBuffers[id], (int)ost, ll);

                if (len - ll < BufferItemSize)
                {
                    id++;
cdy816's avatar
cdy816 已提交
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
                    Buffer.BlockCopy(values, ll + valueoffset, mBuffers[id], 0, len - ll);
                }
                else
                {
                    long ltmp = len - ll;
                    int bcount = ll / BufferItemSize;
                    int i = 0;
                    for (i = 0; i < bcount; i++)
                    {
                        id++;
                        Buffer.BlockCopy(values, valueoffset + ll + i * BufferItemSize, mBuffers[id], 0, BufferItemSize);
                    }
                    int otmp = ll % BufferItemSize;
                    if (otmp > 0)
                    {
                        id++;
                        Buffer.BlockCopy(values, valueoffset + ll + i * BufferItemSize, mBuffers[id], 0, otmp);
                    }
                }
            }

            Position = offset + len;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="values"></param>
        /// <param name="valueoffset"></param>
        /// <param name="len"></param>
        public void WriteBytesDirect(long offset, byte[] values, int valueoffset, int len)
        {
            int id = (int)(offset / BufferItemSize);

            long ost = offset % BufferItemSize;

            if (len + ost < BufferItemSize)
            {
                Buffer.BlockCopy(values, valueoffset, mBuffers[id], (int)ost, len);
            }
            else
            {
                int ll = BufferItemSize - (int)ost;

                Buffer.BlockCopy(values, valueoffset, mBuffers[id], (int)ost, ll);

                if (len - ll < BufferItemSize)
                {
                    id++;
                    Buffer.BlockCopy(values, ll + valueoffset, mBuffers[id], 0, len - ll);
cdy816's avatar
tt  
cdy816 已提交
838 839 840 841 842 843 844 845 846
                }
                else
                {
                    long ltmp = len - ll;
                    int bcount = ll / BufferItemSize;
                    int i = 0;
                    for (i = 0; i < bcount; i++)
                    {
                        id++;
cdy816's avatar
cdy816 已提交
847
                        Buffer.BlockCopy(values, valueoffset + ll + i * BufferItemSize, mBuffers[id], 0, BufferItemSize);
cdy816's avatar
tt  
cdy816 已提交
848 849 850 851 852
                    }
                    int otmp = ll % BufferItemSize;
                    if (otmp > 0)
                    {
                        id++;
cdy816's avatar
cdy816 已提交
853
                        Buffer.BlockCopy(values, valueoffset + ll + i * BufferItemSize, mBuffers[id], 0, otmp);
cdy816's avatar
tt  
cdy816 已提交
854 855 856 857
                    }
                }
            }

cdy816's avatar
cdy816 已提交
858 859 860 861 862
        }

        /// <summary>
        /// 
        /// </summary>
cdy816's avatar
tt  
cdy816 已提交
863
        /// <param name="offset"></param>
cdy816's avatar
cdy816 已提交
864
        /// <param name="value"></param>
cdy816's avatar
tt  
cdy816 已提交
865
        public void WriteByte(long offset, byte value)
cdy816's avatar
cdy816 已提交
866
        {
cdy816's avatar
tt  
cdy816 已提交
867 868
            IntPtr hd;
            long ost;
cdy816's avatar
cdy816 已提交
869
            CheckAndResize(offset + 1);
870
            hd = RelocationAddress(offset, out ost);
cdy816's avatar
tt  
cdy816 已提交
871
            MemoryHelper.WriteByte((void*)hd, ost, value);
cdy816's avatar
cdy816 已提交
872
            Position = offset + 1;
cdy816's avatar
cdy816 已提交
873 874
        }

cdy816's avatar
cdy816 已提交
875 876 877 878 879 880 881 882 883 884 885 886 887
        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="value"></param>
        public void WriteByteDirect(long offset, byte value)
        {
            IntPtr hd;
            long ost;
            hd = RelocationAddress(offset, out ost);
            MemoryHelper.WriteByte((void*)hd, ost, value);
        }

cdy816's avatar
cdy816 已提交
888 889 890
        /// <summary>
        /// 
        /// </summary>
cdy816's avatar
tt  
cdy816 已提交
891 892
        /// <param name="value"></param>
        public void WriteByte(byte value)
cdy816's avatar
cdy816 已提交
893
        {
cdy816's avatar
tt  
cdy816 已提交
894
            WriteByte(mPosition, value);
cdy816's avatar
cdy816 已提交
895 896 897 898 899 900 901 902 903 904
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="value"></param>
        public void WriteDatetime(long offset, DateTime value)
        {
cdy816's avatar
tt  
cdy816 已提交
905 906
            IntPtr hd;
            long ost;
907
            if (IsCrossDataBuffer(offset, 8))
cdy816's avatar
tt  
cdy816 已提交
908 909 910 911 912
            {
                WriteBytes(offset, MemoryHelper.GetBytes(value));
            }
            else
            {
cdy816's avatar
cdy816 已提交
913
                CheckAndResize(offset + 8);
914
                hd = RelocationAddress(offset, out ost);
cdy816's avatar
tt  
cdy816 已提交
915
                MemoryHelper.WriteDateTime((void*)hd, ost, value);
cdy816's avatar
cdy816 已提交
916
                Position = offset + 8;
cdy816's avatar
tt  
cdy816 已提交
917
            }
cdy816's avatar
cdy816 已提交
918 919 920 921 922 923 924 925
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="value"></param>
        public void WriteInt(long offset, int value)
        {
926 927
            IntPtr hd;
            long ost;
cdy816's avatar
cdy816 已提交
928
            if (IsCrossDataBuffer(offset, 4))
929 930 931 932 933
            {
                WriteBytes(offset, BitConverter.GetBytes(value));
            }
            else
            {
cdy816's avatar
cdy816 已提交
934
                CheckAndResize(offset + 4);
935 936
                hd = RelocationAddress(offset, out ost);
                MemoryHelper.WriteInt32((void*)hd, ost, value);
cdy816's avatar
cdy816 已提交
937
                Position = offset + 4;
938
            }
cdy816's avatar
cdy816 已提交
939 940
        }

cdy816's avatar
cdy816 已提交
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956

        public void WriteIntDirect(long offset, int value)
        {
            IntPtr hd;
            long ost;
            if (IsCrossDataBuffer(offset, 4))
            {
                WriteBytesDirect(offset, BitConverter.GetBytes(value));
            }
            else
            {
                hd = RelocationAddress(offset, out ost);
                MemoryHelper.WriteInt32((void*)hd, ost, value);
            }
        }

957 958 959 960 961 962 963
        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="value"></param>
        public void WriteUInt(long offset, uint value)
        {
964 965
            IntPtr hd;
            long ost;
cdy816's avatar
cdy816 已提交
966
            if (IsCrossDataBuffer(offset, 4))
967 968 969 970 971
            {
                WriteBytes(offset, BitConverter.GetBytes(value));
            }
            else
            {
cdy816's avatar
cdy816 已提交
972
                CheckAndResize(offset + 4);
973 974
                hd = RelocationAddress(offset, out ost);
                MemoryHelper.WriteUInt32((void*)hd, ost, value);
cdy816's avatar
cdy816 已提交
975
                Position = offset + 4;
976
            }            
977 978
        }

cdy816's avatar
cdy816 已提交
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="value"></param>
        public void WriteUIntDirect(long offset, uint value)
        {
            IntPtr hd;
            long ost;
            if (IsCrossDataBuffer(offset, 4))
            {
                WriteBytesDirect(offset, BitConverter.GetBytes(value));
            }
            else
            {
                hd = RelocationAddress(offset, out ost);
                MemoryHelper.WriteUInt32((void*)hd, ost, value);
            }
        }

cdy816's avatar
cdy816 已提交
999 1000 1001 1002 1003 1004 1005
        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="value"></param>
        public void WriteShort(long offset, short value)
        {
1006 1007
            IntPtr hd;
            long ost;
cdy816's avatar
cdy816 已提交
1008
            if (IsCrossDataBuffer(offset, 2))
1009 1010 1011 1012 1013
            {
                WriteBytes(offset, BitConverter.GetBytes(value));
            }
            else
            {
cdy816's avatar
cdy816 已提交
1014
                CheckAndResize(offset + 2);
1015 1016
                hd = RelocationAddress(offset, out ost);
                MemoryHelper.WriteShort((void*)hd, ost, value);
cdy816's avatar
cdy816 已提交
1017
                Position = offset + 2;
1018
            }           
cdy816's avatar
cdy816 已提交
1019 1020
        }

cdy816's avatar
cdy816 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="value"></param>
        public void WriteShortDirect(long offset, short value)
        {
            IntPtr hd;
            long ost;
            if (IsCrossDataBuffer(offset, 2))
            {
                WriteBytesDirect(offset, BitConverter.GetBytes(value));
            }
            else
            {
                hd = RelocationAddress(offset, out ost);
                MemoryHelper.WriteShort((void*)hd, ost, value);
            }
        }

1041 1042 1043 1044 1045 1046 1047
        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="value"></param>
        public void WriteUShort(long offset, ushort value)
        {
1048 1049
            IntPtr hd;
            long ost;
cdy816's avatar
cdy816 已提交
1050
            if (IsCrossDataBuffer(offset, 2))
1051 1052 1053 1054 1055
            {
                WriteBytes(offset, BitConverter.GetBytes(value));
            }
            else
            {
cdy816's avatar
cdy816 已提交
1056
                CheckAndResize(offset + 2);
1057 1058
                hd = RelocationAddress(offset, out ost);
                MemoryHelper.WriteUShort((void*)hd, ost, value);
cdy816's avatar
cdy816 已提交
1059
                Position = offset + 2;
1060
            }          
1061 1062
        }

cdy816's avatar
cdy816 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="value"></param>
        public void WriteUShortDirect(long offset, ushort value)
        {
            IntPtr hd;
            long ost;
            if (IsCrossDataBuffer(offset, 2))
            {
                WriteBytesDirect(offset, BitConverter.GetBytes(value));
            }
            else
            {
                hd = RelocationAddress(offset, out ost);
                MemoryHelper.WriteUShort((void*)hd, ost, value);
            }
        }

cdy816's avatar
cdy816 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091
        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="value"></param>
        /// <param name="encode"></param>
        public void WriteString(long offset, string value, Encoding encode)
        {
            var sdata = encode.GetBytes(value);
cdy816's avatar
cdy816 已提交
1092
            CheckAndResize(offset + sdata.Length + 1);
1093
            WriteByte(offset, (byte)sdata.Length);
cdy816's avatar
cdy816 已提交
1094 1095
            WriteBytes(offset+1, sdata);
            Position = offset + sdata.Length + 1;
cdy816's avatar
cdy816 已提交
1096 1097
        }

cdy816's avatar
cdy816 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="value"></param>
        /// <param name="encode"></param>
        public void WriteStringDirect(long offset, string value, Encoding encode)
        {
            var sdata = encode.GetBytes(value);
            WriteByte(offset, (byte)sdata.Length);
            WriteBytes(offset + 1, sdata);
        }

cdy816's avatar
cdy816 已提交
1111 1112 1113 1114 1115 1116 1117 1118
        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <returns></returns>
        public DateTime ReadDateTime(long offset)
        {
            mPosition = offset + sizeof(DateTime);
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131

            IntPtr hd;
            long ost;
            if (IsCrossDataBuffer(offset, 8))
            {
                return MemoryHelper.ReadDateTime(ReadBytesInner(offset, 8));
               // WriteBytes(offset, MemoryHelper.GetBytes(value));
            }
            else
            {
                hd = RelocationAddress(offset, out ost);
                return MemoryHelper.ReadDateTime((void*)hd, ost);
            }
cdy816's avatar
cdy816 已提交
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <returns></returns>

        public int ReadInt(long offset)
        {
            mPosition = offset + sizeof(int);
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
            if (IsCrossDataBuffer(offset, 4))
            {
                return  BitConverter.ToInt32(ReadBytesInner(offset, 4),0);
            }
            else
            {
                long ost;
                var hd = RelocationAddress(offset, out ost);
                return MemoryHelper.ReadInt32((void*)hd, ost);
            }
cdy816's avatar
cdy816 已提交
1153 1154 1155 1156 1157
        }


        public uint ReadUInt(long offset)
        {
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
            mPosition = offset + 4;
            if (IsCrossDataBuffer(offset, 4))
            {
                return BitConverter.ToUInt32(ReadBytesInner(offset, 4), 0);
            }
            else
            {
                long ost;
                var hd = RelocationAddress(offset, out ost);
                return MemoryHelper.ReadUInt32((void*)hd, ost);
            }
cdy816's avatar
cdy816 已提交
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <returns></returns>
        public short ReadShort(long offset)
        {
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
            mPosition = offset + 2;
            if (IsCrossDataBuffer(offset, 2))
            {
                return BitConverter.ToInt16(ReadBytesInner(offset, 2), 0);
            }
            else
            {
                long ost;
                var hd = RelocationAddress(offset, out ost);
                return MemoryHelper.ReadShort((void*)hd, ost);
            }
cdy816's avatar
cdy816 已提交
1190 1191 1192 1193 1194 1195 1196 1197 1198
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <returns></returns>
        public ushort ReadUShort(long offset)
        {
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
            mPosition = offset + 2;
            if (IsCrossDataBuffer(offset, 2))
            {
                return BitConverter.ToUInt16(ReadBytesInner(offset, 2), 0);
            }
            else
            {
                long ost;
                var hd = RelocationAddress(offset, out ost);
                return MemoryHelper.ReadUShort((void*)hd, ost);
            }
            //return MemoryHelper.ReadUShort(handle, offset);
cdy816's avatar
cdy816 已提交
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <returns></returns>
        public float ReadFloat(long offset)
        {
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
            mPosition = offset + 4;
            if (IsCrossDataBuffer(offset, 4))
            {
                return BitConverter.ToSingle(ReadBytesInner(offset, 4), 0);
            }
            else
            {
                long ost;
                var hd = RelocationAddress(offset, out ost);
                return MemoryHelper.ReadFloat((void*)hd, ost);
            }
            //return MemoryHelper.ReadFloat(handle, offset);
cdy816's avatar
cdy816 已提交
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <returns></returns>
        public double ReadDouble(long offset)
        {
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
            mPosition = offset + 8;
            if (IsCrossDataBuffer(offset, 8))
            {
                return BitConverter.ToDouble(ReadBytesInner(offset, 8), 0);
            }
            else
            {
                long ost;
                var hd = RelocationAddress(offset, out ost);
                return MemoryHelper.ReadDouble((void*)hd, ost);
            }
cdy816's avatar
cdy816 已提交
1254 1255 1256 1257 1258 1259 1260 1261 1262
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <returns></returns>
        public long ReadLong(long offset)
        {
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
            mPosition = offset + 8;
            if (IsCrossDataBuffer(offset, 8))
            {
                return BitConverter.ToInt64(ReadBytesInner(offset, 8), 0);
            }
            else
            {
                long ost;
                var hd = RelocationAddress(offset, out ost);
                return MemoryHelper.ReadInt64((void*)hd, ost);
            }
            //mPosition = offset + sizeof(long);
            //return MemoryHelper.ReadInt64(handle, offset);
cdy816's avatar
cdy816 已提交
1276 1277 1278 1279 1280 1281 1282 1283 1284
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <returns></returns>
        public ulong ReadULong(long offset)
        {
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
            mPosition = offset + 8;
            if (IsCrossDataBuffer(offset, 8))
            {
                return BitConverter.ToUInt64(ReadBytesInner(offset, 8), 0);
            }
            else
            {
                long ost;
                var hd = RelocationAddress(offset, out ost);
                return MemoryHelper.ReadUInt64((void*)hd, ost);
            }
            //mPosition = offset + sizeof(long);
            //return MemoryHelper.ReadUInt64(handle, offset);
cdy816's avatar
cdy816 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <returns></returns>
        public byte ReadByte(long offset)
        {
            mPosition = offset + 1;
1308 1309 1310
            long ost;
            var hd = RelocationAddress(offset, out ost);
            return MemoryHelper.ReadByte((void*)hd, ost);
cdy816's avatar
cdy816 已提交
1311 1312 1313 1314 1315 1316 1317 1318 1319
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <returns></returns>
        public string ReadString(long offset, Encoding encoding)
        {
1320
            var len = ReadByte(offset);
cdy816's avatar
cdy816 已提交
1321
            mPosition = offset + len + 1;
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
            if (IsCrossDataBuffer(offset + 1, len))
            {
                return encoding.GetString(ReadBytesInner(offset + 1, len));
            }
            else
            {
                long ost;
                var hd = RelocationAddress(offset, out ost);
                return new string((sbyte*)hd, (int)offset + 1, len, encoding);
            }
cdy816's avatar
cdy816 已提交
1332 1333
        }

1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <returns></returns>
        public string ReadString(long offset)
        {
            return ReadString(offset, Encoding.Unicode);
        }

cdy816's avatar
cdy816 已提交
1344 1345 1346 1347
        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
1348
        /// <param name="len"></param>
cdy816's avatar
cdy816 已提交
1349
        /// <returns></returns>
1350
        private byte[] ReadBytesInner(long offset,int len)
cdy816's avatar
cdy816 已提交
1351 1352
        {
            byte[] re = new byte[len];
1353 1354 1355 1356
            int id = (int)(offset / BufferItemSize);

            long ost = offset % BufferItemSize;

1357
            if (len + ost <= BufferItemSize)
1358 1359 1360 1361 1362 1363 1364 1365 1366
            {
                Buffer.BlockCopy(mBuffers[id], (int)ost,re, 0, len);
            }
            else
            {
                int ll = BufferItemSize - (int)ost;

                Buffer.BlockCopy(mBuffers[id], (int)ost,re,0, ll);

1367
                if (len - ll <= BufferItemSize)
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
                {
                    id++;
                    Buffer.BlockCopy(mBuffers[id], 0, re, ll, len - ll);
                }
                else
                {
                    long ltmp = len - ll;
                    int bcount = ll / BufferItemSize;
                    int i = 0;
                    for (i = 0; i < bcount; i++)
                    {
                        id++;
                        Buffer.BlockCopy(mBuffers[id], 0, re, ll + i * BufferItemSize, BufferItemSize);
                    }
                    int otmp = ll % BufferItemSize;
                    if (otmp > 0)
                    {
                        id++;
                        Buffer.BlockCopy(mBuffers[id], 0,re, ll + i * BufferItemSize, otmp);
                    }
                }
            }

            return re;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <returns></returns>
        public byte[] ReadBytes(long offset,int len)
        {
            byte[] re = ReadBytesInner(offset, len);
            mPosition += len;
cdy816's avatar
cdy816 已提交
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
            return re;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public byte ReadByte()
        {
            return ReadByte(mPosition);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public long ReadLong()
        {
            return ReadLong(mPosition);
        }

1424 1425 1426 1427 1428 1429 1430 1431 1432
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ulong ReadULong()
        {
            return ReadULong(mPosition);
        }

cdy816's avatar
cdy816 已提交
1433 1434 1435 1436 1437 1438 1439 1440 1441
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int ReadInt()
        {
            return ReadInt(mPosition);
        }

1442 1443 1444 1445 1446 1447 1448 1449 1450
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public uint ReadUInt()
        {
            return ReadUInt(mPosition);
        }

cdy816's avatar
cdy816 已提交
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public float ReadFloat()
        {
            return ReadFloat(mPosition);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public double ReadDouble()
        {
            return ReadDouble(mPosition);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public short ReadShort()
        {
            return ReadShort(mPosition);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ushort ReadUShort()
        {
            return ReadUShort(mPosition);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DateTime ReadDateTime()
        {
            return ReadDateTime(mPosition);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public string ReadString(Encoding encoding)
        {
            return ReadString(mPosition,encoding);
        }

1506 1507 1508 1509 1510 1511 1512 1513 1514
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string ReadString()
        {
            return ReadString(mPosition, Encoding.Unicode);
        }

cdy816's avatar
cdy816 已提交
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
        /// <summary>
        /// 
        /// </summary>
        /// <param name="len"></param>
        /// <returns></returns>
        public byte[] ReadBytes(int len)
        {
            return ReadBytes(mPosition, len);
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
cdy816's avatar
cdy816 已提交
1530
        public virtual void Dispose()
cdy816's avatar
cdy816 已提交
1531
        {
1532
            //mDataBuffer = null;
1533 1534
            mBuffers.Clear();
            mHandles.Clear();
cdy816's avatar
cdy816 已提交
1535
            LoggerService.Service.Erro("MemoryBlock", Name + " Disposed ");
1536
            //GC.Collect();
cdy816's avatar
cdy816 已提交
1537 1538
        }

cdy816's avatar
cdy816 已提交
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="index"></param>
        ///// <returns></returns>
        //public byte this[int index]
        //{
        //    get
        //    {
        //        long ost;
        //        var hd = RelocationAddressToArrayIndex(index, out ost);
        //        return this.mBuffers[hd][ost];
        //    }
        //    set
        //    {
        //        long ost;
        //        var hd = RelocationAddressToArrayIndex(index, out ost);
        //        this.mBuffers[hd][ost] = value;
        //    }
        //}

cdy816's avatar
cdy816 已提交
1560 1561 1562 1563 1564
        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
cdy816's avatar
cdy816 已提交
1565
        public byte this[long index]
cdy816's avatar
cdy816 已提交
1566 1567 1568
        {
            get
            {
1569 1570 1571
                long ost;
                var hd = RelocationAddressToArrayIndex(index, out ost);
                return this.mBuffers[hd][ost];
cdy816's avatar
cdy816 已提交
1572 1573 1574
            }
            set
            {
1575 1576 1577
                long ost;
                var hd = RelocationAddressToArrayIndex(index, out ost);
                this.mBuffers[hd][ost] = value;
cdy816's avatar
cdy816 已提交
1578 1579 1580 1581 1582 1583 1584
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="target"></param>
1585
        /// <param name="sourceStart"></param>
cdy816's avatar
cdy816 已提交
1586
        /// <param name="targetStart"></param>
1587
        /// <param name="len"></param>
cdy816's avatar
cdy816 已提交
1588
        public void CopyTo(MemoryBlock target,long sourceStart, long targetStart, long len)
cdy816's avatar
cdy816 已提交
1589 1590
        {
            if (target == null) return;
1591 1592 1593 1594 1595 1596 1597

            long osts,ostt;
            
            var hds = RelocationAddressToArrayIndex(sourceStart, out osts);

            //计算从源数据需要读取数据块索引
            //Array Index,Array Address,Start Index,Data Len
cdy816's avatar
cdy816 已提交
1598
            List<Tuple<int, int, long>> mSourceIndex = new List<Tuple<int, int, long>>();
1599 1600
            if(osts+len<BufferItemSize)
            {
cdy816's avatar
cdy816 已提交
1601
                mSourceIndex.Add(new Tuple<int, int,  long>(hds, (int)osts,  len));
1602 1603 1604 1605 1606
            }
            else
            {
                int ll = BufferItemSize - (int)osts;

cdy816's avatar
cdy816 已提交
1607
                mSourceIndex.Add(new Tuple<int, int, long>(hds, (int)osts, ll));
1608 1609 1610 1611

                if (len - ll < BufferItemSize)
                {
                    hds++;
cdy816's avatar
cdy816 已提交
1612
                    mSourceIndex.Add(new Tuple<int, int,  long>(hds, 0,  len-ll));
1613 1614 1615 1616 1617 1618 1619 1620 1621
                }
                else
                {
                    long ltmp = len - ll;
                    int bcount = ll / BufferItemSize;
                    int i = 0;
                    for (i = 0; i < bcount; i++)
                    {
                        hds++;
cdy816's avatar
cdy816 已提交
1622
                        mSourceIndex.Add(new Tuple<int, int, long>(hds, 0,  BufferItemSize));
1623 1624 1625 1626 1627
                    }
                    int otmp = ll % BufferItemSize;
                    if (otmp > 0)
                    {
                        hds++;
cdy816's avatar
cdy816 已提交
1628
                        mSourceIndex.Add(new Tuple<int, int,long>(hds, 0,  otmp));
1629 1630 1631 1632
                    }
                }
            }

cdy816's avatar
cdy816 已提交
1633
            long targetAddr = targetStart;
cdy816's avatar
cdy816 已提交
1634

1635
            //拷贝数据到目标数据块中
cdy816's avatar
cdy816 已提交
1636
            foreach (var vv in mSourceIndex)
1637
            {
cdy816's avatar
cdy816 已提交
1638
                var hdt = RelocationAddressToArrayIndex(targetAddr, out ostt);
cdy816's avatar
cdy816 已提交
1639
                if (ostt + vv.Item3 < BufferItemSize)
1640
                {
cdy816's avatar
cdy816 已提交
1641
                    Buffer.BlockCopy(this.mBuffers[vv.Item1], vv.Item2, target.mBuffers[hdt], (int)ostt, (int)vv.Item3);
1642 1643 1644
                }
                else
                {
cdy816's avatar
cdy816 已提交
1645
                    var count = vv.Item3+ ostt - BufferItemSize;
1646 1647 1648 1649
                    Buffer.BlockCopy(this.mBuffers[vv.Item1], vv.Item2, target.mBuffers[hdt], (int)ostt, (int)(BufferItemSize - ostt));
                    hdt++;
                    Buffer.BlockCopy(this.mBuffers[vv.Item1], vv.Item2, target.mBuffers[hdt], (int)0, (int)count);
                }
cdy816's avatar
cdy816 已提交
1650
                targetAddr += vv.Item3;
1651 1652
            }
            //Buffer.BlockCopy(this.mDataBuffer, sourceStart, target.mDataBuffer, targgetStart, len);
cdy816's avatar
cdy816 已提交
1653 1654 1655 1656 1657 1658 1659 1660 1661
        }

        #endregion ...Methods...

        #region ... Interfaces ...

        #endregion ...Interfaces...
    }

1662
    public static class MemoryBlockExtends
cdy816's avatar
cdy816 已提交
1663 1664 1665 1666 1667 1668
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="memory"></param>
        /// <returns></returns>
1669
        public static List<long> ToLongList(this MemoryBlock memory)
cdy816's avatar
cdy816 已提交
1670
        {
1671
            List<long> re = new List<long>((int)(memory.AllocSize / 8));
cdy816's avatar
cdy816 已提交
1672
            memory.Position = 0;
1673
            while (memory.Position < memory.AllocSize)
cdy816's avatar
cdy816 已提交
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
            {
                re.Add(memory.ReadLong());
            }
            return re;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="memory"></param>
        /// <returns></returns>
1685
        public static List<int> ToIntList(this MemoryBlock memory)
cdy816's avatar
cdy816 已提交
1686
        {
cdy816's avatar
cdy816 已提交
1687
            List<int> re = new List<int>((int)(memory.Length / 4));
cdy816's avatar
cdy816 已提交
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
            memory.Position = 0;
            while (memory.Position < memory.Length)
            {
                re.Add(memory.ReadInt());
            }
            return re;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="memory"></param>
        /// <returns></returns>
1701
        public static List<double> ToDoubleList(this MemoryBlock memory)
cdy816's avatar
cdy816 已提交
1702
        {
cdy816's avatar
cdy816 已提交
1703
            List<double> re = new List<double>((int)(memory.Length / 8));
cdy816's avatar
cdy816 已提交
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
            memory.Position = 0;
            while (memory.Position < memory.Length)
            {
                re.Add(memory.ReadDouble());
            }
            return re;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="memory"></param>
        /// <returns></returns>
1717
        public static List<double> ToFloatList(this MemoryBlock memory)
cdy816's avatar
cdy816 已提交
1718
        {
cdy816's avatar
cdy816 已提交
1719
            List<double> re = new List<double>((int)(memory.Length / 4));
cdy816's avatar
cdy816 已提交
1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
            memory.Position = 0;
            while (memory.Position < memory.Length)
            {
                re.Add(memory.ReadFloat());
            }
            return re;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="memory"></param>
        /// <returns></returns>
1733
        public static List<short> ToShortList(this MemoryBlock memory)
cdy816's avatar
cdy816 已提交
1734
        {
cdy816's avatar
cdy816 已提交
1735
            List<short> re = new List<short>((int)(memory.Length / 2));
cdy816's avatar
cdy816 已提交
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
            memory.Position = 0;
            while (memory.Position < memory.Length)
            {
                re.Add(memory.ReadShort());
            }
            return re;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="memory"></param>
        /// <returns></returns>
1749
        public static List<DateTime> ToDatetimeList(this MemoryBlock memory)
cdy816's avatar
cdy816 已提交
1750
        {
cdy816's avatar
cdy816 已提交
1751
            List<DateTime> re = new List<DateTime>((int)(memory.Length / 8));
cdy816's avatar
cdy816 已提交
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
            memory.Position = 0;
            while (memory.Position < memory.Length)
            {
                re.Add(memory.ReadDateTime());
            }
            return re;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="memory"></param>
        /// <returns></returns>
1765
        public static List<string> ToStringList(this MemoryBlock memory, Encoding encoding)
cdy816's avatar
cdy816 已提交
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
        {
            List<string> re = new List<string>();
            memory.Position = 0;
            while (memory.Position < memory.Length)
            {
                re.Add(memory.ReadString(encoding));
            }
            return re;
        }

cdy816's avatar
cdy816 已提交
1776 1777 1778 1779 1780 1781 1782
        /// <summary>
        /// 
        /// </summary>
        /// <param name="memory"></param>
        /// <param name="offset"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
1783
        public static List<string> ToStringList(this MemoryBlock memory,int offset, Encoding encoding)
cdy816's avatar
cdy816 已提交
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
        {
            List<string> re = new List<string>();
            memory.Position = offset;
            while (memory.Position < memory.Length)
            {
                re.Add(memory.ReadString(encoding));
            }
            return re;
        }

cdy816's avatar
cdy816 已提交
1794 1795 1796 1797
        /// <summary>
        /// 
        /// </summary>
        /// <param name="memory"></param>
1798
        public static void MakeMemoryBusy(this MemoryBlock memory)
cdy816's avatar
cdy816 已提交
1799
        {
cdy816's avatar
cdy816 已提交
1800
            //LoggerService.Service.Info("MemoryBlock", memory.Name + " is busy.....");
cdy816's avatar
cdy816 已提交
1801
            memory.IsBusy = true;
cdy816's avatar
cdy816 已提交
1802
            //memory.StartMemory[0] = 1;
cdy816's avatar
cdy816 已提交
1803 1804 1805 1806 1807 1808
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="memory"></param>
1809
        public static void MakeMemoryNoBusy(this MemoryBlock memory)
cdy816's avatar
cdy816 已提交
1810
        {
cdy816's avatar
cdy816 已提交
1811
            //LoggerService.Service.Info("MemoryBlock", memory.Name+ " is ready !");
cdy816's avatar
cdy816 已提交
1812
            memory.IsBusy = false;
cdy816's avatar
cdy816 已提交
1813
            //memory.StartMemory[0] = 0;
cdy816's avatar
cdy816 已提交
1814 1815
        }

cdy816's avatar
cdy816 已提交
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
        public static void SaveToFile(this MemoryBlock memory, string fileName)
        {
            var stream = System.IO.File.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
            stream.Write(BitConverter.GetBytes(memory.Buffers.Count),0,4);
            stream.Write(BitConverter.GetBytes(memory.BufferItemSize), 0, 4);
            foreach(var vv in memory.Buffers)
            {
                stream.Write(vv, 0, vv.Length);
            }
            stream.Flush();
            stream.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="memory"></param>
        /// <param name="fileName"></param>
        public static MemoryBlock LoadFileToMemory(this string fileName)
        {
            var stream = (System.IO.File.Open(fileName, FileMode.Open, FileAccess.Read));
            byte[] bb = new byte[4];
            stream.Read(bb, 0, 4);
            int mbcount = BitConverter.ToInt32(bb,0);
            stream.Read(bb, 0, 4);
            int bufferItemSize = BitConverter.ToInt32(bb, 0);

            MemoryBlock memory = new MemoryBlock(bufferItemSize * mbcount);
            for (int i = 0; i < mbcount; i++)
            {
                long size = Math.Min(memory.BufferItemSize, stream.Length - stream.Position);
                stream.Read(memory.Buffers[i], 0, (int)size);
            }
            stream.Close();
            return memory;
        }
    }
cdy816's avatar
cdy816 已提交
1853
}