Index: System.Data.SQLite/SQLiteModule.cs ================================================================== --- System.Data.SQLite/SQLiteModule.cs +++ System.Data.SQLite/SQLiteModule.cs @@ -3366,10 +3366,14 @@ /// The actual size, in bytes, of the memory block specified via the /// native pointer. /// public static int Size(IntPtr pMemory) { +#if DEBUG + SQLiteMarshal.CheckAlignment("Size", pMemory, 0, IntPtr.Size); +#endif + #if !SQLITE_STANDARD return UnsafeNativeMethods.sqlite3_malloc_size_interop(pMemory); #elif TRACK_MEMORY_BYTES // // HACK: Ok, we cannot determine the size of the memory block; @@ -3393,10 +3397,14 @@ /// The native pointer to the memory block previously obtained from the /// method. /// public static void Free(IntPtr pMemory) { +#if DEBUG + SQLiteMarshal.CheckAlignment("Free", pMemory, 0, IntPtr.Size); +#endif + #if TRACK_MEMORY_BYTES if (pMemory != IntPtr.Zero) { int blockSize = Size(pMemory); @@ -3425,10 +3433,15 @@ /// The native pointer to the memory block previously obtained from the /// SQLite core library. /// public static void FreeUntracked(IntPtr pMemory) { +#if DEBUG + SQLiteMarshal.CheckAlignment( + "FreeUntracked", pMemory, 0, IntPtr.Size); +#endif + UnsafeNativeMethods.sqlite3_free(pMemory); } #endregion } #endregion @@ -4028,11 +4041,11 @@ IntPtr pointer, int offset ) { #if DEBUG - CheckAlignment("ReadInt32", pointer, offset); + CheckAlignment("ReadInt32", pointer, offset, sizeof(int)); #endif #if !PLATFORM_COMPACTFRAMEWORK return Marshal.ReadInt32(pointer, offset); #else @@ -4061,11 +4074,11 @@ IntPtr pointer, int offset ) { #if DEBUG - CheckAlignment("ReadInt64", pointer, offset); + CheckAlignment("ReadInt64", pointer, offset, sizeof(long)); #endif #if !PLATFORM_COMPACTFRAMEWORK return Marshal.ReadInt64(pointer, offset); #else @@ -4094,11 +4107,11 @@ IntPtr pointer, int offset ) { #if DEBUG - CheckAlignment("ReadDouble", pointer, offset); + CheckAlignment("ReadDouble", pointer, offset, sizeof(double)); #endif #if !PLATFORM_COMPACTFRAMEWORK return BitConverter.Int64BitsToDouble(Marshal.ReadInt64( pointer, offset)); @@ -4129,11 +4142,11 @@ IntPtr pointer, int offset ) { #if DEBUG - CheckAlignment("ReadIntPtr", pointer, offset); + CheckAlignment("ReadIntPtr", pointer, offset, IntPtr.Size); #endif #if !PLATFORM_COMPACTFRAMEWORK return Marshal.ReadIntPtr(pointer, offset); #else @@ -4165,11 +4178,11 @@ int offset, int value ) { #if DEBUG - CheckAlignment("WriteInt32", pointer, offset); + CheckAlignment("WriteInt32", pointer, offset, sizeof(int)); #endif #if !PLATFORM_COMPACTFRAMEWORK Marshal.WriteInt32(pointer, offset, value); #else @@ -4199,11 +4212,11 @@ int offset, long value ) { #if DEBUG - CheckAlignment("WriteInt64", pointer, offset); + CheckAlignment("WriteInt64", pointer, offset, sizeof(long)); #endif #if !PLATFORM_COMPACTFRAMEWORK Marshal.WriteInt64(pointer, offset, value); #else @@ -4233,11 +4246,11 @@ int offset, double value ) { #if DEBUG - CheckAlignment("WriteDouble", pointer, offset); + CheckAlignment("WriteDouble", pointer, offset, sizeof(double)); #endif #if !PLATFORM_COMPACTFRAMEWORK Marshal.WriteInt64(pointer, offset, BitConverter.DoubleToInt64Bits(value)); @@ -4269,12 +4282,14 @@ int offset, IntPtr value ) { #if DEBUG - CheckAlignment("WriteIntPtr(pointer)", pointer, offset); - CheckAlignment("WriteIntPtr(value)", value, 0); + CheckAlignment( + "WriteIntPtr(pointer)", pointer, offset, IntPtr.Size); + + CheckAlignment("WriteIntPtr(value)", value, 0, IntPtr.Size); #endif #if !PLATFORM_COMPACTFRAMEWORK Marshal.WriteIntPtr(pointer, offset, value); #else @@ -4317,32 +4332,28 @@ /////////////////////////////////////////////////////////////////////// #region Private Methods #if DEBUG - private static void CheckAlignment( + internal static void CheckAlignment( string type, IntPtr pointer, - int offset - ) - { - if ((pointer.ToInt64() % IntPtr.Size) != 0) - { - SQLiteLog.LogMessage(SQLiteErrorCode.Warning, - HelperMethods.StringFormat( - CultureInfo.CurrentCulture, - "{0}: pointer {1} is not aligned to {2}: {3}", - type, pointer, IntPtr.Size, Environment.StackTrace)); - } - - if ((offset % IntPtr.Size) != 0) - { - SQLiteLog.LogMessage(SQLiteErrorCode.Warning, - HelperMethods.StringFormat( - CultureInfo.CurrentCulture, - "{0}: offset {1} is not aligned to {2}: {3}", - type, offset, IntPtr.Size, Environment.StackTrace)); + int offset, + int size + ) + { + IntPtr savedPointer = pointer; + + if (offset != 0) + pointer = new IntPtr(pointer.ToInt64() + offset); + + if ((pointer.ToInt64() % size) != 0) + { + SQLiteLog.LogMessage(SQLiteErrorCode.Warning, + HelperMethods.StringFormat(CultureInfo.CurrentCulture, + "{0}: pointer {1} and offset {2} not aligned to {3}: {4}", + type, savedPointer, offset, size, Environment.StackTrace)); } } #endif #endregion }