diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/json.c | 44 | ||||
-rw-r--r-- | src/printf.c | 82 | ||||
-rw-r--r-- | src/sqliteInt.h | 45 | ||||
-rw-r--r-- | src/vdbemem.c | 20 |
4 files changed, 29 insertions, 162 deletions
diff --git a/src/json.c b/src/json.c index d7d464442..a4a3e65c9 100644 --- a/src/json.c +++ b/src/json.c @@ -1758,7 +1758,6 @@ static int jsonParse( int i; memset(pParse, 0, sizeof(*pParse)); if( zJson==0 ) return 1; -//printf("PARSE %s\n", zJson); pParse->zJson = zJson; pParse->bOwnsJson = bTakeJson; pParse->nJPRef = 1; @@ -1896,7 +1895,6 @@ static JsonParse *jsonParseCached( p->nErr = 0; p->useMod = 0; pMatch = p; -//printf("HIT %s at %d\n", zJson, iKey); }else if( pMatch==0 && p->zAlt!=0 @@ -1907,7 +1905,6 @@ static JsonParse *jsonParseCached( p->nErr = 0; p->useMod = 1; pMatch = p; -//printf("HIT %s at %d-alt\n", zJson, iKey); }else if( p->iHold<iMinHold ){ iMinHold = p->iHold; iMinKey = iKey; @@ -1928,7 +1925,6 @@ static JsonParse *jsonParseCached( /* The input JSON was not found anywhere in the cache. We will need ** to parse it ourselves and generate a new JsonParse object. */ -//printf("MISS %s\n", zJson); p = sqlite3_malloc64( sizeof(*p) ); if( p==0 ){ sqlite3_result_error_nomem(pCtx); @@ -2866,7 +2862,7 @@ static void jsonReplaceFunc( int argc, sqlite3_value **argv ){ - JsonParse x; /* The parse */ + JsonParse *pParse; /* The parse */ JsonNode *pNode; const char *zPath; u32 i; @@ -2876,20 +2872,20 @@ static void jsonReplaceFunc( jsonWrongNumArgs(ctx, "replace"); return; } - if( jsonParse(&x, ctx, (char*)sqlite3_value_text(argv[0]), 0) ) return; - assert( x.nNode ); + pParse = jsonParseCached(ctx, argv[0], ctx, argc>1); + if( pParse==0 ) return; for(i=1; i<(u32)argc; i+=2){ zPath = (const char*)sqlite3_value_text(argv[i]); - pNode = jsonLookup(&x, zPath, 0, ctx); - if( x.nErr ) goto replace_err; + pParse->useMod = 1; + pNode = jsonLookup(pParse, zPath, 0, ctx); + if( pParse->nErr ) goto replace_err; if( pNode ){ - jsonReplaceNode(ctx, &x, (u32)(pNode - x.aNode), argv[i+1]); + jsonReplaceNode(ctx, pParse, (u32)(pNode - pParse->aNode), argv[i+1]); } } - jsonReturnJson(&x, x.aNode, ctx, 0); + jsonReturnJson(pParse, pParse->aNode, ctx, 1); replace_err: - jsonDebugPrintParse(&x); - jsonParseReset(&x); + jsonDebugPrintParse(pParse); } @@ -2910,7 +2906,7 @@ static void jsonSetFunc( int argc, sqlite3_value **argv ){ - JsonParse x; /* The parse */ + JsonParse *pParse; /* The parse */ JsonNode *pNode; const char *zPath; u32 i; @@ -2922,27 +2918,27 @@ static void jsonSetFunc( jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert"); return; } - if( jsonParse(&x, ctx, (char*)sqlite3_value_text(argv[0]), 0) ) return; - assert( x.nNode ); + pParse = jsonParseCached(ctx, argv[0], ctx, argc>1); + if( pParse==0 ) return; for(i=1; i<(u32)argc; i+=2){ zPath = (const char*)sqlite3_value_text(argv[i]); bApnd = 0; - x.useMod = 1; - pNode = jsonLookup(&x, zPath, &bApnd, ctx); - if( x.oom ){ + pParse->useMod = 1; + pNode = jsonLookup(pParse, zPath, &bApnd, ctx); + if( pParse->oom ){ sqlite3_result_error_nomem(ctx); goto jsonSetDone; - }else if( x.nErr ){ + }else if( pParse->nErr ){ goto jsonSetDone; }else if( pNode && (bApnd || bIsSet) ){ - jsonReplaceNode(ctx, &x, (u32)(pNode - x.aNode), argv[i+1]); + jsonReplaceNode(ctx, pParse, (u32)(pNode - pParse->aNode), argv[i+1]); } } - jsonDebugPrintParse(&x); - jsonReturnJson(&x, x.aNode, ctx, 0); + jsonDebugPrintParse(pParse); + jsonReturnJson(pParse, pParse->aNode, ctx, 1); jsonSetDone: - jsonParseReset(&x); + /* no cleanup required */; } /* diff --git a/src/printf.c b/src/printf.c index a55bd2b85..87ad91f79 100644 --- a/src/printf.c +++ b/src/printf.c @@ -1394,34 +1394,13 @@ void sqlite3RCStrUnref(char *z){ assert( p!=0 ); p--; assert( p->nRCRef>0 ); - assert( p->uMagic==SQLITE_RCSTR_MAGIC ); if( p->nRCRef>=2 ){ p->nRCRef--; }else{ - if( p->xFree ) p->xFree(p->pAttach); -#ifdef SQLITE_DEBUG - p->uMagic = 0; -#endif sqlite3_free(p); } } -#if 0 -/* -** Return true if the reference count on the string is exactly one, meaning -** that the string can be modified. Return false if the reference count -** is greater than one. -*/ -int sqlite3RCStrIsWriteable(char *z){ - RCStr *p = (RCStr*)z; - assert( p!=0 ); - p--; - assert( p->nRCRef>0 ); - assert( p->uMagic==SQLITE_RCSTR_MAGIC ); - return p->nRCRef==1; -} -#endif - /* ** Create a new string that is capable of holding N bytes of text, not counting ** the zero byte at the end. The string is uninitialized. @@ -1435,32 +1414,9 @@ char *sqlite3RCStrNew(u64 N){ RCStr *p = sqlite3_malloc64( N + sizeof(*p) + 1 ); if( p==0 ) return 0; p->nRCRef = 1; - p->xFree = 0; - p->pAttach = 0; -#ifdef SQLITE_DEBUG - p->uMagic = SQLITE_RCSTR_MAGIC; -#endif return (char*)&p[1]; } -#if 0 -/* -** Return the number of bytes allocated to the string. The value returned -** does not include the space for the zero-terminator at the end. -*/ -u64 sqlite3RCStrSize(char *z){ - RCStr *p = (RCStr*)z; - u64 N; - assert( p!=0 ); - p--; - assert( p->nRCRef>0 ); - assert( p->uMagic==SQLITE_RCSTR_MAGIC ); - N = sqlite3_msize(p); - N -= sizeof(p) + 1; - return N; -} -#endif - /* ** Change the size of the string so that it is able to hold N bytes. ** The string might be reallocated, so return the new allocation. @@ -1471,7 +1427,6 @@ char *sqlite3RCStrResize(char *z, u64 N){ assert( p!=0 ); p--; assert( p->nRCRef==1 ); - assert( p->uMagic==SQLITE_RCSTR_MAGIC ); pNew = sqlite3_realloc64(p, N+sizeof(RCStr)+1); if( pNew==0 ){ sqlite3_free(p); @@ -1480,40 +1435,3 @@ char *sqlite3RCStrResize(char *z, u64 N){ return (char*)&pNew[1]; } } - -#if 0 -/* -** Add a new attachment to the string. -** -** A string may have no more than one attachment. When a new attachment -** is added, any prior attachment is destroyed. Remove an attachment -** by adding a zero-attachment. -*/ -void sqlite3RCStrAttach(char *z, void *pAttach, void(*xFree)(void*)){ - RCStr *p = (RCStr*)z; - assert( p!=0 ); - p--; - assert( p->nRCRef>0 ); - assert( p->uMagic==SQLITE_RCSTR_MAGIC ); - if( p->xFree ) p->xFree(p->pAttach); - p->xFree = xFree; - p->pAttach = pAttach; -} -#endif - -#if 0 -/* -** Return the attachment associated with a string if the attachment -** has the destructure specified in the second argument. If the -** string has no attachment or if the destructor does not match, -** then return a NULL pointr. -*/ -void *sqlite3RCStrGetAttachment(char *z, void(*xFree)(void*)){ - RCStr *p = (RCStr*)z; - assert( p!=0 ); - p--; - assert( p->nRCRef>0 ); - assert( p->uMagic==SQLITE_RCSTR_MAGIC ); - return p->xFree==xFree ? p->pAttach : 0; -} -#endif diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 358063534..a19a16d50 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -4066,44 +4066,22 @@ struct sqlite3_str { ** The following object is the header for an "RCStr" or "reference-counted ** string". An RCStr is passed around and used like any other char* ** that has been dynamically allocated. The important interface -** difference is that it uses sqlite3RCStrUnref() as its destructor -** rather than sqlite3_free(). Other than that the two are interchangeable. +** differences: ** -** Thus to return an RCStr object as the result of an SQL function use: -** -** sqlite3_result_text64(ctx,z,sz,sqlite3RCStrUnref,SQLITE_UTF8) -** ^^^^^^^^^^^^^^^^^ -** Instead of sqlite3_free() or similar +** 1. RCStr strings are reference counted. They are deallocated +** when the reference count reaches zero. ** -** An SQL function can check its arguments to see if they are RCStr -** strings using the sqlite3ValueIsOfClass() function: +** 2. Use sqlite3RCStrUnref() to free an RCStr string rather than +** sqlite3_free() ** -** sqlite3ValueIsOfClass(argv[i], sqlite3RCStrUnref); -** -** An RCStr string might be better than an ordinary string in some cases -** because: -** -** (1) You can duplicate it using sqlite3RCStrRef(x). -** -** (2) You can also add an associated object to the string. For -** example, if the string is JSON, perhaps the associated object -** is a parse of that JSON. -** -** Methods for an RCStr string begin with "sqlite3RCStr...". +** 3. Make a (read-only) copy of a read-only RCStr string using +** sqlite3RCStrRef(). */ struct RCStr { - u32 nRCRef; /* Number of references */ -#ifdef SQLITE_DEBUG - u32 uMagic; /* Magic number for sanity checking */ -#endif - void *pAttach; /* Attachment to this string */ - void (*xFree)(void*); /* Destructor for the attachment */ + u64 nRCRef; /* Number of references */ + /* Total structure size should be a multiple of 8 bytes for alignment */ }; -/* The Magic number used by RCStr for sanity checking. SQLITE_DEBUG only. */ -#define SQLITE_RCSTR_MAGIC 0x3dc05d54 - - /* ** A pointer to this structure is used to communicate information ** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback. @@ -5222,7 +5200,6 @@ void sqlite3FileSuffix3(const char*, char*); u8 sqlite3GetBoolean(const char *z,u8); const void *sqlite3ValueText(sqlite3_value*, u8); -//int sqlite3ValueIsOfClass(const sqlite3_value*, void(*)(void*)); int sqlite3ValueBytes(sqlite3_value*, u8); void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, void(*)(void*)); @@ -5333,11 +5310,7 @@ int sqlite3OpenTempDatabase(Parse *); char *sqlite3RCStrRef(char*); void sqlite3RCStrUnref(char*); char *sqlite3RCStrNew(u64); -//u64 sqlite3RCStrSize(char*); char *sqlite3RCStrResize(char*,u64); -//int sqlite3RCStrIsWriteable(char*); -//void sqlite3RCStrAttach(char*, void*, void(*)(void*)); -//void *sqlite3RCStrGetAttachment(char*,void(*)(void*)); void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int); int sqlite3StrAccumEnlarge(StrAccum*, i64); diff --git a/src/vdbemem.c b/src/vdbemem.c index d44a41716..c73e59c36 100644 --- a/src/vdbemem.c +++ b/src/vdbemem.c @@ -1368,26 +1368,6 @@ const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){ return valueToText(pVal, enc); } -#if 0 -/* Return true if sqlit3_value object pVal is a string or blob value -** that uses the destructor specified in the second argument. -** -** TODO: Maybe someday promote this interface into a published API so -** that third-party extensions can get access to it? -*/ -int sqlite3ValueIsOfClass(const sqlite3_value *pVal, void(*xFree)(void*)){ - if( ALWAYS(pVal!=0) - && (pVal->flags & (MEM_Str|MEM_Blob))!=0 - && (pVal->flags & MEM_Dyn)!=0 - && pVal->xDel==xFree - ){ - return 1; - }else{ - return 0; - } -} -#endif - /* ** Create a new sqlite3_value object. */ |