From ac555b905c136e91ee92a89d6b225b9a46851e8d Mon Sep 17 00:00:00 2001 From: pooryorick Date: Wed, 17 May 2023 12:26:11 +0000 Subject: New functions: TclUndoRefCount() and TclDictGetSize(). --- generic/tclDictObj.c | 20 ++++++++++++++++++++ generic/tclInt.h | 2 ++ generic/tclObj.c | 22 ++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/generic/tclDictObj.c b/generic/tclDictObj.c index f996f4b..c4ff0fa 100644 --- a/generic/tclDictObj.c +++ b/generic/tclDictObj.c @@ -1048,6 +1048,26 @@ Tcl_DictObjRemove( /* *---------------------------------------------------------------------- * + * Tcl_DictGetSize + * + * Returns the size of dictPtr. Caller must ensure that dictPtr has type + * 'tclDicttype'. + * + * + *---------------------------------------------------------------------- + */ + +Tcl_Size +TclDictGetSize(Tcl_Obj *dictPtr) +{ + Dict *dict; + DictGetInternalRep(dictPtr, dict); + return dict->table.numEntries; +} + +/* + *---------------------------------------------------------------------- + * * Tcl_DictObjSize -- * * How many key,value pairs are there in the dictionary? diff --git a/generic/tclInt.h b/generic/tclInt.h index 19e878e..979284d 100644 --- a/generic/tclInt.h +++ b/generic/tclInt.h @@ -3116,6 +3116,7 @@ MODULE_SCOPE Tcl_Command TclCreateEnsembleInNs(Tcl_Interp *interp, Tcl_Namespace *ensembleNamespacePtr, int flags); MODULE_SCOPE void TclDeleteNamespaceVars(Namespace *nsPtr); MODULE_SCOPE void TclDeleteNamespaceChildren(Namespace *nsPtr); +MODULE_SCOPE Tcl_Size TclDictGetSize(Tcl_Obj *dictPtr); MODULE_SCOPE Tcl_Obj* TclDuplicatePureObj(Tcl_Obj * objPtr); MODULE_SCOPE int TclFindDictElement(Tcl_Interp *interp, const char *dict, Tcl_Size dictLength, @@ -3289,6 +3290,7 @@ MODULE_SCOPE void TclParseInit(Tcl_Interp *interp, const char *string, MODULE_SCOPE Tcl_Size TclParseAllWhiteSpace(const char *src, Tcl_Size numBytes); MODULE_SCOPE int TclProcessReturn(Tcl_Interp *interp, int code, int level, Tcl_Obj *returnOpts); +MODULE_SCOPE void TclUndoRefCount(Tcl_Obj *objPtr); MODULE_SCOPE int TclpObjLstat(Tcl_Obj *pathPtr, Tcl_StatBuf *buf); MODULE_SCOPE Tcl_Obj * TclpTempFileName(void); MODULE_SCOPE Tcl_Obj * TclpTempFileNameForLibrary(Tcl_Interp *interp, diff --git a/generic/tclObj.c b/generic/tclObj.c index 58eb71d..ba3c626 100644 --- a/generic/tclObj.c +++ b/generic/tclObj.c @@ -3780,6 +3780,28 @@ Tcl_DecrRefCount( /* *---------------------------------------------------------------------- * + * TclUndoRefCount -- + * + * Decrement the refCount of objPtr without causing it to be freed if it + * drops from 1 to 0. This allows a function increment a refCount but + * then decrement it and still be able to pass return it to a caller, + * possibly with a refCount of 0. The caller must have previously + * incremented the refCount. + * + *---------------------------------------------------------------------- + */ +void +TclUndoRefCount( + Tcl_Obj *objPtr) /* The object we are releasing a reference to. */ +{ + if (objPtr->refCount > 0) { + --objPtr->refCount; + } +} + +/* + *---------------------------------------------------------------------- + * * Tcl_IsShared -- * * Tests if the object has a ref count greater than one. -- cgit v0.12