System.Data.SQLite
Check-in [c16bb56cfd]
Not logged in

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

Overview
Comment:Move the native memory allocation wrapper methods into the new SQLiteMemory class.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | virtualTables
Files: files | file ages | folders
SHA1: c16bb56cfd403ee3698c53b4f871f9ae7f57e220
User & Date: mistachkin 2013-06-21 05:39:00
Context
2013-06-21
06:06
Modularize various aspects of the virtual table handling in the example SQLiteVirtualTableCursorEnumerable class. check-in: 5033bacee1 user: mistachkin tags: virtualTables
05:39
Move the native memory allocation wrapper methods into the new SQLiteMemory class. check-in: c16bb56cfd user: mistachkin tags: virtualTables
05:14
Fix memory leak in the SQLiteModuleBase class. Add support for optionally tracking memory usage of the SQLiteMarshal class. Make sure sqlite3_*_interop() functions are only used when SQLITE_STANDARD is not defined. check-in: 4aab5f9721 user: mistachkin tags: virtualTables
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to SQLite.NET.Settings.targets.

   196    196                 These counts are intended to be used by the test suite to detect
   197    197                 possible resource leaks.
   198    198       -->
   199    199       <CountHandle Condition="'$(CountHandle)' == ''">false</CountHandle>
   200    200   
   201    201       <!--
   202    202           NOTE: Enable tracking of all outstanding bytes allocated by the
   203         -              SQLiteMarshal class.  By default, this is disabled.  If this is
          203  +              SQLiteMemory class.  By default, this is disabled.  If this is
   204    204                 enabled, this library will keep track of all outstanding bytes
   205         -              allocated by the SQLiteMarshal class.  This has the potential to
          205  +              allocated by the SQLiteMemory class.  This has the potential to
   206    206                 slow down the library a bit.
   207    207       -->
   208         -    <TrackMarshalBytes Condition="'$(TrackMarshalBytes)' == ''">false</TrackMarshalBytes>
          208  +    <TrackMemoryBytes Condition="'$(TrackMemoryBytes)' == ''">false</TrackMemoryBytes>
   209    209   
   210    210       <!--
   211    211           NOTE: For interaction with the native SQLite implementation, use the
   212    212                 custom built interop DLL (i.e. "SQLite.Interop.dll")?  By default,
   213    213                 this is enabled.  This property is mutually exclusive with the
   214    214                 "UseSqliteStandard" one, below.  This should always be disabled in
   215    215                 the project file that builds the NetModule target.

Changes to Setup/set_user_mistachkin_Debug.bat.

    13     13   REM NOTE: Enables the extra debug code helpful in troubleshooting issues.
    14     14   REM
    15     15   SET MSBUILD_ARGS=/p:CheckState=true
    16     16   SET MSBUILD_ARGS=%MSBUILD_ARGS% /p:CountHandle=true
    17     17   SET MSBUILD_ARGS=%MSBUILD_ARGS% /p:TraceConnection=true
    18     18   SET MSBUILD_ARGS=%MSBUILD_ARGS% /p:TraceHandle=true
    19     19   SET MSBUILD_ARGS=%MSBUILD_ARGS% /p:TraceStatement=true
    20         -SET MSBUILD_ARGS=%MSBUILD_ARGS% /p:TrackMarshalBytes=true
           20  +SET MSBUILD_ARGS=%MSBUILD_ARGS% /p:TrackMemoryBytes=true

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

  1605   1605                   throw new SQLiteException(SQLiteErrorCode.Error, GetLastError());
  1606   1606               }
  1607   1607           }
  1608   1608           finally
  1609   1609           {
  1610   1610               if (pName != IntPtr.Zero)
  1611   1611               {
  1612         -                SQLiteMarshal.Free(pName);
         1612  +                SQLiteMemory.Free(pName);
  1613   1613                   pName = IntPtr.Zero;
  1614   1614               }
  1615   1615           }
  1616   1616       }
  1617   1617   
  1618   1618       /// <summary>
  1619   1619       /// Calls the native SQLite core library in order to cleanup the resources
