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 Unified Diffs Show Whitespace Changes Patch

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

3364
3365
3366
3367
3368
3369
3370




3371
3372
3373
3374
3375
3376
3377
....
3391
3392
3393
3394
3395
3396
3397




3398
3399
3400
3401
3402
3403
3404
....
3423
3424
3425
3426
3427
3428
3429





3430
3431
3432
3433
3434
3435
3436
....
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
....
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
....
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
....
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
....
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
....
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
....
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
....
4267
4268
4269
4270
4271
4272
4273

4274

4275
4276
4277
4278
4279
4280
4281
4282
....
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325

4326
4327
4328

4329
4330
4331
4332
4333
4334
4335

4336
4337
4338
4339
4340
4341
4342

4343
4344
4345
4346
4347
4348
4349
4350
        /// </param>
        /// <returns>
        /// The actual size, in bytes, of the memory block specified via the
        /// native pointer.
        /// </returns>
        public static int Size(IntPtr pMemory)
        {




#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;
            //       therefore, just track number of allocations instead.
            //
................................................................................
        /// </summary>
        /// <param name="pMemory">
        /// The native pointer to the memory block previously obtained from the
        /// <see cref="Allocate" /> method.
        /// </param>
        public static void Free(IntPtr pMemory)
        {




#if TRACK_MEMORY_BYTES
            if (pMemory != IntPtr.Zero)
            {
                int blockSize = Size(pMemory);

                if (blockSize > 0)
                {
................................................................................
        /// </summary>
        /// <param name="pMemory">
        /// The native pointer to the memory block previously obtained from the
        /// SQLite core library.
        /// </param>
        public static void FreeUntracked(IntPtr pMemory)
        {





            UnsafeNativeMethods.sqlite3_free(pMemory);
        }
        #endregion
    }
    #endregion

    ///////////////////////////////////////////////////////////////////////////
................................................................................
        /// </returns>
        public static int ReadInt32(
            IntPtr pointer,
            int offset
            )
        {
#if DEBUG
            CheckAlignment("ReadInt32", pointer, offset);
#endif

#if !PLATFORM_COMPACTFRAMEWORK
            return Marshal.ReadInt32(pointer, offset);
#else
            return Marshal.ReadInt32(IntPtrForOffset(pointer, offset));
#endif
................................................................................
        /// </returns>
        public static long ReadInt64(
            IntPtr pointer,
            int offset
            )
        {
#if DEBUG
            CheckAlignment("ReadInt64", pointer, offset);
#endif

#if !PLATFORM_COMPACTFRAMEWORK
            return Marshal.ReadInt64(pointer, offset);
#else
            return Marshal.ReadInt64(IntPtrForOffset(pointer, offset));
#endif
................................................................................
        /// </returns>
        public static double ReadDouble(
            IntPtr pointer,
            int offset
            )
        {
#if DEBUG
            CheckAlignment("ReadDouble", pointer, offset);
#endif

#if !PLATFORM_COMPACTFRAMEWORK
            return BitConverter.Int64BitsToDouble(Marshal.ReadInt64(
                pointer, offset));
#else
            return BitConverter.ToDouble(BitConverter.GetBytes(
................................................................................
        /// </returns>
        public static IntPtr ReadIntPtr(
            IntPtr pointer,
            int offset
            )
        {
#if DEBUG
            CheckAlignment("ReadIntPtr", pointer, offset);
#endif

#if !PLATFORM_COMPACTFRAMEWORK
            return Marshal.ReadIntPtr(pointer, offset);
#else
            return Marshal.ReadIntPtr(IntPtrForOffset(pointer, offset));
#endif
................................................................................
        public static void WriteInt32(
            IntPtr pointer,
            int offset,
            int value
            )
        {
#if DEBUG
            CheckAlignment("WriteInt32", pointer, offset);
#endif

#if !PLATFORM_COMPACTFRAMEWORK
            Marshal.WriteInt32(pointer, offset, value);
#else
            Marshal.WriteInt32(IntPtrForOffset(pointer, offset), value);
#endif
................................................................................
        public static void WriteInt64(
            IntPtr pointer,
            int offset,
            long value
            )
        {
#if DEBUG
            CheckAlignment("WriteInt64", pointer, offset);
#endif

#if !PLATFORM_COMPACTFRAMEWORK
            Marshal.WriteInt64(pointer, offset, value);
#else
            Marshal.WriteInt64(IntPtrForOffset(pointer, offset), value);
#endif
................................................................................
        public static void WriteDouble(
            IntPtr pointer,
            int offset,
            double value
            )
        {
#if DEBUG
            CheckAlignment("WriteDouble", pointer, offset);
#endif

#if !PLATFORM_COMPACTFRAMEWORK
            Marshal.WriteInt64(pointer, offset,
                BitConverter.DoubleToInt64Bits(value));
#else
            Marshal.WriteInt64(IntPtrForOffset(pointer, offset),
................................................................................
        public static void WriteIntPtr(
            IntPtr pointer,
            int offset,
            IntPtr value
            )
        {
#if DEBUG

            CheckAlignment("WriteIntPtr(pointer)", pointer, offset);

            CheckAlignment("WriteIntPtr(value)", value, 0);
#endif

#if !PLATFORM_COMPACTFRAMEWORK
            Marshal.WriteIntPtr(pointer, offset, value);
#else
            Marshal.WriteIntPtr(IntPtrForOffset(pointer, offset), value);
#endif
................................................................................
        }
        #endregion

        ///////////////////////////////////////////////////////////////////////

        #region Private Methods
#if DEBUG
        private 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));
            }
        }
#endif
        #endregion
    }
    #endregion








>
>
>
>







 







>
>
>
>







 







>
>
>
>
>







 







|







 







|







 







|







 







|







 







|







 







|







 







|







 







>
|
>
|







 







|


|
>


<
>
|
<
|
|
<
<
|
>
|
<
<

|
<
<
>
|







3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
....
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
....
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
....
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
....
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
....
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
....
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
....
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
....
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
....
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
....
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
....
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343

4344
4345

4346
4347


4348
4349
4350


4351
4352


4353
4354
4355
4356
4357
4358
4359
4360
4361
        /// </param>
        /// <returns>
        /// The actual size, in bytes, of the memory block specified via the
        /// native pointer.
        /// </returns>
        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;
            //       therefore, just track number of allocations instead.
            //
................................................................................
        /// </summary>
        /// <param name="pMemory">
        /// The native pointer to the memory block previously obtained from the
        /// <see cref="Allocate" /> method.
        /// </param>
        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);

                if (blockSize > 0)
                {
................................................................................
        /// </summary>
        /// <param name="pMemory">
        /// The native pointer to the memory block previously obtained from the
        /// SQLite core library.
        /// </param>
        public static void FreeUntracked(IntPtr pMemory)
        {
#if DEBUG
            SQLiteMarshal.CheckAlignment(
                "FreeUntracked", pMemory, 0, IntPtr.Size);
#endif

            UnsafeNativeMethods.sqlite3_free(pMemory);
        }
        #endregion
    }
    #endregion

    ///////////////////////////////////////////////////////////////////////////
................................................................................
        /// </returns>
        public static int ReadInt32(
            IntPtr pointer,
            int offset
            )
        {
#if DEBUG
            CheckAlignment("ReadInt32", pointer, offset, sizeof(int));
#endif

#if !PLATFORM_COMPACTFRAMEWORK
            return Marshal.ReadInt32(pointer, offset);
#else
            return Marshal.ReadInt32(IntPtrForOffset(pointer, offset));
#endif
................................................................................
        /// </returns>
        public static long ReadInt64(
            IntPtr pointer,
            int offset
            )
        {
#if DEBUG
            CheckAlignment("ReadInt64", pointer, offset, sizeof(long));
#endif

#if !PLATFORM_COMPACTFRAMEWORK
            return Marshal.ReadInt64(pointer, offset);
#else
            return Marshal.ReadInt64(IntPtrForOffset(pointer, offset));
#endif
................................................................................
        /// </returns>
        public static double ReadDouble(
            IntPtr pointer,
            int offset
            )
        {
#if DEBUG
            CheckAlignment("ReadDouble", pointer, offset, sizeof(double));
#endif

#if !PLATFORM_COMPACTFRAMEWORK
            return BitConverter.Int64BitsToDouble(Marshal.ReadInt64(
                pointer, offset));
#else
            return BitConverter.ToDouble(BitConverter.GetBytes(
................................................................................
        /// </returns>
        public static IntPtr ReadIntPtr(
            IntPtr pointer,
            int offset
            )
        {
#if DEBUG
            CheckAlignment("ReadIntPtr", pointer, offset, IntPtr.Size);
#endif

#if !PLATFORM_COMPACTFRAMEWORK
            return Marshal.ReadIntPtr(pointer, offset);
#else
            return Marshal.ReadIntPtr(IntPtrForOffset(pointer, offset));
#endif
................................................................................
        public static void WriteInt32(
            IntPtr pointer,
            int offset,
            int value
            )
        {
#if DEBUG
            CheckAlignment("WriteInt32", pointer, offset, sizeof(int));
#endif

#if !PLATFORM_COMPACTFRAMEWORK
            Marshal.WriteInt32(pointer, offset, value);
#else
            Marshal.WriteInt32(IntPtrForOffset(pointer, offset), value);
#endif
................................................................................
        public static void WriteInt64(
            IntPtr pointer,
            int offset,
            long value
            )
        {
#if DEBUG
            CheckAlignment("WriteInt64", pointer, offset, sizeof(long));
#endif

#if !PLATFORM_COMPACTFRAMEWORK
            Marshal.WriteInt64(pointer, offset, value);
#else
            Marshal.WriteInt64(IntPtrForOffset(pointer, offset), value);
#endif
................................................................................
        public static void WriteDouble(
            IntPtr pointer,
            int offset,
            double value
            )
        {
#if DEBUG
            CheckAlignment("WriteDouble", pointer, offset, sizeof(double));
#endif

#if !PLATFORM_COMPACTFRAMEWORK
            Marshal.WriteInt64(pointer, offset,
                BitConverter.DoubleToInt64Bits(value));
#else
            Marshal.WriteInt64(IntPtrForOffset(pointer, offset),
................................................................................
        public static void WriteIntPtr(
            IntPtr pointer,
            int offset,
            IntPtr value
            )
        {
#if DEBUG
            CheckAlignment(
                "WriteIntPtr(pointer)", pointer, offset, IntPtr.Size);

            CheckAlignment("WriteIntPtr(value)", value, 0, IntPtr.Size);
#endif

#if !PLATFORM_COMPACTFRAMEWORK
            Marshal.WriteIntPtr(pointer, offset, value);
#else
            Marshal.WriteIntPtr(IntPtrForOffset(pointer, offset), value);
#endif
................................................................................
        }
        #endregion

        ///////////////////////////////////////////////////////////////////////

        #region Private Methods
#if DEBUG
        internal static void CheckAlignment(
            string type,
            IntPtr pointer,
            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
    }
    #endregion