Tk Source Code

Artifact [b643917f]
Login

Artifact b643917ffd671b0c975ab46a80f167d6fabe763b:

Attachment "2492179-2.patch" to ticket [2492179f] added by dgp 2009-12-16 01:06:53.
Index: generic/tkConfig.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/generic/tkConfig.c,v
retrieving revision 1.31
diff -u -r1.31 tkConfig.c
--- generic/tkConfig.c	30 Jun 2009 00:56:29 -0000	1.31
+++ generic/tkConfig.c	15 Dec 2009 17:58:54 -0000
@@ -102,6 +102,8 @@
 				 * chain. */
     int numOptions;		/* The number of items in the options array
 				 * below. */
+    int refCount2; /* Reference counter for controlling the freeing
+                    * of the memory occupied by this OptionTable */
     Option options[1];		/* Information about the individual options in
 				 * the table. This must be the last field in
 				 * the structure: the actual size of the array
@@ -127,6 +129,7 @@
 static Option *		GetOptionFromObj(Tcl_Interp *interp,
 			    Tcl_Obj *objPtr, OptionTable *tablePtr);
 static int		ObjectIsEmpty(Tcl_Obj *objPtr);
+static void		FreeOptionInternalRep(Tcl_Obj *objPtr);
 static int		SetOptionFromAny(Tcl_Interp *interp, Tcl_Obj *objPtr);
 
 /*
@@ -136,9 +139,9 @@
  * the internalPtr2 field points to the entry that matched.
  */
 
-const Tcl_ObjType tkOptionObjType = {
+static const Tcl_ObjType optionObjType = {
     "option",			/* name */
-    NULL,			/* freeIntRepProc */
+    FreeOptionInternalRep,	/* freeIntRepProc */
     NULL,			/* dupIntRepProc */
     NULL,			/* updateStringProc */
     SetOptionFromAny		/* setFromAnyProc */
@@ -222,6 +225,7 @@
     tablePtr = (OptionTable *) (ckalloc(sizeof(OptionTable)
 	    + (numOptions * sizeof(Option))));
     tablePtr->refCount = 1;
+    tablePtr->refCount2 = 1;
     tablePtr->hashEntryPtr = hashEntryPtr;
     tablePtr->nextPtr = NULL;
     tablePtr->numOptions = numOptions;
@@ -357,7 +361,10 @@
 	}
     }
     Tcl_DeleteHashEntry(tablePtr->hashEntryPtr);
-    ckfree((char *) tablePtr);
+    tablePtr->refCount2--;
+    if (tablePtr->refCount2 <= 0) {
+	ckfree((char *) tablePtr);
+    }
 }
 
 /*
@@ -1130,7 +1137,7 @@
      * First, check to see if the object already has the answer cached.
      */
 
-    if (objPtr->typePtr == &tkOptionObjType) {
+    if (objPtr->typePtr == &optionObjType) {
 	if (objPtr->internalRep.twoPtrValue.ptr1 == (void *) tablePtr) {
 	    return (Option *) objPtr->internalRep.twoPtrValue.ptr2;
 	}
@@ -1152,7 +1159,8 @@
     }
     objPtr->internalRep.twoPtrValue.ptr1 = (void *) tablePtr;
     objPtr->internalRep.twoPtrValue.ptr2 = (void *) bestPtr;
-    objPtr->typePtr = &tkOptionObjType;
+    objPtr->typePtr = &optionObjType;
+    tablePtr->refCount2++;
     return bestPtr;
 
   error:
@@ -1229,6 +1237,41 @@
 	    NULL);
     return TCL_ERROR;
 }
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * FreeOptionInternalRep --
+ *
+ *	Part of the option Tcl object type implementation. Frees the storage
+ *	associated with a option object's internal representation unless it
+ *	is still in use.
+ *
+ * Results:
+ *	None.
+ *
+ * Side effects:
+ *	The option object's internal rep is marked invalid and its memory
+ *	gets freed unless it is still in use somewhere. In that case the
+ *	cleanup is delayed until the last reference goes away.
+ *
+ *----------------------------------------------------------------------
+ */
+
+static void
+FreeOptionInternalRep(
+    register Tcl_Obj *objPtr)	/* Object whose internal rep to free. */
+{
+    register OptionTable *tablePtr = (OptionTable *) objPtr->internalRep.twoPtrValue.ptr1;
+
+    tablePtr->refCount2--;
+    if (tablePtr->refCount2 <= 0) {
+	ckfree((char *)tablePtr);
+    }
+    objPtr->typePtr = NULL;
+    objPtr->internalRep.twoPtrValue.ptr1 = NULL;
+    objPtr->internalRep.twoPtrValue.ptr2 = NULL;
+}
 
 /*
  *--------------------------------------------------------------
Index: generic/tkInt.h
===================================================================
RCS file: /cvsroot/tktoolkit/tk/generic/tkInt.h,v
retrieving revision 1.114
diff -u -r1.114 tkInt.h
--- generic/tkInt.h	9 Dec 2009 10:45:30 -0000	1.114
+++ generic/tkInt.h	15 Dec 2009 17:58:54 -0000
@@ -926,7 +926,6 @@
 MODULE_SCOPE const Tcl_ObjType tkColorObjType;
 MODULE_SCOPE const Tcl_ObjType tkCursorObjType;
 MODULE_SCOPE const Tcl_ObjType tkFontObjType;
-MODULE_SCOPE const Tcl_ObjType tkOptionObjType;
 MODULE_SCOPE const Tcl_ObjType tkStateKeyObjType;
 MODULE_SCOPE const Tcl_ObjType tkTextIndexType;
 
Index: generic/tkObj.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/generic/tkObj.c,v
retrieving revision 1.26
diff -u -r1.26 tkObj.c
--- generic/tkObj.c	3 Feb 2009 23:55:47 -0000	1.26
+++ generic/tkObj.c	15 Dec 2009 17:58:54 -0000
@@ -1075,7 +1075,6 @@
     Tcl_RegisterObjType(&tkCursorObjType);
     Tcl_RegisterObjType(&tkFontObjType);
     Tcl_RegisterObjType(&mmObjType);
-    Tcl_RegisterObjType(&tkOptionObjType);
     Tcl_RegisterObjType(&pixelObjType);
     Tcl_RegisterObjType(&tkStateKeyObjType);
     Tcl_RegisterObjType(&windowObjType);