/ Check-in [c63903a4]
Login

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

Overview
Comment:Merge the pointer-passing interface changes from the 3.20 branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: c63903a4c5d52a490e3f26707aa85fb54d4e2e8a3ce31ca26a9c615fe7a51e97
User & Date: drh 2017-07-27 18:49:19
Context
2017-07-27
19:59
Increase the version number to 3.21.0 in anticipation for changes to go into the next release. check-in: 0645f25c user: drh tags: trunk
18:49
Merge the pointer-passing interface changes from the 3.20 branch. check-in: c63903a4 user: drh tags: trunk
18:43
Simplified documentation for the pointer passing interface. check-in: 2dfcd9a8 user: drh tags: branch-3.20
2017-07-26
19:59
Add the new sqlite3.mDbFlags field. Factor out bits of sqlite3.flags that do not interact with PRAGMA statements into sqlite3.mDbFlags. check-in: 3808a00f user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/fts3/fts3.c.

  3349   3349   
  3350   3350     /* The column value supplied by SQLite must be in range. */
  3351   3351     assert( iCol>=0 && iCol<=p->nColumn+2 );
  3352   3352   
  3353   3353     switch( iCol-p->nColumn ){
  3354   3354       case 0:
  3355   3355         /* The special 'table-name' column */
  3356         -      sqlite3_result_pointer(pCtx, pCsr, "fts3cursor");
         3356  +      sqlite3_result_pointer(pCtx, pCsr, "fts3cursor", 0);
  3357   3357         break;
  3358   3358   
  3359   3359       case 1:
  3360   3360         /* The docid column */
  3361   3361         sqlite3_result_int64(pCtx, pCsr->iPrevId);
  3362   3362         break;
  3363   3363   

Changes to ext/fts5/fts5_tcl.c.

   100    100       fts5_api *pApi = 0;
   101    101   
   102    102       rc = sqlite3_prepare_v2(db, "SELECT fts5(?1)", -1, &pStmt, 0);
   103    103       if( rc!=SQLITE_OK ){
   104    104         Tcl_AppendResult(interp, "error: ", sqlite3_errmsg(db), 0);
   105    105         return TCL_ERROR;
   106    106       }
   107         -    sqlite3_bind_pointer(pStmt, 1, (void*)&pApi, "fts5_api_ptr");
          107  +    sqlite3_bind_pointer(pStmt, 1, (void*)&pApi, "fts5_api_ptr", 0);
   108    108       sqlite3_step(pStmt);
   109    109   
   110    110       if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
   111    111         Tcl_AppendResult(interp, "error: ", sqlite3_errmsg(db), 0);
   112    112         return TCL_ERROR;
   113    113       }
   114    114   

Changes to ext/fts5/fts5_test_mi.c.

    71     71   static int fts5_api_from_db(sqlite3 *db, fts5_api **ppApi){
    72     72     sqlite3_stmt *pStmt = 0;
    73     73     int rc;
    74     74   
    75     75     *ppApi = 0;
    76     76     rc = sqlite3_prepare(db, "SELECT fts5(?1)", -1, &pStmt, 0);
    77     77     if( rc==SQLITE_OK ){
    78         -    sqlite3_bind_pointer(pStmt, 1, (void*)ppApi, "fts5_api_ptr");
           78  +    sqlite3_bind_pointer(pStmt, 1, (void*)ppApi, "fts5_api_ptr", 0);
    79     79       (void)sqlite3_step(pStmt);
    80     80       rc = sqlite3_finalize(pStmt);
    81     81     }
    82     82   
    83     83     return rc;
    84     84   }
    85     85   

