diff options
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 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); } |
