Browse Source

Fix unaligned accesses

pull/8529/head
petris 4 years ago
parent
commit
c6e12a6e32
No known key found for this signature in database GPG Key ID: CEA06F305F42279F
  1. 6
      src/Avalonia.Base/Rendering/Composition/Transport/BatchStream.cs
  2. 3
      src/Avalonia.X11/X11Structs.cs

6
src/Avalonia.Base/Rendering/Composition/Transport/BatchStream.cs

@ -72,7 +72,7 @@ internal class BatchStreamWriter : IDisposable
var size = Unsafe.SizeOf<T>();
if (_currentDataSegment.Data == IntPtr.Zero || _currentDataSegment.ElementCount + size > _memoryPool.BufferSize)
NextDataSegment();
*(T*)((byte*)_currentDataSegment.Data + _currentDataSegment.ElementCount) = item;
Unsafe.WriteUnaligned<T>((byte*)_currentDataSegment.Data + _currentDataSegment.ElementCount, item);
_currentDataSegment.ElementCount += size;
}
@ -123,7 +123,7 @@ internal class BatchStreamReader : IDisposable
if (_memoryOffset + size > _currentDataSegment.ElementCount)
throw new InvalidOperationException("Attempted to read more memory then left in the current segment");
var rv = *(T*)((byte*)_currentDataSegment.Data + _memoryOffset);
var rv = Unsafe.ReadUnaligned<T>((byte*)_currentDataSegment.Data + _memoryOffset);
_memoryOffset += size;
if (_memoryOffset == _currentDataSegment.ElementCount)
{
@ -181,4 +181,4 @@ internal class BatchStreamReader : IDisposable
while (_input.Objects.Count > 0)
_objectPool.Return(_input.Objects.Dequeue().Data);
}
}
}

3
src/Avalonia.X11/X11Structs.cs

@ -32,6 +32,7 @@ using System.Collections;
using System.Drawing;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// ReSharper disable FieldCanBeMadeReadOnly.Global
// ReSharper disable IdentifierTypo
@ -545,7 +546,7 @@ namespace Avalonia.X11 {
{
if (data == null)
throw new InvalidOperationException();
return *(T*)data;
return Unsafe.ReadUnaligned<T>(data);
}
}

Loading…
Cancel
Save