summaryrefslogtreecommitdiffstats
path: root/generic/tclBinary.c
diff options
context:
space:
mode:
authorapnadkarni <apnmbx-wits@yahoo.com>2023-05-04 17:52:06 (GMT)
committerapnadkarni <apnmbx-wits@yahoo.com>2023-05-04 17:52:06 (GMT)
commit92dd14e77c81c060ff6ede641885b928afdb9ec3 (patch)
tree09f32df9e6435a6c51c396584c19e9d284869e2b /generic/tclBinary.c
parent8a45e47c4c3881f7c0db88276adfa26ac9712459 (diff)
downloadtcl-92dd14e77c81c060ff6ede641885b928afdb9ec3.zip
tcl-92dd14e77c81c060ff6ede641885b928afdb9ec3.tar.gz
tcl-92dd14e77c81c060ff6ede641885b928afdb9ec3.tar.bz2
Refactor reallocation in preparation for experimentation with different growth factors
Diffstat (limited to 'generic/tclBinary.c')
-rw-r--r--generic/tclBinary.c36
1 files changed, 10 insertions, 26 deletions
diff --git a/generic/tclBinary.c b/generic/tclBinary.c
index dd8b292..4215913 100644
--- a/generic/tclBinary.c
+++ b/generic/tclBinary.c
@@ -768,37 +768,21 @@ 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));
+ /* First try to overallocate, reducing overallocation on each fail */
+ attempt =
+ TclUpsizeAlloc(byteArrayPtr->allocated, needed, BYTEARRAY_MAX_LEN);
+ while (attempt > needed) {
+ ptr = (ByteArray *)Tcl_AttemptRealloc(byteArrayPtr,
+ BYTEARRAY_SIZE(attempt));
+ if (ptr)
+ break;
+ attempt = TclUpsizeRetry(needed, 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.
- */
-
+ /* Last chance: Try to allocate exactly what is needed. */
attempt = needed;
ptr = (ByteArray *)Tcl_Realloc(byteArrayPtr, BYTEARRAY_SIZE(attempt));
}