示例#1
0
        public static unsafe SafeHeapBlockHandle Utf16ToUtf8(char *start, char *end)
        {
            if (start >= end)
            {
                throw new ArgumentException();
            }

            var charCount = new IntPtr(end - start).ToInt64();

            if (charCount >= Int32.MaxValue)
            {
                throw new ArgumentException("Size limited to Int32 size.");
            }

            var encoder   = Encoding.UTF8.GetEncoder();
            var byteCount = encoder.GetByteCount(start, (int)charCount, true);
            var newBlock  = HeapAllocStatic.Alloc(byteCount);
            var result    = encoder.GetBytes(start, (int)charCount, (byte *)newBlock.Pointer.ToPointer(),
                                             byteCount, true);

            if (result != byteCount)
            {
                throw new Exception("Error converting string from UTF16 to UTF8.");
            }
            return(newBlock);
        }
示例#2
0
        public static unsafe SafeHeapBlockHandle Utf8ToUtf16(byte *start, byte *end)
        {
            if (start >= end)
            {
                throw new ArgumentException();
            }

            var length = new IntPtr(end - start).ToInt64();

            if (length >= Int32.MaxValue)
            {
                throw new ArgumentException("Size limited to Int32 size.");
            }

            var byteLength = (int)length;
            var decoder    = Encoding.UTF8.GetDecoder();
            var charCount  = decoder.GetCharCount(start, byteLength, true);
            var newBlock   = HeapAllocStatic.Alloc(charCount * sizeof(char));
            var result     = decoder.GetChars(start, byteLength, (char *)newBlock.Pointer.ToPointer(),
                                              charCount, true);

            if (result != charCount)
            {
                throw new Exception("Error converting UTF8 string to UTF16 string.");
            }
            return(newBlock);
        }
示例#3
0
        private static SafeHeapBlockHandle ReadFileWorker(SlimFileInfo fileInfo, int trailingByteCount)
        {
            using (
                var fileHandle = NativeMethods.CreateFile(fileInfo.FullPathName.FullName, NativeAccessFlags.GenericRead, FileShare.Read, IntPtr.Zero,
                                                          FileMode.Open, 0, IntPtr.Zero)) {
                if (fileHandle.IsInvalid)
                {
                    throw new Win32Exception();
                }

                // Note: We are limited to 2GB files by design.
                var len       = (int)fileInfo.Length;
                var heap      = HeapAllocStatic.Alloc(len + trailingByteCount);
                var bytesRead = new int[1];

                if (!NativeMethods.ReadFile(fileHandle, heap.Pointer, len, bytesRead, null))
                {
                    throw new Win32Exception();
                }

                if (bytesRead[0] != len)
                {
                    throw new Exception("File read operation didn't read the whole file");
                }

                return(heap);
            }
        }
示例#4
0
        private static SafeHeapBlockHandle ReadFileWorker(SlimFileInfo fileInfo, int trailingByteCount)
        {
            using (
                var fileHandle = NativeMethods.CreateFile(fileInfo.FullPath.Value, NativeAccessFlags.GenericRead, FileShare.Read, IntPtr.Zero,
                                                          FileMode.Open, 0, IntPtr.Zero)) {
                if (fileHandle.IsInvalid)
                {
                    throw new Win32Exception();
                }

                // Note: We are limited to 2GB files by design.
                int maxLen = Int32.MaxValue - trailingByteCount;
                if (fileInfo.Length >= maxLen)
                {
                    Logger.LogWarning("File too big, truncated to {0} bytes", maxLen);
                }
                var len       = (int)Math.Min(maxLen, fileInfo.Length);
                var heap      = HeapAllocStatic.Alloc(len + trailingByteCount);
                var bytesRead = new int[1];

                if (!NativeMethods.ReadFile(fileHandle, heap.Pointer, len, bytesRead, null))
                {
                    throw new Win32Exception();
                }

                if (bytesRead[0] != len)
                {
                    throw new Exception("File read operation didn't read the whole file");
                }

                return(heap);
            }
        }
示例#5
0
        public unsafe void AsciiSearchForVariousPatternsWorks(
            long blockByteLength,
            string pattern,
            NativeMethods.SearchOptions searchOptions,
            int patternOccurrenceCount,
            int iterationCount)
        {
            Trace.WriteLine(
                string.Format(
                    "Searching {0} time(s) for pattern \"{1}\" with {2} occurrence(s) in a memory block of {3:n0} bytes.",
                    iterationCount, pattern, patternOccurrenceCount, blockByteLength));

            Assert.IsTrue(iterationCount >= 1);
            using (var textBlock = HeapAllocStatic.Alloc(blockByteLength)) {
                FillWithNonNulCharacters(textBlock);

                SetSearchMatches(textBlock, pattern, patternOccurrenceCount);

                using (var search = new AsciiStringSearchStrStr(pattern, searchOptions)) {
                    var sw         = Stopwatch.StartNew();
                    var matchCount = PerformSearch(textBlock, search, iterationCount);
                    sw.Stop();
                    Assert.AreEqual(patternOccurrenceCount, matchCount);
                    Trace.WriteLine(string.Format("  StrStr: Found {0:n0} occurrence(s) {1} times in {2}s ({3:n0} KB/s.)",
                                                  matchCount, iterationCount, sw.Elapsed.TotalSeconds,
                                                  ComputeThroughput(sw, blockByteLength, iterationCount)));
                }
                using (var search = new AsciiStringSearchBoyerMoore(pattern, searchOptions)) {
                    var sw         = Stopwatch.StartNew();
                    var matchCount = PerformSearch(textBlock, search, iterationCount);
                    sw.Stop();
                    Assert.AreEqual(patternOccurrenceCount, matchCount);
                    Trace.WriteLine(string.Format("  Boyer-Moore: Found {0:n0} occurrence(s) {1} times in {2} s ({3:n0} KB/s.)",
                                                  matchCount, iterationCount, sw.Elapsed.TotalSeconds,
                                                  ComputeThroughput(sw, blockByteLength, iterationCount)));
                }
                using (var search = new AsciiStringSearchBndm32(pattern, searchOptions)) {
                    var sw         = Stopwatch.StartNew();
                    var matchCount = PerformSearch(textBlock, search, iterationCount);
                    sw.Stop();
                    Assert.AreEqual(patternOccurrenceCount, matchCount);
                    Trace.WriteLine(string.Format("  BNDM-32: Found {0:n0} occurrence(s) {1} times in {2} s ({3:n0} KB/s.)",
                                                  matchCount, iterationCount, sw.Elapsed.TotalSeconds,
                                                  ComputeThroughput(sw, blockByteLength, iterationCount)));
                }
                using (var search = new AsciiStringSearchBndm64(pattern, searchOptions)) {
                    var sw         = Stopwatch.StartNew();
                    var matchCount = PerformSearch(textBlock, search, iterationCount);
                    sw.Stop();
                    Assert.AreEqual(patternOccurrenceCount, matchCount);
                    Trace.WriteLine(string.Format("  BNDM-64: Found {0:n0} occurrence(s) {1} times in {2} s ({3:n0} KB/s.)",
                                                  matchCount, iterationCount, sw.Elapsed.TotalSeconds,
                                                  ComputeThroughput(sw, blockByteLength, iterationCount)));
                }
            }
        }
示例#6
0
        public void HeapAllocWorks()
        {
            var block = HeapAllocStatic.Alloc(1024);

            block.Close();
        }