summaryrefslogtreecommitdiffstats
path: root/generic/tclBinary.c
diff options
context:
space:
mode:
authorapnadkarni <apnmbx-wits@yahoo.com>2023-05-20 16:39:50 (GMT)
committerapnadkarni <apnmbx-wits@yahoo.com>2023-05-20 16:39:50 (GMT)
commitf19e2665d309df5a484a53ea6d9b1098275e33db (patch)
tree6906aa04fcb87740b514d40f1b4c83435a53a645 /generic/tclBinary.c
parent72dfeb60e23c679d2c7258db4eb9ccce0a0661fa (diff)
parent96d441c29a2a47269655285a02c546765c163fd2 (diff)
downloadtcl-f19e2665d309df5a484a53ea6d9b1098275e33db.zip
tcl-f19e2665d309df5a484a53ea6d9b1098275e33db.tar.gz
tcl-f19e2665d309df5a484a53ea6d9b1098275e33db.tar.bz2
Fix [c9663296fd]. Also refactor memory reallocation.
Diffstat (limited to 'generic/tclBinary.c')
-rw-r--r--generic/tclBinary.c45
1 files changed, 8 insertions, 37 deletions
diff --git a/generic/tclBinary.c b/generic/tclBinary.c
index dd8b292..81ea3f3 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);
}