................................................................................
  1688   1688   
  1689   1689               return n;
  1690   1690           }
  1691   1691           finally
  1692   1692           {
  1693   1693               if (pSql != IntPtr.Zero)
  1694   1694               {
  1695         -                SQLiteMarshal.Free(pSql);
         1695  +                SQLiteMemory.Free(pSql);
  1696   1696                   pSql = IntPtr.Zero;
  1697   1697               }
  1698   1698           }
  1699   1699       }
  1700   1700   
  1701   1701       /// <summary>
  1702   1702       /// Enables or disabled extension loading by SQLite.

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

   108    108               "TRACE_STATEMENT",
   109    109   #endif
   110    110   
   111    111   #if TRACE_WARNING
   112    112               "TRACE_WARNING",
   113    113   #endif
   114    114   
   115         -#if TRACK_MARSHAL_BYTES
   116         -            "TRACK_MARSHAL_BYTES",
          115  +#if TRACK_MEMORY_BYTES
          116  +            "TRACK_MEMORY_BYTES",
   117    117   #endif
   118    118   
   119    119   #if USE_INTEROP_DLL
   120    120               "USE_INTEROP_DLL",
   121    121   #endif
   122    122   
   123    123   #if USE_PREPARE_V2

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

     6      6    ********************************************************/
     7      7   
     8      8   using System.Collections.Generic;
     9      9   using System.Globalization;
    10     10   using System.Runtime.InteropServices;
    11     11   using System.Text;
    12     12   
    13         -#if TRACK_MARSHAL_BYTES
           13  +#if TRACK_MEMORY_BYTES
    14     14   using System.Threading;
    15     15   #endif
    16     16   
    17     17   namespace System.Data.SQLite
    18     18   {
    19     19       #region SQLite Context Helper Class
    20     20       public sealed class SQLiteContext
................................................................................
  1339   1339               int savepoint             /* in */
  1340   1340               );
  1341   1341       }
  1342   1342       #endregion
  1343   1343   
  1344   1344       ///////////////////////////////////////////////////////////////////////////
  1345   1345   
  1346         -    #region SQLiteMarshal Class
  1347         -    internal static class SQLiteMarshal
         1346  +    #region SQLiteMemory Class
         1347  +    internal static class SQLiteMemory
  1348   1348       {
  1349         -        #region Private Constants
  1350         -        private static int ThirtyBits = 0x3fffffff;
  1351         -        private static readonly Encoding Utf8Encoding = Encoding.UTF8;
  1352         -        #endregion
  1353         -
  1354         -        ///////////////////////////////////////////////////////////////////////
  1355         -
  1356   1349           #region Private Data
  1357         -#if TRACK_MARSHAL_BYTES
         1350  +#if TRACK_MEMORY_BYTES
  1358   1351           private static int bytesAllocated;
  1359   1352           private static int maximumBytesAllocated;
  1360   1353   #endif
  1361   1354           #endregion
  1362   1355   
  1363   1356           ///////////////////////////////////////////////////////////////////////
  1364   1357   
  1365         -        #region IntPtr Helper Methods
  1366         -        internal static IntPtr IntPtrForOffset(
  1367         -            IntPtr pointer,
  1368         -            int offset
  1369         -            )
  1370         -        {
  1371         -            return new IntPtr(pointer.ToInt64() + offset);
  1372         -        }
  1373         -        #endregion
  1374         -
  1375         -        ///////////////////////////////////////////////////////////////////////
  1376         -
  1377         -        #region Marshal Read Helper Methods
  1378         -        internal static int ReadInt32(
  1379         -            IntPtr pointer,
  1380         -            int offset
  1381         -            )
  1382         -        {
  1383         -#if !PLATFORM_COMPACTFRAMEWORK
  1384         -            return Marshal.ReadInt32(pointer, offset);
  1385         -#else
  1386         -            return Marshal.ReadInt32(IntPtrForOffset(pointer, offset));
  1387         -#endif
  1388         -        }
  1389         -
  1390         -        ///////////////////////////////////////////////////////////////////////
  1391         -
  1392         -        internal static double ReadDouble(
  1393         -            IntPtr pointer,
  1394         -            int offset
  1395         -            )
  1396         -        {
  1397         -#if !PLATFORM_COMPACTFRAMEWORK
  1398         -            return BitConverter.Int64BitsToDouble(Marshal.ReadInt64(
  1399         -                pointer, offset));
  1400         -#else
  1401         -            return BitConverter.ToDouble(BitConverter.GetBytes(
  1402         -                Marshal.ReadInt64(IntPtrForOffset(pointer, offset))), 0);
  1403         -#endif
  1404         -        }
  1405         -
  1406         -        ///////////////////////////////////////////////////////////////////////
  1407         -
  1408         -        internal static IntPtr ReadIntPtr(
  1409         -            IntPtr pointer,
  1410         -            int offset
  1411         -            )
  1412         -        {
  1413         -#if !PLATFORM_COMPACTFRAMEWORK
  1414         -            return Marshal.ReadIntPtr(pointer, offset);
  1415         -#else
  1416         -            return Marshal.ReadIntPtr(IntPtrForOffset(pointer, offset));
  1417         -#endif
  1418         -        }
  1419         -        #endregion
  1420         -
  1421         -        ///////////////////////////////////////////////////////////////////////
  1422         -
  1423         -        #region Marshal Write Helper Methods
  1424         -        internal static void WriteInt32(
  1425         -            IntPtr pointer,
  1426         -            int offset,
  1427         -            int value
  1428         -            )
  1429         -        {
  1430         -#if !PLATFORM_COMPACTFRAMEWORK
  1431         -            Marshal.WriteInt32(pointer, offset, value);
  1432         -#else
  1433         -            Marshal.WriteInt32(IntPtrForOffset(pointer, offset), value);
  1434         -#endif
  1435         -        }
  1436         -
  1437         -        ///////////////////////////////////////////////////////////////////////
  1438         -
  1439         -        internal static void WriteDouble(
  1440         -            IntPtr pointer,
  1441         -            int offset,
  1442         -            double value
  1443         -            )
  1444         -        {
  1445         -#if !PLATFORM_COMPACTFRAMEWORK
  1446         -            Marshal.WriteInt64(pointer, offset,
  1447         -                BitConverter.DoubleToInt64Bits(value));
  1448         -#else
  1449         -            Marshal.WriteInt64(IntPtrForOffset(pointer, offset),
  1450         -                BitConverter.ToInt64(BitConverter.GetBytes(value), 0));
  1451         -#endif
  1452         -        }
  1453         -
  1454         -        ///////////////////////////////////////////////////////////////////////
  1455         -
  1456         -        internal static void WriteIntPtr(
  1457         -            IntPtr pointer,
  1458         -            int offset,
  1459         -            IntPtr value
  1460         -            )
  1461         -        {
  1462         -#if !PLATFORM_COMPACTFRAMEWORK
  1463         -            Marshal.WriteIntPtr(pointer, offset, value);
  1464         -#else
  1465         -            Marshal.WriteIntPtr(IntPtrForOffset(pointer, offset), value);
  1466         -#endif
  1467         -        }
  1468         -        #endregion
  1469         -
  1470         -        ///////////////////////////////////////////////////////////////////////
  1471         -
  1472   1358           #region Memory Allocation Helper Methods
  1473         -        internal static IntPtr Allocate(int size)
         1359  +        public static IntPtr Allocate(int size)
  1474   1360           {
  1475   1361               IntPtr pMemory = UnsafeNativeMethods.sqlite3_malloc(size);
  1476   1362   
  1477         -#if TRACK_MARSHAL_BYTES
         1363  +#if TRACK_MEMORY_BYTES
  1478   1364               if (pMemory != IntPtr.Zero)
  1479   1365               {
  1480   1366                   int blockSize = Size(pMemory);
  1481   1367   
  1482   1368                   if (blockSize > 0)
  1483   1369                   {
  1484   1370                       Interlocked.Add(ref bytesAllocated, blockSize);
................................................................................
  1488   1374   #endif
  1489   1375   
  1490   1376               return pMemory;
  1491   1377           }
  1492   1378   
  1493   1379           ///////////////////////////////////////////////////////////////////////
  1494   1380   
  1495         -        internal static int Size(IntPtr pMemory)
         1381  +        public static int Size(IntPtr pMemory)
  1496   1382           {
  1497   1383   #if !SQLITE_STANDARD
  1498         -            try
  1499         -            {
  1500         -                return UnsafeNativeMethods.sqlite3_malloc_size_interop(
  1501         -                    pMemory);
  1502         -            }
  1503         -            catch
  1504         -            {
  1505         -                // do nothing.
  1506         -            }
         1384  +            return UnsafeNativeMethods.sqlite3_malloc_size_interop(pMemory);
         1385  +#else
         1386  +            return 0;
  1507   1387   #endif
  1508         -
  1509         -            return 0;
  1510   1388           }
  1511   1389   
  1512   1390           ///////////////////////////////////////////////////////////////////////
  1513   1391   
  1514         -        internal static void Free(IntPtr pMemory)
         1392  +        public static void Free(IntPtr pMemory)
  1515   1393           {
  1516         -#if TRACK_MARSHAL_BYTES
         1394  +#if TRACK_MEMORY_BYTES
  1517   1395               if (pMemory != IntPtr.Zero)
  1518   1396               {
  1519   1397                   int blockSize = Size(pMemory);
  1520   1398   
  1521   1399                   if (blockSize > 0)
  1522   1400                       Interlocked.Add(ref bytesAllocated, -blockSize);
  1523   1401               }
  1524   1402   #endif
  1525   1403   
  1526   1404               UnsafeNativeMethods.sqlite3_free(pMemory);
  1527   1405           }
  1528   1406           #endregion
         1407  +    }
         1408  +    #endregion
         1409  +
         1410  +    ///////////////////////////////////////////////////////////////////////////
         1411  +
         1412  +    #region SQLiteMarshal Class
         1413  +    internal static class SQLiteMarshal
         1414  +    {
         1415  +        #region Private Constants
         1416  +        private static int ThirtyBits = 0x3fffffff;
         1417  +        private static readonly Encoding Utf8Encoding = Encoding.UTF8;
         1418  +        #endregion
         1419  +
         1420  +        ///////////////////////////////////////////////////////////////////////
         1421  +
         1422  +        #region IntPtr Helper Methods
         1423  +        public static IntPtr IntPtrForOffset(
         1424  +            IntPtr pointer,
         1425  +            int offset
         1426  +            )
         1427  +        {
         1428  +            return new IntPtr(pointer.ToInt64() + offset);
         1429  +        }
         1430  +        #endregion
         1431  +
         1432  +        ///////////////////////////////////////////////////////////////////////
         1433  +
         1434  +        #region Marshal Read Helper Methods
         1435  +        public static int ReadInt32(
         1436  +            IntPtr pointer,
         1437  +            int offset
         1438  +            )
         1439  +        {
         1440  +#if !PLATFORM_COMPACTFRAMEWORK
         1441  +            return Marshal.ReadInt32(pointer, offset);
         1442  +#else
         1443  +            return Marshal.ReadInt32(IntPtrForOffset(pointer, offset));
         1444  +#endif
         1445  +        }
         1446  +
         1447  +        ///////////////////////////////////////////////////////////////////////
         1448  +
         1449  +        public static double ReadDouble(
         1450  +            IntPtr pointer,
         1451  +            int offset
         1452  +            )
         1453  +        {
         1454  +#if !PLATFORM_COMPACTFRAMEWORK
         1455  +            return BitConverter.Int64BitsToDouble(Marshal.ReadInt64(
         1456  +                pointer, offset));
         1457  +#else
         1458  +            return BitConverter.ToDouble(BitConverter.GetBytes(
         1459  +                Marshal.ReadInt64(IntPtrForOffset(pointer, offset))), 0);
         1460  +#endif
         1461  +        }
         1462  +
         1463  +        ///////////////////////////////////////////////////////////////////////
         1464  +
         1465  +        public static IntPtr ReadIntPtr(
         1466  +            IntPtr pointer,
         1467  +            int offset
         1468  +            )
         1469  +        {
         1470  +#if !PLATFORM_COMPACTFRAMEWORK
         1471  +            return Marshal.ReadIntPtr(pointer, offset);
         1472  +#else
         1473  +            return Marshal.ReadIntPtr(IntPtrForOffset(pointer, offset));
         1474  +#endif
         1475  +        }
         1476  +        #endregion
         1477  +
         1478  +        ///////////////////////////////////////////////////////////////////////
         1479  +
         1480  +        #region Marshal Write Helper Methods
         1481  +        public static void WriteInt32(
         1482  +            IntPtr pointer,
         1483  +            int offset,
         1484  +            int value
         1485  +            )
         1486  +        {
         1487  +#if !PLATFORM_COMPACTFRAMEWORK
         1488  +            Marshal.WriteInt32(pointer, offset, value);
         1489  +#else
         1490  +            Marshal.WriteInt32(IntPtrForOffset(pointer, offset), value);
         1491  +#endif
         1492  +        }
         1493  +
         1494  +        ///////////////////////////////////////////////////////////////////////
         1495  +
         1496  +        public static void WriteDouble(
         1497  +            IntPtr pointer,
         1498  +            int offset,
         1499  +            double value
         1500  +            )
         1501  +        {
         1502  +#if !PLATFORM_COMPACTFRAMEWORK
         1503  +            Marshal.WriteInt64(pointer, offset,
         1504  +                BitConverter.DoubleToInt64Bits(value));
         1505  +#else
         1506  +            Marshal.WriteInt64(IntPtrForOffset(pointer, offset),
         1507  +                BitConverter.ToInt64(BitConverter.GetBytes(value), 0));
         1508  +#endif
         1509  +        }
         1510  +
         1511  +        ///////////////////////////////////////////////////////////////////////
         1512  +
         1513  +        public static void WriteIntPtr(
         1514  +            IntPtr pointer,
         1515  +            int offset,
         1516  +            IntPtr value
         1517  +            )
         1518  +        {
         1519  +#if !PLATFORM_COMPACTFRAMEWORK
         1520  +            Marshal.WriteIntPtr(pointer, offset, value);
         1521  +#else
         1522  +            Marshal.WriteIntPtr(IntPtrForOffset(pointer, offset), value);
         1523  +#endif
         1524  +        }
         1525  +        #endregion
  1529   1526   
  1530   1527           ///////////////////////////////////////////////////////////////////////
  1531   1528   
  1532   1529           #region Byte Array Helper Methods
  1533         -        internal static byte[] BytesFromIntPtr(
         1530  +        public static byte[] BytesFromIntPtr(
  1534   1531               IntPtr pValue,
  1535   1532               int length
  1536   1533               )
  1537   1534           {
  1538   1535               if (pValue == IntPtr.Zero)
  1539   1536                   return null;
  1540   1537   
................................................................................
  1546   1543               Marshal.Copy(pValue, result, 0, length);
  1547   1544   
  1548   1545               return result;
  1549   1546           }
  1550   1547   
  1551   1548           ///////////////////////////////////////////////////////////////////////
  1552   1549   
  1553         -        internal static IntPtr BytesToIntPtr(
         1550  +        public static IntPtr BytesToIntPtr(
  1554   1551               byte[] value
  1555   1552               )
  1556   1553           {
  1557   1554               if (value == null)
  1558   1555                   return IntPtr.Zero;
  1559   1556   
  1560   1557               int length = value.Length;
  1561   1558   
  1562   1559               if (length == 0)
  1563   1560                   return IntPtr.Zero;
  1564   1561   
  1565         -            IntPtr result = Allocate(length);
         1562  +            IntPtr result = SQLiteMemory.Allocate(length);
  1566   1563   
  1567   1564               if (result == IntPtr.Zero)
  1568   1565                   return IntPtr.Zero;
  1569   1566   
  1570   1567               Marshal.Copy(value, 0, result, length);
  1571   1568   
  1572   1569               return result;
  1573   1570           }
  1574   1571           #endregion
  1575   1572   
  1576   1573           ///////////////////////////////////////////////////////////////////////
  1577   1574   
  1578   1575           #region UTF-8 Encoding Helper Methods
  1579         -        internal static byte[] GetUtf8BytesFromString(
         1576  +        public static byte[] GetUtf8BytesFromString(
  1580   1577               string value
  1581   1578               )
  1582   1579           {
  1583   1580               if (value == null)
  1584   1581                   return null;
  1585   1582   
  1586   1583               return Utf8Encoding.GetBytes(value);
  1587   1584           }
  1588   1585   
  1589   1586           ///////////////////////////////////////////////////////////////////////
  1590   1587   
  1591         -        internal static string GetStringFromUtf8Bytes(
         1588  +        public static string GetStringFromUtf8Bytes(
  1592   1589               byte[] bytes
  1593   1590               )
  1594   1591           {
  1595   1592               if (bytes == null)
  1596   1593                   return null;
  1597   1594   
  1598   1595   #if !PLATFORM_COMPACTFRAMEWORK
................................................................................
  1602   1599   #endif
  1603   1600           }
  1604   1601           #endregion
  1605   1602   
  1606   1603           ///////////////////////////////////////////////////////////////////////
  1607   1604   
  1608   1605           #region UTF-8 String Helper Methods
  1609         -        internal static int ProbeForUtf8ByteLength(
         1606  +        public static int ProbeForUtf8ByteLength(
  1610   1607               IntPtr pValue,
  1611   1608               int limit
  1612   1609               )
  1613   1610           {
  1614   1611               int length = 0;
  1615   1612   
  1616   1613               if (pValue != IntPtr.Zero)
................................................................................
  1628   1625               }
  1629   1626   
  1630   1627               return length;
  1631   1628           }
  1632   1629   
  1633   1630           ///////////////////////////////////////////////////////////////////////
  1634   1631   
  1635         -        internal static string StringFromUtf8IntPtr(
         1632  +        public static string StringFromUtf8IntPtr(
  1636   1633               IntPtr pValue
  1637   1634               )
  1638   1635           {
  1639   1636               return StringFromUtf8IntPtr(pValue,
  1640   1637                   ProbeForUtf8ByteLength(pValue, ThirtyBits));
  1641   1638           }
  1642   1639   
  1643   1640           ///////////////////////////////////////////////////////////////////////
  1644   1641   
  1645         -        internal static string StringFromUtf8IntPtr(
         1642  +        public static string StringFromUtf8IntPtr(
  1646   1643               IntPtr pValue,
  1647   1644               int length
  1648   1645               )
  1649   1646           {
  1650   1647               if (pValue == IntPtr.Zero)
  1651   1648                   return null;
  1652   1649   
................................................................................
  1660   1657               }
  1661   1658   
  1662   1659               return String.Empty;
  1663   1660           }
  1664   1661   
  1665   1662           ///////////////////////////////////////////////////////////////////////
  1666   1663   
  1667         -        internal static IntPtr Utf8IntPtrFromString(
         1664  +        public static IntPtr Utf8IntPtrFromString(
  1668   1665               string value
  1669   1666               )
  1670   1667           {
  1671   1668               if (value == null)
  1672   1669                   return IntPtr.Zero;
  1673   1670   
  1674   1671               IntPtr result = IntPtr.Zero;
................................................................................
  1675   1672               byte[] bytes = GetUtf8BytesFromString(value);
  1676   1673   
  1677   1674               if (bytes == null)
  1678   1675                   return IntPtr.Zero;
  1679   1676   
  1680   1677               int length = bytes.Length;
  1681   1678   
  1682         -            result = Allocate(length + 1);
         1679  +            result = SQLiteMemory.Allocate(length + 1);
  1683   1680   
  1684   1681               if (result == IntPtr.Zero)
  1685   1682                   return IntPtr.Zero;
  1686   1683   
  1687   1684               Marshal.Copy(bytes, 0, result, length);
  1688   1685               Marshal.WriteByte(result, length, 0);
  1689   1686   
................................................................................
  1690   1687               return result;
  1691   1688           }
  1692   1689           #endregion
  1693   1690   
  1694   1691           ///////////////////////////////////////////////////////////////////////
  1695   1692   
  1696   1693           #region UTF-8 String Array Helper Methods
  1697         -        internal static string[] StringArrayFromUtf8IntPtrArray(
         1694  +        public static string[] StringArrayFromUtf8IntPtrArray(
  1698   1695               IntPtr[] pValues
  1699   1696               )
  1700   1697           {
  1701   1698               if (pValues == null)
  1702   1699                   return null;
  1703   1700   
  1704   1701               string[] result = new string[pValues.Length];
................................................................................
  1707   1704                   result[index] = StringFromUtf8IntPtr(pValues[index]);
  1708   1705   
  1709   1706               return result;
  1710   1707           }
  1711   1708   
  1712   1709           ///////////////////////////////////////////////////////////////////////
  1713   1710   
  1714         -        internal static IntPtr[] Utf8IntPtrArrayFromStringArray(
         1711  +        public static IntPtr[] Utf8IntPtrArrayFromStringArray(
  1715   1712               string[] values
  1716   1713               )
  1717   1714           {
  1718   1715               if (values == null)
  1719   1716                   return null;
  1720   1717   
  1721   1718               IntPtr[] result = new IntPtr[values.Length];
................................................................................
  1726   1723               return result;
  1727   1724           }
  1728   1725           #endregion
  1729   1726   
  1730   1727           ///////////////////////////////////////////////////////////////////////
  1731   1728   
  1732   1729           #region SQLiteValue Helper Methods
  1733         -        internal static SQLiteValue[] ValueArrayFromSizeAndIntPtr(
         1730  +        public static SQLiteValue[] ValueArrayFromSizeAndIntPtr(
  1734   1731               int nData,
  1735   1732               IntPtr apData
  1736   1733               )
  1737   1734           {
  1738   1735               if (nData < 0)
  1739   1736                   return null;
  1740   1737   
................................................................................
  1754   1751               }
  1755   1752   
  1756   1753               return result;
  1757   1754           }
  1758   1755   
  1759   1756           ///////////////////////////////////////////////////////////////////////
  1760   1757   
  1761         -        internal static SQLiteValue[] ValueArrayFromIntPtrArray(
         1758  +        public static SQLiteValue[] ValueArrayFromIntPtrArray(
  1762   1759               IntPtr[] values
  1763   1760               )
  1764   1761           {
  1765   1762               if (values == null)
  1766   1763                   return null;
  1767   1764   
  1768   1765               SQLiteValue[] result = new SQLiteValue[values.Length];
................................................................................
  1773   1770               return result;
  1774   1771           }
  1775   1772           #endregion
  1776   1773   
  1777   1774           ///////////////////////////////////////////////////////////////////////
  1778   1775   
  1779   1776           #region SQLiteIndex Helper Methods
  1780         -        internal static void IndexFromIntPtr(
         1777  +        public static void IndexFromIntPtr(
  1781   1778               IntPtr pIndex,
  1782   1779               ref SQLiteIndex index
  1783   1780               )
  1784   1781           {
  1785   1782               if (pIndex == IntPtr.Zero)
  1786   1783                   return;
  1787   1784   
................................................................................
  1872   1869                   index.Outputs.ConstraintUsages[iConstraint] =
  1873   1870                       new SQLiteIndexConstraintUsage(constraintUsage);
  1874   1871               }
  1875   1872           }
  1876   1873   
  1877   1874           ///////////////////////////////////////////////////////////////////////
  1878   1875   
  1879         -        internal static void IndexToIntPtr(
         1876  +        public static void IndexToIntPtr(
  1880   1877               SQLiteIndex index,
  1881   1878               IntPtr pIndex
  1882   1879               )
  1883   1880           {
  1884   1881               if ((index == null) || (index.Inputs == null) ||
  1885   1882                   (index.Inputs.Constraints == null) ||
  1886   1883                   (index.Inputs.OrderBys == null) || (index.Outputs == null) ||
................................................................................
  2055   2052           #region Protected Members
  2056   2053           #region Native Table Helper Methods
  2057   2054           protected virtual IntPtr AllocateTable()
  2058   2055           {
  2059   2056               int size = Marshal.SizeOf(typeof(
  2060   2057                   UnsafeNativeMethods.sqlite3_vtab));
  2061   2058   
  2062         -            return SQLiteMarshal.Allocate(size);
         2059  +            return SQLiteMemory.Allocate(size);
  2063   2060           }
  2064   2061   
  2065   2062           ///////////////////////////////////////////////////////////////////////
  2066   2063   
  2067   2064           protected virtual void ZeroTable(IntPtr pVtab)
  2068   2065           {
  2069   2066               if (pVtab == IntPtr.Zero)
................................................................................
  2083   2080           }
  2084   2081   
  2085   2082           ///////////////////////////////////////////////////////////////////////
  2086   2083   
  2087   2084           protected virtual void FreeTable(IntPtr pVtab)
  2088   2085           {
  2089   2086               SetTableError(pVtab, null);
  2090         -            SQLiteMarshal.Free(pVtab);
         2087  +            SQLiteMemory.Free(pVtab);
  2091   2088           }
  2092   2089           #endregion
  2093   2090   
  2094   2091           ///////////////////////////////////////////////////////////////////////
  2095   2092   
  2096   2093           #region Native Cursor Helper Methods
  2097   2094           protected virtual IntPtr AllocateCursor()
  2098   2095           {
  2099   2096               int size = Marshal.SizeOf(typeof(
  2100   2097                   UnsafeNativeMethods.sqlite3_vtab_cursor));
  2101   2098   
  2102         -            return SQLiteMarshal.Allocate(size);
         2099  +            return SQLiteMemory.Allocate(size);
  2103   2100           }
  2104   2101   
  2105   2102           ///////////////////////////////////////////////////////////////////////
  2106   2103   
  2107   2104           protected virtual void FreeCursor(IntPtr pCursor)
  2108   2105           {
  2109         -            SQLiteMarshal.Free(pCursor);
         2106  +            SQLiteMemory.Free(pCursor);
  2110   2107           }
  2111   2108           #endregion
  2112   2109   
  2113   2110           ///////////////////////////////////////////////////////////////////////
  2114   2111   
  2115   2112           #region Table Lookup Methods
  2116   2113           protected virtual IntPtr TableFromCursor(
................................................................................
  2289   2286                   return false;
  2290   2287   
  2291   2288               int offset = IntPtr.Size + sizeof(int);
  2292   2289               IntPtr pError = SQLiteMarshal.ReadIntPtr(pVtab, offset);
  2293   2290   
  2294   2291               if (pError != IntPtr.Zero)
  2295   2292               {
  2296         -                SQLiteMarshal.Free(pError); pError = IntPtr.Zero;
         2293  +                SQLiteMemory.Free(pError); pError = IntPtr.Zero;
  2297   2294                   SQLiteMarshal.WriteIntPtr(pVtab, offset, pError);
  2298   2295               }
  2299   2296   
  2300   2297               if (error == null)
  2301   2298                   return true;
  2302   2299   
  2303   2300               bool success = false;
................................................................................
  2308   2305                   SQLiteMarshal.WriteIntPtr(pVtab, offset, pError);
  2309   2306                   success = true;
  2310   2307               }
  2311   2308               finally
  2312   2309               {
  2313   2310                   if (!success && (pError != IntPtr.Zero))
  2314   2311                   {
  2315         -                    SQLiteMarshal.Free(pError);
         2312  +                    SQLiteMemory.Free(pError);
  2316   2313                       pError = IntPtr.Zero;
  2317   2314                   }
  2318   2315               }
  2319   2316   
  2320   2317               return success;
  2321   2318           }
  2322   2319   

Changes to System.Data.SQLite/System.Data.SQLite.Properties.targets.

    74     74         NOTE: Enable counting of the CriticalHandle derived object instances?
    75     75     -->
    76     76     <PropertyGroup Condition="'$(CountHandle)' != 'false'">
    77     77       <DefineConstants>$(DefineConstants);COUNT_HANDLE</DefineConstants>
    78     78     </PropertyGroup>
    79     79   
    80     80     <!--
    81         -      NOTE: Enable tracking of bytes allocated by the SQLiteMarshal class?
           81  +      NOTE: Enable tracking of bytes allocated by the SQLiteMemory class?
    82     82     -->
    83         -  <PropertyGroup Condition="'$(TrackMarshalBytes)' != 'false'">
    84         -    <DefineConstants>$(DefineConstants);TRACK_MARSHAL_BYTES</DefineConstants>
           83  +  <PropertyGroup Condition="'$(TrackMemoryBytes)' != 'false'">
           84  +    <DefineConstants>$(DefineConstants);TRACK_MEMORY_BYTES</DefineConstants>
    85     85     </PropertyGroup>
    86     86   
    87     87     <!--
    88     88         NOTE: For interaction with the native SQLite implementation, use the
    89     89               custom build interop DLL (i.e. "SQLite.Interop.DLL")?
    90     90     -->
    91     91     <PropertyGroup Condition="'$(UseInteropDll)' != 'false'">

Changes to Tests/common.eagle.

  1474   1474       }
  1475   1475   
  1476   1476       proc reportSQLiteResources { channel {quiet false} {collect true} } {
  1477   1477         #
  1478   1478         # NOTE: Skip all output if we are running in "quiet" mode.
  1479   1479         #
  1480   1480         if {[haveConstraint \
  1481         -              defineConstant.System.Data.SQLite.TRACK_MARSHAL_BYTES]} then {
         1481  +              defineConstant.System.Data.SQLite.TRACK_MEMORY_BYTES]} then {
  1482   1482           if {!$quiet} then {
  1483         -          tputs $channel "---- current memory in use by SQLiteMarshal... "
         1483  +          tputs $channel "---- current memory in use by SQLiteMemory... "
  1484   1484           }
  1485   1485   
  1486   1486           if {[catch {object invoke -flags +NonPublic \
  1487         -                System.Data.SQLite.SQLiteMarshal \
         1487  +                System.Data.SQLite.SQLiteMemory \
  1488   1488                   bytesAllocated} memory] == 0} then {
  1489   1489             if {!$quiet} then {
  1490   1490               tputs $channel [appendArgs $memory " bytes\n"]
  1491   1491             }
  1492   1492           } else {
  1493   1493             set memory unknown
  1494   1494   
  1495   1495             if {!$quiet} then {
  1496   1496               tputs $channel [appendArgs $memory \n]
  1497   1497             }
  1498   1498           }
  1499   1499   
  1500   1500           if {!$quiet} then {
  1501         -          tputs $channel "---- maximum memory in use by SQLiteMarshal... "
         1501  +          tputs $channel "---- maximum memory in use by SQLiteMemory... "
  1502   1502           }
  1503   1503   
  1504   1504           if {[catch {object invoke -flags +NonPublic \
  1505         -                System.Data.SQLite.SQLiteMarshal \
         1505  +                System.Data.SQLite.SQLiteMemory \
  1506   1506                   maximumBytesAllocated} memory] == 0} then {
  1507   1507             if {!$quiet} then {
  1508   1508               tputs $channel [appendArgs $memory " bytes\n"]
  1509   1509             }
  1510   1510           } else {
  1511   1511             set memory unknown
  1512   1512   
................................................................................
  1951   1951           foreach defineConstant [list \
  1952   1952               CHECK_STATE COUNT_HANDLE DEBUG INTEROP_CODEC INTEROP_DEBUG \
  1953   1953               INTEROP_EXTENSION_FUNCTIONS INTEROP_LEGACY_CLOSE INTEROP_LOG \
  1954   1954               INTEROP_TEST_EXTENSION NET_20 NET_35 NET_40 NET_45 NET_COMPACT_20 \
  1955   1955               PLATFORM_COMPACTFRAMEWORK PRELOAD_NATIVE_LIBRARY RETARGETABLE \
  1956   1956               SQLITE_STANDARD THROW_ON_DISPOSED TRACE TRACE_CONNECTION \
  1957   1957               TRACE_HANDLE TRACE_PRELOAD TRACE_STATEMENT TRACE_WARNING \
  1958         -            TRACK_MARSHAL_BYTES USE_INTEROP_DLL USE_PREPARE_V2 WINDOWS] {
         1958  +            TRACK_MEMORY_BYTES USE_INTEROP_DLL USE_PREPARE_V2 WINDOWS] {
  1959   1959             #
  1960   1960             # NOTE: Check if the compile-time option is listed in the list of
  1961   1961             #       "define constants" kept track of by the managed assembly.
  1962   1962             #
  1963   1963             checkForSQLiteDefineConstant $::test_channel $defineConstant
  1964   1964           }
  1965   1965