System.Data.SQLite
Check-in [52d29df7aa]
Not logged in

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

Overview
Comment:Fix an incorrect memory allocation count in the SQLiteIndex class for SQLite memory freed without using the SQLiteMemory class. Only debug builds with TRACK_MEMORY_BYTES defined were impacted.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 52d29df7aa8700fc23a04b047ec210b38c82690b
User & Date: mistachkin 2017-11-07 03:33:37
Context
2017-11-07
03:38
Update a doc comment. check-in: 67190e9b52 user: mistachkin tags: trunk
03:33
Fix an incorrect memory allocation count in the SQLiteIndex class for SQLite memory freed without using the SQLiteMemory class. Only debug builds with TRACK_MEMORY_BYTES defined were impacted. check-in: 52d29df7aa user: mistachkin tags: trunk
01:42
Fix incorrect memory allocation counts in the SQLiteSession class for SQLite memory obtained without using the SQLiteMemory class. Only debug builds with TRACK_MEMORY_BYTES defined were impacted. check-in: 395909320d user: mistachkin tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

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

1981
1982
1983
1984
1985
1986
1987
1988

1989
1990
1991
1992
1993
1994
1995
....
3323
3324
3325
3326
3327
3328
3329























3330
3331
3332
3333
3334
3335
3336
....
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
....
3593
3594
3595
3596
3597
3598
3599
3600



























3601
3602

























3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615





3616
3617
3618
3619
3620
3621
3622
3623
3624

3625
3626
3627
3628
3629
3630
3631
....
3632
3633
3634
3635
3636
3637
3638

3639


3640
3641
3642
3643
3644
3645
3646
....
3697
3698
3699
3700
3701
3702
3703





3704
3705
3706
3707
3708
3709

3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
            SQLiteMarshal.WriteInt32(pIndex, offset,
                outputs.IndexNumber);

            offset = SQLiteMarshal.NextOffsetOf(
                offset, sizeof(int), IntPtr.Size);

            SQLiteMarshal.WriteIntPtr(pIndex, offset,
                SQLiteString.Utf8IntPtrFromString(outputs.IndexString));


            offset = SQLiteMarshal.NextOffsetOf(
                offset, IntPtr.Size, sizeof(int));

            //
            // NOTE: We just allocated the IndexString field; therefore, we
            //       need to set make sure the NeedToFreeIndexString field
................................................................................
#endif

            return pMemory;
        }

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
























        /// <summary>
        /// Gets and returns the actual size of the specified memory block that
        /// was previously obtained from the <see cref="Allocate" /> method or
        /// the SQLite core library.
        /// </summary>
        /// <param name="pMemory">
        /// The native pointer to the memory block previously obtained from the