Changes to ext/misc/carray.c.

    20     20   ** at the address $ptr.  $ptr is a pointer to the array of integers.
    21     21   ** The pointer value must be assigned to $ptr using the
    22     22   ** sqlite3_bind_pointer() interface with a pointer type of "carray".
    23     23   ** For example:
    24     24   **
    25     25   **    static int aX[] = { 53, 9, 17, 2231, 4, 99 };
    26     26   **    int i = sqlite3_bind_parameter_index(pStmt, "$ptr");
    27         -**    sqlite3_bind_value(pStmt, i, aX, "carray");
           27  +**    sqlite3_bind_value(pStmt, i, aX, "carray", 0);
    28     28   **
    29     29   ** There is an optional third parameter to determine the datatype of
    30     30   ** the C-language array.  Allowed values of the third parameter are
    31     31   ** 'int32', 'int64', 'double', 'char*'.  Example:
    32     32   **
    33     33   **      SELECT * FROM carray($ptr,10,'char*');
    34     34   **
................................................................................
   373    373     i64 = sqlite3_value_int64(argv[0]);
   374    374     if( sizeof(i64)==sizeof(p) ){
   375    375       memcpy(&p, &i64, sizeof(p));
   376    376     }else{
   377    377       int i32 = i64 & 0xffffffff;
   378    378       memcpy(&p, &i32, sizeof(p));
   379    379     }
   380         -  sqlite3_result_pointer(context, p, "carray");
          380  +  sqlite3_result_pointer(context, p, "carray", 0);
   381    381   }
   382    382   #endif /* SQLITE_TEST */
   383    383   
   384    384   #endif /* SQLITE_OMIT_VIRTUALTABLE */
   385    385   
   386    386   #ifdef _WIN32
   387    387   __declspec(dllexport)

Changes to ext/rtree/rtree.c.

   335    335   struct RtreeGeomCallback {
   336    336     int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
   337    337     int (*xQueryFunc)(sqlite3_rtree_query_info*);
   338    338     void (*xDestructor)(void*);
   339    339     void *pContext;
   340    340   };
   341    341   
   342         -
   343         -/*
   344         -** Value for the first field of every RtreeMatchArg object. The MATCH
   345         -** operator tests that the first field of a blob operand matches this
   346         -** value to avoid operating on invalid blobs (which could cause a segfault).
   347         -*/
   348         -#define RTREE_GEOMETRY_MAGIC 0x891245AB
   349         -
   350    342   /*
   351    343   ** An instance of this structure (in the form of a BLOB) is returned by
   352    344   ** the SQL functions that sqlite3_rtree_geometry_callback() and
   353    345   ** sqlite3_rtree_query_callback() create, and is read as the right-hand
   354    346   ** operand to the MATCH operator of an R-Tree.
   355    347   */
   356    348   struct RtreeMatchArg {
   357         -  u32 magic;                  /* Always RTREE_GEOMETRY_MAGIC */
          349  +  u32 iSize;                  /* Size of this object */
   358    350     RtreeGeomCallback cb;       /* Info about the callback functions */
   359    351     int nParam;                 /* Number of parameters to the SQL function */
   360    352     sqlite3_value **apSqlParam; /* Original SQL parameter values */
   361    353     RtreeDValue aParam[1];      /* Values for parameters to the SQL function */
   362    354   };
   363    355   
   364    356   #ifndef MAX
