-
Notifications
You must be signed in to change notification settings - Fork 376
/
vdbemem.c
1781 lines (1662 loc) · 47.5 KB
/
vdbemem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2010-2017, Tarantool AUTHORS, please see AUTHORS file.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
*
* This file contains code use to manipulate "Mem" structure. A "Mem"
* stores a single value in the VDBE. Mem is an opaque structure visible
* only within the VDBE. Interface routines refer to a Mem using the
* name sql_value
*/
#include "sqlInt.h"
#include "vdbeInt.h"
#include "tarantoolInt.h"
#include "box/schema.h"
#include "box/tuple.h"
#include "mpstream.h"
#ifdef SQL_DEBUG
/*
* Check invariants on a Mem object.
*
* This routine is intended for use inside of assert() statements, like
* this: assert( sqlVdbeCheckMemInvariants(pMem) );
*/
int
sqlVdbeCheckMemInvariants(Mem * p)
{
/* If MEM_Dyn is set then Mem.xDel!=0.
* Mem.xDel is might not be initialized if MEM_Dyn is clear.
*/
assert((p->flags & MEM_Dyn) == 0 || p->xDel != 0);
/* MEM_Dyn may only be set if Mem.szMalloc==0. In this way we
* ensure that if Mem.szMalloc>0 then it is safe to do
* Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.
* That saves a few cycles in inner loops.
*/
assert((p->flags & MEM_Dyn) == 0 || p->szMalloc == 0);
/* Cannot be both MEM_Int and MEM_Real at the same time */
assert((p->flags & (MEM_Int | MEM_Real)) != (MEM_Int | MEM_Real));
/* The szMalloc field holds the correct memory allocation size */
assert(p->szMalloc == 0
|| p->szMalloc == sqlDbMallocSize(p->db, p->zMalloc));
/* If p holds a string or blob, the Mem.z must point to exactly
* one of the following:
*
* (1) Memory in Mem.zMalloc and managed by the Mem object
* (2) Memory to be freed using Mem.xDel
* (3) An ephemeral string or blob
* (4) A static string or blob
*/
if ((p->flags & (MEM_Str | MEM_Blob)) && p->n > 0) {
assert(((p->szMalloc > 0 && p->z == p->zMalloc) ? 1 : 0) +
((p->flags & MEM_Dyn) != 0 ? 1 : 0) +
((p->flags & MEM_Ephem) != 0 ? 1 : 0) +
((p->flags & MEM_Static) != 0 ? 1 : 0) == 1);
}
return 1;
}
#endif
/*
* Make sure pMem->z points to a writable allocation of at least
* min(n,32) bytes.
*
* If the bPreserve argument is true, then copy of the content of
* pMem->z into the new allocation. pMem must be either a string or
* blob if bPreserve is true. If bPreserve is false, any prior content
* in pMem->z is discarded.
*/
SQL_NOINLINE int
sqlVdbeMemGrow(Mem * pMem, int n, int bPreserve)
{
assert(sqlVdbeCheckMemInvariants(pMem));
testcase(pMem->db == 0);
/* If the bPreserve flag is set to true, then the memory cell must already
* contain a valid string or blob value.
*/
assert(bPreserve == 0 || pMem->flags & (MEM_Blob | MEM_Str));
testcase(bPreserve && pMem->z == 0);
assert(pMem->szMalloc == 0
|| pMem->szMalloc == sqlDbMallocSize(pMem->db,
pMem->zMalloc));
if (pMem->szMalloc < n) {
if (n < 32)
n = 32;
if (bPreserve && pMem->szMalloc > 0 && pMem->z == pMem->zMalloc) {
pMem->z = pMem->zMalloc =
sqlDbReallocOrFree(pMem->db, pMem->z, n);
bPreserve = 0;
} else {
if (pMem->szMalloc > 0)
sqlDbFree(pMem->db, pMem->zMalloc);
pMem->zMalloc = sqlDbMallocRaw(pMem->db, n);
}
if (pMem->zMalloc == 0) {
sqlVdbeMemSetNull(pMem);
pMem->z = 0;
pMem->szMalloc = 0;
return SQL_NOMEM;
} else {
pMem->szMalloc =
sqlDbMallocSize(pMem->db, pMem->zMalloc);
}
}
if (bPreserve && pMem->z && pMem->z != pMem->zMalloc) {
memcpy(pMem->zMalloc, pMem->z, pMem->n);
}
if ((pMem->flags & MEM_Dyn) != 0) {
assert(pMem->xDel != 0 && pMem->xDel != SQL_DYNAMIC);
pMem->xDel((void *)(pMem->z));
}
pMem->z = pMem->zMalloc;
pMem->flags &= ~(MEM_Dyn | MEM_Ephem | MEM_Static);
return SQL_OK;
}
/*
* Change the pMem->zMalloc allocation to be at least szNew bytes.
* If pMem->zMalloc already meets or exceeds the requested size, this
* routine is a no-op.
*
* Any prior string or blob content in the pMem object may be discarded.
* The pMem->xDel destructor is called, if it exists. Though MEM_Str
* and MEM_Blob values may be discarded, MEM_Int, MEM_Real, and MEM_Null
* values are preserved.
*
* Return SQL_OK on success or an error code (probably SQL_NOMEM)
* if unable to complete the resizing.
*/
int
sqlVdbeMemClearAndResize(Mem * pMem, int szNew)
{
assert(szNew > 0);
assert((pMem->flags & MEM_Dyn) == 0 || pMem->szMalloc == 0);
if (pMem->szMalloc < szNew) {
return sqlVdbeMemGrow(pMem, szNew, 0);
}
assert((pMem->flags & MEM_Dyn) == 0);
pMem->z = pMem->zMalloc;
pMem->flags &= (MEM_Null | MEM_Int | MEM_Real);
return SQL_OK;
}
/*
* Change pMem so that its MEM_Str or MEM_Blob value is stored in
* MEM.zMalloc, where it can be safely written.
*
* Return SQL_OK on success or SQL_NOMEM if malloc fails.
*/
int
sqlVdbeMemMakeWriteable(Mem * pMem)
{
if ((pMem->flags & (MEM_Str | MEM_Blob)) != 0) {
if (ExpandBlob(pMem))
return SQL_NOMEM;
if (pMem->szMalloc == 0 || pMem->z != pMem->zMalloc) {
if (sqlVdbeMemGrow(pMem, pMem->n + 2, 1)) {
return SQL_NOMEM;
}
pMem->z[pMem->n] = 0;
pMem->z[pMem->n + 1] = 0;
pMem->flags |= MEM_Term;
}
}
pMem->flags &= ~MEM_Ephem;
#ifdef SQL_DEBUG
pMem->pScopyFrom = 0;
#endif
return SQL_OK;
}
/*
* If the given Mem* has a zero-filled tail, turn it into an ordinary
* blob stored in dynamically allocated space.
*/
#ifndef SQL_OMIT_INCRBLOB
int
sqlVdbeMemExpandBlob(Mem * pMem)
{
int nByte;
assert(pMem->flags & MEM_Zero);
assert(pMem->flags & MEM_Blob);
/* Set nByte to the number of bytes required to store the expanded blob. */
nByte = pMem->n + pMem->u.nZero;
if (nByte <= 0) {
nByte = 1;
}
if (sqlVdbeMemGrow(pMem, nByte, 1)) {
return SQL_NOMEM;
}
memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
pMem->n += pMem->u.nZero;
pMem->flags &= ~(MEM_Zero | MEM_Term);
return SQL_OK;
}
#endif
/*
* It is already known that pMem contains an unterminated string.
* Add the zero terminator.
*/
static SQL_NOINLINE int
vdbeMemAddTerminator(Mem * pMem)
{
if (sqlVdbeMemGrow(pMem, pMem->n + 2, 1)) {
return SQL_NOMEM;
}
pMem->z[pMem->n] = 0;
pMem->z[pMem->n + 1] = 0;
pMem->flags |= MEM_Term;
return SQL_OK;
}
/*
* Make sure the given Mem is \u0000 terminated.
*/
int
sqlVdbeMemNulTerminate(Mem * pMem)
{
testcase((pMem->flags & (MEM_Term | MEM_Str)) == (MEM_Term | MEM_Str));
testcase((pMem->flags & (MEM_Term | MEM_Str)) == 0);
if ((pMem->flags & (MEM_Term | MEM_Str)) != MEM_Str) {
return SQL_OK; /* Nothing to do */
} else {
return vdbeMemAddTerminator(pMem);
}
}
/*
* Add MEM_Str to the set of representations for the given Mem. Numbers
* are converted using sql_snprintf(). Converting a BLOB to a string
* is a no-op.
*
* Existing representations MEM_Int and MEM_Real are invalidated if
* bForce is true but are retained if bForce is false.
*
* A MEM_Null value will never be passed to this function. This function is
* used for converting values to text for returning to the user (i.e. via
* sql_value_text()), or for ensuring that values to be used as btree
* keys are strings. In the former case a NULL pointer is returned the
* user and the latter is an internal programming error.
*/
int
sqlVdbeMemStringify(Mem * pMem, u8 bForce)
{
int fg = pMem->flags;
const int nByte = 32;
if ((fg & (MEM_Null | MEM_Str | MEM_Blob)) != 0)
return SQL_OK;
assert(!(fg & MEM_Zero));
assert(fg & (MEM_Int | MEM_Real));
assert(EIGHT_BYTE_ALIGNMENT(pMem));
if (sqlVdbeMemClearAndResize(pMem, nByte)) {
return SQL_NOMEM;
}
if (fg & MEM_Int) {
sql_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
} else {
assert(fg & MEM_Real);
sql_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r);
}
pMem->n = sqlStrlen30(pMem->z);
pMem->flags |= MEM_Str | MEM_Term;
if (bForce)
pMem->flags &= ~(MEM_Int | MEM_Real);
return SQL_OK;
}
/*
* Memory cell pMem contains the context of an aggregate function.
* This routine calls the finalize method for that function. The
* result of the aggregate is stored back into pMem.
*
* Return SQL_ERROR if the finalizer reports an error. SQL_OK
* otherwise.
*/
int
sqlVdbeMemFinalize(Mem * pMem, FuncDef * pFunc)
{
int rc = SQL_OK;
if (ALWAYS(pFunc && pFunc->xFinalize)) {
sql_context ctx;
Mem t;
assert((pMem->flags & MEM_Null) != 0 || pFunc == pMem->u.pDef);
memset(&ctx, 0, sizeof(ctx));
memset(&t, 0, sizeof(t));
t.flags = MEM_Null;
t.db = pMem->db;
ctx.pOut = &t;
ctx.pMem = pMem;
ctx.pFunc = pFunc;
pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
assert((pMem->flags & MEM_Dyn) == 0);
if (pMem->szMalloc > 0)
sqlDbFree(pMem->db, pMem->zMalloc);
memcpy(pMem, &t, sizeof(t));
rc = ctx.isError;
}
return rc;
}
/*
* If the memory cell contains a value that must be freed by
* invoking the external callback in Mem.xDel, then this routine
* will free that value. It also sets Mem.flags to MEM_Null.
*
* This is a helper routine for sqlVdbeMemSetNull() and
* for sqlVdbeMemRelease(). Use those other routines as the
* entry point for releasing Mem resources.
*/
static SQL_NOINLINE void
vdbeMemClearExternAndSetNull(Mem * p)
{
assert(VdbeMemDynamic(p));
if (p->flags & MEM_Agg) {
sqlVdbeMemFinalize(p, p->u.pDef);
assert((p->flags & MEM_Agg) == 0);
testcase(p->flags & MEM_Dyn);
}
if (p->flags & MEM_Dyn) {
assert(p->xDel != SQL_DYNAMIC && p->xDel != 0);
p->xDel((void *)p->z);
} else if (p->flags & MEM_Frame) {
VdbeFrame *pFrame = p->u.pFrame;
pFrame->pParent = pFrame->v->pDelFrame;
pFrame->v->pDelFrame = pFrame;
}
p->flags = MEM_Null;
}
/*
* Release memory held by the Mem p, both external memory cleared
* by p->xDel and memory in p->zMalloc.
*
* This is a helper routine invoked by sqlVdbeMemRelease() in
* the unusual case where there really is memory in p that needs
* to be freed.
*/
static SQL_NOINLINE void
vdbeMemClear(Mem * p)
{
if (VdbeMemDynamic(p)) {
vdbeMemClearExternAndSetNull(p);
}
if (p->szMalloc) {
sqlDbFree(p->db, p->zMalloc);
p->szMalloc = 0;
}
p->z = 0;
}
/*
* Release any memory resources held by the Mem. Both the memory that is
* free by Mem.xDel and the Mem.zMalloc allocation are freed.
*
* Use this routine prior to clean up prior to abandoning a Mem, or to
* reset a Mem back to its minimum memory utilization.
*
* Use sqlVdbeMemSetNull() to release just the Mem.xDel space
* prior to inserting new content into the Mem.
*/
void
sqlVdbeMemRelease(Mem * p)
{
assert(sqlVdbeCheckMemInvariants(p));
if (VdbeMemDynamic(p) || p->szMalloc) {
vdbeMemClear(p);
}
}
/*
* Convert a 64-bit IEEE double into a 64-bit signed integer.
* If the double is out of range of a 64-bit signed integer then
* return the closest available 64-bit signed integer.
*/
static int
doubleToInt64(double r, int64_t *i)
{
#ifdef SQL_OMIT_FLOATING_POINT
/* When floating-point is omitted, double and int64 are the same thing */
*i = r;
return 0;
#else
/*
* Many compilers we encounter do not define constants for the
* minimum and maximum 64-bit integers, or they define them
* inconsistently. And many do not understand the "LL" notation.
* So we define our own static constants here using nothing
* larger than a 32-bit integer constant.
*/
static const int64_t maxInt = LARGEST_INT64;
static const int64_t minInt = SMALLEST_INT64;
if (r <= (double)minInt) {
*i = minInt;
return -1;
} else if (r >= (double)maxInt) {
*i = maxInt;
return -1;
} else {
*i = (int64_t) r;
return *i != r;
}
#endif
}
/*
* Return some kind of integer value which is the best we can do
* at representing the value that *pMem describes as an integer.
* If pMem is an integer, then the value is exact. If pMem is
* a floating-point then the value returned is the integer part.
* If pMem is a string or blob, then we make an attempt to convert
* it into an integer and return that. If pMem represents an
* an SQL-NULL value, return 0.
*
* If pMem represents a string value, its encoding might be changed.
*/
int
sqlVdbeIntValue(Mem * pMem, int64_t *i)
{
int flags;
assert(EIGHT_BYTE_ALIGNMENT(pMem));
flags = pMem->flags;
if (flags & MEM_Int) {
*i = pMem->u.i;
return 0;
} else if (flags & MEM_Real) {
return doubleToInt64(pMem->u.r, i);
} else if (flags & (MEM_Str)) {
assert(pMem->z || pMem->n == 0);
if (sql_atoi64(pMem->z, (int64_t *)i, pMem->n) == 0)
return 0;
}
return -1;
}
/*
* Return the best representation of pMem that we can get into a
* double. If pMem is already a double or an integer, return its
* value. If it is a string or blob, try to convert it to a double.
* If it is a NULL, return 0.0.
*/
int
sqlVdbeRealValue(Mem * pMem, double *v)
{
assert(EIGHT_BYTE_ALIGNMENT(pMem));
if (pMem->flags & MEM_Real) {
*v = pMem->u.r;
return 0;
} else if (pMem->flags & (MEM_Int | MEM_Unsigned)) {
*v = (double)(u64)pMem->u.i;
return 0;
} else if (pMem->flags & MEM_Int) {
*v = (double)pMem->u.i;
return 0;
} else if (pMem->flags & MEM_Str) {
if (sqlAtoF(pMem->z, v, pMem->n))
return 0;
}
return -1;
}
/*
* The MEM structure is already a MEM_Real. Try to also make it a
* MEM_Int if we can.
*/
int
mem_apply_integer_type(Mem *pMem)
{
int rc;
i64 ix;
assert(pMem->flags & MEM_Real);
assert(EIGHT_BYTE_ALIGNMENT(pMem));
if ((rc = doubleToInt64(pMem->u.r, (int64_t *) &ix)) == 0) {
pMem->u.i = ix;
MemSetTypeFlag(pMem, MEM_Int);
}
return rc;
}
/*
* Convert pMem to type integer. Invalidate any prior representations.
*/
int
sqlVdbeMemIntegerify(Mem * pMem, bool is_forced)
{
assert(EIGHT_BYTE_ALIGNMENT(pMem));
int64_t i;
if (sqlVdbeIntValue(pMem, &i) == 0) {
pMem->u.i = i;
MemSetTypeFlag(pMem, MEM_Int);
return 0;
} else if ((pMem->flags & MEM_Real) != 0 && is_forced) {
if (pMem->u.r >= INT64_MAX || pMem->u.r < INT64_MIN)
return -1;
pMem->u.i = (int64_t) pMem->u.r;
MemSetTypeFlag(pMem, MEM_Int);
return 0;
}
double d;
if (sqlVdbeRealValue(pMem, &d) || (int64_t) d != d) {
return SQL_ERROR;
}
pMem->u.i = (int64_t) d;
MemSetTypeFlag(pMem, MEM_Int);
return 0;
}
/*
* Convert pMem so that it is of type MEM_Real.
* Invalidate any prior representations.
*/
int
sqlVdbeMemRealify(Mem * pMem)
{
assert(EIGHT_BYTE_ALIGNMENT(pMem));
double v;
if (sqlVdbeRealValue(pMem, &v))
return SQL_ERROR;
pMem->u.r = v;
MemSetTypeFlag(pMem, MEM_Real);
return SQL_OK;
}
/*
* Convert pMem so that it has types MEM_Real or MEM_Int or both.
* Invalidate any prior representations.
*
* Every effort is made to force the conversion, even if the input
* is a string that does not look completely like a number. Convert
* as much of the string as we can and ignore the rest.
*/
int
sqlVdbeMemNumerify(Mem * pMem)
{
if ((pMem->flags & (MEM_Int | MEM_Real | MEM_Null)) == 0) {
assert((pMem->flags & (MEM_Blob | MEM_Str)) != 0);
if (0 == sql_atoi64(pMem->z, (int64_t *)&pMem->u.i, pMem->n)) {
MemSetTypeFlag(pMem, MEM_Int);
} else {
double v;
if (sqlVdbeRealValue(pMem, &v))
return SQL_ERROR;
pMem->u.r = v;
MemSetTypeFlag(pMem, MEM_Real);
mem_apply_integer_type(pMem);
}
}
assert((pMem->flags & (MEM_Int | MEM_Real | MEM_Null)) != 0);
pMem->flags &= ~(MEM_Str | MEM_Blob | MEM_Zero);
return SQL_OK;
}
/*
* Cast the datatype of the value in pMem according to the type
* @type. Casting is different from applying type in that a cast
* is forced. In other words, the value is converted into the desired
* type even if that results in loss of data. This routine is
* used (for example) to implement the SQL "cast()" operator.
*/
int
sqlVdbeMemCast(Mem * pMem, enum field_type type)
{
assert(type < field_type_MAX);
if (pMem->flags & MEM_Null)
return SQL_OK;
if ((pMem->flags & MEM_Blob) != 0 && type == FIELD_TYPE_NUMBER) {
if (sql_atoi64(pMem->z, (int64_t *) &pMem->u.i, pMem->n) == 0) {
MemSetTypeFlag(pMem, MEM_Real);
pMem->u.r = pMem->u.i;
return 0;
}
return ! sqlAtoF(pMem->z, &pMem->u.r, pMem->n);
}
switch (type) {
case FIELD_TYPE_SCALAR:
return 0;
case FIELD_TYPE_INTEGER:
if ((pMem->flags & MEM_Blob) != 0) {
if (sql_atoi64(pMem->z, (int64_t *) &pMem->u.i,
pMem->n) != 0)
return -1;
MemSetTypeFlag(pMem, MEM_Int);
return 0;
}
return sqlVdbeMemIntegerify(pMem, true);
case FIELD_TYPE_NUMBER:
return sqlVdbeMemRealify(pMem);
default:
assert(type == FIELD_TYPE_STRING);
assert(MEM_Str == (MEM_Blob >> 3));
pMem->flags |= (pMem->flags & MEM_Blob) >> 3;
sql_value_apply_type(pMem, FIELD_TYPE_STRING);
assert(pMem->flags & MEM_Str || pMem->db->mallocFailed);
pMem->flags &= ~(MEM_Int | MEM_Real | MEM_Blob | MEM_Zero);
return SQL_OK;
}
}
/*
* Initialize bulk memory to be a consistent Mem object.
*
* The minimum amount of initialization feasible is performed.
*/
void
sqlVdbeMemInit(Mem * pMem, sql * db, u32 flags)
{
assert((flags & ~MEM_TypeMask) == 0);
pMem->flags = flags;
pMem->db = db;
pMem->szMalloc = 0;
}
/*
* Delete any previous value and set the value stored in *pMem to NULL.
*
* This routine calls the Mem.xDel destructor to dispose of values that
* require the destructor. But it preserves the Mem.zMalloc memory allocation.
* To free all resources, use sqlVdbeMemRelease(), which both calls this
* routine to invoke the destructor and deallocates Mem.zMalloc.
*
* Use this routine to reset the Mem prior to insert a new value.
*
* Use sqlVdbeMemRelease() to complete erase the Mem prior to abandoning it.
*/
void
sqlVdbeMemSetNull(Mem * pMem)
{
if (VdbeMemDynamic(pMem)) {
vdbeMemClearExternAndSetNull(pMem);
} else {
pMem->flags = MEM_Null;
}
}
void
sqlValueSetNull(sql_value * p)
{
sqlVdbeMemSetNull((Mem *) p);
}
/*
* Delete any previous value and set the value to be a BLOB of length
* n containing all zeros.
*/
void
sqlVdbeMemSetZeroBlob(Mem * pMem, int n)
{
sqlVdbeMemRelease(pMem);
pMem->flags = MEM_Blob | MEM_Zero;
pMem->n = 0;
if (n < 0)
n = 0;
pMem->u.nZero = n;
pMem->z = 0;
}
/*
* The pMem is known to contain content that needs to be destroyed prior
* to a value change. So invoke the destructor, then set the value to
* a 64-bit integer.
*/
static SQL_NOINLINE void
vdbeReleaseAndSetInt64(Mem * pMem, i64 val)
{
sqlVdbeMemSetNull(pMem);
pMem->u.i = val;
pMem->flags = MEM_Int;
}
/*
* Delete any previous value and set the value stored in *pMem to val,
* manifest type INTEGER.
*/
void
sqlVdbeMemSetInt64(Mem * pMem, i64 val)
{
if (VdbeMemDynamic(pMem)) {
vdbeReleaseAndSetInt64(pMem, val);
} else {
pMem->u.i = val;
pMem->flags = MEM_Int;
}
}
#ifndef SQL_OMIT_FLOATING_POINT
/*
* Delete any previous value and set the value stored in *pMem to val,
* manifest type REAL.
*/
void
sqlVdbeMemSetDouble(Mem * pMem, double val)
{
sqlVdbeMemSetNull(pMem);
if (!sqlIsNaN(val)) {
pMem->u.r = val;
pMem->flags = MEM_Real;
}
}
#endif
/*
* Return true if the Mem object contains a TEXT or BLOB that is
* too large - whose size exceeds SQL_MAX_LENGTH.
*/
int
sqlVdbeMemTooBig(Mem * p)
{
assert(p->db != 0);
if (p->flags & (MEM_Str | MEM_Blob)) {
int n = p->n;
if (p->flags & MEM_Zero) {
n += p->u.nZero;
}
return n > p->db->aLimit[SQL_LIMIT_LENGTH];
}
return 0;
}
#ifdef SQL_DEBUG
/*
* This routine prepares a memory cell for modification by breaking
* its link to a shallow copy and by marking any current shallow
* copies of this cell as invalid.
*
* This is used for testing and debugging only - to make sure shallow
* copies are not misused.
*/
void
sqlVdbeMemAboutToChange(Vdbe * pVdbe, Mem * pMem)
{
int i;
Mem *pX;
for (i = 0, pX = pVdbe->aMem; i < pVdbe->nMem; i++, pX++) {
if (pX->pScopyFrom == pMem) {
pX->flags |= MEM_Undefined;
pX->pScopyFrom = 0;
}
}
pMem->pScopyFrom = 0;
}
#endif /* SQL_DEBUG */
/*
* Make an shallow copy of pFrom into pTo. Prior contents of
* pTo are freed. The pFrom->z field is not duplicated. If
* pFrom->z is used, then pTo->z points to the same thing as pFrom->z
* and flags gets srcType (either MEM_Ephem or MEM_Static).
*/
static SQL_NOINLINE void
vdbeClrCopy(Mem * pTo, const Mem * pFrom, int eType)
{
vdbeMemClearExternAndSetNull(pTo);
assert(!VdbeMemDynamic(pTo));
sqlVdbeMemShallowCopy(pTo, pFrom, eType);
}
void
sqlVdbeMemShallowCopy(Mem * pTo, const Mem * pFrom, int srcType)
{
assert(pTo->db == pFrom->db);
if (VdbeMemDynamic(pTo)) {
vdbeClrCopy(pTo, pFrom, srcType);
return;
}
memcpy(pTo, pFrom, MEMCELLSIZE);
if ((pFrom->flags & MEM_Static) == 0) {
pTo->flags &= ~(MEM_Dyn | MEM_Static | MEM_Ephem);
assert(srcType == MEM_Ephem || srcType == MEM_Static);
pTo->flags |= srcType;
}
}
/*
* Make a full copy of pFrom into pTo. Prior contents of pTo are
* freed before the copy is made.
*/
int
sqlVdbeMemCopy(Mem * pTo, const Mem * pFrom)
{
int rc = SQL_OK;
if (VdbeMemDynamic(pTo))
vdbeMemClearExternAndSetNull(pTo);
memcpy(pTo, pFrom, MEMCELLSIZE);
pTo->flags &= ~MEM_Dyn;
if (pTo->flags & (MEM_Str | MEM_Blob)) {
if (0 == (pFrom->flags & MEM_Static)) {
pTo->flags |= MEM_Ephem;
rc = sqlVdbeMemMakeWriteable(pTo);
}
}
return rc;
}
/*
* Transfer the contents of pFrom to pTo. Any existing value in pTo is
* freed. If pFrom contains ephemeral data, a copy is made.
*
* pFrom contains an SQL NULL when this routine returns.
*/
void
sqlVdbeMemMove(Mem * pTo, Mem * pFrom)
{
assert(pFrom->db == 0 || pTo->db == 0 || pFrom->db == pTo->db);
sqlVdbeMemRelease(pTo);
memcpy(pTo, pFrom, sizeof(Mem));
pFrom->flags = MEM_Null;
pFrom->szMalloc = 0;
}
/*
* Change the value of a Mem to be a string or a BLOB.
*
* The memory management strategy depends on the value of the xDel
* parameter. If the value passed is SQL_TRANSIENT, then the
* string is copied into a (possibly existing) buffer managed by the
* Mem structure. Otherwise, any existing buffer is freed and the
* pointer copied.
*
* If the string is too large (if it exceeds the SQL_LIMIT_LENGTH
* size limit) then no memory allocation occurs. If the string can be
* stored without allocating memory, then it is. If a memory allocation
* is required to store the string, then value of pMem is unchanged. In
* either case, SQL_TOOBIG is returned.
*/
int
sqlVdbeMemSetStr(Mem * pMem, /* Memory cell to set to string value */
const char *z, /* String pointer */
int n, /* Bytes in string, or negative */
u8 not_blob, /* Encoding of z. 0 for BLOBs */
void (*xDel) (void *) /* Destructor function */
)
{
int nByte = n; /* New value for pMem->n */
int iLimit; /* Maximum allowed string or blob size */
u16 flags = 0; /* New value for pMem->flags */
/* If z is a NULL pointer, set pMem to contain an SQL NULL. */
if (!z) {
sqlVdbeMemSetNull(pMem);
return SQL_OK;
}
if (pMem->db) {
iLimit = pMem->db->aLimit[SQL_LIMIT_LENGTH];
} else {
iLimit = SQL_MAX_LENGTH;
}
flags = (not_blob == 0 ? MEM_Blob : MEM_Str);
if (nByte < 0) {
assert(not_blob != 0);
nByte = sqlStrlen30(z);
if (nByte > iLimit)
nByte = iLimit + 1;
flags |= MEM_Term;
}
/* The following block sets the new values of Mem.z and Mem.xDel. It
* also sets a flag in local variable "flags" to indicate the memory
* management (one of MEM_Dyn or MEM_Static).
*/
if (xDel == SQL_TRANSIENT) {
int nAlloc = nByte;
if (flags & MEM_Term) {
nAlloc += 1; //SQL_UTF8
}
if (nByte > iLimit) {
return SQL_TOOBIG;
}
testcase(nAlloc == 0);
testcase(nAlloc == 31);
testcase(nAlloc == 32);
if (sqlVdbeMemClearAndResize(pMem, MAX(nAlloc, 32))) {
return SQL_NOMEM;
}
memcpy(pMem->z, z, nAlloc);
} else if (xDel == SQL_DYNAMIC) {
sqlVdbeMemRelease(pMem);
pMem->zMalloc = pMem->z = (char *)z;
pMem->szMalloc = sqlDbMallocSize(pMem->db, pMem->zMalloc);
} else {
sqlVdbeMemRelease(pMem);
pMem->z = (char *)z;
pMem->xDel = xDel;
flags |= ((xDel == SQL_STATIC) ? MEM_Static : MEM_Dyn);
}
pMem->n = nByte;
pMem->flags = flags;
if (nByte > iLimit) {
return SQL_TOOBIG;
}
return SQL_OK;
}
/*
* Move data out of a btree key or data field and into a Mem structure.
* The data is payload from the entry that pCur is currently pointing
* to. offset and amt determine what portion of the data or key to retrieve.
* The result is written into the pMem element.
*
* The pMem object must have been initialized. This routine will use
* pMem->zMalloc to hold the content from the btree, if possible. New
* pMem->zMalloc space will be allocated if necessary. The calling routine
* is responsible for making sure that the pMem object is eventually
* destroyed.
*
* If this routine fails for any reason (malloc returns NULL or unable
* to read from the disk) then the pMem is left in an inconsistent state.
*/
static SQL_NOINLINE int
vdbeMemFromBtreeResize(BtCursor * pCur, /* Cursor pointing at record to retrieve. */
u32 offset, /* Offset from the start of data to return bytes from. */
u32 amt, /* Number of bytes to return. */
Mem * pMem /* OUT: Return data in this Mem structure. */
)
{
int rc;
pMem->flags = MEM_Null;
if (SQL_OK == (rc = sqlVdbeMemClearAndResize(pMem, amt + 2))) {
rc = sqlCursorPayload(pCur, offset, amt, pMem->z);
if (rc == SQL_OK) {
pMem->z[amt] = 0;
pMem->z[amt + 1] = 0;
pMem->flags = MEM_Blob | MEM_Term;
pMem->n = (int)amt;
} else {
sqlVdbeMemRelease(pMem);
}
}
return rc;
}
int
sqlVdbeMemFromBtree(BtCursor * pCur, /* Cursor pointing at record to retrieve. */
u32 offset, /* Offset from the start of data to return bytes from. */
u32 amt, /* Number of bytes to return. */
Mem * pMem /* OUT: Return data in this Mem structure. */
)
{
char *zData; /* Data from the btree layer */
u32 available = 0; /* Number of bytes available on the local btree page */
int rc = SQL_OK; /* Return code */
assert(sqlCursorIsValid(pCur));
assert(!VdbeMemDynamic(pMem));
assert(pCur->curFlags & BTCF_TaCursor ||
pCur->curFlags & BTCF_TEphemCursor);