Fork me on GitHub

Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • external/tcl/tclObj.c

    radeddd8 rd7d2da3  
    3636
    3737char *tclEmptyStringRep = NULL;
     38
     39/*
     40 * Count of the number of Tcl objects every allocated (by Tcl_NewObj) and
     41 * freed (by TclFreeObj).
     42 */
     43
     44#ifdef TCL_COMPILE_STATS
     45long tclObjsAlloced = 0;
     46long tclObjsFreed = 0;
     47#endif /* TCL_COMPILE_STATS */
    3848
    3949/*
     
    407417 * Tcl_NewObj --
    408418 *
    409  *      This procedure creates new Tcl objects that denote the empty string.
    410  *      These objects have a NULL object type and NULL string representation
    411  *      byte pointer. Type managers call this routine to allocate new objects
    412  *      that they further initialize.
     419 *      This procedure is normally called when not debugging: i.e., when
     420 *      TCL_MEM_DEBUG is not defined. It creates new Tcl objects that denote
     421 *      the empty string. These objects have a NULL object type and NULL
     422 *      string representation byte pointer. Type managers call this routine
     423 *      to allocate new objects that they further initialize.
     424 *
     425 *      When TCL_MEM_DEBUG is defined, this procedure just returns the
     426 *      result of calling the debugging version Tcl_DbNewObj.
    413427 *
    414428 * Results:
     
    417431 *      is set to 0.
    418432 *
    419  *----------------------------------------------------------------------
    420  */
     433 * Side effects:
     434 *      If compiling with TCL_COMPILE_STATS, this procedure increments
     435 *      the global count of allocated objects (tclObjsAlloced).
     436 *
     437 *----------------------------------------------------------------------
     438 */
     439
     440#ifdef TCL_MEM_DEBUG
     441#undef Tcl_NewObj
     442
     443Tcl_Obj *
     444Tcl_NewObj()
     445{
     446    return Tcl_DbNewObj("unknown", 0);
     447}
     448
     449#else /* if not TCL_MEM_DEBUG */
    421450
    422451Tcl_Obj *
     
    439468    objPtr->length   = 0;
    440469    objPtr->typePtr  = NULL;
     470#ifdef TCL_COMPILE_STATS
     471    tclObjsAlloced++;
     472#endif /* TCL_COMPILE_STATS */
    441473    return objPtr;
    442474}
     475#endif /* TCL_MEM_DEBUG */
     476
     477
     478/*
     479 *----------------------------------------------------------------------
     480 *
     481 * Tcl_DbNewObj --
     482 *
     483 *      This procedure is normally called when debugging: i.e., when
     484 *      TCL_MEM_DEBUG is defined. It creates new Tcl objects that denote the
     485 *      empty string. It is the same as the Tcl_NewObj procedure above
     486 *      except that it calls Tcl_DbCkalloc directly with the file name and
     487 *      line number from its caller. This simplifies debugging since then
     488 *      the checkmem command will report the correct file name and line
     489 *      number when reporting objects that haven't been freed.
     490 *
     491 *      When TCL_MEM_DEBUG is not defined, this procedure just returns the
     492 *      result of calling Tcl_NewObj.
     493 *
     494 * Results:
     495 *      The result is a newly allocated that represents the empty string.
     496 *      The new object's typePtr is set NULL and its ref count is set to 0.
     497 *
     498 * Side effects:
     499 *      If compiling with TCL_COMPILE_STATS, this procedure increments
     500 *      the global count of allocated objects (tclObjsAlloced).
     501 *
     502 *----------------------------------------------------------------------
     503 */
     504
     505#ifdef TCL_MEM_DEBUG
     506
     507Tcl_Obj *
     508Tcl_DbNewObj(file, line)
     509    register char *file;        /* The name of the source file calling this
     510                                 * procedure; used for debugging. */
     511    register int line;          /* Line number in the source file; used
     512                                 * for debugging. */
     513{
     514    register Tcl_Obj *objPtr;
     515
     516    /*
     517     * If debugging Tcl's memory usage, allocate the object using ckalloc.
     518     * Otherwise, allocate it using the list of free Tcl_Objs we maintain.
     519     */
     520
     521    objPtr = (Tcl_Obj *) Tcl_DbCkalloc(sizeof(Tcl_Obj), file, line);
     522    objPtr->refCount = 0;
     523    objPtr->bytes    = tclEmptyStringRep;
     524    objPtr->length   = 0;
     525    objPtr->typePtr  = NULL;
     526#ifdef TCL_COMPILE_STATS
     527    tclObjsAlloced++;
     528#endif /* TCL_COMPILE_STATS */
     529    return objPtr;
     530}
     531
     532#else /* if not TCL_MEM_DEBUG */
     533
     534Tcl_Obj *
     535Tcl_DbNewObj(file, line)
     536    char *file;                 /* The name of the source file calling this
     537                                 * procedure; used for debugging. */
     538    int line;                   /* Line number in the source file; used
     539                                 * for debugging. */
     540{
     541    return Tcl_NewObj();
     542}
     543#endif /* TCL_MEM_DEBUG */
    443544
    444545
     
    508609 *      after deallocating the string representation and calling the
    509610 *      type-specific Tcl_FreeInternalRepProc to deallocate the object's
    510  *      internal representation.
     611 *      internal representation. If compiling with TCL_COMPILE_STATS,
     612 *      this procedure increments the global count of freed objects
     613 *      (tclObjsFreed).
    511614 *
    512615 *----------------------------------------------------------------------
     
    519622    register Tcl_ObjType *typePtr = objPtr->typePtr;
    520623   
     624#ifdef TCL_MEM_DEBUG
     625    if ((objPtr)->refCount < -1) {
     626        panic("Reference count for %lx was negative", objPtr);
     627    }
     628#endif /* TCL_MEM_DEBUG */
     629
    521630    Tcl_InvalidateStringRep(objPtr);
    522631    if ((typePtr != NULL) && (typePtr->freeIntRepProc != NULL)) {
     
    525634
    526635    /*
    527      * Deallocate the object by adding it onto the list of free
     636     * If debugging Tcl's memory usage, deallocate the object using ckfree.
     637     * Otherwise, deallocate it by adding it onto the list of free
    528638     * Tcl_Objs we maintain.
    529639     */
    530640   
     641#ifdef TCL_MEM_DEBUG
     642    ckfree((char *) objPtr);
     643#else
    531644    objPtr->internalRep.otherValuePtr = (VOID *) tclFreeObjList;
    532645    tclFreeObjList = objPtr;
     646#endif /* TCL_MEM_DEBUG */
     647
     648#ifdef TCL_COMPILE_STATS   
     649    tclObjsFreed++;
     650#endif /* TCL_COMPILE_STATS */   
    533651}
    534652
     
    676794 * Tcl_NewBooleanObj --
    677795 *
    678  *      This procedure creates a new boolean object and initializes it from
    679  *      the argument boolean value. A nonzero "boolValue" is coerced to 1.
     796 *      This procedure is normally called when not debugging: i.e., when
     797 *      TCL_MEM_DEBUG is not defined. It creates a new boolean object and
     798 *      initializes it from the argument boolean value. A nonzero
     799 *      "boolValue" is coerced to 1.
     800 *
     801 *      When TCL_MEM_DEBUG is defined, this procedure just returns the
     802 *      result of calling the debugging version Tcl_DbNewBooleanObj.
    680803 *
    681804 * Results:
     
    688811 *----------------------------------------------------------------------
    689812 */
     813
     814#ifdef TCL_MEM_DEBUG
     815#undef Tcl_NewBooleanObj
    690816
    691817Tcl_Obj *
     
    693819    register int boolValue;     /* Boolean used to initialize new object. */
    694820{
     821    return Tcl_DbNewBooleanObj(boolValue, "unknown", 0);
     822}
     823
     824#else /* if not TCL_MEM_DEBUG */
     825
     826Tcl_Obj *
     827Tcl_NewBooleanObj(boolValue)
     828    register int boolValue;     /* Boolean used to initialize new object. */
     829{
    695830    register Tcl_Obj *objPtr;
    696831
     
    702837    return objPtr;
    703838}
     839#endif /* TCL_MEM_DEBUG */
     840
     841
     842/*
     843 *----------------------------------------------------------------------
     844 *
     845 * Tcl_DbNewBooleanObj --
     846 *
     847 *      This procedure is normally called when debugging: i.e., when
     848 *      TCL_MEM_DEBUG is defined. It creates new boolean objects. It is the
     849 *      same as the Tcl_NewBooleanObj procedure above except that it calls
     850 *      Tcl_DbCkalloc directly with the file name and line number from its
     851 *      caller. This simplifies debugging since then the checkmem command
     852 *      will report the correct file name and line number when reporting
     853 *      objects that haven't been freed.
     854 *
     855 *      When TCL_MEM_DEBUG is not defined, this procedure just returns the
     856 *      result of calling Tcl_NewBooleanObj.
     857 *
     858 * Results:
     859 *      The newly created object is returned. This object will have an
     860 *      invalid string representation. The returned object has ref count 0.
     861 *
     862 * Side effects:
     863 *      None.
     864 *
     865 *----------------------------------------------------------------------
     866 */
     867
     868#ifdef TCL_MEM_DEBUG
     869
     870Tcl_Obj *
     871Tcl_DbNewBooleanObj(boolValue, file, line)
     872    register int boolValue;     /* Boolean used to initialize new object. */
     873    char *file;                 /* The name of the source file calling this
     874                                 * procedure; used for debugging. */
     875    int line;                   /* Line number in the source file; used
     876                                 * for debugging. */
     877{
     878    register Tcl_Obj *objPtr;
     879
     880    TclDbNewObj(objPtr, file, line);
     881    objPtr->bytes = NULL;
     882   
     883    objPtr->internalRep.longValue = (boolValue? 1 : 0);
     884    objPtr->typePtr = &tclBooleanType;
     885    return objPtr;
     886}
     887
     888#else /* if not TCL_MEM_DEBUG */
     889
     890Tcl_Obj *
     891Tcl_DbNewBooleanObj(boolValue, file, line)
     892    register int boolValue;     /* Boolean used to initialize new object. */
     893    char *file;                 /* The name of the source file calling this
     894                                 * procedure; used for debugging. */
     895    int line;                   /* Line number in the source file; used
     896                                 * for debugging. */
     897{
     898    return Tcl_NewBooleanObj(boolValue);
     899}
     900#endif /* TCL_MEM_DEBUG */
    704901
    705902
     
    9811178 * Tcl_NewDoubleObj --
    9821179 *
    983  *      This procedure creates a new double object and initializes it from
    984  *      the argument double value.
     1180 *      This procedure is normally called when not debugging: i.e., when
     1181 *      TCL_MEM_DEBUG is not defined. It creates a new double object and
     1182 *      initializes it from the argument double value.
     1183 *
     1184 *      When TCL_MEM_DEBUG is defined, this procedure just returns the
     1185 *      result of calling the debugging version Tcl_DbNewDoubleObj.
    9851186 *
    9861187 * Results:
     
    9931194 *----------------------------------------------------------------------
    9941195 */
     1196
     1197#ifdef TCL_MEM_DEBUG
     1198#undef Tcl_NewDoubleObj
    9951199
    9961200Tcl_Obj *
     
    9981202    register double dblValue;   /* Double used to initialize the object. */
    9991203{
     1204    return Tcl_DbNewDoubleObj(dblValue, "unknown", 0);
     1205}
     1206
     1207#else /* if not TCL_MEM_DEBUG */
     1208
     1209Tcl_Obj *
     1210Tcl_NewDoubleObj(dblValue)
     1211    register double dblValue;   /* Double used to initialize the object. */
     1212{
    10001213    register Tcl_Obj *objPtr;
    10011214
     
    10071220    return objPtr;
    10081221}
     1222#endif /* if TCL_MEM_DEBUG */
     1223
     1224
     1225/*
     1226 *----------------------------------------------------------------------
     1227 *
     1228 * Tcl_DbNewDoubleObj --
     1229 *
     1230 *      This procedure is normally called when debugging: i.e., when
     1231 *      TCL_MEM_DEBUG is defined. It creates new double objects. It is the
     1232 *      same as the Tcl_NewDoubleObj procedure above except that it calls
     1233 *      Tcl_DbCkalloc directly with the file name and line number from its
     1234 *      caller. This simplifies debugging since then the checkmem command
     1235 *      will report the correct file name and line number when reporting
     1236 *      objects that haven't been freed.
     1237 *
     1238 *      When TCL_MEM_DEBUG is not defined, this procedure just returns the
     1239 *      result of calling Tcl_NewDoubleObj.
     1240 *
     1241 * Results:
     1242 *      The newly created object is returned. This object will have an
     1243 *      invalid string representation. The returned object has ref count 0.
     1244 *
     1245 * Side effects:
     1246 *      None.
     1247 *
     1248 *----------------------------------------------------------------------
     1249 */
     1250
     1251#ifdef TCL_MEM_DEBUG
     1252
     1253Tcl_Obj *
     1254Tcl_DbNewDoubleObj(dblValue, file, line)
     1255    register double dblValue;   /* Double used to initialize the object. */
     1256    char *file;                 /* The name of the source file calling this
     1257                                 * procedure; used for debugging. */
     1258    int line;                   /* Line number in the source file; used
     1259                                 * for debugging. */
     1260{
     1261    register Tcl_Obj *objPtr;
     1262
     1263    TclDbNewObj(objPtr, file, line);
     1264    objPtr->bytes = NULL;
     1265   
     1266    objPtr->internalRep.doubleValue = dblValue;
     1267    objPtr->typePtr = &tclDoubleType;
     1268    return objPtr;
     1269}
     1270
     1271#else /* if not TCL_MEM_DEBUG */
     1272
     1273Tcl_Obj *
     1274Tcl_DbNewDoubleObj(dblValue, file, line)
     1275    register double dblValue;   /* Double used to initialize the object. */
     1276    char *file;                 /* The name of the source file calling this
     1277                                 * procedure; used for debugging. */
     1278    int line;                   /* Line number in the source file; used
     1279                                 * for debugging. */
     1280{
     1281    return Tcl_NewDoubleObj(dblValue);
     1282}
     1283#endif /* TCL_MEM_DEBUG */
    10091284
    10101285
     
    12561531 * Tcl_NewIntObj --
    12571532 *
    1258  *      Calls to Tcl_NewIntObj result in a call to one of the two
     1533 *      If a client is compiled with TCL_MEM_DEBUG defined, calls to
     1534 *      Tcl_NewIntObj to create a new integer object end up calling the
     1535 *      debugging procedure Tcl_DbNewLongObj instead.
     1536 *
     1537 *      Otherwise, if the client is compiled without TCL_MEM_DEBUG defined,
     1538 *      calls to Tcl_NewIntObj result in a call to one of the two
    12591539 *      Tcl_NewIntObj implementations below. We provide two implementations
    12601540 *      so that the Tcl core can be compiled to do memory debugging of the
     
    12761556 */
    12771557
     1558#ifdef TCL_MEM_DEBUG
     1559#undef Tcl_NewIntObj
     1560
    12781561Tcl_Obj *
    12791562Tcl_NewIntObj(intValue)
    12801563    register int intValue;      /* Int used to initialize the new object. */
    12811564{
     1565    return Tcl_DbNewLongObj((long)intValue, "unknown", 0);
     1566}
     1567
     1568#else /* if not TCL_MEM_DEBUG */
     1569
     1570Tcl_Obj *
     1571Tcl_NewIntObj(intValue)
     1572    register int intValue;      /* Int used to initialize the new object. */
     1573{
    12821574    register Tcl_Obj *objPtr;
    12831575
     
    12891581    return objPtr;
    12901582}
     1583#endif /* if TCL_MEM_DEBUG */
    12911584
    12921585
     
    15631856 * Tcl_NewLongObj --
    15641857 *
    1565  *      Calls to Tcl_NewLongObj result in a call to one of the two
     1858 *      If a client is compiled with TCL_MEM_DEBUG defined, calls to
     1859 *      Tcl_NewLongObj to create a new long integer object end up calling
     1860 *      the debugging procedure Tcl_DbNewLongObj instead.
     1861 *
     1862 *      Otherwise, if the client is compiled without TCL_MEM_DEBUG defined,
     1863 *      calls to Tcl_NewLongObj result in a call to one of the two
    15661864 *      Tcl_NewLongObj implementations below. We provide two implementations
    15671865 *      so that the Tcl core can be compiled to do memory debugging of the
     
    15831881 */
    15841882
     1883#ifdef TCL_MEM_DEBUG
     1884#undef Tcl_NewLongObj
     1885
    15851886Tcl_Obj *
    15861887Tcl_NewLongObj(longValue)
     
    15881889                                 * new object. */
    15891890{
     1891    return Tcl_DbNewLongObj(longValue, "unknown", 0);
     1892}
     1893
     1894#else /* if not TCL_MEM_DEBUG */
     1895
     1896Tcl_Obj *
     1897Tcl_NewLongObj(longValue)
     1898    register long longValue;    /* Long integer used to initialize the
     1899                                 * new object. */
     1900{
    15901901    register Tcl_Obj *objPtr;
    15911902
     
    15971908    return objPtr;
    15981909}
     1910#endif /* if TCL_MEM_DEBUG */
    15991911
    16001912
     
    16041916 * Tcl_DbNewLongObj --
    16051917 *
    1606  *      This procedure just returns the result of calling Tcl_NewLongObj.
     1918 *      If a client is compiled with TCL_MEM_DEBUG defined, calls to
     1919 *      Tcl_NewIntObj and Tcl_NewLongObj to create new integer or
     1920 *      long integer objects end up calling the debugging procedure
     1921 *      Tcl_DbNewLongObj instead. We provide two implementations of
     1922 *      Tcl_DbNewLongObj so that whether the Tcl core is compiled to do
     1923 *      memory debugging of the core is independent of whether a client
     1924 *      requests debugging for itself.
     1925 *
     1926 *      When the core is compiled with TCL_MEM_DEBUG defined,
     1927 *      Tcl_DbNewLongObj calls Tcl_DbCkalloc directly with the file name and
     1928 *      line number from its caller. This simplifies debugging since then
     1929 *      the checkmem command will report the caller's file name and line
     1930 *      number when reporting objects that haven't been freed.
     1931 *
     1932 *      Otherwise, when the core is compiled without TCL_MEM_DEBUG defined,
     1933 *      this procedure just returns the result of calling Tcl_NewLongObj.
    16071934 *
    16081935 * Results:
     
    16161943 *----------------------------------------------------------------------
    16171944 */
     1945
     1946#ifdef TCL_MEM_DEBUG
    16181947
    16191948Tcl_Obj *
     
    16261955                                 * for debugging. */
    16271956{
     1957    register Tcl_Obj *objPtr;
     1958
     1959    TclDbNewObj(objPtr, file, line);
     1960    objPtr->bytes = NULL;
     1961   
     1962    objPtr->internalRep.longValue = longValue;
     1963    objPtr->typePtr = &tclIntType;
     1964    return objPtr;
     1965}
     1966
     1967#else /* if not TCL_MEM_DEBUG */
     1968
     1969Tcl_Obj *
     1970Tcl_DbNewLongObj(longValue, file, line)
     1971    register long longValue;    /* Long integer used to initialize the
     1972                                 * new object. */
     1973    char *file;                 /* The name of the source file calling this
     1974                                 * procedure; used for debugging. */
     1975    int line;                   /* Line number in the source file; used
     1976                                 * for debugging. */
     1977{
    16281978    return Tcl_NewLongObj(longValue);
    16291979}
     1980#endif /* TCL_MEM_DEBUG */
    16301981
    16311982
     
    17092060    return result;
    17102061}
     2062
     2063
     2064/*
     2065 *----------------------------------------------------------------------
     2066 *
     2067 * Tcl_DbIncrRefCount --
     2068 *
     2069 *      This procedure is normally called when debugging: i.e., when
     2070 *      TCL_MEM_DEBUG is defined. This checks to see whether or not
     2071 *      the memory has been freed before incrementing the ref count.
     2072 *
     2073 *      When TCL_MEM_DEBUG is not defined, this procedure just increments
     2074 *      the reference count of the object.
     2075 *
     2076 * Results:
     2077 *      None.
     2078 *
     2079 * Side effects:
     2080 *      The object's ref count is incremented.
     2081 *
     2082 *----------------------------------------------------------------------
     2083 */
     2084
     2085void
     2086Tcl_DbIncrRefCount(objPtr, file, line)
     2087    register Tcl_Obj *objPtr;   /* The object we are adding a reference to. */
     2088    char *file;                 /* The name of the source file calling this
     2089                                 * procedure; used for debugging. */
     2090    int line;                   /* Line number in the source file; used
     2091                                 * for debugging. */
     2092{
     2093#ifdef TCL_MEM_DEBUG
     2094    if (objPtr->refCount == 0x61616161) {
     2095        fprintf(stderr, "file = %s, line = %d\n", file, line);
     2096        fflush(stderr);
     2097        panic("Trying to increment refCount of previously disposed object.");
     2098    }
     2099#endif
     2100    ++(objPtr)->refCount;
     2101}
     2102
     2103
     2104/*
     2105 *----------------------------------------------------------------------
     2106 *
     2107 * Tcl_DbDecrRefCount --
     2108 *
     2109 *      This procedure is normally called when debugging: i.e., when
     2110 *      TCL_MEM_DEBUG is defined. This checks to see whether or not
     2111 *      the memory has been freed before incrementing the ref count.
     2112 *
     2113 *      When TCL_MEM_DEBUG is not defined, this procedure just increments
     2114 *      the reference count of the object.
     2115 *
     2116 * Results:
     2117 *      None.
     2118 *
     2119 * Side effects:
     2120 *      The object's ref count is incremented.
     2121 *
     2122 *----------------------------------------------------------------------
     2123 */
     2124
     2125void
     2126Tcl_DbDecrRefCount(objPtr, file, line)
     2127    register Tcl_Obj *objPtr;   /* The object we are adding a reference to. */
     2128    char *file;                 /* The name of the source file calling this
     2129                                 * procedure; used for debugging. */
     2130    int line;                   /* Line number in the source file; used
     2131                                 * for debugging. */
     2132{
     2133#ifdef TCL_MEM_DEBUG
     2134    if (objPtr->refCount == 0x61616161) {
     2135        fprintf(stderr, "file = %s, line = %d\n", file, line);
     2136        fflush(stderr);
     2137        panic("Trying to decrement refCount of previously disposed object.");
     2138    }
     2139#endif
     2140    if (--(objPtr)->refCount <= 0) {
     2141        TclFreeObj(objPtr);
     2142    }
     2143}
     2144
     2145
     2146/*
     2147 *----------------------------------------------------------------------
     2148 *
     2149 * Tcl_DbIsShared --
     2150 *
     2151 *      This procedure is normally called when debugging: i.e., when
     2152 *      TCL_MEM_DEBUG is defined. This checks to see whether or not
     2153 *      the memory has been freed before incrementing the ref count.
     2154 *
     2155 *      When TCL_MEM_DEBUG is not defined, this procedure just decrements
     2156 *      the reference count of the object and throws it away if the count
     2157 *      is 0 or less.
     2158 *
     2159 * Results:
     2160 *      None.
     2161 *
     2162 * Side effects:
     2163 *      The object's ref count is incremented.
     2164 *
     2165 *----------------------------------------------------------------------
     2166 */
     2167
     2168int
     2169Tcl_DbIsShared(objPtr, file, line)
     2170    register Tcl_Obj *objPtr;   /* The object we are adding a reference to. */
     2171    char *file;                 /* The name of the source file calling this
     2172                                 * procedure; used for debugging. */
     2173    int line;                   /* Line number in the source file; used
     2174                                 * for debugging. */
     2175{
     2176#ifdef TCL_MEM_DEBUG
     2177    if (objPtr->refCount == 0x61616161) {
     2178        fprintf(stderr, "file = %s, line = %d\n", file, line);
     2179        fflush(stderr);
     2180        panic("Trying to check whether previously disposed object is shared.");
     2181    }
     2182#endif
     2183    return ((objPtr)->refCount > 1);
     2184}
Note: See TracChangeset for help on using the changeset viewer.