................................................................................
  1645   1637   /*
  1646   1638   ** This function is called to configure the RtreeConstraint object passed
  1647   1639   ** as the second argument for a MATCH constraint. The value passed as the
  1648   1640   ** first argument to this function is the right-hand operand to the MATCH
  1649   1641   ** operator.
  1650   1642   */
  1651   1643   static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
  1652         -  RtreeMatchArg *pBlob;              /* BLOB returned by geometry function */
         1644  +  RtreeMatchArg *pBlob, *pSrc;       /* BLOB returned by geometry function */
  1653   1645     sqlite3_rtree_query_info *pInfo;   /* Callback information */
  1654         -  int nBlob;                         /* Size of the geometry function blob */
  1655         -  int nExpected;                     /* Expected size of the BLOB */
  1656   1646   
  1657         -  /* Check that value is actually a blob. */
  1658         -  if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
  1659         -
  1660         -  /* Check that the blob is roughly the right size. */
  1661         -  nBlob = sqlite3_value_bytes(pValue);
  1662         -  if( nBlob<(int)sizeof(RtreeMatchArg) ){
  1663         -    return SQLITE_ERROR;
  1664         -  }
  1665         -
  1666         -  pInfo = (sqlite3_rtree_query_info*)sqlite3_malloc( sizeof(*pInfo)+nBlob );
         1647  +  pSrc = sqlite3_value_pointer(pValue, "RtreeMatchArg");
         1648  +  if( pSrc==0 ) return SQLITE_ERROR;
         1649  +  pInfo = (sqlite3_rtree_query_info*)
         1650  +                sqlite3_malloc64( sizeof(*pInfo)+pSrc->iSize );
  1667   1651     if( !pInfo ) return SQLITE_NOMEM;
  1668   1652     memset(pInfo, 0, sizeof(*pInfo));
  1669   1653     pBlob = (RtreeMatchArg*)&pInfo[1];
  1670         -
  1671         -  memcpy(pBlob, sqlite3_value_blob(pValue), nBlob);
  1672         -  nExpected = (int)(sizeof(RtreeMatchArg) +
  1673         -                    pBlob->nParam*sizeof(sqlite3_value*) +
  1674         -                    (pBlob->nParam-1)*sizeof(RtreeDValue));
  1675         -  if( pBlob->magic!=RTREE_GEOMETRY_MAGIC || nBlob!=nExpected ){
  1676         -    sqlite3_free(pInfo);
  1677         -    return SQLITE_ERROR;
  1678         -  }
         1654  +  memcpy(pBlob, pSrc, pSrc->iSize);
  1679   1655     pInfo->pContext = pBlob->cb.pContext;
  1680   1656     pInfo->nParam = pBlob->nParam;
  1681   1657     pInfo->aParam = pBlob->aParam;
  1682   1658     pInfo->apSqlParam = pBlob->apSqlParam;
  1683   1659   
  1684   1660     if( pBlob->cb.xGeom ){
  1685   1661       pCons->u.xGeom = pBlob->cb.xGeom;
................................................................................
  3709   3685     nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue)
  3710   3686              + nArg*sizeof(sqlite3_value*);
  3711   3687     pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
  3712   3688     if( !pBlob ){
  3713   3689       sqlite3_result_error_nomem(ctx);
  3714   3690     }else{
  3715   3691       int i;
  3716         -    pBlob->magic = RTREE_GEOMETRY_MAGIC;
         3692  +    pBlob->iSize = nBlob;
  3717   3693       pBlob->cb = pGeomCtx[0];
  3718   3694       pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
  3719   3695       pBlob->nParam = nArg;
  3720   3696       for(i=0; i<nArg; i++){
  3721   3697         pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]);
  3722   3698         if( pBlob->apSqlParam[i]==0 ) memErr = 1;
  3723   3699   #ifdef SQLITE_RTREE_INT_ONLY
................................................................................
  3726   3702         pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
  3727   3703   #endif
  3728   3704       }
  3729   3705       if( memErr ){
  3730   3706         sqlite3_result_error_nomem(ctx);
  3731   3707         rtreeMatchArgFree(pBlob);
  3732   3708       }else{
  3733         -      sqlite3_result_blob(ctx, pBlob, nBlob, rtreeMatchArgFree);
         3709  +      sqlite3_result_pointer(ctx, pBlob, "RtreeMatchArg", rtreeMatchArgFree);
  3734   3710       }
  3735   3711     }
  3736   3712   }
  3737   3713   
  3738   3714   /*
  3739   3715   ** Register a new geometry function for use with the r-tree MATCH operator.
  3740   3716   */

