diff options
author | griffin <briang42@easystreet.net> | 2023-05-23 21:36:46 (GMT) |
---|---|---|
committer | griffin <briang42@easystreet.net> | 2023-05-23 21:36:46 (GMT) |
commit | e91f967003cb7c4aa4512e9afc4bf5199e48ba92 (patch) | |
tree | 90757e0d4dc74d64240ebf00ddaa08f36f1a4854 /generic/tclBinary.c | |
parent | fa313109d6a46e437c1583c859b6e79e0a6dae2a (diff) | |
parent | 54a1d7e517922684d8612dea07b479a6d493e2f1 (diff) | |
download | tcl-e91f967003cb7c4aa4512e9afc4bf5199e48ba92.zip tcl-e91f967003cb7c4aa4512e9afc4bf5199e48ba92.tar.gz tcl-e91f967003cb7c4aa4512e9afc4bf5199e48ba92.tar.bz2 |
Merge trunk
Diffstat (limited to 'generic/tclBinary.c')
-rw-r--r-- | generic/tclBinary.c | 45 |
1 files changed, 8 insertions, 37 deletions
diff --git a/generic/tclBinary.c b/generic/tclBinary.c index de5ee73..c97ee2e 100644 --- a/generic/tclBinary.c +++ b/generic/tclBinary.c @@ -767,43 +767,14 @@ TclAppendBytesToByteArray( } needed = byteArrayPtr->used + len; if (needed > byteArrayPtr->allocated) { - ByteArray *ptr = NULL; - - /* - * Try to allocate double the total space that is needed. - */ - - Tcl_Size attempt; - - /* Make sure we do not wrap when doubling */ - if (needed <= (BYTEARRAY_MAX_LEN - needed)) { - attempt = 2 * needed; - ptr = (ByteArray *) Tcl_AttemptRealloc(byteArrayPtr, - BYTEARRAY_SIZE(attempt)); - } - - if (ptr == NULL) { - /* - * Try to allocate double the increment that is needed. - * (Originally TCL_MIN_GROWTH was added as well but that would - * need one more separate overflow check so forget it.) - */ - if (len <= (BYTEARRAY_MAX_LEN - needed)) { - attempt = needed + len; - ptr = (ByteArray *)Tcl_AttemptRealloc(byteArrayPtr, - BYTEARRAY_SIZE(attempt)); - } - } - if (ptr == NULL) { - /* - * Last chance: Try to allocate exactly what is needed. - */ - - attempt = needed; - ptr = (ByteArray *)Tcl_Realloc(byteArrayPtr, BYTEARRAY_SIZE(attempt)); - } - byteArrayPtr = ptr; - byteArrayPtr->allocated = attempt; + Tcl_Size newCapacity; + byteArrayPtr = + (ByteArray *)TclReallocElemsEx(byteArrayPtr, + needed, + 1, + offsetof(ByteArray, bytes), + &newCapacity); + byteArrayPtr->allocated = newCapacity; SET_BYTEARRAY(irPtr, byteArrayPtr); } |