public override int Deserialize(System.Byte[] serialized, int startIndex) { int curIndex = startIndex; data = BitConverter.ToUInt64(serialized, curIndex); curIndex += BitConverter.GetBytes(data).Length; return (curIndex - startIndex); }
public static uint dtHashRef(dtPolyRef a) { a = (~a) + (a << 18); // a = (a << 18) - a - 1; a = a ^ (a >> 31); a = a * 21; // a = (a + (a << 2)) + (a << 4); a = a ^ (a >> 11); a = a + (a << 6); a = a ^ (a >> 22); return (uint)a; }
public static uint dtHashRef(dtPolyRef a) { a += ~(a << 15); a ^= (a >> 10); a += (a << 3); a ^= (a >> 6); a += ~(a << 11); a ^= (a >> 16); return (uint)a; }
protected override Resource _create( string name, ResourceHandle handle, string group, bool isManual, IManualResourceLoader loader, NameValuePairList createParams ) { if ( createParams == null || !createParams.ContainsKey( "type" ) ) { throw new AxiomException( "You must supply a 'type' parameter." ); } if ( createParams[ "type" ] == "vertex_program" ) { return new D3D9GpuVertexProgram( this, name, handle, group, isManual, loader ); } else { return new D3D9GpuFragmentProgram( this, name, handle, group, isManual, loader ); } }
protected abstract Resource _create(string name, ResourceHandle handle, string group, bool isManual, IManualResourceLoader loader, GpuProgramType type, string syntaxCode);
protected abstract bool Read_UInt64(ref System.UInt64 ioData);
protected D3D9GpuProgram( ResourceManager parent, string name, ResourceHandle handle, string group, bool isManual, IManualResourceLoader loader ) : base( parent, name, handle, group, isManual, loader ) { D3D9RenderSystem.ResourceManager.NotifyResourceCreated( this ); }
private static size_t CurlSendCallback(IntPtr buffer, size_t size, size_t nitems, IntPtr context) { CurlHandler.VerboseTrace("size: " + size + ", nitems: " + nitems); int length = checked((int)(size * nitems)); Debug.Assert(length <= RequestBufferSize, "length " + length + " should not be larger than RequestBufferSize " + RequestBufferSize); if (length == 0) { return 0; } EasyRequest easy; if (TryGetEasyRequestFromContext(context, out easy)) { Debug.Assert(easy._requestContentStream != null, "We should only be in the send callback if we have a request content stream"); Debug.Assert(easy._associatedMultiAgent != null, "The request should be associated with a multi agent."); try { // Transfer data from the request's content stream to libcurl return TransferDataFromRequestStream(buffer, length, easy); } catch (Exception ex) { easy.FailRequest(ex); // cleanup will be handled by main processing loop } } // Something went wrong. return Interop.libcurl.CURL_READFUNC_ABORT; }
/// <summary> /// Internal method for created programs, must be implemented by subclasses /// </summary> /// <param name="name"> Name of the program to create.</param> /// <param name="handle">Handle of the program</param> /// <param name="group">resource group of the program</param> /// <param name="isManual">is this program manually created</param> /// <param name="loader">The ManualResourceLoader if this program is manually loaded</param> /// <param name="type">Type of the program to create, i.e. vertex or fragment.</param> /// <param name="syntaxCode">Syntax of the program, i.e. vs_1_1, arbvp1, etc.</param> /// <returns>A new instance of GpuProgram.</returns> protected abstract Resource _create( string name, ResourceHandle handle, string group, bool isManual, IManualResourceLoader loader, GpuProgramType type, string syntaxCode );
public List<dtNode> findNodes(dtPolyRef id, int maxNodes) { List<dtNode> ret = new List<dtNode>(); uint bucket = (uint)(DetourNode.dtHashRef(id) & (m_hashSize - 1)); dtNodeIndex i = m_first[bucket]; while (i != DetourNode.DT_NULL_IDX) { if (m_nodes[i].id == id) { if (ret.Count >= maxNodes) { return ret; } ret.Add(m_nodes[i]); } i = m_next[i]; } return ret; }
/// <summary> /// Initializes new instance of ObjectHandle class with ObjectId set to CK_INVALID_HANDLE /// </summary> public ObjectHandle() { _objectId = CK.CK_INVALID_HANDLE; }
public void Initialize(PackedMemorySnapshot snapshot, AbstractMemoryReader memoryReader, System.UInt64 address, PackedManagedType type) { m_Snapshot = snapshot; m_Address = address; m_Type = type; m_MemoryReader = memoryReader; m_Title = string.Format("{0} Visualizer", type.name); OnInitialize(); }
public static unsafe void PutUInt64(UInt64 x, byte[] b, int pos) { Debug.Assert(b.Length - pos >= sizeof(UInt64)); fixed (byte* sb = b) { *((UInt64*)(sb + pos)) = x; } }
private static size_t CurlSendCallback(IntPtr buffer, size_t size, size_t nitems, IntPtr context) { CurlHandler.VerboseTrace("size: " + size + ", nitems: " + nitems); int length = checked((int)(size * nitems)); Debug.Assert(length <= RequestBufferSize, "length " + length + " should not be larger than RequestBufferSize " + RequestBufferSize); if (length == 0) { return 0; } EasyRequest easy; if (TryGetEasyRequestFromContext(context, out easy)) { Debug.Assert(easy._requestContentStream != null, "We should only be in the send callback if we have a request content stream"); Debug.Assert(easy._associatedMultiAgent != null, "The request should be associated with a multi agent."); // Transfer data from the request's content stream to libcurl try { if (easy._requestContentStream is MemoryStream) { // If the request content stream is a memory stream (a very common case, as it's the default // stream type used by the base HttpContent type), then we can simply perform the read synchronously // knowing that it won't block. return (size_t)TransferDataFromRequestMemoryStream(buffer, length, easy); } else { // Otherwise, we need to be concerned about blocking, due to this being called from the only thread able to // service the multi agent (a multi handle can only be accessed by one thread at a time). The whole // operation, across potentially many callbacks, will be performed asynchronously: we issue the request // asynchronously, and if it's not completed immediately, we pause the connection until the data // is ready to be consumed by libcurl. return TransferDataFromRequestStream(buffer, length, easy); } } catch (Exception ex) { easy.FailRequest(ex); // cleanup will be handled by main processing loop } } // Something went wrong. return Interop.libcurl.CURL_READFUNC_ABORT; }
public BulletShape() { type = BSPhysicsShapeType.SHAPE_UNKNOWN; shapeKey = (System.UInt64)FixedShapeKey.KEY_NONE; isNativeShape = false; }
protected override Resource _create( string name, ResourceHandle handle, string group, bool isManual, IManualResourceLoader loader, GpuProgramType type, string syntaxCode ) { if ( type == GpuProgramType.Vertex ) { return new D3D9GpuVertexProgram( this, name, handle, group, isManual, loader ); } else { return new D3D9GpuFragmentProgram( this, name, handle, group, isManual, loader ); } }
/// <summary> /// Initializes new instance of SlotFlags class /// </summary> /// <param name="flags">Bits flags that provide capabilities of the slot</param> protected internal SlotFlags(NativeULong flags) { _flags = flags; }
/// <summary> /// Initializes new instance of ObjectHandle class /// </summary> /// <param name="objectId">PKCS#11 handle of object</param> public ObjectHandle(ulong objectId) { _objectId = ConvertUtils.UInt64FromUInt64(objectId); }
public void SetAddress(NodeIndex nodeIndex, Address nodeAddress) { Debug.Assert(m_nodeAddresses[(int)nodeIndex] == 0, "Calling SetAddress twice for node index " + nodeIndex); m_nodeAddresses[(int)nodeIndex] = nodeAddress; }
public DesktopInterfaceData(ClrType type, Address ptr) { _type = type; _interface = ptr; }
public dtNode findNode(dtPolyRef id) { uint bucket = (uint)(DetourNode.dtHashRef(id) & (m_hashSize - 1)); dtNodeIndex i = m_first[bucket]; while (i != DetourNode.DT_NULL_IDX) { if (m_nodes[i].id == id) return m_nodes[i]; i = m_next[i]; } return null; }
public void bufferData (GLenum target, GLsizei size, GLenum usage) { }
/// <summary> /// Lorsque le match se crée. /// </summary> /// <param name="success"></param> /// <param name="extendedInfo"></param> /// <param name="matchInfo"></param> public override void OnMatchCreate(bool success, string extendedInfo, MatchInfo matchInfo) { base.OnMatchCreate(success, extendedInfo, matchInfo); _currentMatchID = (System.UInt64)matchInfo.networkId; }
internal D3D9GpuFragmentProgram( ResourceManager parent, string name, ResourceHandle handle, string group, bool isManual, IManualResourceLoader loader ) : base( parent, name, handle, group, isManual, loader ) { Type = GpuProgramType.Fragment; }
public D3D9HLSLProgram( ResourceManager parent, string name, ResourceHandle handle, string group, bool isManual, IManualResourceLoader loader ) : base( parent, name, handle, group, isManual, loader ) { UseColumnMajorMatrices = true; }
public override void OnMatchCreate(UnityEngine.Networking.Match.CreateMatchResponse matchInfo) { base.OnMatchCreate(matchInfo); _currentMatchID = (System.UInt64)matchInfo.networkId; }
// Returns &inputBuffer[inputLength] if the input buffer is valid. /// <summary> /// Given an input buffer <paramref name="pInputBuffer"/> of byte length <paramref name="inputLength"/>, /// returns a pointer to where the first invalid data appears in <paramref name="pInputBuffer"/>. /// </summary> /// <remarks> /// Returns a pointer to the end of <paramref name="pInputBuffer"/> if the buffer is well-formed. /// </remarks> public static byte *GetPointerToFirstInvalidByte(byte *pInputBuffer, int inputLength, out int utf16CodeUnitCountAdjustment, out int scalarCountAdjustment) { Debug.Assert(inputLength >= 0, "Input length must not be negative."); Debug.Assert(pInputBuffer != null || inputLength == 0, "Input length must be zero if input buffer pointer is null."); // First, try to drain off as many ASCII bytes as we can from the beginning. { nuint numAsciiBytesCounted = ASCIIUtility.GetIndexOfFirstNonAsciiByte(pInputBuffer, (uint)inputLength); pInputBuffer += numAsciiBytesCounted; // Quick check - did we just end up consuming the entire input buffer? // If so, short-circuit the remainder of the method. inputLength -= (int)numAsciiBytesCounted; if (inputLength == 0) { utf16CodeUnitCountAdjustment = 0; scalarCountAdjustment = 0; return(pInputBuffer); } } #if DEBUG // Keep these around for final validation at the end of the method. byte *pOriginalInputBuffer = pInputBuffer; int originalInputLength = inputLength; #endif // Enregistered locals that we'll eventually out to our caller. int tempUtf16CodeUnitCountAdjustment = 0; int tempScalarCountAdjustment = 0; if (inputLength < sizeof(uint)) { goto ProcessInputOfLessThanDWordSize; } byte *pFinalPosWhereCanReadDWordFromInputBuffer = pInputBuffer + (uint)inputLength - sizeof(uint); // Begin the main loop. #if DEBUG byte *pLastBufferPosProcessed = null; // used for invariant checking in debug builds #endif while (pInputBuffer <= pFinalPosWhereCanReadDWordFromInputBuffer) { // Read 32 bits at a time. This is enough to hold any possible UTF8-encoded scalar. uint thisDWord = Unsafe.ReadUnaligned <uint>(pInputBuffer); AfterReadDWord: #if DEBUG Debug.Assert(pLastBufferPosProcessed < pInputBuffer, "Algorithm should've made forward progress since last read."); pLastBufferPosProcessed = pInputBuffer; #endif // First, check for the common case of all-ASCII bytes. if (ASCIIUtility.AllBytesInUInt32AreAscii(thisDWord)) { // We read an all-ASCII sequence. pInputBuffer += sizeof(uint); // If we saw a sequence of all ASCII, there's a good chance a significant amount of following data is also ASCII. // Below is basically unrolled loops with poor man's vectorization. // Below check is "can I read at least five DWORDs from the input stream?" // n.b. Since we incremented pInputBuffer above the below subtraction may result in a negative value, // hence using nint instead of nuint. if ((nint)(void *)Unsafe.ByteOffset(ref *pInputBuffer, ref *pFinalPosWhereCanReadDWordFromInputBuffer) >= 4 * sizeof(uint)) { // We want reads in the inner loop to be aligned. So let's perform a quick // ASCII check of the next 32 bits (4 bytes) now, and if that succeeds bump // the read pointer up to the next aligned address. thisDWord = Unsafe.ReadUnaligned <uint>(pInputBuffer); if (!ASCIIUtility.AllBytesInUInt32AreAscii(thisDWord)) { goto AfterReadDWordSkipAllBytesAsciiCheck; } pInputBuffer = (byte *)((nuint)(pInputBuffer + 4) & ~(nuint)3); // At this point, the input buffer offset points to an aligned DWORD. We also know that there's // enough room to read at least four DWORDs from the buffer. (Heed the comment a few lines above: // the original 'if' check confirmed that there were 5 DWORDs before the alignment check, and // the alignment check consumes at most a single DWORD.) byte *pInputBufferFinalPosAtWhichCanSafelyLoop = pFinalPosWhereCanReadDWordFromInputBuffer - 3 * sizeof(uint); // can safely read 4 DWORDs here uint mask; do { if (Sse2.IsSupported && Bmi1.IsSupported) { // pInputBuffer is 32-bit aligned but not necessary 128-bit aligned, so we're // going to perform an unaligned load. We don't necessarily care about aligning // this because we pessimistically assume we'll encounter non-ASCII data at some // point in the not-too-distant future (otherwise we would've stayed entirely // within the all-ASCII vectorized code at the entry to this method). mask = (uint)Sse2.MoveMask(Sse2.LoadVector128((byte *)pInputBuffer)); if (mask != 0) { goto Sse2LoopTerminatedEarlyDueToNonAsciiData; } } else { if (!ASCIIUtility.AllBytesInUInt32AreAscii(((uint *)pInputBuffer)[0] | ((uint *)pInputBuffer)[1])) { goto LoopTerminatedEarlyDueToNonAsciiDataInFirstPair; } if (!ASCIIUtility.AllBytesInUInt32AreAscii(((uint *)pInputBuffer)[2] | ((uint *)pInputBuffer)[3])) { goto LoopTerminatedEarlyDueToNonAsciiDataInSecondPair; } } pInputBuffer += 4 * sizeof(uint); // consumed 4 DWORDs } while (pInputBuffer <= pInputBufferFinalPosAtWhichCanSafelyLoop); continue; // need to perform a bounds check because we might be running out of data Sse2LoopTerminatedEarlyDueToNonAsciiData: Debug.Assert(BitConverter.IsLittleEndian); Debug.Assert(Sse2.IsSupported); Debug.Assert(Bmi1.IsSupported); // The 'mask' value will have a 0 bit for each ASCII byte we saw and a 1 bit // for each non-ASCII byte we saw. We can count the number of ASCII bytes, // bump our input counter by that amount, and resume processing from the // "the first byte is no longer ASCII" portion of the main loop. Debug.Assert(mask != 0); pInputBuffer += Bmi1.TrailingZeroCount(mask); if (pInputBuffer > pFinalPosWhereCanReadDWordFromInputBuffer) { goto ProcessRemainingBytesSlow; } thisDWord = Unsafe.ReadUnaligned <uint>(pInputBuffer); // no longer guaranteed to be aligned goto BeforeProcessTwoByteSequence; LoopTerminatedEarlyDueToNonAsciiDataInSecondPair: pInputBuffer += 2 * sizeof(uint); // consumed 2 DWORDs LoopTerminatedEarlyDueToNonAsciiDataInFirstPair: // We know that there's *at least* two DWORDs of data remaining in the buffer. // We also know that one of them (or both of them) contains non-ASCII data somewhere. // Let's perform a quick check here to bypass the logic at the beginning of the main loop. thisDWord = *(uint *)pInputBuffer; // still aligned here if (ASCIIUtility.AllBytesInUInt32AreAscii(thisDWord)) { pInputBuffer += sizeof(uint); // consumed 1 more DWORD thisDWord = *(uint *)pInputBuffer; // still aligned here } goto AfterReadDWordSkipAllBytesAsciiCheck; } continue; // not enough data remaining to unroll loop - go back to beginning with bounds checks } AfterReadDWordSkipAllBytesAsciiCheck: Debug.Assert(!ASCIIUtility.AllBytesInUInt32AreAscii(thisDWord)); // this should have been handled earlier // Next, try stripping off ASCII bytes one at a time. // We only handle up to three ASCII bytes here since we handled the four ASCII byte case above. { uint numLeadingAsciiBytes = ASCIIUtility.CountNumberOfLeadingAsciiBytesFromUInt32WithSomeNonAsciiData(thisDWord); pInputBuffer += numLeadingAsciiBytes; if (pFinalPosWhereCanReadDWordFromInputBuffer < pInputBuffer) { goto ProcessRemainingBytesSlow; // Input buffer doesn't contain enough data to read a DWORD } else { // The input buffer at the current offset contains a non-ASCII byte. // Read an entire DWORD and fall through to multi-byte consumption logic. thisDWord = Unsafe.ReadUnaligned <uint>(pInputBuffer); } } BeforeProcessTwoByteSequence: // At this point, we suspect we're working with a multi-byte code unit sequence, // but we haven't yet validated it for well-formedness. // The masks and comparands are derived from the Unicode Standard, Table 3-6. // Additionally, we need to check for valid byte sequences per Table 3-7. // Check the 2-byte case. thisDWord -= (BitConverter.IsLittleEndian) ? 0x0000_80C0u : 0xC080_0000u; if ((thisDWord & (BitConverter.IsLittleEndian ? 0x0000_C0E0u : 0xE0C0_0000u)) == 0) { // Per Table 3-7, valid sequences are: // [ C2..DF ] [ 80..BF ] // // Due to our modification of 'thisDWord' above, this becomes: // [ 02..1F ] [ 00..3F ] // // We've already checked that the leading byte was originally in the range [ C0..DF ] // and that the trailing byte was originally in the range [ 80..BF ], so now we only need // to check that the modified leading byte is >= [ 02 ]. if ((BitConverter.IsLittleEndian && (byte)thisDWord < 0x02u) || (!BitConverter.IsLittleEndian && thisDWord < 0x0200_0000u)) { goto Error; // overlong form - leading byte was [ C0 ] or [ C1 ] } ProcessTwoByteSequenceSkipOverlongFormCheck: // Optimization: If this is a two-byte-per-character language like Cyrillic or Hebrew, // there's a good chance that if we see one two-byte run then there's another two-byte // run immediately after. Let's check that now. // On little-endian platforms, we can check for the two-byte UTF8 mask *and* validate that // the value isn't overlong using a single comparison. On big-endian platforms, we'll need // to validate the mask and validate that the sequence isn't overlong as two separate comparisons. if ((BitConverter.IsLittleEndian && UInt32EndsWithValidUtf8TwoByteSequenceLittleEndian(thisDWord)) || (!BitConverter.IsLittleEndian && (UInt32EndsWithUtf8TwoByteMask(thisDWord) && !UInt32EndsWithOverlongUtf8TwoByteSequence(thisDWord)))) { // We have two runs of two bytes each. pInputBuffer += 4; tempUtf16CodeUnitCountAdjustment -= 2; // 4 UTF-8 code units -> 2 UTF-16 code units (and 2 scalars) if (pInputBuffer <= pFinalPosWhereCanReadDWordFromInputBuffer) { // Optimization: If we read a long run of two-byte sequences, the next sequence is probably // also two bytes. Check for that first before going back to the beginning of the loop. thisDWord = Unsafe.ReadUnaligned <uint>(pInputBuffer); if (BitConverter.IsLittleEndian) { if (UInt32BeginsWithValidUtf8TwoByteSequenceLittleEndian(thisDWord)) { // The next sequence is a valid two-byte sequence. goto ProcessTwoByteSequenceSkipOverlongFormCheck; } } else { if (UInt32BeginsWithUtf8TwoByteMask(thisDWord)) { if (UInt32BeginsWithOverlongUtf8TwoByteSequence(thisDWord)) { goto Error; // The next sequence purports to be a 2-byte sequence but is overlong. } goto ProcessTwoByteSequenceSkipOverlongFormCheck; } } // If we reached this point, the next sequence is something other than a valid // two-byte sequence, so go back to the beginning of the loop. goto AfterReadDWord; } else { goto ProcessRemainingBytesSlow; // Running out of data - go down slow path } } // The buffer contains a 2-byte sequence followed by 2 bytes that aren't a 2-byte sequence. // Unlikely that a 3-byte sequence would follow a 2-byte sequence, so perhaps remaining // bytes are ASCII? tempUtf16CodeUnitCountAdjustment--; // 2-byte sequence + (some number of ASCII bytes) -> 1 UTF-16 code units (and 1 scalar) [+ trailing] if (UInt32ThirdByteIsAscii(thisDWord)) { if (UInt32FourthByteIsAscii(thisDWord)) { pInputBuffer += 4; } else { pInputBuffer += 3; // A two-byte sequence followed by an ASCII byte followed by a non-ASCII byte. // Read in the next DWORD and jump directly to the start of the multi-byte processing block. if (pInputBuffer <= pFinalPosWhereCanReadDWordFromInputBuffer) { thisDWord = Unsafe.ReadUnaligned <uint>(pInputBuffer); goto BeforeProcessTwoByteSequence; } } } else { pInputBuffer += 2; } continue; } // Check the 3-byte case. // We need to restore the C0 leading byte we stripped out earlier, then we can strip out the expected E0 byte. thisDWord -= (BitConverter.IsLittleEndian) ? (0x0080_00E0u - 0x0000_00C0u) : (0xE000_8000u - 0xC000_0000u); if ((thisDWord & (BitConverter.IsLittleEndian ? 0x00C0_C0F0u : 0xF0C0_C000u)) == 0) { ProcessThreeByteSequenceWithCheck: // We assume the caller has confirmed that the bit pattern is representative of a three-byte // sequence, but it may still be overlong or surrogate. We need to check for these possibilities. // // Per Table 3-7, valid sequences are: // [ E0 ] [ A0..BF ] [ 80..BF ] // [ E1..EC ] [ 80..BF ] [ 80..BF ] // [ ED ] [ 80..9F ] [ 80..BF ] // [ EE..EF ] [ 80..BF ] [ 80..BF ] // // Big-endian examples of using the above validation table: // E0A0 = 1110 0000 1010 0000 => invalid (overlong ) patterns are 1110 0000 100# #### // ED9F = 1110 1101 1001 1111 => invalid (surrogate) patterns are 1110 1101 101# #### // If using the bitmask ......................................... 0000 1111 0010 0000 (=0F20), // Then invalid (overlong) patterns match the comparand ......... 0000 0000 0000 0000 (=0000), // And invalid (surrogate) patterns match the comparand ......... 0000 1101 0010 0000 (=0D20). // // It's ok if the caller has manipulated 'thisDWord' (e.g., by subtracting 0xE0 or 0x80) // as long as they haven't touched the bits we're about to use in our mask checking below. if (BitConverter.IsLittleEndian) { // The "overlong or surrogate" check can be implemented using a single jump, but there's // some overhead to moving the bits into the correct locations in order to perform the // correct comparison, and in practice the processor's branch prediction capability is // good enough that we shouldn't bother. So we'll use two jumps instead. // Can't extract this check into its own helper method because JITter produces suboptimal // assembly, even with aggressive inlining. // Code below becomes 5 instructions: test, jz, lea, test, jz if (((thisDWord & 0x0000_200Fu) == 0) || (((thisDWord - 0x0000_200Du) & 0x0000_200Fu) == 0)) { goto Error; // overlong or surrogate } } else { if (((thisDWord & 0x0F20_0000u) == 0) || (((thisDWord - 0x0D20_0000u) & 0x0F20_0000u) == 0)) { goto Error; // overlong or surrogate } } ProcessSingleThreeByteSequenceSkipOverlongAndSurrogateChecks: // Occasionally one-off ASCII characters like spaces, periods, or newlines will make their way // in to the text. If this happens strip it off now before seeing if the next character // consists of three code units. // Branchless: consume a 3-byte UTF-8 sequence and optionally an extra ASCII byte hanging off the end nint asciiAdjustment; if (BitConverter.IsLittleEndian) { asciiAdjustment = (int)thisDWord >> 31; // smear most significant bit across entire value } else { asciiAdjustment = (nint)(sbyte)thisDWord >> 7; // smear most significant bit of least significant byte across entire value } // asciiAdjustment = 0 if fourth byte is ASCII; -1 otherwise // Please *DO NOT* reorder the below two lines. It provides extra defense in depth in case this method // is ever changed such that pInputBuffer becomes a 'ref byte' instead of a simple 'byte*'. It's valid // to add 4 before backing up since we already checked previously that the input buffer contains at // least a DWORD's worth of data, so we're not going to run past the end of the buffer where the GC can // no longer track the reference. However, we can't back up before adding 4, since we might back up to // before the start of the buffer, and the GC isn't guaranteed to be able to track this. pInputBuffer += 4; // optimistically, assume consumed a 3-byte UTF-8 sequence plus an extra ASCII byte pInputBuffer += asciiAdjustment; // back up if we didn't actually consume an ASCII byte tempUtf16CodeUnitCountAdjustment -= 2; // 3 (or 4) UTF-8 bytes -> 1 (or 2) UTF-16 code unit (and 1 [or 2] scalar) SuccessfullyProcessedThreeByteSequence: if (IntPtr.Size >= 8 && BitConverter.IsLittleEndian) { // x64 little-endian optimization: A three-byte character could indicate CJK text, // which makes it likely that the character following this one is also CJK. // We'll try to process several three-byte sequences at a time. // The check below is really "can we read 9 bytes from the input buffer?" since 'pFinalPos...' is already offset // n.b. The subtraction below could result in a negative value (since we advanced pInputBuffer above), so // use nint instead of nuint. if ((nint)(pFinalPosWhereCanReadDWordFromInputBuffer - pInputBuffer) >= 5) { ulong thisQWord = Unsafe.ReadUnaligned <ulong>(pInputBuffer); // Stage the next 32 bits into 'thisDWord' so that it's ready for us in case we need to jump backward // to a previous location in the loop. This offers defense against reading main memory again (which may // have been modified and could lead to a race condition). thisDWord = (uint)thisQWord; // Is this three 3-byte sequences in a row? // thisQWord = [ 10yyyyyy 1110zzzz | 10xxxxxx 10yyyyyy 1110zzzz | 10xxxxxx 10yyyyyy 1110zzzz ] [ 10xxxxxx ] // ---- CHAR 3 ---- --------- CHAR 2 --------- --------- CHAR 1 --------- -CHAR 3- if ((thisQWord & 0xC0F0_C0C0_F0C0_C0F0ul) == 0x80E0_8080_E080_80E0ul && IsUtf8ContinuationByte(in pInputBuffer[8])) { // Saw a proper bitmask for three incoming 3-byte sequences, perform the // overlong and surrogate sequence checking now. // Check the first character. // If the first character is overlong or a surrogate, fail immediately. if ((((uint)thisQWord & 0x200Fu) == 0) || ((((uint)thisQWord - 0x200Du) & 0x200Fu) == 0)) { goto Error; } // Check the second character. // At this point, we now know the first three bytes represent a well-formed sequence. // If there's an error beyond here, we'll jump back to the "process three known good bytes" // logic. thisQWord >>= 24; if ((((uint)thisQWord & 0x200Fu) == 0) || ((((uint)thisQWord - 0x200Du) & 0x200Fu) == 0)) { goto ProcessSingleThreeByteSequenceSkipOverlongAndSurrogateChecks; } // Check the third character (we already checked that it's followed by a continuation byte). thisQWord >>= 24; if ((((uint)thisQWord & 0x200Fu) == 0) || ((((uint)thisQWord - 0x200Du) & 0x200Fu) == 0)) { goto ProcessSingleThreeByteSequenceSkipOverlongAndSurrogateChecks; } pInputBuffer += 9; tempUtf16CodeUnitCountAdjustment -= 6; // 9 UTF-8 bytes -> 3 UTF-16 code units (and 3 scalars) goto SuccessfullyProcessedThreeByteSequence; } // Is this two 3-byte sequences in a row? // thisQWord = [ ######## ######## | 10xxxxxx 10yyyyyy 1110zzzz | 10xxxxxx 10yyyyyy 1110zzzz ] // --------- CHAR 2 --------- --------- CHAR 1 --------- if ((thisQWord & 0xC0C0_F0C0_C0F0ul) == 0x8080_E080_80E0ul) { // Saw a proper bitmask for two incoming 3-byte sequences, perform the // overlong and surrogate sequence checking now. // Check the first character. // If the first character is overlong or a surrogate, fail immediately. if ((((uint)thisQWord & 0x200Fu) == 0) || ((((uint)thisQWord - 0x200Du) & 0x200Fu) == 0)) { goto Error; } // Check the second character. // At this point, we now know the first three bytes represent a well-formed sequence. // If there's an error beyond here, we'll jump back to the "process three known good bytes" // logic. thisQWord >>= 24; if ((((uint)thisQWord & 0x200Fu) == 0) || ((((uint)thisQWord - 0x200Du) & 0x200Fu) == 0)) { goto ProcessSingleThreeByteSequenceSkipOverlongAndSurrogateChecks; } pInputBuffer += 6; tempUtf16CodeUnitCountAdjustment -= 4; // 6 UTF-8 bytes -> 2 UTF-16 code units (and 2 scalars) // The next byte in the sequence didn't have a 3-byte marker, so it's probably // an ASCII character. Jump back to the beginning of loop processing. continue; } if (UInt32BeginsWithUtf8ThreeByteMask(thisDWord)) { // A single three-byte sequence. goto ProcessThreeByteSequenceWithCheck; } else { // Not a three-byte sequence; perhaps ASCII? goto AfterReadDWord; } } } if (pInputBuffer <= pFinalPosWhereCanReadDWordFromInputBuffer) { thisDWord = Unsafe.ReadUnaligned <uint>(pInputBuffer); // Optimization: A three-byte character could indicate CJK text, which makes it likely // that the character following this one is also CJK. We'll check for a three-byte sequence // marker now and jump directly to three-byte sequence processing if we see one, skipping // all of the logic at the beginning of the loop. if (UInt32BeginsWithUtf8ThreeByteMask(thisDWord)) { goto ProcessThreeByteSequenceWithCheck; // Found another [not yet validated] three-byte sequence; process } else { goto AfterReadDWord; // Probably ASCII punctuation or whitespace; go back to start of loop } } else { goto ProcessRemainingBytesSlow; // Running out of data } } // Assume the 4-byte case, but we need to validate. if (BitConverter.IsLittleEndian) { thisDWord &= 0xC0C0_FFFFu; // After the above modifications earlier in this method, we expect 'thisDWord' // to have the structure [ 10000000 00000000 00uuzzzz 00010uuu ]. We'll now // perform two checks to confirm this. The first will verify the // [ 10000000 00000000 00###### ######## ] structure by taking advantage of two's // complement representation to perform a single *signed* integer check. if ((int)thisDWord > unchecked ((int)0x8000_3FFF)) { goto Error; // didn't have three trailing bytes } // Now we want to confirm that 0x01 <= uuuuu (otherwise this is an overlong encoding) // and that uuuuu <= 0x10 (otherwise this is an out-of-range encoding). thisDWord = BitOperations.RotateRight(thisDWord, 8); // Now, thisDWord = [ 00010uuu 10000000 00000000 00uuzzzz ]. // The check is now a simple add / cmp / jcc combo. if (!UnicodeUtility.IsInRangeInclusive(thisDWord, 0x1080_0010u, 0x1480_000Fu)) { goto Error; // overlong or out-of-range } } else { thisDWord -= 0x80u; // After the above modifications earlier in this method, we expect 'thisDWord' // to have the structure [ 00010uuu 00uuzzzz 00yyyyyy 00xxxxxx ]. We'll now // perform two checks to confirm this. The first will verify the // [ ######## 00###### 00###### 00###### ] structure. if ((thisDWord & 0x00C0_C0C0u) != 0) { goto Error; // didn't have three trailing bytes } // Now we want to confirm that 0x01 <= uuuuu (otherwise this is an overlong encoding) // and that uuuuu <= 0x10 (otherwise this is an out-of-range encoding). // This is a simple range check. (We don't care about the low two bytes.) if (!UnicodeUtility.IsInRangeInclusive(thisDWord, 0x1010_0000u, 0x140F_FFFFu)) { goto Error; // overlong or out-of-range } } // Validation of 4-byte case complete. pInputBuffer += 4; tempUtf16CodeUnitCountAdjustment -= 2; // 4 UTF-8 bytes -> 2 UTF-16 code units tempScalarCountAdjustment--; // 2 UTF-16 code units -> 1 scalar continue; // go back to beginning of loop for processing } goto ProcessRemainingBytesSlow; ProcessInputOfLessThanDWordSize: Debug.Assert(inputLength < 4); nuint inputBufferRemainingBytes = (uint)inputLength; goto ProcessSmallBufferCommon; ProcessRemainingBytesSlow: inputBufferRemainingBytes = (nuint)(void *)Unsafe.ByteOffset(ref *pInputBuffer, ref *pFinalPosWhereCanReadDWordFromInputBuffer) + 4; ProcessSmallBufferCommon: Debug.Assert(inputBufferRemainingBytes < 4); while (inputBufferRemainingBytes > 0) { uint firstByte = pInputBuffer[0]; if ((byte)firstByte < 0x80u) { // 1-byte (ASCII) case pInputBuffer++; inputBufferRemainingBytes--; continue; } else if (inputBufferRemainingBytes >= 2) { uint secondByte = pInputBuffer[1]; // typed as 32-bit since we perform arithmetic (not just comparisons) on this value if ((byte)firstByte < 0xE0u) { // 2-byte case if ((byte)firstByte >= 0xC2u && IsLowByteUtf8ContinuationByte(secondByte)) { pInputBuffer += 2; tempUtf16CodeUnitCountAdjustment--; // 2 UTF-8 bytes -> 1 UTF-16 code unit (and 1 scalar) inputBufferRemainingBytes -= 2; continue; } } else if (inputBufferRemainingBytes >= 3) { if ((byte)firstByte < 0xF0u) { if ((byte)firstByte == 0xE0u) { if (!UnicodeUtility.IsInRangeInclusive(secondByte, 0xA0u, 0xBFu)) { goto Error; // overlong encoding } } else if ((byte)firstByte == 0xEDu) { if (!UnicodeUtility.IsInRangeInclusive(secondByte, 0x80u, 0x9Fu)) { goto Error; // would be a UTF-16 surrogate code point } } else { if (!IsLowByteUtf8ContinuationByte(secondByte)) { goto Error; // first trailing byte doesn't have proper continuation marker } } if (IsUtf8ContinuationByte(in pInputBuffer[2])) { pInputBuffer += 3; tempUtf16CodeUnitCountAdjustment -= 2; // 3 UTF-8 bytes -> 2 UTF-16 code units (and 2 scalars) inputBufferRemainingBytes -= 3; continue; } } } } // Error - no match. goto Error; } // If we reached this point, we're out of data, and we saw no bad UTF8 sequence. #if DEBUG // Quick check that for the success case we're going to fulfill our contract of returning &inputBuffer[inputLength]. Debug.Assert(pOriginalInputBuffer + originalInputLength == pInputBuffer, "About to return an unexpected value."); #endif Error: // Report back to our caller how far we got before seeing invalid data. // (Also used for normal termination when falling out of the loop above.) utf16CodeUnitCountAdjustment = tempUtf16CodeUnitCountAdjustment; scalarCountAdjustment = tempScalarCountAdjustment; return(pInputBuffer); }
private static size_t CurlReceiveBodyCallback( IntPtr buffer, size_t size, size_t nitems, IntPtr context) { CurlHandler.VerboseTrace("size: " + size + ", nitems: " + nitems); size *= nitems; EasyRequest easy; if (TryGetEasyRequestFromContext(context, out easy)) { try { Debug.Assert(easy._paused == EasyRequest.PauseState.Unpaused, "We shouldn't be getting callbacks for a paused request."); if (!(easy.Task.IsCanceled || easy.Task.IsFaulted)) { // Complete the task if it hasn't already been. This will make the // stream available to consumers. A previous write callback // may have already completed the task to publish the response. easy.EnsureResponseMessagePublished(); // Try to transfer the data to a reader. This will return either the // amount of data transferred (equal to the amount requested // to be transferred), or it will return a pause request. return easy._responseMessage.ResponseStream.TransferDataToStream(buffer, (long)size); } } catch (Exception ex) { easy.FailRequest(ex); // cleanup will be handled by main processing loop } } // Returing a value other than size fails the callback and forces // request completion with an error. CurlHandler.VerboseTrace("Error: returning a bad size to abort the request"); return (size > 0) ? size - 1 : 1; }
public ValueTypePropertyGridItem(PropertyGridControl owner, PackedMemorySnapshot snapshot, System.UInt64 address, AbstractMemoryReader memoryReader) : base(owner, snapshot, address, memoryReader) { }
public void Serialize(string inKey, ref System.UInt64 ioData, System.UInt64 inDefault, FieldOptions inOptions = FieldOptions.None) { DoSerialize <System.UInt64>(inKey, ref ioData, inDefault, inOptions, Read_UInt64_Cached ?? (Read_UInt64_Cached = Read_UInt64), Write_UInt64_Cached ?? (Write_UInt64_Cached = Write_UInt64)); }
Truncate(KTL_LOG_ASN TruncationPoint, KTL_LOG_ASN PreferredTruncationPoint) { this._NativeStream.Truncate(TruncationPoint, PreferredTruncationPoint); }
protected abstract void Write_UInt64(ref System.UInt64 ioData);
protected override ref TLink GetRightReference(TLink node) => ref LinksIndexParts[node].RightAsTarget;
public static extern ZyanStatus FormatInstruction(ref Formatter formatter, ref DecodedInstruction instruction, [MarshalAs(UnmanagedType.LPStr)] StringBuilder buffer, ZyanUSize length, ZyanU64 runtimeAddress);
protected override TLink GetLeft(TLink node) => LinksIndexParts[node].LeftAsTarget;
public bool IsInGraph(Address objectAddress) { return(m_addressToNodeIndex.ContainsKey(objectAddress)); }
protected override TLink GetRight(TLink node) => LinksIndexParts[node].RightAsTarget;
private static size_t CurlReceiveBodyCallback( IntPtr buffer, size_t size, size_t nitems, IntPtr context) { size *= nitems; EasyRequest easy; if (TryGetEasyRequestFromContext(context, out easy)) { try { if (!(easy.Task.IsCanceled || easy.Task.IsFaulted)) { // Complete the task if it hasn't already been. This will make the // stream available to consumers. A previous write callback // may have already completed the task to publish the response. easy.EnsureResponseMessagePublished(); // Wait for a reader // TODO: The below call blocks till all the data has been read since // response body is not supported to be buffered in memory. // Figure out some way to work around this. For example, we could // potentially return CURL_WRITEFUNC_PAUSE to pause the connection // until a reader is ready to read, at which point we could unpause. if (size != 0) { easy.ResponseMessage.ContentStream.WaitAndSignalReaders(buffer, (long)size); } return size; } } catch (Exception ex) { easy.FailRequest(ex); // cleanup will be handled by main processing loop } } // Returing a value other than size fails the callback and forces // request completion with an error. return (size > 0) ? size - 1 : 1; }
protected override void SetLeft(TLink node, TLink left) => LinksIndexParts[node].LeftAsTarget = left;
protected override void SetRight(TLink node, TLink right) => LinksIndexParts[node].RightAsTarget = right;
protected override TLink GetSize(TLink node) => LinksIndexParts[node].SizeAsTarget;
public dtNode getNode(dtPolyRef id, uint state) { uint bucket = (uint)(DetourNode.dtHashRef(id) & (m_hashSize - 1)); dtNodeIndex i = m_first[bucket]; dtNode node = null; while (i != DetourNode.DT_NULL_IDX) { if (m_nodes[i].id == id && m_nodes[i].state == state) return m_nodes[i]; i = m_next[i]; } if (m_nodeCount >= m_maxNodes) return null; i = (dtNodeIndex)m_nodeCount; m_nodeCount++; node = m_nodes[i]; node.pidx = 0; node.cost = 0; node.total = 0; node.id = id; node.state = state; node.flags = 0; m_next[i] = m_first[bucket]; m_first[bucket] = i; return node; }
protected override void SetSize(TLink node, TLink size) => LinksIndexParts[node].SizeAsTarget = size;
public UInt64() { data = 0; }
protected override TLink GetBasePartValue(TLink node) => LinksDataParts[node].Target;
internal DesktopRCWData(DesktopGCHeap heap, Address rcw, IRCWData data) { _addr = rcw; _rcw = data; _heap = heap; _osThreadID = uint.MaxValue; }
protected override bool FirstIsToTheRightOfSecond(TLink firstSource, TLink firstTarget, TLink secondSource, TLink secondTarget) => firstTarget > secondTarget || firstTarget == secondTarget && firstSource > secondSource;
internal DesktopCCWData(DesktopGCHeap heap, Address ccw, ICCWData data) { _addr = ccw; _ccw = data; _heap = heap; }
protected override void ClearNode(TLink node) { ref var link = ref LinksIndexParts[node];
public void bindBuffer (GLenum target, WebGLBuffer buffer) { throw new System.NotSupportedException (); }
/// <summary> /// Get a stable ID for a GcAddress. This ID can be compared for object identity. /// This only works at the current point in time when scanning the source. /// </summary> public GCReferenceID GetReferenceForGCAddress(Address GcAddress) { return((GCReferenceID)(int)GcAddress); }
public void bufferData (GLenum target, ArrayBuffer data, GLenum usage) { }
/// <summary> /// Initializes a new instance of the CkRc2Params class. /// </summary> /// <param name='effectiveBits'>Effective number of bits in the RC2 search space</param> public CkRc2Params(NativeULong effectiveBits) { _lowLevelStruct.EffectiveBits = effectiveBits; }
private static size_t CurlReceiveHeadersCallback(IntPtr buffer, size_t size, size_t nitems, IntPtr context) { CurlHandler.VerboseTrace("size: " + size + ", nitems: " + nitems); size *= nitems; if (size == 0) { return 0; } EasyRequest easy; if (TryGetEasyRequestFromContext(context, out easy)) { try { // The callback is invoked once per header; multi-line headers get merged into a single line. string responseHeader = Marshal.PtrToStringAnsi(buffer).Trim(); HttpResponseMessage response = easy._responseMessage; if (!TryParseStatusLine(response, responseHeader, easy)) { int index = 0; string headerName = CurlResponseParseUtils.ReadHeaderName(responseHeader, out index); if (headerName != null) { string headerValue = responseHeader.Substring(index).Trim(); if (!response.Headers.TryAddWithoutValidation(headerName, headerValue)) { response.Content.Headers.TryAddWithoutValidation(headerName, headerValue); } else if (easy._isRedirect && string.Equals(headerName, HttpKnownHeaderNames.Location, StringComparison.OrdinalIgnoreCase)) { HandleRedirectLocationHeader(easy, headerValue); } } } return size; } catch (Exception ex) { easy.FailRequest(ex); // cleanup will be handled by main processing loop } } // Returing a value other than size fails the callback and forces // request completion with an error return size - 1; }
public static unsafe void ClearWithoutReferences(ref byte b, nuint byteLength) { if (byteLength == 0) { return; } #if CORECLR && (AMD64 || ARM64) // The exact matrix on when RhZeroMemory is faster than InitBlockUnaligned is very complex. The factors to consider include // type of hardware and memory aligment. This threshold was chosen as a good balance accross different configurations. if (byteLength > 768) { goto PInvoke; } Unsafe.InitBlockUnaligned(ref b, 0, (uint)byteLength); return; #else // TODO: Optimize other platforms to be on par with AMD64 CoreCLR // Note: It's important that this switch handles lengths at least up to 22. // See notes below near the main loop for why. // The switch will be very fast since it can be implemented using a jump // table in assembly. See http://stackoverflow.com/a/449297/4077294 for more info. switch (byteLength) { case 1: b = 0; return; case 2: Unsafe.As <byte, short>(ref b) = 0; return; case 3: Unsafe.As <byte, short>(ref b) = 0; Unsafe.Add <byte>(ref b, 2) = 0; return; case 4: Unsafe.As <byte, int>(ref b) = 0; return; case 5: Unsafe.As <byte, int>(ref b) = 0; Unsafe.Add <byte>(ref b, 4) = 0; return; case 6: Unsafe.As <byte, int>(ref b) = 0; Unsafe.As <byte, short>(ref Unsafe.Add <byte>(ref b, 4)) = 0; return; case 7: Unsafe.As <byte, int>(ref b) = 0; Unsafe.As <byte, short>(ref Unsafe.Add <byte>(ref b, 4)) = 0; Unsafe.Add <byte>(ref b, 6) = 0; return; case 8: #if BIT64 Unsafe.As <byte, long>(ref b) = 0; #else Unsafe.As <byte, int>(ref b) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 4)) = 0; #endif return; case 9: #if BIT64 Unsafe.As <byte, long>(ref b) = 0; #else Unsafe.As <byte, int>(ref b) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 4)) = 0; #endif Unsafe.Add <byte>(ref b, 8) = 0; return; case 10: #if BIT64 Unsafe.As <byte, long>(ref b) = 0; #else Unsafe.As <byte, int>(ref b) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 4)) = 0; #endif Unsafe.As <byte, short>(ref Unsafe.Add <byte>(ref b, 8)) = 0; return; case 11: #if BIT64 Unsafe.As <byte, long>(ref b) = 0; #else Unsafe.As <byte, int>(ref b) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 4)) = 0; #endif Unsafe.As <byte, short>(ref Unsafe.Add <byte>(ref b, 8)) = 0; Unsafe.Add <byte>(ref b, 10) = 0; return; case 12: #if BIT64 Unsafe.As <byte, long>(ref b) = 0; #else Unsafe.As <byte, int>(ref b) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 4)) = 0; #endif Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 8)) = 0; return; case 13: #if BIT64 Unsafe.As <byte, long>(ref b) = 0; #else Unsafe.As <byte, int>(ref b) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 4)) = 0; #endif Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 8)) = 0; Unsafe.Add <byte>(ref b, 12) = 0; return; case 14: #if BIT64 Unsafe.As <byte, long>(ref b) = 0; #else Unsafe.As <byte, int>(ref b) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 4)) = 0; #endif Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 8)) = 0; Unsafe.As <byte, short>(ref Unsafe.Add <byte>(ref b, 12)) = 0; return; case 15: #if BIT64 Unsafe.As <byte, long>(ref b) = 0; #else Unsafe.As <byte, int>(ref b) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 4)) = 0; #endif Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 8)) = 0; Unsafe.As <byte, short>(ref Unsafe.Add <byte>(ref b, 12)) = 0; Unsafe.Add <byte>(ref b, 14) = 0; return; case 16: #if BIT64 Unsafe.As <byte, long>(ref b) = 0; Unsafe.As <byte, long>(ref Unsafe.Add <byte>(ref b, 8)) = 0; #else Unsafe.As <byte, int>(ref b) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 4)) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 8)) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 12)) = 0; #endif return; case 17: #if BIT64 Unsafe.As <byte, long>(ref b) = 0; Unsafe.As <byte, long>(ref Unsafe.Add <byte>(ref b, 8)) = 0; #else Unsafe.As <byte, int>(ref b) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 4)) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 8)) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 12)) = 0; #endif Unsafe.Add <byte>(ref b, 16) = 0; return; case 18: #if BIT64 Unsafe.As <byte, long>(ref b) = 0; Unsafe.As <byte, long>(ref Unsafe.Add <byte>(ref b, 8)) = 0; #else Unsafe.As <byte, int>(ref b) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 4)) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 8)) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 12)) = 0; #endif Unsafe.As <byte, short>(ref Unsafe.Add <byte>(ref b, 16)) = 0; return; case 19: #if BIT64 Unsafe.As <byte, long>(ref b) = 0; Unsafe.As <byte, long>(ref Unsafe.Add <byte>(ref b, 8)) = 0; #else Unsafe.As <byte, int>(ref b) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 4)) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 8)) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 12)) = 0; #endif Unsafe.As <byte, short>(ref Unsafe.Add <byte>(ref b, 16)) = 0; Unsafe.Add <byte>(ref b, 18) = 0; return; case 20: #if BIT64 Unsafe.As <byte, long>(ref b) = 0; Unsafe.As <byte, long>(ref Unsafe.Add <byte>(ref b, 8)) = 0; #else Unsafe.As <byte, int>(ref b) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 4)) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 8)) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 12)) = 0; #endif Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 16)) = 0; return; case 21: #if BIT64 Unsafe.As <byte, long>(ref b) = 0; Unsafe.As <byte, long>(ref Unsafe.Add <byte>(ref b, 8)) = 0; #else Unsafe.As <byte, int>(ref b) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 4)) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 8)) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 12)) = 0; #endif Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 16)) = 0; Unsafe.Add <byte>(ref b, 20) = 0; return; case 22: #if BIT64 Unsafe.As <byte, long>(ref b) = 0; Unsafe.As <byte, long>(ref Unsafe.Add <byte>(ref b, 8)) = 0; #else Unsafe.As <byte, int>(ref b) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 4)) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 8)) = 0; Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 12)) = 0; #endif Unsafe.As <byte, int>(ref Unsafe.Add <byte>(ref b, 16)) = 0; Unsafe.As <byte, short>(ref Unsafe.Add <byte>(ref b, 20)) = 0; return; } // P/Invoke into the native version for large lengths if (byteLength >= 512) { goto PInvoke; } nuint i = 0; // byte offset at which we're copying if (((nuint)Unsafe.AsPointer(ref b) & 3) != 0) { if (((nuint)Unsafe.AsPointer(ref b) & 1) != 0) { b = 0; i += 1; if (((nuint)Unsafe.AsPointer(ref b) & 2) != 0) { goto IntAligned; } } Unsafe.As <byte, short>(ref Unsafe.AddByteOffset <byte>(ref b, i)) = 0; i += 2; } IntAligned: // On 64-bit IntPtr.Size == 8, so we want to advance to the next 8-aligned address. If // (int)b % 8 is 0, 5, 6, or 7, we will already have advanced by 0, 3, 2, or 1 // bytes to the next aligned address (respectively), so do nothing. On the other hand, // if it is 1, 2, 3, or 4 we will want to copy-and-advance another 4 bytes until // we're aligned. // The thing 1, 2, 3, and 4 have in common that the others don't is that if you // subtract one from them, their 3rd lsb will not be set. Hence, the below check. if ((((nuint)Unsafe.AsPointer(ref b) - 1) & 4) == 0) { Unsafe.As <byte, int>(ref Unsafe.AddByteOffset <byte>(ref b, i)) = 0; i += 4; } nuint end = byteLength - 16; byteLength -= i; // lower 4 bits of byteLength represent how many bytes are left *after* the unrolled loop // We know due to the above switch-case that this loop will always run 1 iteration; max // bytes we clear before checking is 23 (7 to align the pointers, 16 for 1 iteration) so // the switch handles lengths 0-22. Debug.Assert(end >= 7 && i <= end); // This is separated out into a different variable, so the i + 16 addition can be // performed at the start of the pipeline and the loop condition does not have // a dependency on the writes. nuint counter; do { counter = i + 16; // This loop looks very costly since there appear to be a bunch of temporary values // being created with the adds, but the jit (for x86 anyways) will convert each of // these to use memory addressing operands. // So the only cost is a bit of code size, which is made up for by the fact that // we save on writes to b. #if BIT64 Unsafe.As <byte, long>(ref Unsafe.AddByteOffset <byte>(ref b, i)) = 0; Unsafe.As <byte, long>(ref Unsafe.AddByteOffset <byte>(ref b, i + 8)) = 0; #else Unsafe.As <byte, int>(ref Unsafe.AddByteOffset <byte>(ref b, i)) = 0; Unsafe.As <byte, int>(ref Unsafe.AddByteOffset <byte>(ref b, i + 4)) = 0; Unsafe.As <byte, int>(ref Unsafe.AddByteOffset <byte>(ref b, i + 8)) = 0; Unsafe.As <byte, int>(ref Unsafe.AddByteOffset <byte>(ref b, i + 12)) = 0; #endif i = counter; // See notes above for why this wasn't used instead // i += 16; }while (counter <= end); if ((byteLength & 8) != 0) { #if BIT64 Unsafe.As <byte, long>(ref Unsafe.AddByteOffset <byte>(ref b, i)) = 0; #else Unsafe.As <byte, int>(ref Unsafe.AddByteOffset <byte>(ref b, i)) = 0; Unsafe.As <byte, int>(ref Unsafe.AddByteOffset <byte>(ref b, i + 4)) = 0; #endif i += 8; } if ((byteLength & 4) != 0) { Unsafe.As <byte, int>(ref Unsafe.AddByteOffset <byte>(ref b, i)) = 0; i += 4; } if ((byteLength & 2) != 0) { Unsafe.As <byte, short>(ref Unsafe.AddByteOffset <byte>(ref b, i)) = 0; i += 2; } if ((byteLength & 1) != 0) { Unsafe.AddByteOffset <byte>(ref b, i) = 0; // We're not using i after this, so not needed // i += 1; } return; #endif PInvoke: RuntimeImports.RhZeroMemory(ref b, byteLength); }
private static size_t CurlReceiveHeadersCallback(IntPtr buffer, size_t size, size_t nitems, IntPtr context) { CurlHandler.VerboseTrace("size: " + size + ", nitems: " + nitems); size *= nitems; if (size == 0) { return 0; } EasyRequest easy; if (TryGetEasyRequestFromContext(context, out easy)) { try { // The callback is invoked once per header; multi-line headers get merged into a single line. string responseHeader = Marshal.PtrToStringAnsi(buffer).Trim(); HttpResponseMessage response = easy._responseMessage; if (!TryParseStatusLine(response, responseHeader, easy)) { int colonIndex = responseHeader.IndexOf(':'); // Skip malformed header lines that are missing the colon character. if (colonIndex > 0) { string headerName = responseHeader.Substring(0, colonIndex); string headerValue = responseHeader.Substring(colonIndex + 1); if (!response.Headers.TryAddWithoutValidation(headerName, headerValue)) { response.Content.Headers.TryAddWithoutValidation(headerName, headerValue); } } } return size; } catch (Exception ex) { easy.FailRequest(ex); // cleanup will be handled by main processing loop } } // Returing a value other than size fails the callback and forces // request completion with an error return size - 1; }
public static unsafe void ClearWithReferences(ref IntPtr ip, nuint pointerSizeLength) { Debug.Assert((int)Unsafe.AsPointer(ref ip) % sizeof(IntPtr) == 0, "Should've been aligned on natural word boundary."); // First write backward 8 natural words at a time. // Writing backward allows us to get away with only simple modifications to the // mov instruction's base and index registers between loop iterations. for (; pointerSizeLength >= 8; pointerSizeLength -= 8) { Unsafe.Add(ref Unsafe.Add(ref ip, (IntPtr)pointerSizeLength), -1) = default; Unsafe.Add(ref Unsafe.Add(ref ip, (IntPtr)pointerSizeLength), -2) = default; Unsafe.Add(ref Unsafe.Add(ref ip, (IntPtr)pointerSizeLength), -3) = default; Unsafe.Add(ref Unsafe.Add(ref ip, (IntPtr)pointerSizeLength), -4) = default; Unsafe.Add(ref Unsafe.Add(ref ip, (IntPtr)pointerSizeLength), -5) = default; Unsafe.Add(ref Unsafe.Add(ref ip, (IntPtr)pointerSizeLength), -6) = default; Unsafe.Add(ref Unsafe.Add(ref ip, (IntPtr)pointerSizeLength), -7) = default; Unsafe.Add(ref Unsafe.Add(ref ip, (IntPtr)pointerSizeLength), -8) = default; } Debug.Assert(pointerSizeLength <= 7); // The logic below works by trying to minimize the number of branches taken for any // given range of lengths. For example, the lengths [ 4 .. 7 ] are handled by a single // branch, [ 2 .. 3 ] are handled by a single branch, and [ 1 ] is handled by a single // branch. // // We can write both forward and backward as a perf improvement. For example, // the lengths [ 4 .. 7 ] can be handled by zeroing out the first four natural // words and the last 3 natural words. In the best case (length = 7), there are // no overlapping writes. In the worst case (length = 4), there are three // overlapping writes near the middle of the buffer. In perf testing, the // penalty for performing duplicate writes is less expensive than the penalty // for complex branching. if (pointerSizeLength >= 4) { goto Write4To7; } else if (pointerSizeLength >= 2) { goto Write2To3; } else if (pointerSizeLength > 0) { goto Write1; } else { return; // nothing to write } Write4To7: Debug.Assert(pointerSizeLength >= 4); // Write first four and last three. Unsafe.Add(ref ip, 2) = default; Unsafe.Add(ref ip, 3) = default; Unsafe.Add(ref Unsafe.Add(ref ip, (IntPtr)pointerSizeLength), -3) = default; Unsafe.Add(ref Unsafe.Add(ref ip, (IntPtr)pointerSizeLength), -2) = default; Write2To3: Debug.Assert(pointerSizeLength >= 2); // Write first two and last one. Unsafe.Add(ref ip, 1) = default; Unsafe.Add(ref Unsafe.Add(ref ip, (IntPtr)pointerSizeLength), -1) = default; Write1: Debug.Assert(pointerSizeLength >= 1); // Write only element. ip = default; }
private static size_t CurlSendCallback(IntPtr buffer, size_t size, size_t nitems, IntPtr context) { CurlHandler.VerboseTrace("size: " + size + ", nitems: " + nitems); size *= nitems; if (size == 0) { return 0; } EasyRequest easy; if (TryGetEasyRequestFromContext(context, out easy)) { Debug.Assert(easy._requestContentStream != null, "We should only be in the send callback if we have a request content stream"); Debug.Assert(easy._associatedMultiAgent != null, "The request should be associated with a multi agent."); // Transfer data from the request's content stream to libcurl try { byte[] arr = easy._associatedMultiAgent._transferBuffer; int numBytes = easy._requestContentStream.Read(arr, 0, Math.Min(arr.Length, (int)size)); // TODO: Fix potential blocking here Debug.Assert(numBytes >= 0 && (ulong)numBytes <= size, "Read more bytes than requested"); if (numBytes > 0) { Marshal.Copy(arr, 0, buffer, numBytes); } return (size_t)numBytes; } catch (Exception ex) { easy.FailRequest(ex); // cleanup will be handled by main processing loop } } // Something went wrong. return CURLcode.CURLE_ABORTED_BY_CALLBACK; }
//---------------------------------------------------------------------------------------- public static int ToDO(cPosicion pos, cMov[] mlist, int mPos, bitbrd target, color us, type Type, cInfoJaque ci) { bool Checks = Type==cMovType.QUIET_CHECKS; mPos=Peones(pos, mlist, mPos, target, ci, us, Type); mPos=Piezas(pos, mlist, mPos, us, target, ci, cPieza.CABALLO, Checks); mPos=Piezas(pos, mlist, mPos, us, target, ci, cPieza.ALFIL, Checks); mPos=Piezas(pos, mlist, mPos, us, target, ci, cPieza.TORRE, Checks); mPos=Piezas(pos, mlist, mPos, us, target, ci, cPieza.DAMA, Checks); if(Type!=cMovType.QUIET_CHECKS&&Type!=cMovType.EVASIONS) { sq ksq = pos.GetRey(us); bitbrd b = pos.attacks_from_square_piecetype(ksq, cPieza.REY)⌖ while(b!=0) mlist[mPos++].m=cTypes.CreaMov(ksq, cBitBoard.GetLSB(ref b)); } if(Type!=cMovType.CAPTURES&&Type!=cMovType.EVASIONS&&pos.CanEnroque(us)!=0) { if (pos.IsChess960() != false) { mPos=Enroques(pos, mlist, mPos, us, ci, (new cEnroque(us, cEnroque.LADO_REY)).m_Tipo, Checks, true); mPos=Enroques(pos, mlist, mPos, us, ci, (new cEnroque(us, cEnroque.LADO_DAMA)).m_Tipo, Checks, true); } else { mPos=Enroques(pos, mlist, mPos, us, ci, (new cEnroque(us, cEnroque.LADO_REY)).m_Tipo, Checks, false); mPos=Enroques(pos, mlist, mPos, us, ci, (new cEnroque(us, cEnroque.LADO_DAMA)).m_Tipo, Checks, false); } } return mPos; }