Changes to src/sqlite.h.in.

  3879   3879   ** is filled with zeroes.  ^A zeroblob uses a fixed amount of memory
  3880   3880   ** (just an integer to hold its size) while it is being processed.
  3881   3881   ** Zeroblobs are intended to serve as placeholders for BLOBs whose
  3882   3882   ** content is later written using
  3883   3883   ** [sqlite3_blob_open | incremental BLOB I/O] routines.
  3884   3884   ** ^A negative value for the zeroblob results in a zero-length BLOB.
  3885   3885   **
  3886         -** ^The sqlite3_bind_pointer(S,I,P,T) routine causes the I-th parameter in
         3886  +** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
  3887   3887   ** [prepared statement] S to have an SQL value of NULL, but to also be
  3888         -** associated with the pointer P of type T.
  3889         -** ^The sqlite3_bind_pointer() routine can be used to pass
  3890         -** host-language pointers into [application-defined SQL functions].
  3891         -** ^A parameter that is initialized using [sqlite3_bind_pointer()] appears
  3892         -** to be an ordinary SQL NULL value to everything other than
  3893         -** [sqlite3_value_pointer()].  The T parameter should be a static string,
  3894         -** preferably a string literal.  The procedure that invokes 
  3895         -** sqlite3_bind_pointer(S,I,P,T) continues to own the P and T pointers and
  3896         -** must guarantee that those pointers remain valid until after the last
  3897         -** access via [sqlite3_value_pointer()].  The sqlite3_bind_pointer() routine
  3898         -** is part of the [pointer passing interface] added for SQLite 3.20.0.
         3888  +** associated with the pointer P of type T.  ^D is either a NULL pointer or
         3889  +** a pointer to a destructor function for P. ^SQLite will invoke the
         3890  +** destructor D with a single argument of P when it is finished using
         3891  +** P.  The T parameter should be a static string, preferably a string
         3892  +** literal. The sqlite3_bind_pointer() routine is part of the
         3893  +** [pointer passing interface] added for SQLite 3.20.0.
  3899   3894   **
  3900   3895   ** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
  3901   3896   ** for the [prepared statement] or with a prepared statement for which
  3902   3897   ** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
  3903   3898   ** then the call will return [SQLITE_MISUSE].  If any sqlite3_bind_()
  3904   3899   ** routine is passed a [prepared statement] that has been finalized, the
  3905   3900   ** result is undefined and probably harmful.
