fCraft.BufferUtil.MemCpy C# (CSharp) Method

MemCpy() public static method

Copies contents of src buffer to dest buffer, as efficiently as possible.
public static MemCpy ( [ src, [ dest, int len ) : void
src [ Source array/pointer.
dest [ Destination array/pointer.
len int Number of bytes to copy.
return void
        public static void MemCpy( [NotNull] byte* src, [NotNull] byte* dest, int len ) {
            if( src == null ) throw new ArgumentNullException( "src" );
            if( dest == null ) throw new ArgumentNullException( "dest" );
            if( len >= 0x10 ) {
                do {
                    *( (int*)dest ) = *( (int*)src );
                    *( (int*)( dest + 4 ) ) = *( (int*)( src + 4 ) );
                    *( (int*)( dest + 8 ) ) = *( (int*)( src + 8 ) );
                    *( (int*)( dest + 12 ) ) = *( (int*)( src + 12 ) );
                    dest += 0x10;
                    src += 0x10;
                } while( ( len -= 0x10 ) >= 0x10 );
            }
            if( len > 0 ) {
                if( ( len & 8 ) != 0 ) {
                    *( (int*)dest ) = *( (int*)src );
                    *( (int*)( dest + 4 ) ) = *( (int*)( src + 4 ) );
                    dest += 8;
                    src += 8;
                }
                if( ( len & 4 ) != 0 ) {
                    *( (int*)dest ) = *( (int*)src );
                    dest += 4;
                    src += 4;
                }
                if( ( len & 2 ) != 0 ) {
                    *( (short*)dest ) = *( (short*)src );
                    dest += 2;
                    src += 2;
                }
                if( ( len & 1 ) != 0 ) {
                    dest++;
                    src++;
                    dest[0] = src[0];
                }
            }
        }

Usage Example

Example #1
0
        void Preload()
        {
            using (FileStream fs = OpenRead()) {
                CacheSize = (int)(fs.Length / BlockDBEntry.Size);
                EnsureCapacity(CacheSize);
                LastFlushedIndex = CacheSize;

                // Converting from byte[] to BlockDBEntry[] on the fly
                // This is possible because BlockDBEntry is a sequentially packed struct
                fixed(BlockDBEntry *pCacheStart = cacheStore)
                {
                    fixed(byte *pBuffer = ioBuffer)
                    {
                        byte *pCache = (byte *)pCacheStart;

                        while (fs.Position < fs.Length)
                        {
                            int bytesToRead   = Math.Min(BufferSize, (int)(fs.Length - fs.Position));
                            int bytesInBuffer = 0;
                            do
                            {
                                int bytesRead = fs.Read(ioBuffer, bytesInBuffer, BufferSize - bytesInBuffer);
                                bytesInBuffer += bytesRead;
                            } while(bytesInBuffer < bytesToRead);
                            BufferUtil.MemCpy(pBuffer, pCache, bytesInBuffer);
                            pCache += bytesInBuffer;
                        }
                    }
                }
            }
        }