................................................................................
            UnsafeNativeMethods.sqlite3_free(pMemory);
        }

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

        /// <summary>
        /// Frees a memory block previously obtained from the SQLite core
        /// library without adjusting the number of bytes currently allocated
        /// by this class.  This is useful when dealing with blocks of memory
        /// that were not allocated using this class.
        /// </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)
        {
................................................................................
        /// <returns>
        /// The native NUL-terminated UTF-8 string pointer or
        /// <see cref="IntPtr.Zero" /> upon failure.
        /// </returns>
        public static IntPtr Utf8IntPtrFromString(
            string value
            )
        {



























            int length = 0;


























            return Utf8IntPtrFromString(value, ref length);
        }

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

        /// <summary>
        /// Converts the specified managed string into a native NUL-terminated
        /// UTF-8 string pointer using memory obtained from the SQLite core
        /// library.
        /// </summary>
        /// <param name="value">
        /// The managed string to convert.
        /// </param>





        /// <param name="length">
        /// The length of the native string, in bytes.
        /// </param>
        /// <returns>
        /// The native NUL-terminated UTF-8 string pointer or
        /// <see cref="IntPtr.Zero" /> upon failure.
        /// </returns>
        public static IntPtr Utf8IntPtrFromString(
            string value,

            ref int length
            )
        {
            if (value == null)
                return IntPtr.Zero;

            IntPtr result = IntPtr.Zero;
................................................................................
            byte[] bytes = GetUtf8BytesFromString(value);

            if (bytes == null)
                return IntPtr.Zero;

            length = bytes.Length;


            result = SQLiteMemory.Allocate(length + 1);



            if (result == IntPtr.Zero)
                return IntPtr.Zero;

            Marshal.Copy(bytes, 0, result, length);
            Marshal.WriteByte(result, length, 0);

................................................................................
        /// <summary>
        /// Converts an array of managed strings into an array of native
        /// NUL-terminated UTF-8 string pointers.
        /// </summary>
        /// <param name="values">
        /// The array of managed strings to convert.
        /// </param>





        /// <returns>
        /// The array of native NUL-terminated UTF-8 string pointers or null
        /// upon failure.
        /// </returns>
        public static IntPtr[] Utf8IntPtrArrayFromStringArray(
            string[] values

            )
        {
            if (values == null)
                return null;

            IntPtr[] result = new IntPtr[values.Length];

            for (int index = 0; index < result.Length; index++)
                result[index] = Utf8IntPtrFromString(values[index]);

            return result;
        }
        #endregion
    }
    #endregion








|
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
|
|







 








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|












>
>
>
>
>









>







 







>
|
>
>







 







>
>
>
>
>





|
>








|







1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
....
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
....
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
....
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
....
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
....
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
            SQLiteMarshal.WriteInt32(pIndex, offset,
                outputs.IndexNumber);

            offset = SQLiteMarshal.NextOffsetOf(
                offset, sizeof(int), IntPtr.Size);

            SQLiteMarshal.WriteIntPtr(pIndex, offset,
                SQLiteString.Utf8IntPtrFromString(
                    outputs.IndexString, false)); /* OK: FREED BY CORE*/

            offset = SQLiteMarshal.NextOffsetOf(
                offset, IntPtr.Size, sizeof(int));

            //
            // NOTE: We just allocated the IndexString field; therefore, we
            //       need to set make sure the NeedToFreeIndexString field
................................................................................
#endif

            return pMemory;
        }

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

        /// <summary>
        /// Allocates at least the specified number of bytes of native memory
        /// via the SQLite core library sqlite3_malloc() function and returns
        /// the resulting native pointer without adjusting the number of
        /// allocated bytes currently tracked by this class.  This is useful
        /// when dealing with blocks of memory that will be freed directly by
        /// the SQLite core library.
        /// </summary>
        /// <param name="size">
        /// The number of bytes to allocate.
        /// </param>
        /// <returns>
        /// The native pointer that points to a block of memory of at least the
        /// specified size -OR- <see cref="IntPtr.Zero" /> if the memory could
        /// not be allocated.
        /// </returns>
        public static IntPtr AllocateUntracked(int size)
        {
            return UnsafeNativeMethods.sqlite3_malloc(size);
        }

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

        /// <summary>
        /// Gets and returns the actual size of the specified memory block that
        /// was previously obtained from the <see cref="Allocate" /> method or
        /// the SQLite core library.
        /// </summary>
        /// <param name="pMemory">
        /// The native pointer to the memory block previously obtained from the