................................................................................
  3926   3921   int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
  3927   3922   int sqlite3_bind_null(sqlite3_stmt*, int);
  3928   3923   int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
  3929   3924   int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
  3930   3925   int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
  3931   3926                            void(*)(void*), unsigned char encoding);
  3932   3927   int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
  3933         -int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*);
         3928  +int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*));
  3934   3929   int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
  3935   3930   int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64);
  3936   3931   
  3937   3932   /*
  3938   3933   ** CAPI3REF: Number Of SQL Parameters
  3939   3934   ** METHOD: sqlite3_stmt
  3940   3935   **
................................................................................
  4759   4754   **
  4760   4755   ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
  4761   4756   ** in the native byte-order of the host machine.  ^The
  4762   4757   ** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
  4763   4758   ** extract UTF-16 strings as big-endian and little-endian respectively.
  4764   4759   **
  4765   4760   ** ^If [sqlite3_value] object V was initialized 
  4766         -** using [sqlite3_bind_pointer(S,I,P,X)] or [sqlite3_result_pointer(C,P,X)]
         4761  +** using [sqlite3_bind_pointer(S,I,P,X,D)] or [sqlite3_result_pointer(C,P,X,D)]
  4767   4762   ** and if X and Y are strings that compare equal according to strcmp(X,Y),
  4768   4763   ** then sqlite3_value_pointer(V,Y) will return the pointer P.  ^Otherwise,
  4769         -** sqlite3_value_pointer(V,Y) returns a NULL.
         4764  +** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer() 
         4765  +** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
  4770   4766   **
  4771   4767   ** ^(The sqlite3_value_type(V) interface returns the
  4772   4768   ** [SQLITE_INTEGER | datatype code] for the initial datatype of the
  4773   4769   ** [sqlite3_value] object V. The returned value is one of [SQLITE_INTEGER],
  4774   4770   ** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].)^
  4775   4771   ** Other interfaces might change the datatype for an sqlite3_value object.
  4776   4772   ** For example, if the datatype is initially SQLITE_INTEGER and
................................................................................
  5097   5093   ** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
  5098   5094   ** so that the [sqlite3_value] specified in the parameter may change or
  5099   5095   ** be deallocated after sqlite3_result_value() returns without harm.
  5100   5096   ** ^A [protected sqlite3_value] object may always be used where an
  5101   5097   ** [unprotected sqlite3_value] object is required, so either
  5102   5098   ** kind of [sqlite3_value] object can be used with this interface.
  5103   5099   **
  5104         -** ^The sqlite3_result_pointer(C,P,T) interface sets the result to an
         5100  +** ^The sqlite3_result_pointer(C,P,T,D) interface sets the result to an
  5105   5101   ** SQL NULL value, just like [sqlite3_result_null(C)], except that it
  5106   5102   ** also associates the host-language pointer P or type T with that 
  5107   5103   ** NULL value such that the pointer can be retrieved within an
  5108   5104   ** [application-defined SQL function] using [sqlite3_value_pointer()].
  5109         -** The T parameter should be a static string and preferably a string
  5110         -** literal. The procedure that invokes sqlite3_result_pointer(C,P,T)
  5111         -** continues to own the P and T pointers and must guarantee that 
  5112         -** those pointers remain valid until after the last access via
  5113         -** [sqlite3_value_pointer()].  The sqlite3_result_pointer() routine
  5114         -** is part of the [pointer passing interface] added for SQLite 3.20.0.
         5105  +** ^If the D parameter is not NULL, then it is a pointer to a destructor
         5106  +** for the P parameter.  ^SQLite invokes D with P as its only argument
         5107  +** when SQLite is finished with P.  The T parameter should be a static
         5108  +** string and preferably a string literal. The sqlite3_result_pointer()
         5109  +** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
  5115   5110   **
  5116   5111   ** If these routines are called from within the different thread
  5117   5112   ** than the one containing the application-defined function that received
  5118   5113   ** the [sqlite3_context] pointer, the results are undefined.
  5119   5114   */
  5120   5115   void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
  5121   5116   void sqlite3_result_blob64(sqlite3_context*,const void*,
................................................................................
  5132   5127   void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
  5133   5128   void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
  5134   5129                              void(*)(void*), unsigned char encoding);
  5135   5130   void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
  5136   5131   void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
  5137   5132   void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
  5138   5133   void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
  5139         -void sqlite3_result_pointer(sqlite3_context*, void*, const char*);
         5134  +void sqlite3_result_pointer(sqlite3_context*, void*,const char*,void(*)(void*));
  5140   5135   void sqlite3_result_zeroblob(sqlite3_context*, int n);
  5141   5136   int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
  5142   5137   
  5143   5138   
  5144   5139   /*
  5145   5140   ** CAPI3REF: Setting The Subtype Of An SQL Function
  5146   5141   ** METHOD: sqlite3_context

Changes to src/sqlite3ext.h.

   285    285     /* Version 3.18.0 and later */
   286    286     void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64);
   287    287     /* Version 3.20.0 and later */
   288    288     int (*prepare_v3)(sqlite3*,const char*,int,unsigned int,
   289    289                       sqlite3_stmt**,const char**);
   290    290     int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
   291    291                         sqlite3_stmt**,const void**);
   292         -  int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*);
   293         -  void (*result_pointer)(sqlite3_context*,void*,const char*);
          292  +  int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
          293  +  void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
   294    294     void *(*value_pointer)(sqlite3_value*,const char*);
   295    295   };
   296    296   
   297    297   /*
   298    298   ** This is the function signature used for all extension entry points.  It
   299    299   ** is also defined in the file "loadext.c".
   300    300   */

