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.
//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:
// 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.
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.
//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.
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
// 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.
// 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.
// 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).
// 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.
// 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;
// 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
// when the program exits).
// Now reset this variable (caller has ensured it's non-static) to create a "new layer" for it, keeping
// its backup intact but allowing this variable (or formal parameter) to be given a new value in the future:
mCapacity = 0; // Invariant: Anyone setting mCapacity to 0 must also set...
mContents = sEmptyString; // ...mContents to the empty string.
if (mType != VAR_ALIAS) // Fix for v1.0.42.07: Don't reset mLength if the other member of the union is in effect.
mLength = 0; // Otherwise, functions that recursively pass ByRef parameters can crash because mType stays as VAR_ALIAS.
mHowAllocated = ALLOC_MALLOC; // Never NONE because that would permit SIMPLE. See comments higher above.
mAttrib &= ~VAR_ATTRIB_BINARY_CLIP; // But the VAR_ATTRIB_STATIC flag isn't altered.
}
void Var::FreeAndRestoreFunctionVars(Func &aFunc, VarBkp *&aVarBackup, int &aVarBackupCount)
{
int i;
for (i = 0; i < aFunc.mVarCount; ++i)
aFunc.mVar[i]->Free(VAR_ALWAYS_FREE_BUT_EXCLUDE_STATIC, true); // Pass "true" to exclude aliases, since their targets should not be freed (they don't belong to this function).
// The freeing (above) MUST be done prior to the restore-from-backup below (otherwise there would be
// a memory leak). Static variables are never backed up and thus do not exist in the aVarBackup array.
// This is because by definition, the contents of statics are not freed or altered by the calling procedure
// (regardless how how recursive or multi-threaded the function is).
if (aVarBackup) // This is the indicator that a backup was made; thus a restore is also needed.
{
for (i = 0; i < aVarBackupCount; ++i) // Static variables were never backed up so they won't be in this array. See comments above.
{
VarBkp &bkp = aVarBackup[i]; // Resolve only once for performance.
Var &var = *bkp.mVar; //
var.mContents = bkp.mContents;
var.mLength = bkp.mLength; // Since it's a union, it might actually be restoring mAliasFor, which is desired.
var.mCapacity = bkp.mCapacity;
var.mHowAllocated = bkp.mHowAllocated; // This might be ALLOC_SIMPLE or ALLOC_NONE if backed up variable was at the lowest layer of the call stack.
var.mAttrib = bkp.mAttrib;
}
free(aVarBackup);
aVarBackup = NULL; // Some callers want this reset; it's an indicator of whether the next function call in this expression (if any) will have a backup.
}
}
ResultType Var::ValidateName(char *aName, bool aIsRuntime, int aDisplayError)
// Returns OK or FAIL.
{
if (!*aName) return FAIL;
// Seems best to disallow variables that start with numbers so that more advanced
// parsing (e.g. expressions) can be more easily added in the future. UPDATE: For
// backward compatibility with AutoIt2's technique of storing command line args in
// a numically-named var (e.g. %1% is the first arg), decided not to do this:
//if (*aName >= '0' && *aName <= '9')
// return g_script.ScriptError("This variable name starts with a number, which is not allowed.", aName);