................................................................................
            UnsafeNativeMethods.sqlite3_free(pMemory);
        }

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

        /// <summary>
        /// Frees a memory block previously obtained from the SQLite core
        /// library without adjusting the number of allocated bytes currently
        /// tracked by this class.  This is useful when dealing with blocks of
        /// memory that were not allocated using this class.
        /// </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)
        {
................................................................................
        /// <returns>
        /// The native NUL-terminated UTF-8 string pointer or
        /// <see cref="IntPtr.Zero" /> upon failure.
        /// </returns>
        public static IntPtr Utf8IntPtrFromString(
            string value
            )
        {
            return Utf8IntPtrFromString(value, true);
        }

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

        /// <summary>
        /// Converts the specified managed string into a native NUL-terminated
        /// UTF-8 string pointer using memory obtained from the SQLite core
        /// library.
        /// </summary>
        /// <param name="value">
        /// The managed string to convert.
        /// </param>
        /// <param name="tracked">
        /// Non-zero to obtain memory from the SQLite core library without
        /// adjusting the number of allocated bytes currently being tracked
        /// by the <see cref="SQLiteMemory" /> class.
        /// </param>
        /// <returns>
        /// The native NUL-terminated UTF-8 string pointer or
        /// <see cref="IntPtr.Zero" /> upon failure.
        /// </returns>
        public static IntPtr Utf8IntPtrFromString(
            string value,
            bool tracked
            )
        {
            int length = 0;

            return Utf8IntPtrFromString(value, tracked, ref length);
        }

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

        /// <summary>
        /// Converts the specified managed string into a native NUL-terminated
        /// UTF-8 string pointer using memory obtained from the SQLite core
        /// library.
        /// </summary>
        /// <param name="value">
        /// The managed string to convert.
        /// </param>
        /// <param name="length">
        /// The length of the native string, in bytes.
        /// </param>
        /// <returns>
        /// The native NUL-terminated UTF-8 string pointer or
        /// <see cref="IntPtr.Zero" /> upon failure.
        /// </returns>
        public static IntPtr Utf8IntPtrFromString(
            string value,
            ref int length
            )
        {
            return Utf8IntPtrFromString(value, true, ref length);
        }

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

        /// <summary>
        /// Converts the specified managed string into a native NUL-terminated
        /// UTF-8 string pointer using memory obtained from the SQLite core
        /// library.
        /// </summary>
        /// <param name="value">
        /// The managed string to convert.
        /// </param>
        /// <param name="tracked">
        /// Non-zero to obtain memory from the SQLite core library without
        /// adjusting the number of allocated bytes currently being tracked
        /// by the <see cref="SQLiteMemory" /> class.
        /// </param>
        /// <param name="length">
        /// The length of the native string, in bytes.
        /// </param>
        /// <returns>
        /// The native NUL-terminated UTF-8 string pointer or
        /// <see cref="IntPtr.Zero" /> upon failure.
        /// </returns>
        public static IntPtr Utf8IntPtrFromString(
            string value,
            bool tracked,
            ref int length
            )
        {
            if (value == null)
                return IntPtr.Zero;

            IntPtr result = IntPtr.Zero;
................................................................................
            byte[] bytes = GetUtf8BytesFromString(value);

            if (bytes == null)
                return IntPtr.Zero;

            length = bytes.Length;

            if (tracked)
                result = SQLiteMemory.Allocate(length + 1);
            else
                result = SQLiteMemory.AllocateUntracked(length + 1);

            if (result == IntPtr.Zero)
                return IntPtr.Zero;

            Marshal.Copy(bytes, 0, result, length);
            Marshal.WriteByte(result, length, 0);

................................................................................
        /// <summary>
        /// Converts an array of managed strings into an array of native
        /// NUL-terminated UTF-8 string pointers.
        /// </summary>
        /// <param name="values">
        /// The array of managed strings to convert.
        /// </param>
        /// <param name="tracked">
        /// Non-zero to obtain memory from the SQLite core library without
        /// adjusting the number of allocated bytes currently being tracked
        /// by the <see cref="SQLiteMemory" /> class.
        /// </param>
        /// <returns>
        /// The array of native NUL-terminated UTF-8 string pointers or null
        /// upon failure.
        /// </returns>
        public static IntPtr[] Utf8IntPtrArrayFromStringArray(
            string[] values,
            bool tracked
            )
        {
            if (values == null)
                return null;

            IntPtr[] result = new IntPtr[values.Length];

            for (int index = 0; index < result.Length; index++)
                result[index] = Utf8IntPtrFromString(values[index], tracked);

            return result;
        }
        #endregion
    }
    #endregion