System.Data.SQLite
Check-in [7ca42fb6c0]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Correct and enhance the alignment diagnostics added by check-in [0621d7037c].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 7ca42fb6c0f655a65e56391d939f8af7cdece19e
User & Date: mistachkin 2018-01-15 18:18:20
Context
2018-01-15
19:22
In SetTableError, avoid logging null virtual table error messages. check-in: 2de277f554 user: mistachkin tags: trunk
18:18
Correct and enhance the alignment diagnostics added by check-in [0621d7037c]. check-in: 7ca42fb6c0 user: mistachkin tags: trunk
17:53
Add comments with the offsets of various native structure members. check-in: 2e6de1738c user: mistachkin tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to System.Data.SQLite/SQLiteModule.cs.

  3364   3364           /// </param>
  3365   3365           /// <returns>
  3366   3366           /// The actual size, in bytes, of the memory block specified via the
  3367   3367           /// native pointer.
  3368   3368           /// </returns>
  3369   3369           public static int Size(IntPtr pMemory)
  3370   3370           {
         3371  +#if DEBUG
         3372  +            SQLiteMarshal.CheckAlignment("Size", pMemory, 0, IntPtr.Size);
         3373  +#endif
         3374  +
  3371   3375   #if !SQLITE_STANDARD
  3372   3376               return UnsafeNativeMethods.sqlite3_malloc_size_interop(pMemory);
  3373   3377   #elif TRACK_MEMORY_BYTES
  3374   3378               //
  3375   3379               // HACK: Ok, we cannot determine the size of the memory block;
  3376   3380               //       therefore, just track number of allocations instead.
  3377   3381               //
................................................................................
  3391   3395           /// </summary>
  3392   3396           /// <param name="pMemory">
  3393   3397           /// The native pointer to the memory block previously obtained from the
  3394   3398           /// <see cref="Allocate" /> method.
  3395   3399           /// </param>
  3396   3400           public static void Free(IntPtr pMemory)
  3397   3401           {
         3402  +#if DEBUG
         3403  +            SQLiteMarshal.CheckAlignment("Free", pMemory, 0, IntPtr.Size);
         3404  +#endif
         3405  +
  3398   3406   #if TRACK_MEMORY_BYTES
  3399   3407               if (pMemory != IntPtr.Zero)
  3400   3408               {
  3401   3409                   int blockSize = Size(pMemory);
  3402   3410   
  3403   3411                   if (blockSize > 0)
  3404   3412                   {
................................................................................
  3423   3431           /// </summary>
  3424   3432           /// <param name="pMemory">
  3425   3433           /// The native pointer to the memory block previously obtained from the
  3426   3434           /// SQLite core library.
  3427   3435           /// </param>
  3428   3436           public static void FreeUntracked(IntPtr pMemory)
  3429   3437           {
         3438  +#if DEBUG
         3439  +            SQLiteMarshal.CheckAlignment(
         3440  +                "FreeUntracked", pMemory, 0, IntPtr.Size);
         3441  +#endif
         3442  +
  3430   3443               UnsafeNativeMethods.sqlite3_free(pMemory);
  3431   3444           }
  3432   3445           #endregion
  3433   3446       }
  3434   3447       #endregion
  3435   3448   
  3436   3449       ///////////////////////////////////////////////////////////////////////////
................................................................................
  4026   4039           /// </returns>
  4027   4040           public static int ReadInt32(
  4028   4041               IntPtr pointer,
  4029   4042               int offset
  4030   4043               )
  4031   4044           {
  4032   4045   #if DEBUG
  4033         -            CheckAlignment("ReadInt32", pointer, offset);
         4046  +            CheckAlignment("ReadInt32", pointer, offset, sizeof(int));
  4034   4047   #endif
  4035   4048   
  4036   4049   #if !PLATFORM_COMPACTFRAMEWORK
  4037   4050               return Marshal.ReadInt32(pointer, offset);
  4038   4051   #else
  4039   4052               return Marshal.ReadInt32(IntPtrForOffset(pointer, offset));
  4040   4053   #endif
................................................................................
  4059   4072           /// </returns>
  4060   4073           public static long ReadInt64(
  4061   4074               IntPtr pointer,
  4062   4075               int offset
  4063   4076               )
  4064   4077           {
  4065   4078   #if DEBUG
  4066         -            CheckAlignment("ReadInt64", pointer, offset);
         4079  +            CheckAlignment("ReadInt64", pointer, offset, sizeof(long));
  4067   4080   #endif
  4068   4081   
  4069   4082   #if !PLATFORM_COMPACTFRAMEWORK
  4070   4083               return Marshal.ReadInt64(pointer, offset);
  4071   4084   #else
  4072   4085               return Marshal.ReadInt64(IntPtrForOffset(pointer, offset));
  4073   4086   #endif
................................................................................
  4092   4105           /// </returns>
  4093   4106           public static double ReadDouble(
  4094   4107               IntPtr pointer,
  4095   4108               int offset
  4096   4109               )
  4097   4110           {
  4098   4111   #if DEBUG
  4099         -            CheckAlignment("ReadDouble", pointer, offset);
         4112  +            CheckAlignment("ReadDouble", pointer, offset, sizeof(double));
  4100   4113   #endif
  4101   4114   
  4102   4115   #if !PLATFORM_COMPACTFRAMEWORK
  4103   4116               return BitConverter.Int64BitsToDouble(Marshal.ReadInt64(
  4104   4117                   pointer, offset));
  4105   4118   #else
  4106   4119               return BitConverter.ToDouble(BitConverter.GetBytes(
................................................................................
  4127   4140           /// </returns>
  4128   4141           public static IntPtr ReadIntPtr(
  4129   4142               IntPtr pointer,
  4130   4143               int offset
  4131   4144               )
  4132   4145           {
  4133   4146   #if DEBUG
  4134         -            CheckAlignment("ReadIntPtr", pointer, offset);
         4147  +            CheckAlignment("ReadIntPtr", pointer, offset, IntPtr.Size);
  4135   4148   #endif
  4136   4149   
  4137   4150   #if !PLATFORM_COMPACTFRAMEWORK
  4138   4151               return Marshal.ReadIntPtr(pointer, offset);
  4139   4152   #else
  4140   4153               return Marshal.ReadIntPtr(IntPtrForOffset(pointer, offset));
  4141   4154   #endif
................................................................................
  4163   4176           public static void WriteInt32(
  4164   4177               IntPtr pointer,
  4165   4178               int offset,
  4166   4179               int value
  4167   4180               )
  4168   4181           {
  4169   4182   #if DEBUG
  4170         -            CheckAlignment("WriteInt32", pointer, offset);
         4183  +            CheckAlignment("WriteInt32", pointer, offset, sizeof(int));
  4171   4184   #endif
  4172   4185   
  4173   4186   #if !PLATFORM_COMPACTFRAMEWORK
  4174   4187               Marshal.WriteInt32(pointer, offset, value);
  4175   4188   #else
  4176   4189               Marshal.WriteInt32(IntPtrForOffset(pointer, offset), value);
  4177   4190   #endif
................................................................................
  4197   4210           public static void WriteInt64(
  4198   4211               IntPtr pointer,
  4199   4212               int offset,
  4200   4213               long value
  4201   4214               )
  4202   4215           {
  4203   4216   #if DEBUG
  4204         -            CheckAlignment("WriteInt64", pointer, offset);
         4217  +            CheckAlignment("WriteInt64", pointer, offset, sizeof(long));
  4205   4218   #endif
  4206   4219   
  4207   4220   #if !PLATFORM_COMPACTFRAMEWORK
  4208   4221               Marshal.WriteInt64(pointer, offset, value);
  4209   4222   #else
  4210   4223               Marshal.WriteInt64(IntPtrForOffset(pointer, offset), value);
  4211   4224   #endif
................................................................................
  4231   4244           public static void WriteDouble(
  4232   4245               IntPtr pointer,
  4233   4246               int offset,
  4234   4247               double value
  4235   4248               )
  4236   4249           {
  4237   4250   #if DEBUG
  4238         -            CheckAlignment("WriteDouble", pointer, offset);
         4251  +            CheckAlignment("WriteDouble", pointer, offset, sizeof(double));
  4239   4252   #endif
  4240   4253   
  4241   4254   #if !PLATFORM_COMPACTFRAMEWORK
  4242   4255               Marshal.WriteInt64(pointer, offset,
  4243   4256                   BitConverter.DoubleToInt64Bits(value));
  4244   4257   #else
  4245   4258               Marshal.WriteInt64(IntPtrForOffset(pointer, offset),
................................................................................
  4267   4280           public static void WriteIntPtr(
  4268   4281               IntPtr pointer,
  4269   4282               int offset,
  4270   4283               IntPtr value
  4271   4284               )
  4272   4285           {
  4273   4286   #if DEBUG
  4274         -            CheckAlignment("WriteIntPtr(pointer)", pointer, offset);
  4275         -            CheckAlignment("WriteIntPtr(value)", value, 0);
         4287  +            CheckAlignment(
         4288  +                "WriteIntPtr(pointer)", pointer, offset, IntPtr.Size);
         4289  +
         4290  +            CheckAlignment("WriteIntPtr(value)", value, 0, IntPtr.Size);
  4276   4291   #endif
  4277   4292   
  4278   4293   #if !PLATFORM_COMPACTFRAMEWORK
  4279   4294               Marshal.WriteIntPtr(pointer, offset, value);
  4280   4295   #else
  4281   4296               Marshal.WriteIntPtr(IntPtrForOffset(pointer, offset), value);
  4282   4297   #endif
................................................................................
  4315   4330           }
  4316   4331           #endregion
  4317   4332   
  4318   4333           ///////////////////////////////////////////////////////////////////////
  4319   4334   
  4320   4335           #region Private Methods
  4321   4336   #if DEBUG
  4322         -        private static void CheckAlignment(
         4337  +        internal static void CheckAlignment(
  4323   4338               string type,
  4324   4339               IntPtr pointer,
  4325         -            int offset
         4340  +            int offset,
         4341  +            int size
  4326   4342               )
  4327   4343           {
  4328         -            if ((pointer.ToInt64() % IntPtr.Size) != 0)
  4329         -            {
  4330         -                SQLiteLog.LogMessage(SQLiteErrorCode.Warning,
  4331         -                    HelperMethods.StringFormat(
  4332         -                    CultureInfo.CurrentCulture,
  4333         -                    "{0}: pointer {1} is not aligned to {2}: {3}",
  4334         -                    type, pointer, IntPtr.Size, Environment.StackTrace));
  4335         -            }
         4344  +            IntPtr savedPointer = pointer;
  4336   4345   
  4337         -            if ((offset % IntPtr.Size) != 0)
         4346  +            if (offset != 0)
         4347  +                pointer = new IntPtr(pointer.ToInt64() + offset);
         4348  +
         4349  +            if ((pointer.ToInt64() % size) != 0)
  4338   4350               {
  4339   4351                   SQLiteLog.LogMessage(SQLiteErrorCode.Warning,
  4340         -                    HelperMethods.StringFormat(
  4341         -                    CultureInfo.CurrentCulture,
  4342         -                    "{0}: offset {1} is not aligned to {2}: {3}",
  4343         -                    type, offset, IntPtr.Size, Environment.StackTrace));
         4352  +                    HelperMethods.StringFormat(CultureInfo.CurrentCulture,
         4353  +                    "{0}: pointer {1} and offset {2} not aligned to {3}: {4}",
         4354  +                    type, savedPointer, offset, size, Environment.StackTrace));
  4344   4355               }
  4345   4356           }
  4346   4357   #endif
  4347   4358           #endregion
  4348   4359       }
  4349   4360       #endregion
  4350   4361