summaryrefslogtreecommitdiffstats
path: root/generic/tclIO.c
diff options
context:
space:
mode:
Diffstat (limited to 'generic/tclIO.c')
-rw-r--r--generic/tclIO.c88
1 files changed, 55 insertions, 33 deletions
diff --git a/generic/tclIO.c b/generic/tclIO.c
index 1a9ff65..7216423 100644
--- a/generic/tclIO.c
+++ b/generic/tclIO.c
@@ -1547,6 +1547,7 @@ Tcl_CreateChannel(
chanPtr->upChanPtr = NULL;
chanPtr->inQueueHead = NULL;
chanPtr->inQueueTail = NULL;
+ chanPtr->refCount = 0;
/*
* TIP #219, Tcl Channel Reflection API
@@ -1762,6 +1763,7 @@ Tcl_StackChannel(
chanPtr->upChanPtr = NULL;
chanPtr->inQueueHead = NULL;
chanPtr->inQueueTail = NULL;
+ chanPtr->refCount = 0;
/*
* Place new block at the head of a possibly existing list of previously
@@ -1789,6 +1791,31 @@ Tcl_StackChannel(
return (Tcl_Channel) chanPtr;
}
+
+void
+TclChannelPreserve(
+ Tcl_Channel chan)
+{
+ ((Channel *)chan)->refCount++;
+}
+
+void
+TclChannelRelease(
+ Tcl_Channel chan)
+{
+ Channel *chanPtr = (Channel *) chan;
+
+ if (chanPtr->refCount == 0) {
+ Tcl_Panic("Channel released more than preserved");
+ }
+ if (--chanPtr->refCount) {
+ return;
+ }
+ if (chanPtr->typePtr == NULL) {
+ ckfree((char *)chanPtr);
+ }
+}
+
/*
*----------------------------------------------------------------------
@@ -1949,11 +1976,6 @@ Tcl_UnstackChannel(
chanPtr->typePtr = NULL;
- /*
- * AK: Tcl_NotifyChannel may hold a reference to this block of memory
- */
-
- Tcl_EventuallyFree(chanPtr, TCL_DYNAMIC);
UpdateInterest(statePtr->topChanPtr);
if (result != 0) {
@@ -2545,6 +2567,7 @@ FlushChannel(
* of the queued output to the channel.
*/
+ TclChannelPreserve((Tcl_Channel)chanPtr);
while (statePtr->outQueueHead) {
bufPtr = statePtr->outQueueHead;
@@ -2692,7 +2715,7 @@ FlushChannel(
if (GotFlag(statePtr, BG_FLUSH_SCHEDULED)) {
if (wroteSome) {
- return errorCode;
+ goto done;
} else if (statePtr->outQueueHead == NULL) {
ResetFlag(statePtr, BG_FLUSH_SCHEDULED);
(chanPtr->typePtr->watchProc)(chanPtr->instanceData,
@@ -2716,8 +2739,10 @@ FlushChannel(
(statePtr->outQueueHead == NULL) &&
((statePtr->curOutPtr == NULL) ||
IsBufferEmpty(statePtr->curOutPtr))) {
- return CloseChannel(interp, chanPtr, errorCode);
+ errorCode = CloseChannel(interp, chanPtr, errorCode);
}
+ done:
+ TclChannelRelease((Tcl_Channel)chanPtr);
return errorCode;
}
@@ -2890,7 +2915,6 @@ CloseChannel(
downChanPtr->upChanPtr = NULL;
chanPtr->typePtr = NULL;
- Tcl_EventuallyFree(chanPtr, TCL_DYNAMIC);
return Tcl_Close(interp, (Tcl_Channel) downChanPtr);
}
@@ -2898,13 +2922,11 @@ CloseChannel(
* There is only the TOP Channel, so we free the remaining pointers we
* have and then ourselves. Since this is the last of the channels in the
* stack, make sure to free the ChannelState structure associated with it.
- * We use Tcl_EventuallyFree to allow for any last references.
*/
chanPtr->typePtr = NULL;
Tcl_EventuallyFree(statePtr, TCL_DYNAMIC);
- Tcl_EventuallyFree(chanPtr, TCL_DYNAMIC);
return errorCode;
}
@@ -3965,7 +3987,7 @@ Tcl_GetsObj(
*/
chanPtr = statePtr->topChanPtr;
- Tcl_Preserve(chanPtr);
+ TclChannelPreserve((Tcl_Channel)chanPtr);
bufPtr = statePtr->inQueueHead;
encoding = statePtr->encoding;
@@ -4201,9 +4223,9 @@ Tcl_GetsObj(
*/
if (chanPtr != statePtr->topChanPtr) {
- Tcl_Release(chanPtr);
+ TclChannelRelease((Tcl_Channel)chanPtr);
chanPtr = statePtr->topChanPtr;
- Tcl_Preserve(chanPtr);
+ TclChannelPreserve((Tcl_Channel)chanPtr);
}
bufPtr = gs.bufPtr;
@@ -4239,9 +4261,9 @@ Tcl_GetsObj(
* self-modifying reflected transforms.
*/
if (chanPtr != statePtr->topChanPtr) {
- Tcl_Release(chanPtr);
+ TclChannelRelease((Tcl_Channel)chanPtr);
chanPtr = statePtr->topChanPtr;
- Tcl_Preserve(chanPtr);
+ TclChannelPreserve((Tcl_Channel)chanPtr);
}
bufPtr = statePtr->inQueueHead;
if (bufPtr != NULL) {
@@ -4283,12 +4305,12 @@ Tcl_GetsObj(
* self-modifying reflected transforms.
*/
if (chanPtr != statePtr->topChanPtr) {
- Tcl_Release(chanPtr);
+ TclChannelRelease((Tcl_Channel)chanPtr);
chanPtr = statePtr->topChanPtr;
- Tcl_Preserve(chanPtr);
+ TclChannelPreserve((Tcl_Channel)chanPtr);
}
UpdateInterest(chanPtr);
- Tcl_Release(chanPtr);
+ TclChannelRelease((Tcl_Channel)chanPtr);
return copiedTotal;
}
@@ -4334,7 +4356,7 @@ TclGetsObjBinary(
*/
chanPtr = statePtr->topChanPtr;
- Tcl_Preserve(chanPtr);
+ TclChannelPreserve((Tcl_Channel)chanPtr);
bufPtr = statePtr->inQueueHead;
@@ -4535,7 +4557,7 @@ TclGetsObjBinary(
done:
UpdateInterest(chanPtr);
- Tcl_Release(chanPtr);
+ TclChannelRelease((Tcl_Channel)chanPtr);
return copiedTotal;
}
@@ -5159,7 +5181,7 @@ DoReadChars(
chanPtr = statePtr->topChanPtr;
encoding = statePtr->encoding;
factor = UTF_EXPANSION_FACTOR;
- Tcl_Preserve(chanPtr);
+ TclChannelPreserve((Tcl_Channel)chanPtr);
binaryMode = (encoding == NULL)
&& (statePtr->inputTranslation == TCL_TRANSLATE_LF)
@@ -5221,9 +5243,9 @@ DoReadChars(
}
result = GetInput(chanPtr);
if (chanPtr != statePtr->topChanPtr) {
- Tcl_Release(chanPtr);
+ TclChannelRelease((Tcl_Channel)chanPtr);
chanPtr = statePtr->topChanPtr;
- Tcl_Preserve(chanPtr);
+ TclChannelPreserve((Tcl_Channel)chanPtr);
}
if (result != 0) {
if (!GotFlag(statePtr, CHANNEL_BLOCKED)) {
@@ -5251,9 +5273,9 @@ DoReadChars(
* self-modifying reflected transforms.
*/
if (chanPtr != statePtr->topChanPtr) {
- Tcl_Release(chanPtr);
+ TclChannelRelease((Tcl_Channel)chanPtr);
chanPtr = statePtr->topChanPtr;
- Tcl_Preserve(chanPtr);
+ TclChannelPreserve((Tcl_Channel)chanPtr);
}
/*
@@ -5261,7 +5283,7 @@ DoReadChars(
* in the buffers.
*/
UpdateInterest(chanPtr);
- Tcl_Release(chanPtr);
+ TclChannelRelease((Tcl_Channel)chanPtr);
return copied;
}
@@ -7659,7 +7681,7 @@ Tcl_NotifyChannel(
* Preserve the channel struct in case the script closes it.
*/
- Tcl_Preserve(channel);
+ TclChannelPreserve((Tcl_Channel)channel);
Tcl_Preserve(statePtr);
/*
@@ -7709,7 +7731,7 @@ Tcl_NotifyChannel(
}
Tcl_Release(statePtr);
- Tcl_Release(channel);
+ TclChannelRelease(channel);
tsdPtr->nestedHandlerPtr = nh.nestedHandlerPtr;
}
@@ -8189,7 +8211,7 @@ TclChannelEventScriptInvoker(
*/
Tcl_Preserve(interp);
- Tcl_Preserve(chanPtr);
+ TclChannelPreserve((Tcl_Channel)chanPtr);
result = Tcl_EvalObjEx(interp, esPtr->scriptPtr, TCL_EVAL_GLOBAL);
/*
@@ -8206,7 +8228,7 @@ TclChannelEventScriptInvoker(
}
TclBackgroundException(interp, result);
}
- Tcl_Release(chanPtr);
+ TclChannelRelease((Tcl_Channel)chanPtr);
Tcl_Release(interp);
}
@@ -8808,7 +8830,7 @@ DoRead(
ChannelState *statePtr = chanPtr->state;
char *p = dst;
- Tcl_Preserve(chanPtr);
+ TclChannelPreserve((Tcl_Channel)chanPtr);
while (bytesToRead) {
/*
* Each pass through the loop is intended to process up to
@@ -8847,7 +8869,7 @@ DoRead(
if (code) {
/* Read error */
UpdateInterest(chanPtr);
- Tcl_Release(chanPtr);
+ TclChannelRelease((Tcl_Channel)chanPtr);
return -1;
}
@@ -8945,7 +8967,7 @@ DoRead(
ResetFlag(statePtr, CHANNEL_BLOCKED);
}
- Tcl_Release(chanPtr);
+ TclChannelRelease((Tcl_Channel)chanPtr);
return (int)(p - dst);
}