-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvar.cpp
1102 lines (1000 loc) · 59.1 KB
/
var.cpp
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
/*
AutoHotkey
Copyright 2003-2008 Chris Mallett ([email protected])
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include "stdafx.h" // pre-compiled headers
#include "var.h"
#include "globaldata.h" // for g_script
// Init static vars:
char Var::sEmptyString[] = ""; // For explanation, see its declaration in .h file.
ResultType Var::AssignHWND(HWND aWnd)
{
// Convert to unsigned 64-bit to support for 64-bit pointers. Since most script operations --
// such as addition and comparison -- read strings in as signed 64-bit, it is documented that
// math and other numerical operations should never be performed on these while they exist
// as strings in script variables:
//#define ASSIGN_HWND_TO_VAR(var, hwnd) var->Assign((unsigned __int64)hwnd)
// UPDATE: Always assign as hex for better compatibility with Spy++ and other apps that
// report window handles:
char buf[64];
*buf = '0';
buf[1] = 'x';
_ui64toa((unsigned __int64)aWnd, buf + 2, 16);
return Assign(buf);
}
ResultType Var::Assign(DWORD aValueToAssign) // For some reason, this function is actually faster when not inline.
// Returns OK or FAIL.
{
char value_string[256];
return Assign(UTOA(aValueToAssign, value_string));
}
ResultType Var::Assign(int aValueToAssign) // For some reason, this function is actually faster when not inline.
{
char value_string[256];
// ITOA() seems to perform quite a bit better than sprintf() in this case:
return Assign(ITOA(aValueToAssign, value_string));
//snprintf(value_string, sizeof(value_string), "%d", aValueToAssign);
//return Assign(value_string);
}
ResultType Var::Assign(__int64 aValueToAssign) // For some reason, this function is actually faster when not inline.
// Returns OK or FAIL.
{
char value_string[256];
return Assign(ITOA64(aValueToAssign, value_string));
}
// Currently not needed:
//ResultType Var::Assign(unsigned __int64 aValueToAssign)
//// Since most script features that "read in" a number from a string (such as addition and comparison)
//// read the strings as signed values, this function here should only be used when it is documented
//// that math and comparisons should not be performed on such values.
//// Returns OK or FAIL.
//{
// char value_string[256];
// UTOA64(aValueToAssign, value_string);
// return Assign(value_string);
//}
ResultType Var::Assign(double aValueToAssign)
// It's best to call this method -- rather than manually converting to double -- so that the
// digits/formatting/precision is consistent throughout the program.
// Returns OK or FAIL.
{
char value_string[MAX_FORMATTED_NUMBER_LENGTH + 1];
return Assign(value_string
, snprintf(value_string, sizeof(value_string), g.FormatFloat, aValueToAssign)); // "%0.6f"; %f can handle doubles in MSVC++.);
}
ResultType Var::Assign(ExprTokenType &aToken)
// Returns OK or FAIL.
// Writes aToken's value into aOutputVar based on the type of the token.
// Caller must ensure that aToken.symbol is an operand (not an operator or other symbol).
// Caller must ensure that if aToken.symbol==SYM_VAR, aToken.var->Type()==VAR_NORMAL, not the clipboard or
// any built-in var. However, this->Type() can be VAR_CLIPBOARD.
{
switch (aToken.symbol)
{
case SYM_INTEGER: return Assign(aToken.value_int64); // Listed first for performance because it's Likely the most common from our callers.
case SYM_VAR:
{
// Below: Caller has ensured that aToken.var's Type() is always VAR_NORMAL.
// Below also relies on the fact that aliases can't point to other aliases (enforced by UpdateAlias()).
Var &source_var = *(aToken.var->mType == VAR_ALIAS ? aToken.var->mAliasFor : aToken.var);
return (source_var.mAttrib & VAR_ATTRIB_BINARY_CLIP) // Caller has ensured that source_var's Type() is VAR_NORMAL.
? AssignBinaryClip(source_var) // Caller wants a variable with binary contents assigned (copied) to another variable (usually VAR_CLIPBOARD).
: Assign(source_var.mContents, source_var.mLength); // Pass length to improve performance.
}
case SYM_FLOAT: return Assign(aToken.value_double); // Listed last because it's probably the least common.
default: return Assign(aToken.marker); // SYM_STRING or SYM_OPERAND.
}
}
ResultType Var::AssignClipboardAll()
// Caller must ensure that "this" is a normal variable or the clipboard (though if it's the clipboard, this
// function does nothing).
{
if (mType == VAR_ALIAS)
// For maintainability, it seems best not to use the following method:
// Var &var = *(mType == VAR_ALIAS ? mAliasFor : this);
// If that were done, bugs would be easy to introduce in a long function like this one
// if your forget at use the implicit "this" by accident. So instead, just call self.
return mAliasFor->AssignClipboardAll();
if (mType == VAR_CLIPBOARD) // Seems pointless to do Clipboard:=ClipboardAll, and the below isn't equipped
return OK; // to handle it, so make this have no effect.
if (!g_clip.Open())
return g_script.ScriptError(CANT_OPEN_CLIPBOARD_READ);
// Calculate the size needed:
// EnumClipboardFormats() retrieves all formats, including synthesized formats that don't
// actually exist on the clipboard but are instead constructed on demand. Unfortunately,
// there doesn't appear to be any way to reliably determine which formats are real and
// which are synthesized (if there were such a way, a large memory savings could be
// realized by omitting the synthesized formats from the saved version). One thing that
// is certain is that the "real" format(s) come first and the synthesized ones afterward.
// However, that's not quite enough because although it is recommended that apps store
// the primary/preferred format first, the OS does not enforce this. For example, testing
// shows that the apps do not have to store CF_UNICODETEXT prior to storing CF_TEXT,
// in which case the clipboard might have inaccurate CF_TEXT as the first element and
// more accurate/complete (non-synthesized) CF_UNICODETEXT stored as the next.
// In spite of the above, the below seems likely to be accurate 99% or more of the time,
// which seems worth it given the large savings of memory that are achieved, especially
// for large quantities of text or large images. Confidence is further raised by the
// fact that MSDN says there's no advantage/reason for an app to place multiple formats
// onto the clipboard if those formats are available through synthesis.
// And since CF_TEXT always(?) yields synthetic CF_OEMTEXT and CF_UNICODETEXT, and
// probably (but less certainly) vice versa: if CF_TEXT is listed first, it might certainly
// mean that the other two do not need to be stored. There is some slight doubt about this
// in a situation where an app explicitly put CF_TEXT onto the clipboard and then followed
// it with CF_UNICODETEXT that isn't synthesized, nor does it match what would have been
// synthesized. However, that seems extremely unlikely (it would be much more likely for
// an app to store CF_UNICODETEXT *first* followed by custom/non-synthesized CF_TEXT, but
// even that might be unheard of in practice). So for now -- since there is no documentation
// to be found about this anywhere -- it seems best to omit some of the most common
// synthesized formats:
// CF_TEXT is the first of three text formats to appear: Omit CF_OEMTEXT and CF_UNICODETEXT.
// (but not vice versa since those are less certain to be synthesized)
// (above avoids using four times the amount of memory that would otherwise be required)
// UPDATE: Only the first text format is included now, since MSDN says there is no
// advantage/reason to having multiple non-synthesized text formats on the clipboard.
// CF_DIB: Always omit this if CF_DIBV5 is available (which must be present on Win2k+, at least
// as a synthesized format, whenever CF_DIB is present?) This policy seems likely to avoid
// the issue where CF_DIB occurs first yet CF_DIBV5 that comes later is *not* synthesized,
// perhaps simply because the app stored DIB prior to DIBV5 by mistake (though there is
// nothing mandatory, so maybe it's not really a mistake). Note: CF_DIBV5 supports alpha
// channel / transparency, and perhaps other things, and it is likely that when synthesized,
// no information of the original CF_DIB is lost. Thus, when CF_DIBV5 is placed back onto
// the clipboard, any app that needs CF_DIB will have it synthesized back to the original
// data (hopefully). It's debatable whether to do it that way or store whichever comes first
// under the theory that an app would never store both formats on the clipboard since MSDN
// says: "If the system provides an automatic type conversion for a particular clipboard format,
// there is no advantage to placing the conversion format(s) on the clipboard."
bool format_is_text;
HGLOBAL hglobal;
SIZE_T size;
UINT format;
VarSizeType space_needed;
UINT dib_format_to_omit = 0, meta_format_to_omit = 0, text_format_to_include = 0;
// Start space_needed off at 4 to allow room for guaranteed final termination of the variable's contents.
// The termination must be of the same size as format because a single-byte terminator would
// be read in as a format of 0x00?????? where ?????? is an access violation beyond the buffer.
for (space_needed = sizeof(format), format = 0; format = EnumClipboardFormats(format);)
{
// No point in calling GetLastError() since it would never be executed because the loop's
// condition breaks on zero return value.
format_is_text = (format == CF_TEXT || format == CF_OEMTEXT || format == CF_UNICODETEXT);
if ((format_is_text && text_format_to_include) // The first text format has already been found and included, so exclude all other text formats.
|| format == dib_format_to_omit) // ... or this format was marked excluded by a prior iteration.
continue;
// GetClipboardData() causes Task Manager to report a (sometimes large) increase in
// memory utilization for the script, which is odd since it persists even after the
// clipboard is closed. However, when something new is put onto the clipboard by the
// the user or any app, that memory seems to get freed automatically. Also,
// GetClipboardData(49356) fails in MS Visual C++ when the copied text is greater than
// about 200 KB (but GetLastError() returns ERROR_SUCCESS). When pasting large sections
// of colorized text into MS Word, it can't get the colorized text either (just the plain
// text). Because of this example, it seems likely it can fail in other places or under
// other circumstances, perhaps by design of the app. Therefore, be tolerant of failures
// because partially saving the clipboard seems much better than aborting the operation.
if (hglobal = g_clip.GetClipboardDataTimeout(format))
{
space_needed += (VarSizeType)(sizeof(format) + sizeof(size) + GlobalSize(hglobal)); // The total amount of storage space required for this item.
if (format_is_text) // If this is true, then text_format_to_include must be 0 since above didn't "continue".
text_format_to_include = format;
if (!dib_format_to_omit)
{
if (format == CF_DIB)
dib_format_to_omit = CF_DIBV5;
else if (format == CF_DIBV5)
dib_format_to_omit = CF_DIB;
}
if (!meta_format_to_omit) // Checked for the same reasons as dib_format_to_omit.
{
if (format == CF_ENHMETAFILE)
meta_format_to_omit = CF_METAFILEPICT;
else if (format == CF_METAFILEPICT)
meta_format_to_omit = CF_ENHMETAFILE;
}
}
//else omit this format from consideration.
}
if (space_needed == sizeof(format)) // This works because even a single empty format requires space beyond sizeof(format) for storing its format+size.
{
g_clip.Close();
return Assign(); // Nothing on the clipboard, so just make the variable blank.
}
// Resize the output variable, if needed:
if (!Assign(NULL, space_needed - 1, true, false))
{
g_clip.Close();
return FAIL; // Above should have already reported the error.
}
// Retrieve and store all the clipboard formats. Because failures of GetClipboardData() are now
// tolerated, it seems safest to recalculate the actual size (actual_space_needed) of the data
// in case it varies from that found in the estimation phase. This is especially necessary in
// case GlobalLock() ever fails, since that isn't even attempted during the estimation phase.
// Otherwise, the variable's mLength member would be set to something too high (the estimate),
// which might cause problems elsewhere.
LPVOID hglobal_locked;
LPVOID binary_contents = mContents;
VarSizeType added_size, actual_space_used;
for (actual_space_used = sizeof(format), format = 0; format = EnumClipboardFormats(format);)
{
// No point in calling GetLastError() since it would never be executed because the loop's
// condition breaks on zero return value.
if ((format == CF_TEXT || format == CF_OEMTEXT || format == CF_UNICODETEXT) && format != text_format_to_include
|| format == dib_format_to_omit || format == meta_format_to_omit)
continue;
// Although the GlobalSize() documentation implies that a valid HGLOBAL should not be zero in
// size, it does happen, at least in MS Word and for CF_BITMAP. Therefore, in order to save
// the clipboard as accurately as possible, also save formats whose size is zero. Note that
// GlobalLock() fails to work on hglobals of size zero, so don't do it for them.
if ((hglobal = g_clip.GetClipboardDataTimeout(format)) // This and the next line rely on short-circuit boolean order.
&& (!(size = GlobalSize(hglobal)) || (hglobal_locked = GlobalLock(hglobal)))) // Size of zero or lock succeeded: Include this format.
{
// Any changes made to how things are stored here should also be made to the size-estimation
// phase so that space_needed matches what is done here:
added_size = (VarSizeType)(sizeof(format) + sizeof(size) + size);
actual_space_used += added_size;
if (actual_space_used > mCapacity) // Tolerate incorrect estimate by omitting formats that won't fit. Note that mCapacity is the granted capacity, which might be a little larger than requested.
actual_space_used -= added_size;
else
{
*(UINT *)binary_contents = format;
binary_contents = (char *)binary_contents + sizeof(format);
*(SIZE_T *)binary_contents = size;
binary_contents = (char *)binary_contents + sizeof(size);
if (size)
{
memcpy(binary_contents, hglobal_locked, size);
binary_contents = (char *)binary_contents + size;
}
//else hglobal_locked is not valid, so don't reference it or unlock it.
}
if (size)
GlobalUnlock(hglobal); // hglobal not hglobal_locked.
}
}
g_clip.Close();
*(UINT *)binary_contents = 0; // Final termination (must be UINT, see above).
mLength = actual_space_used - 1; // Omit the final zero-byte from the length in case any other routines assume that exactly one zero exists at the end of var's length.
mAttrib |= VAR_ATTRIB_BINARY_CLIP;
return OK;
}
ResultType Var::AssignBinaryClip(Var &aSourceVar)
// Caller must ensure that this->Type() is VAR_NORMAL or VAR_CLIPBOARD (usually via load-time validation).
// Caller must ensure that aSourceVar->Type()==VAR_NORMAL and aSourceVar->IsBinaryClip()==true.
{
if (mType == VAR_ALIAS)
// For maintainability, it seems best not to use the following method:
// Var &var = *(mType == VAR_ALIAS ? mAliasFor : this);
// If that were done, bugs would be easy to introduce in a long function like this one
// if your forget at use the implicit "this" by accident. So instead, just call self.
return mAliasFor->AssignBinaryClip(aSourceVar);
// Resolve early for maintainability.
// Relies on the fact that aliases can't point to other aliases (enforced by UpdateAlias()).
Var &source_var = (aSourceVar.mType == VAR_ALIAS) ? *aSourceVar.mAliasFor : aSourceVar;
if (mType == VAR_NORMAL) // Copy a binary variable to another variable that isn't the clipboard.
{
if (source_var.mContents == mContents) // v1.0.45: source==dest, so nothing to do. It's compared this way in case aSourceVar is a ByRef/alias. This covers even that situation.
return OK;
if (!Assign(NULL, source_var.mLength))
return FAIL; // Above should have already reported the error.
memcpy(mContents, source_var.mContents, source_var.mLength + 1); // Add 1 not sizeof(format). Note that mContents might have just changed due Assign() above.
mAttrib |= VAR_ATTRIB_BINARY_CLIP;
return OK; // No need to call Close() in this case.
}
// SINCE ABOVE DIDN'T RETURN, A VARIABLE CONTAINING BINARY CLIPBOARD DATA IS BEING COPIED BACK ONTO THE CLIPBOARD.
if (!g_clip.Open())
return g_script.ScriptError(CANT_OPEN_CLIPBOARD_WRITE);
EmptyClipboard(); // Failure is not checked for since it's probably impossible under these conditions.
// In case the variable contents are incomplete or corrupted (such as having been read in from a
// bad file with FileRead), prevent reading beyond the end of the variable:
LPVOID next, binary_contents = source_var.mContents; // Fix for v1.0.47.05: Changed aSourceVar to source_var in this line and the next.
LPVOID binary_contents_max = (char *)binary_contents + source_var.mLength + 1; // The last acessible byte, which should be the last byte of the (UINT)0 terminator.
HGLOBAL hglobal;
LPVOID hglobal_locked;
UINT format;
SIZE_T size;
while ((next = (char *)binary_contents + sizeof(format)) <= binary_contents_max
&& (format = *(UINT *)binary_contents)) // Get the format. Relies on short-circuit boolean order.
{
binary_contents = next;
if ((next = (char *)binary_contents + sizeof(size)) > binary_contents_max)
break;
size = *(UINT *)binary_contents; // Get the size of this format's data.
binary_contents = next;
if ((next = (char *)binary_contents + size) > binary_contents_max)
break;
if ( !(hglobal = GlobalAlloc(GMEM_MOVEABLE, size)) ) // size==0 is okay.
{
g_clip.Close();
return g_script.ScriptError(ERR_OUTOFMEM); // Short msg since so rare.
}
if (size) // i.e. Don't try to lock memory of size zero. It won't work and it's not needed.
{
if ( !(hglobal_locked = GlobalLock(hglobal)) )
{
GlobalFree(hglobal);
g_clip.Close();
return g_script.ScriptError("GlobalLock"); // Short msg since so rare.
}
memcpy(hglobal_locked, binary_contents, size);
GlobalUnlock(hglobal);
binary_contents = next;
}
//else hglobal is just an empty format, but store it for completeness/accuracy (e.g. CF_BITMAP).
SetClipboardData(format, hglobal); // The system now owns hglobal.
}
return g_clip.Close();
}
ResultType Var::Assign(char *aBuf, VarSizeType aLength, bool aExactSize, bool aObeyMaxMem)
// Returns OK or FAIL.
// If aBuf isn't NULL, caller must ensure that aLength is either VARSIZE_MAX (which tells us that the
// entire strlen() of aBuf should be used) or an explicit length (can be zero) that the caller must
// ensure is less than or equal to the total length of aBuf (if less, only a substring is copied).
// If aBuf is NULL, the variable will be set up to handle a string of at least aLength
// in length. In addition, if the var is the clipboard, it will be prepared for writing.
// Any existing contents of this variable will be destroyed regardless of whether aBuf is NULL.
// Note that aBuf's memory can safely overlap with that of this->Contents() because in that case the
// new length of the contents will always be less than or equal to the old length, and thus no
// reallocation/expanion is needed (such an expansion would free the source before it could be
// written to the destination). This is because callers pass in an aBuf that is either:
// 1) Between this->Contents() and its terminator.
// 2) Equal to this->Contents() but with aLength passed in as shorter than this->Length().
//
// Caller can omit both params to set a var to be empty-string, but in that case, if the variable
// is of large capacity, its memory will not be freed. This is by design because it allows the
// caller to exploit its knowledge of whether the var's large capacity is likely to be needed
// again in the near future, thus reducing the expected amount of memory fragmentation.
// To explicitly free the memory, use Assign("").
{
if (mType == VAR_ALIAS)
// For maintainability, it seems best not to use the following method:
// Var &var = *(mType == VAR_ALIAS ? mAliasFor : this);
// If that were done, bugs would be easy to introduce in a long function like this one
// if your forget at use the implicit "this" by accident. So instead, just call self.
return mAliasFor->Assign(aBuf, aLength, aExactSize);
bool do_assign = true; // Set defaults.
bool free_it_if_large = true; //
if (!aBuf)
if (aLength == VARSIZE_MAX) // Caller omitted this param too, so it wants to assign empty string.
{
free_it_if_large = false;
aLength = 0; // aBuf is set to "" further below.
}
else // Caller gave a NULL buffer to signal us to ensure the var is at least aLength in capacity.
do_assign = false;
else // Caller provided a non-NULL buffer.
if (aLength == VARSIZE_MAX) // Caller wants us to determine its length.
aLength = (mContents == aBuf) ? mLength : (VarSizeType)strlen(aBuf); // v1.0.45: Added optimization check: (mContents == aBuf).
//else leave aLength as the caller-specified value in case it's explicitly shorter than the apparent length.
if (!aBuf)
aBuf = ""; // From here on, make sure it's the empty string for all uses (read-only empty string vs. sEmptyString seems more appropriate in this case).
size_t space_needed = aLength + 1; // +1 for the zero terminator.
if (mType == VAR_CLIPBOARD)
{
if (do_assign)
// Just return the result of this. Note: The clipboard var's attributes,
// such as mLength, are not maintained because it's a variable whose
// contents usually aren't under our control. UPDATE: aTrimIt isn't
// needed because the clipboard is never assigned something that needs
// to be trimmed in this way (i.e. PerformAssign handles the trimming
// on its own for the clipboard, due to the fact that dereferencing
// into the temp buf is unnecessary when the clipboard is the target):
return g_clip.Set(aBuf, aLength); //, aTrimIt);
else
// We open it for write now, because some caller's don't call
// this function to write to the contents of the var, they
// do it themselves. Note: Below call will have displayed
// any error that occurred:
return g_clip.PrepareForWrite(space_needed) ? OK : FAIL;
}
// Since above didn't return, this variable isn't the clipboard.
if (space_needed > g_MaxVarCapacity && aObeyMaxMem) // v1.0.43.03: aObeyMaxMem was added since some callers aren't supposed to obey it.
return g_script.ScriptError(ERR_MEM_LIMIT_REACHED);
if (space_needed < 2) // Variable is being assigned the empty string (or a deref that resolves to it).
{
Free(free_it_if_large ? VAR_FREE_IF_LARGE : VAR_NEVER_FREE); // This also makes the variable blank and removes VAR_ATTRIB_BINARY_CLIP.
return OK;
}
// The below is done regardless of whether the section that follows it fails and returns early because
// it's the correct thing to do in all cases.
// For simplicity, this is done unconditionally even though it should be needed only
// when do_assign is true. It's the caller's responsibility to turn on the binary-clip
// attribute (if appropriate) by calling Var::Close() with the right option.
mAttrib &= ~VAR_ATTRIB_BINARY_CLIP;
// HOWEVER, other things like making mLength 0 and mContents blank are not done here for performance
// reasons (it seems too rare that early return/failure will occur below, since it's only due to
// out-of-memory... and even if it does happen, there a probably no consequences to leaving the variable
// the way it is now (rather than forcing it to be black) since the script thread that caused the error
// will be ended.
if (space_needed > mCapacity)
{
size_t new_size; // Use a new name, rather than overloaidng space_needed, for maintainability.
char *new_mem;
switch (mHowAllocated)
{
case ALLOC_NONE:
case ALLOC_SIMPLE:
if (space_needed <= MAX_ALLOC_SIMPLE)
{
// v1.0.31: Conserve memory within large arrays by allowing elements of length 1 or 7, for such
// things as the storage of boolean values, or the storage of short numbers (it's best to use
// multiples of 4 for size due to byte alignment in SimpleHeap; e.g. lengths of 3 and 7).
// Because the above checked that space_needed > mCapacity, the capacity will increase but
// never decrease in this section, which prevent a memory leak by only ever wasting a maximum
// of 2+7+MAX_ALLOC_SIMPLE for each variable (and then only in the worst case -- in the average
// case, it saves memory by avoiding the overhead incurred for each separate malloc'd block).
if (space_needed < 5) // Even for aExactSize, it seems best to prevent variables from having only a zero terminator in them because that would usually waste 3 bytes due to byte alignment in SimpleHeap.
new_size = 4; // v1.0.45: Increased from 2 to 4 to exploit byte alignment in SimpleHeap.
else if (aExactSize) // Allows VarSetCapacity() to make more flexible use of SimpleHeap.
new_size = space_needed;
else
{
if (space_needed < 9)
new_size = 8; // v1.0.45: Increased from 7 to 8 to exploit 32-bit alignment in SimpleHeap.
else // space_needed <= MAX_ALLOC_SIMPLE
new_size = MAX_ALLOC_SIMPLE;
}
// In the case of mHowAllocated==ALLOC_SIMPLE, the following will allocate another block
// from SimpleHeap even though the var already had one. This is by design because it can
// happen only a limited number of times per variable. See comments further above for details.
if ( !(new_mem = SimpleHeap::Malloc(new_size)) )
return FAIL; // It already displayed the error. Leave all var members unchanged so that they're consistent with each other. Don't bother making the var blank and its length zero for reasons described higher above.
mHowAllocated = ALLOC_SIMPLE; // In case it was previously ALLOC_NONE. This step must be done only after the alloc succeeded.
break;
}
// ** ELSE DON'T BREAK, JUST FALL THROUGH TO THE NEXT CASE. **
// **
case ALLOC_MALLOC: // Can also reach here by falling through from above.
// This case can happen even if space_needed is less than MAX_ALLOC_SIMPLE
// because once a var becomes ALLOC_MALLOC, it should never change to
// one of the other alloc modes. See comments higher above for explanation.
new_size = space_needed; // Below relies on this being initialized unconditionally.
if (!aExactSize)
{
// Allow a little room for future expansion to cut down on the number of
// free's and malloc's we expect to have to do in the future for this var:
if (new_size < 16) // v1.0.45.03: Added this new size to prevent all local variables in a recursive
new_size = 16; // function from having a minimum size of MAX_PATH. 16 seems like a good size because it holds nearly any number. It seems counterproductive to go too small because each malloc, no matter how small, could have around 40 bytes of overhead.
else if (new_size < MAX_PATH)
new_size = MAX_PATH; // An amount that will fit all standard filenames seems good.
else if (new_size < (160 * 1024)) // MAX_PATH to 160 KB or less -> 10% extra.
new_size = (size_t)(new_size * 1.1);
else if (new_size < (1600 * 1024)) // 160 to 1600 KB -> 16 KB extra
new_size += (16 * 1024);
else if (new_size < (6400 * 1024)) // 1600 to 6400 KB -> 1% extra
new_size = (size_t)(new_size * 1.01);
else // 6400 KB or more: Cap the extra margin at some reasonable compromise of speed vs. mem usage: 64 KB
new_size += (64 * 1024);
if (new_size > g_MaxVarCapacity && aObeyMaxMem) // v1.0.43.03: aObeyMaxMem was added since some callers aren't supposed to obey it.
new_size = g_MaxVarCapacity; // which has already been verified to be enough.
}
//else space_needed was already verified higher above to be within bounds.
// In case the old memory area is large, free it before allocating the new one. This reduces
// the peak memory load on the system and reduces the chance of an actual out-of-memory error.
bool memory_was_freed;
if (memory_was_freed = (mHowAllocated == ALLOC_MALLOC && mCapacity)) // Verified correct: 1) Both are checked because it might have fallen through from case ALLOC_SIMPLE; 2) mCapacity indicates for certain whether mContents contains the empty string.
free(mContents); // The other members are left temporarily out-of-sync for performance (they're resync'd only if an error occurs).
//else mContents contains a "" or it points to memory on SimpleHeap, so don't attempt to free it.
if ( new_size > 2147483647 || !(new_mem = (char *)malloc(new_size)) ) // v1.0.44.10: Added a sanity limit of 2 GB so that small negatives like VarSetCapacity(Var, -2) [and perhaps other callers of this function] don't crash.
{
if (memory_was_freed) // Resync members to reflect the fact that it was freed (it's done this way for performance).
{
mCapacity = 0; // Invariant: Anyone setting mCapacity to 0 must also set
mContents = sEmptyString; // mContents to the empty string.
mLength = 0;
}
// IMPORTANT: else it's the empty string (a constant) or it points to memory on SimpleHeap,
// so don't change mContents/Capacity (that would cause a memory leak for reasons described elsewhere).
// Also, don't bother making the variable blank and its length zero. Just leave its contents
// untouched due to the rarity of out-of-memory and the fact that the script thread will be terminated
// anyway, so in most cases won't care what the contents are.
return g_script.ScriptError(ERR_OUTOFMEM ERR_ABORT); // ERR_ABORT since an error is most likely to occur at runtime.
}
// Below is necessary because it might have fallen through from case ALLOC_SIMPLE.
// This step must be done only after the alloc succeeded (because otherwise, want to keep it
// set to ALLOC_SIMPLE (fall-through), if that's what it was).
mHowAllocated = ALLOC_MALLOC;
break;
} // switch()
// Since above didn't return, the alloc succeeded. Because that's true, all the members (except those
// set in their sections above) are updated together so that they stay consistent with each other:
mContents = new_mem;
mCapacity = (VarSizeType)new_size;
} // if (space_needed > mCapacity)
if (do_assign)
{
// Above has ensured that space_needed is either strlen(aBuf)-1 or the length of some
// substring within aBuf starting at aBuf. However, aBuf might overlap mContents or
// even be the same memory address (due to something like GlobalVar := YieldGlobalVar(),
// in which case ACT_ASSIGNEXPR calls us to assign GlobalVar to GlobalVar).
if (mContents != aBuf)
{
// Don't use strlcpy() or such because:
// 1) Caller might have specified that only part of aBuf's total length should be copied.
// 2) mContents and aBuf might overlap (see above comment), in which case strcpy()'s result
// is undefined, but memmove() is guaranteed to work (and performs about the same).
memmove(mContents, aBuf, aLength); // Some callers such as RegEx routines might rely on this copying binary zeroes over rather than stopping at the first binary zero.
}
//else nothing needs to be done since source and target are identical. Some callers probably rely on
// this optimization.
mContents[aLength] = '\0'; // v1.0.45: This is now done unconditionally in case caller wants to shorten a variable's existing contents (no known callers do this, but it helps robustness).
}
else // Caller only wanted the variable resized as a preparation for something it will do later.
{
// Init for greater robustness/safety (the ongoing conflict between robustness/redundancy and performance).
// This has been in effect for so long that some callers probably rely on it.
*mContents = '\0'; // If it's sEmptyVar, that's okay too because it's writable.
// We've done everything except the actual assignment. Let the caller handle that.
// Also, the length will be set below to the expected length in case the caller
// doesn't override this.
// Below: Already verified that the length value will fit into VarSizeType.
}
// Writing to union is safe because above already ensured that "this" isn't an alias.
mLength = aLength; // aLength was verified accurate higher above.
return OK;
}
VarSizeType Var::Get(char *aBuf)
// Returns the length of this var's contents. In addition, if aBuf isn't NULL, it will copy the contents into aBuf.
{
// For v1.0.25, don't do this because in some cases the existing contents of aBuf will not
// be altered. Instead, it will be set to blank as needed further below.
//if (aBuf) *aBuf = '\0'; // Init early to get it out of the way, in case of early return.
DWORD result;
VarSizeType length;
char buf_temp[1]; // Just a fake buffer to pass to some API functions in lieu of a NULL, to avoid any chance of misbehavior. Keep the size at 1 so that API functions will always fail to copy to buf.
switch(mType)
{
case VAR_NORMAL: // Listed first for performance.
if (!g_NoEnv && !mLength) // If auto-env retrival is on and the var is empty, check to see if it's really an env. var.
{
// Regardless of whether aBuf is NULL or not, we don't know at this stage
// whether mName is the name of a valid environment variable. Therefore,
// GetEnvironmentVariable() is currently called once in the case where
// aBuf is NULL and twice in the case where it's not. There may be some
// way to reduce it to one call always, but that is an optimization for
// the future. Another reason: Calling it twice seems safer, because we can't
// be completely sure that it wouldn't act on our (possibly undersized) aBuf
// buffer even if it doesn't find the env. var.
// UPDATE: v1.0.36.02: It turns out that GetEnvironmentVariable() is a fairly
// high overhead call. To improve the speed of accessing blank variables that
// aren't environment variables (and most aren't), cached_empty_var is used
// to indicate that the previous size-estimation call to us yielded "no such
// environment variable" so that the upcoming get-contents call to us can avoid
// calling GetEnvironmentVariable() again. Testing shows that this doubles
// the speed of a simple loop that accesses an empty variable such as the following:
// SetBatchLines -1
// Loop 500000
// if Var = Test
// ...
static Var *cached_empty_var = NULL; // Doubles the speed of accessing empty variables that aren't environment variables (i.e. most of them).
if (!(cached_empty_var == this && aBuf) && (result = GetEnvironmentVariable(mName, buf_temp, 0)))
{
// This env. var exists.
cached_empty_var = NULL; // i.e. one use only to avoid cache from hiding the fact that an environment variable has newly come into existence since the previous call.
return aBuf
? GetEnvVarReliable(mName, aBuf) // The caller has ensured, probably via previous call to this function with aBuf == NULL, that aBuf is large enough to hold the result.
: result - 1; // -1 because GetEnvironmentVariable() returns total size needed when called that way.
}
else // No matching env. var. or the cache indicates that GetEnvironmentVariable() need not be called.
{
if (aBuf)
{
*aBuf = '\0';
cached_empty_var = NULL; // i.e. one use only to avoid cache from hiding the fact that an environment variable has newly come into existence since the previous call.
}
else // Size estimation phase: Since there is no such env. var., flag it for the upcoming get-contents phase.
cached_empty_var = this;
return 0;
}
}
// Otherwise (since above didn't return), it's not an environment variable (or it is, but there's
// a script variable of non-zero length that's eclipsing it).
if (!aBuf)
return mLength;
else // Caller provider buffer, so if mLength is zero, just make aBuf empty now and return early (for performance).
if (!mLength)
{
*aBuf = '\0';
return 0;
}
//else continue on below.
if (aBuf == mContents)
// When we're called from ExpandArg() that was called from PerformAssign(), PerformAssign()
// relies on this check to avoid the overhead of copying a variables contents onto itself.
return mLength;
else if (mLength < 100000)
{
// Copy the var contents into aBuf. Although a little bit slower than CopyMemory() for large
// variables (say, over 100K), this loop seems much faster for small ones, which is the typical
// case. Also of note is that this code section is the main bottleneck for scripts that manipulate
// large variables, such as this:
//start_time = %A_TICKCOUNT%
//my = 0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
//tempvar =
//loop, 2000
// tempvar = %tempvar%%My%
//elapsed_time = %A_TICKCOUNT%
//elapsed_time -= %start_time%
//msgbox, elapsed_time = %elapsed_time%
//return
for (char *cp = mContents; *cp; *aBuf++ = *cp++);
*aBuf = '\0';
}
else
{
CopyMemory(aBuf, mContents, mLength); // Faster for large vars, but large vars aren't typical.
aBuf[mLength] = '\0'; // This is done as a step separate from above in case mLength is inaccurate (e.g. due to script's improper use of DllCall).
}
return mLength;
case VAR_ALIAS:
// For maintainability, it seems best not to use the following method:
// Var &var = *(mType == VAR_ALIAS ? mAliasFor : this);
// If that were done, bugs would be easy to introduce in a long function like this one
// if your forget at use the implicit "this" by accident. So instead, just call self.
return mAliasFor->Get(aBuf);
// Built-in vars with volatile contents:
case VAR_CLIPBOARD:
{
length = (VarSizeType)g_clip.Get(aBuf); // It will also copy into aBuf if it's non-NULL.
if (length == CLIPBOARD_FAILURE)
{
// Above already displayed the error, so just return.
// If we were called only to determine the size, the
// next call to g_clip.Get() will not put anything into
// aBuf (it will either fail again, or it will return
// a length of zero due to the clipboard not already
// being open & ready), so there's no danger of future
// buffer overflows as a result of returning zero here.
// Also, due to this function's return type, there's
// no easy way to terminate the current hotkey
// subroutine (or entire script) due to this error.
// However, due to the fact that multiple attempts
// are made to open the clipboard, failure should
// be extremely rare. And the user will be notified
// with a MsgBox anyway, during which the subroutine
// will be suspended:
length = 0;
}
if (aBuf)
aBuf[length] = '\0'; // Might not be necessary, but kept in case it ever is.
return length;
}
case VAR_CLIPBOARDALL: // There's a slight chance this case is never executed; but even if true, it should be kept for maintainability.
// This variable is directly handled at a higher level. As documented, any use of ClipboardAll outside of
// the supported modes yields an empty string.
if (aBuf)
*aBuf = '\0';
return 0;
default: // v1.0.46.16: VAR_BUILTIN: Call the function associated with this variable to retrieve its contents. This change reduced uncompressed coded size by 6 KB.
return mBIV(aBuf, mName);
} // switch(mType)
}
void Var::Free(int aWhenToFree, bool aExcludeAliases)
// The name "Free" is a little misleading because this function:
// ALWAYS sets the variable to be blank (except for static variables and aExcludeAliases==true).
// BUT ONLY SOMETIMES frees the memory, depending on various factors described further below.
// Caller must be aware that ALLOC_SIMPLE (due to its nature) is never freed.
{
// Not checked because even if it's not VAR_NORMAL, there are few if any consequences to continuing.
//if (mType != VAR_NORMAL) // For robustness, since callers generally shouldn't call it this way.
// return;
if (mType == VAR_ALIAS) // For simplicity and reduced code size, just make a recursive call to self.
{
if (!aExcludeAliases)
// For maintainability, it seems best not to use the following method:
// Var &var = *(mType == VAR_ALIAS ? mAliasFor : this);
// If that were done, bugs would be easy to introduce in a long function like this one
// if your forget at use the implicit "this" by accident. So instead, just call self.
mAliasFor->Free(aWhenToFree);
//else caller didn't want the target of the alias freed, so do nothing.
return;
}
// Must check this one first because caller relies not only on var not being freed in this case,
// but also on its contents not being set to an empty string:
if (aWhenToFree == VAR_ALWAYS_FREE_BUT_EXCLUDE_STATIC && (mAttrib & VAR_ATTRIB_STATIC))
return; // This is the only case in which the variable ISN'T made blank.
mLength = 0; // Writing to union is safe because above already ensured that "this" isn't an alias.
mAttrib &= ~VAR_ATTRIB_BINARY_CLIP; // Even if it isn't free'd, variable will be made blank. So it seems proper to always remove the binary_clip attribute (since it can't be used that way after it's been made blank).
switch (mHowAllocated)
{
// Shouldn't be necessary to check the following because by definition, ALLOC_NONE
// means mContents==sEmptyString (this policy is enforced in other places).
//
//case ALLOC_NONE:
// mContents = sEmptyString;
// break;
case ALLOC_SIMPLE:
// Don't set to sEmptyString because then we'd have a memory leak. i.e. once a var becomes
// ALLOC_SIMPLE, it should never become ALLOC_NONE again (though it can become ALLOC_MALLOC).
*mContents = '\0';
break;
case ALLOC_MALLOC:
// Setting a var whose contents are very large to be nothing or blank is currently the
// only way to free up the memory of that var. Shrinking it dynamically seems like it
// might introduce too much memory fragmentation and overhead (since in many cases,
// it would likely need to grow back to its former size in the near future). So we
// only free relatively large vars:
if (mCapacity)
{
// aWhenToFree==VAR_FREE_IF_LARGE: the memory is not freed if it is a small area because
// it might help reduce memory fragmentation amd improve performance in cases where
// the memory will soon be needed again (in which case one free+malloc is saved).
if ( aWhenToFree < VAR_ALWAYS_FREE_LAST // Fixed for v1.0.40.07 to prevent memory leak in recursive script-function calls.
|| aWhenToFree == VAR_FREE_IF_LARGE && mCapacity > (4 * 1024) )
{
free(mContents);
mCapacity = 0; // Invariant: Anyone setting mCapacity to 0 must also set
mContents = sEmptyString; // mContents to the empty string.
// BUT DON'T CHANGE mHowAllocated to ALLOC_NONE (see comments further below).
}
else // Don't actually free it, but make it blank (callers rely on this).
*mContents = '\0';
}
//else mCapacity==0, so mContents is already the empty string, so don't attempt to free
// it or assign to it. It was the responsibility of whoever set mCapacity to 0 to ensure mContents
// was set properly (which is why it's not done here).
// But do not change mHowAllocated to be ALLOC_NONE because it would cause a
// a memory leak in this sequence of events:
// var1 is assigned something short enough to make it ALLOC_SIMPLE
// var1 is assigned something large enough to require malloc()
// var1 is set to empty string and its mem is thus free()'d by the above.
// var1 is once again assigned something short enough to make it ALLOC_SIMPLE
// The last step above would be a problem because the 2nd ALLOC_SIMPLE can't
// reclaim the spot in SimpleHeap that had been in use by the first. In other
// words, when a var makes the transition from ALLOC_SIMPLE to ALLOC_MALLOC,
// its ALLOC_SIMPLE memory is lost to the system until the program exits.
// But since this loss occurs at most once per distinct variable name,
// it's not considered a memory leak because the loss can't exceed a fixed
// amount regardless of how long the program runs. The reason for all of this
// is that allocating dynamic memory is costly: it causes system memory fragmentation,
// (especially if a var were to be malloc'd and free'd thousands of times in a loop)
// and small-sized mallocs have a large overhead: it's been said that every block
// of dynamic mem, even those of size 1 or 2, incurs about 40 bytes of overhead
// (and testing of SimpleHeap supports this).
// UPDATE: Yes it's true that a script could create an unlimited number of new variables
// by constantly using different dynamic names when creating them. But it's still supportable
// that there's no memory leak because even if the worst-case series of events mentioned
// higher above occurs for every variable, only a fixed amount of memory is ever lost per
// variable. So you could think of the lost memory as part of the basic overhead of creating
// a variable (since variables are never truly destroyed, just their contents freed).
// The odds against all of these worst-case factors occurring simultaneously in anything other
// than a theoretical test script seem nearly astronomical.
break;
} // switch()
}
ResultType Var::AppendIfRoom(char *aStr, VarSizeType aLength)
// Returns OK if there's room enough to append aStr and it succeeds.
// Returns FAIL otherwise (also returns FAIL for VAR_CLIPBOARD).
// Environment variables aren't supported here; instead, aStr is appended directly onto the actual/internal
// contents of the "this" variable.
{
// Relies on the fact that aliases can't point to other aliases (enforced by UpdateAlias()):
Var &var = *(mType == VAR_ALIAS ? mAliasFor : this);
if (var.mType != VAR_NORMAL) // e.g. VAR_CLIPBOARD. Some callers do call it this way, but even if not it should be kept for maintainability.
return FAIL; // CHECK THIS FIRST, BEFORE BELOW, BECAUSE CALLERS ALWAYS WANT IT TO BE A FAILURE.
if (!aLength) // Consider the appending of nothing (even onto unsupported things like clipboard) to be a success.
return OK;
VarSizeType var_length = LengthIgnoreBinaryClip(); // Get the apparent length because one caller is a concat that wants consistent behavior of the .= operator regardless of whether this shortcut succeeds or not.
VarSizeType new_length = var_length + aLength;
if (new_length >= var.mCapacity) // Not enough room.
return FAIL;
memmove(var.mContents + var_length, aStr, aLength); // memmove() vs. memcpy() in case there's any overlap between source and dest.
var.mContents[new_length] = '\0'; // Terminate it as a separate step in case caller passed a length shorter than the apparent length of aStr.
var.mLength = new_length;
// If this is a binary-clip variable, appending has probably "corrupted" it; so don't allow it to ever be
// put back onto the clipboard as binary data (the routine that does that is designed to detect corruption,
// but it might not be perfect since corruption is so rare).
var.mAttrib &= ~VAR_ATTRIB_BINARY_CLIP;
return OK;
}
void Var::AcceptNewMem(char *aNewMem, VarSizeType aLength)
// Caller provides a new malloc'd memory block (currently must be non-NULL). That block and its
// contents are directly hung onto this variable in place of its old block, which is freed (except
// in the case of VAR_CLIPBOARD, in which case the memory is copied onto the clipboard then freed).
// Caller must ensure that mType == VAR_NORMAL or VAR_CLIPBOARD.
// This function was added in v1.0.45 to aid callers in improving performance.
{
// Relies on the fact that aliases can't point to other aliases (enforced by UpdateAlias()).
Var &var = *(mType == VAR_ALIAS ? mAliasFor : this);
if (var.mType == VAR_CLIPBOARD)
{
var.Assign(aNewMem, aLength); // Clipboard requires GlobalAlloc memory so can't directly accept aNewMem. So just copy it the normal way.
free(aNewMem); // Caller gave it to us to take charge of, but we have no further use for it.
}
else // VAR_NORMAL
{
var.Free(VAR_ALWAYS_FREE); // Release the variable's old memory.
var.mHowAllocated = ALLOC_MALLOC; // Must always be this type to avoid complications and possible memory leaks.
var.mAttrib &= ~VAR_ATTRIB_BINARY_CLIP; // New memory is always non-binary-clip. A new parameter could be added to change this if it's ever needed.
var.mContents = aNewMem;
var.mLength = aLength;
var.mCapacity = (VarSizeType)_msize(aNewMem); // Get actual capacity in case it's a lot bigger than aLength+1. _msize() is only about 36 bytes of code and probably a very fast call.
// Shrink the memory if there's a lot of wasted space because the extra capacity is seldom utilized
// in real-world scripts.
// A simple rule seems best because shrinking is probably fast regardless of the sizes involved,
// plus and there's consierable rarity to ever needing capacity beyond what's in a variable
// (concat/append is one example). This will leave a large percentage of extra space in small variables,
// but in those rare cases when a script needs to create thousands of such variables, there may be a
// current or future way to shrink an existing variable to contain only its current length, such as
// VarShrink().
if (var.mCapacity - var.mLength > 64)
{
var.mCapacity = var.mLength + 1; // This will become the new capacity.
// _expand() is only about 75 bytes of uncompressed code size and probably performs very quickly
// when shrinking. Also, MSDN implies that when shrinking, failure won't happen unless something
// is terribly wrong (e.g. corrupted heap). But for robustness it is checked anyway:
if ( !(var.mContents = (char *)_expand(var.mContents, var.mCapacity)) )
{
var.mLength = 0;
var.mCapacity = 0;
}
}
}
}
void Var::SetLengthFromContents()
// Function added in v1.0.43.06. It updates the mLength member to reflect the actual current length of mContents.
{
// Relies on the fact that aliases can't point to other aliases (enforced by UpdateAlias()).
Var &var = *(mType == VAR_ALIAS ? mAliasFor : this);
VarSizeType capacity = var.Capacity();
// Below uses Contents() and Length() vs. direct members for better maintainability, even though
// it might not be strictly necessary.
char *contents = var.Contents();
if (capacity > 0)
{
contents[capacity - 1] = '\0'; // Caller wants us to ensure it's terminated, to avoid crashing strlen() below.
var.mLength = (VarSizeType)strlen(contents);
}
//else it has no capacity, so do nothing (it could also be a reserved/built-in variable).
}
ResultType Var::BackupFunctionVars(Func &aFunc, VarBkp *&aVarBackup, int &aVarBackupCount)
// All parameters except the first are output parameters that are set for our caller (though caller
// is responsible for having initialized aVarBackup to NULL).
// If there is nothing to backup, only the aVarBackupCount is changed (to zero).
// Returns OK or FAIL.
{
if ( !(aVarBackupCount = aFunc.mVarCount + aFunc.mLazyVarCount) ) // Nothing needs to be backed up.
return OK; // Leave aVarBackup set to NULL as set by the caller.
// NOTES ABOUT MALLOC(): Apparently, the implementation of malloc() is quite good, at least for small blocks
// needed to back up 50 or less variables. It nearly as fast as alloca(), at least when the system
// isn't under load and has the memory to spare without swapping. Therefore, the attempt to use alloca to
// speed up recursive script-functions didn't result in enough of a speed-up (only 1 to 5%) to be worth the
// added complexity.
// Since Var is not a POD struct (it contains private members, a custom constructor, etc.), the VarBkp
// POD struct is used to hold the backup because it's probably better performance than using Var's
// constructor to create each backup array element.
if ( !(aVarBackup = (VarBkp *)malloc(aVarBackupCount * sizeof(VarBkp))) ) // Caller will take care of freeing it.
return FAIL;
int i;
aVarBackupCount = 0; // Init only once prior to both loops. aVarBackupCount is being "overloaded" to track the current item in aVarBackup, BUT ALSO its being updated to an actual count in case some statics are omitted from the array.
// Note that Backup() does not make the variable empty after backing it up because that is something
// that must be done by our caller at a later stage.
for (i = 0; i < aFunc.mVarCount; ++i)
if (!(aFunc.mVar[i]->mAttrib & VAR_ATTRIB_STATIC)) // Don't bother backing up statics because they won't need to be restored.
aFunc.mVar[i]->Backup(aVarBackup[aVarBackupCount++]);
for (i = 0; i < aFunc.mLazyVarCount; ++i)
if (!(aFunc.mLazyVar[i]->mAttrib & VAR_ATTRIB_STATIC)) // Don't bother backing up statics because they won't need to be restored.
aFunc.mLazyVar[i]->Backup(aVarBackup[aVarBackupCount++]);
return OK;
}
void Var::Backup(VarBkp &aVarBkp)
// Caller must not call this function for static variables because it's not equipped to deal with them
// (they don't need to be backed up or restored anyway).
// This method is used rather than struct copy (=) because it's of expected higher performance than
// using the Var::constructor to make a copy of each var. Also note that something like memcpy()
// can't be used on Var objects since they're not POD (e.g. they have a contructor and they have
// private members).
{
aVarBkp.mVar = this; // Allows the restoration process to always know its target without searching.
aVarBkp.mContents = mContents;
aVarBkp.mLength = mLength; // Since it's a union, it might actually be backing up mAliasFor (happens at least for recursive functions that pass parameters ByRef).
aVarBkp.mCapacity = mCapacity;
aVarBkp.mHowAllocated = mHowAllocated; // This might be ALLOC_SIMPLE or ALLOC_NONE if backed up variable was at the lowest layer of the call stack.
aVarBkp.mAttrib = mAttrib;
aVarBkp.mType = mType; // Fix for v1.0.47.06: Must also back up and restore mType in case an optional ByRef parameter is omitted by one call by specified by another thread that interrupts the first thread's call.
// Once the backup is made, Free() is not called because the whole point of the backup is to
// preserve the original memory/contents of each variable. Instead, clear the variable
// completely and set it up to become ALLOC_MALLOC in case anything actually winds up using
// the variable prior to the restoration of the backup. In other words, ALLOC_SIMPLE and NONE
// retained (if present) because that would cause a memory leak when multiple layers are all
// allowed to use ALLOC_SIMPLE yet none are ever able to free it (the bottommost layer is
// allowed to use ALLOC_SIMPLE because that's a fixed/constant amount of memory gets freed