Changes to src/vdbeInt.h.

   185    185   ** structures. Each Mem struct may cache multiple representations (string,
   186    186   ** integer etc.) of the same value.
   187    187   */
   188    188   struct sqlite3_value {
   189    189     union MemValue {
   190    190       double r;           /* Real value used when MEM_Real is set in flags */
   191    191       i64 i;              /* Integer value used when MEM_Int is set in flags */
   192         -    int nZero;          /* Used when bit MEM_Zero is set in flags */
   193         -    void *pPtr;         /* Pointer when flags=MEM_NULL and eSubtype='p' */
          192  +    int nZero;          /* Extra zero bytes when MEM_Zero and MEM_Blob set */
          193  +    const char *zPType; /* Pointer type when MEM_Term|MEM_Subtype|MEM_Null */
   194    194       FuncDef *pDef;      /* Used only when flags==MEM_Agg */
   195    195       RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
   196    196       VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
   197    197     } u;
   198    198     u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
   199    199     u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
   200    200     u8  eSubtype;       /* Subtype for this value */
................................................................................
   218    218   */
   219    219   #define MEMCELLSIZE offsetof(Mem,zMalloc)
   220    220   
   221    221   /* One or more of the following flags are set to indicate the validOK
   222    222   ** representations of the value stored in the Mem struct.
   223    223   **
   224    224   ** If the MEM_Null flag is set, then the value is an SQL NULL value.
   225         -** No other flags may be set in this case.
          225  +** For a pointer type created using sqlite3_bind_pointer() or
          226  +** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
   226    227   **
   227    228   ** If the MEM_Str flag is set then Mem.z points at a string representation.
   228    229   ** Usually this is encoded in the same unicode encoding as the main
   229    230   ** database (see below for exceptions). If the MEM_Term flag is also
   230    231   ** set, then the string is nul terminated. The MEM_Int and MEM_Real 
   231    232   ** flags may coexist with the MEM_Str flag.
   232    233   */
   233         -#define MEM_Null      0x0001   /* Value is NULL */
          234  +#define MEM_Null      0x0001   /* Value is NULL (or a pointer) */
   234    235   #define MEM_Str       0x0002   /* Value is a string */
   235    236   #define MEM_Int       0x0004   /* Value is an integer */
   236    237   #define MEM_Real      0x0008   /* Value is a real number */
   237    238   #define MEM_Blob      0x0010   /* Value is a BLOB */
   238    239   #define MEM_AffMask   0x001f   /* Mask of affinity bits */
   239    240   #define MEM_RowSet    0x0020   /* Value is a RowSet object */
   240    241   #define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
   241    242   #define MEM_Undefined 0x0080   /* Value is undefined */
   242    243   #define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
   243         -#define MEM_TypeMask  0x81ff   /* Mask of type bits */
          244  +#define MEM_TypeMask  0xc1ff   /* Mask of type bits */
   244    245   
   245    246   
   246    247   /* Whenever Mem contains a valid string or blob representation, one of
   247    248   ** the following flags must be set to determine the memory management
   248    249   ** policy for Mem.z.  The MEM_Term flag tells us whether or not the
   249    250   ** string is \000 or \u0000 terminated
   250    251   */
   251         -#define MEM_Term      0x0200   /* String rep is nul terminated */
          252  +#define MEM_Term      0x0200   /* String in Mem.z is zero terminated */
   252    253   #define MEM_Dyn       0x0400   /* Need to call Mem.xDel() on Mem.z */
   253    254   #define MEM_Static    0x0800   /* Mem.z points to a static string */
   254    255   #define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
   255    256   #define MEM_Agg       0x2000   /* Mem.z points to an agg function context */
   256    257   #define MEM_Zero      0x4000   /* Mem.i contains count of 0s appended to blob */
   257    258   #define MEM_Subtype   0x8000   /* Mem.eSubtype is valid */
   258    259   #ifdef SQLITE_OMIT_INCRBLOB
................................................................................
   472    473   int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
   473    474   void sqlite3VdbeMemSetInt64(Mem*, i64);
   474    475   #ifdef SQLITE_OMIT_FLOATING_POINT
   475    476   # define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
   476    477   #else
   477    478     void sqlite3VdbeMemSetDouble(Mem*, double);
   478    479   #endif
   479         -void sqlite3VdbeMemSetPointer(Mem*, void*, const char*);
          480  +void sqlite3VdbeMemSetPointer(Mem*, void*, const char*, void(*)(void*));
   480    481   void sqlite3VdbeMemInit(Mem*,sqlite3*,u16);
   481    482   void sqlite3VdbeMemSetNull(Mem*);
   482    483   void sqlite3VdbeMemSetZeroBlob(Mem*,int);
   483    484   void sqlite3VdbeMemSetRowSet(Mem*);
   484    485   int sqlite3VdbeMemMakeWriteable(Mem*);
   485    486   int sqlite3VdbeMemStringify(Mem*, u8, u8);
   486    487   i64 sqlite3VdbeIntValue(Mem*);

Changes to src/vdbeapi.c.

   197    197   }
   198    198   unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
   199    199     Mem *pMem = (Mem*)pVal;
   200    200     return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
   201    201   }
   202    202   void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){
   203    203     Mem *p = (Mem*)pVal;
   204         -  if( p->flags==(MEM_Null|MEM_Subtype|MEM_Term|MEM_Static)
          204  +  if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
          205  +                 (MEM_Null|MEM_Term|MEM_Subtype)
   205    206      && zPType!=0
   206    207      && p->eSubtype=='p'
   207         -   && strcmp(p->z, zPType)==0
          208  +   && strcmp(p->u.zPType, zPType)==0
   208    209     ){
   209         -    return p->u.pPtr;
          210  +    return (void*)p->z;
   210    211     }else{
   211    212       return 0;
   212    213     }
   213    214   }
   214    215   const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
   215    216     return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
   216    217   }
................................................................................
   385    386     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   386    387     sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
   387    388   }
   388    389   void sqlite3_result_null(sqlite3_context *pCtx){
   389    390     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   390    391     sqlite3VdbeMemSetNull(pCtx->pOut);
   391    392   }
   392         -void sqlite3_result_pointer(sqlite3_context *pCtx, void *pPtr, const char *zPT){
          393  +void sqlite3_result_pointer(
          394  +  sqlite3_context *pCtx,
          395  +  void *pPtr,
          396  +  const char *zPType,
          397  +  void (*xDestructor)(void*)
          398  +){
   393    399     Mem *pOut = pCtx->pOut;
   394    400     assert( sqlite3_mutex_held(pOut->db->mutex) );
   395    401     sqlite3VdbeMemSetNull(pOut);
   396         -  sqlite3VdbeMemSetPointer(pOut, pPtr, zPT);
          402  +  sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor);
   397    403   }
   398    404   void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
   399    405     Mem *pOut = pCtx->pOut;
   400    406     assert( sqlite3_mutex_held(pOut->db->mutex) );
   401    407     pOut->eSubtype = eSubtype & 0xff;
   402    408     pOut->flags |= MEM_Subtype;
   403    409   }
................................................................................
  1394   1400     Vdbe *p = (Vdbe*)pStmt;
  1395   1401     rc = vdbeUnbind(p, i);
  1396   1402     if( rc==SQLITE_OK ){
  1397   1403       sqlite3_mutex_leave(p->db->mutex);
  1398   1404     }
  1399   1405     return rc;
  1400   1406   }
  1401         -int sqlite3_bind_pointer(sqlite3_stmt *pStmt, int i, void *pPtr,const char *zT){
         1407  +int sqlite3_bind_pointer(
         1408  +  sqlite3_stmt *pStmt,
         1409  +  int i,
         1410  +  void *pPtr,
         1411  +  const char *zPTtype,
         1412  +  void (*xDestructor)(void*)
         1413  +){
  1402   1414     int rc;
  1403   1415     Vdbe *p = (Vdbe*)pStmt;
  1404   1416     rc = vdbeUnbind(p, i);
  1405   1417     if( rc==SQLITE_OK ){
  1406         -    sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zT);
         1418  +    sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
  1407   1419       sqlite3_mutex_leave(p->db->mutex);
         1420  +  }else if( xDestructor ){
         1421  +    xDestructor(pPtr);
  1408   1422     }
  1409   1423     return rc;
  1410   1424   }
  1411   1425   int sqlite3_bind_text( 
  1412   1426     sqlite3_stmt *pStmt, 
  1413   1427     int i, 
  1414   1428     const char *zData, 

Changes to src/vdbemem.c.

    23     23   ** Check invariants on a Mem object.
    24     24   **
    25     25   ** This routine is intended for use inside of assert() statements, like
    26     26   ** this:    assert( sqlite3VdbeCheckMemInvariants(pMem) );
    27     27   */
    28     28   int sqlite3VdbeCheckMemInvariants(Mem *p){
    29     29     /* If MEM_Dyn is set then Mem.xDel!=0.  
    30         -  ** Mem.xDel is might not be initialized if MEM_Dyn is clear.
           30  +  ** Mem.xDel might not be initialized if MEM_Dyn is clear.
    31     31     */
    32     32     assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
    33     33   
    34     34     /* MEM_Dyn may only be set if Mem.szMalloc==0.  In this way we
    35     35     ** ensure that if Mem.szMalloc>0 then it is safe to do
    36     36     ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.
    37     37     ** That saves a few cycles in inner loops. */
    38     38     assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
    39     39   
    40     40     /* Cannot be both MEM_Int and MEM_Real at the same time */
    41     41     assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) );
    42     42   
    43         -  /* Cannot be both MEM_Null and some other type */
    44         -  assert( (p->flags & MEM_Null)==0 ||
    45         -          (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob))==0 );
           43  +  if( p->flags & MEM_Null ){
           44  +    /* Cannot be both MEM_Null and some other type */
           45  +    assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob
           46  +                         |MEM_RowSet|MEM_Frame|MEM_Agg|MEM_Zero))==0 );
           47  +
           48  +    /* If MEM_Null is set, then either the value is a pure NULL (the usual
           49  +    ** case) or it is a pointer set using sqlite3_bind_pointer() or
           50  +    ** sqlite3_result_pointer().  If a pointer, then MEM_Term must also be
           51  +    ** set.
           52  +    */
           53  +    if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
           54  +      /* This is a pointer type.  There may be a flag to indicate what to
           55  +      ** do with the pointer. */
           56  +      assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
           57  +              ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
           58  +              ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
           59  +
           60  +      /* No other bits set */
           61  +      assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype
           62  +                           |MEM_Dyn|MEM_Ephem|MEM_Static))==0 );
           63  +    }else{
           64  +      /* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn,
           65  +      ** MEM_Ephem, MEM_Cleared, or MEM_Subtype */
           66  +    }
           67  +  }else{
           68  +    /* The MEM_Cleared bit is only allowed on NULLs */
           69  +    assert( (p->flags & MEM_Cleared)==0 );
           70  +  }
    46     71   
    47     72     /* The szMalloc field holds the correct memory allocation size */
    48     73     assert( p->szMalloc==0
    49     74          || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) );
    50     75   
    51     76     /* If p holds a string or blob, the Mem.z must point to exactly
    52     77     ** one of the following:
................................................................................
   701    726       vdbeReleaseAndSetInt64(pMem, val);
   702    727     }else{
   703    728       pMem->u.i = val;
   704    729       pMem->flags = MEM_Int;
   705    730     }
   706    731   }
   707    732   
          733  +/* A no-op destructor */
          734  +static void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }
          735  +
   708    736   /*
   709    737   ** Set the value stored in *pMem should already be a NULL.
   710    738   ** Also store a pointer to go with it.
   711    739   */
   712         -void sqlite3VdbeMemSetPointer(Mem *pMem, void *pPtr, const char *zPType){
          740  +void sqlite3VdbeMemSetPointer(
          741  +  Mem *pMem,
          742  +  void *pPtr,
          743  +  const char *zPType,
          744  +  void (*xDestructor)(void*)
          745  +){
   713    746     assert( pMem->flags==MEM_Null );
   714         -  if( zPType ){
   715         -    pMem->flags = MEM_Null|MEM_Subtype|MEM_Term|MEM_Static;
   716         -    pMem->u.pPtr = pPtr;
   717         -    pMem->eSubtype = 'p';
   718         -    pMem->z = (char*)zPType;
   719         -  }
          747  +  pMem->u.zPType = zPType ? zPType : "";
          748  +  pMem->z = pPtr;
          749  +  pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
          750  +  pMem->eSubtype = 'p';
          751  +  pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
   720    752   }
   721    753   
   722    754   #ifndef SQLITE_OMIT_FLOATING_POINT
   723    755   /*
   724    756   ** Delete any previous value and set the value stored in *pMem to val,
   725    757   ** manifest type REAL.
   726    758   */