| 1 |
/* |
| 2 |
* tclObj.c -- |
| 3 |
* |
| 4 |
* This file contains Tcl object-related functions that are used by many |
| 5 |
* Tcl commands. |
| 6 |
* |
| 7 |
* Copyright (c) 1995-1997 Sun Microsystems, Inc. |
| 8 |
* Copyright (c) 1999 Scriptics Corporation. |
| 9 |
* Copyright (c) 2001 ActiveState Corporation. |
| 10 |
* Copyright (c) 2005 Kevin B. Kenny. All rights reserved. |
| 11 |
* Copyright (c) 2007 Daniel A. Steffen <[email protected]> |
| 12 |
* |
| 13 |
* See the file "license.terms" for information on usage and redistribution of |
| 14 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "tclInt.h" |
| 18 |
#include "tommath.h" |
| 19 |
#include <math.h> |
| 20 |
|
| 21 |
/* |
| 22 |
* Table of all object types. |
| 23 |
*/ |
| 24 |
|
| 25 |
static Tcl_HashTable typeTable; |
| 26 |
static int typeTableInitialized = 0; /* 0 means not yet initialized. */ |
| 27 |
TCL_DECLARE_MUTEX(tableMutex) |
| 28 |
|
| 29 |
/* |
| 30 |
* Head of the list of free Tcl_Obj structs we maintain. |
| 31 |
*/ |
| 32 |
|
| 33 |
Tcl_Obj *tclFreeObjList = NULL; |
| 34 |
|
| 35 |
/* |
| 36 |
* The object allocator is single threaded. This mutex is referenced by the |
| 37 |
* TclNewObj macro, however, so must be visible. |
| 38 |
*/ |
| 39 |
|
| 40 |
#ifdef TCL_THREADS |
| 41 |
MODULE_SCOPE Tcl_Mutex tclObjMutex; |
| 42 |
Tcl_Mutex tclObjMutex; |
| 43 |
#endif |
| 44 |
|
| 45 |
/* |
| 46 |
* Pointer to a heap-allocated string of length zero that the Tcl core uses as |
| 47 |
* the value of an empty string representation for an object. This value is |
| 48 |
* shared by all new objects allocated by Tcl_NewObj. |
| 49 |
*/ |
| 50 |
|
| 51 |
char tclEmptyString = '\0'; |
| 52 |
char *tclEmptyStringRep = &tclEmptyString; |
| 53 |
|
| 54 |
#if defined(TCL_MEM_DEBUG) && defined(TCL_THREADS) |
| 55 |
/* |
| 56 |
* Structure for tracking the source file and line number where a given |
| 57 |
* Tcl_Obj was allocated. We also track the pointer to the Tcl_Obj itself, |
| 58 |
* for sanity checking purposes. |
| 59 |
*/ |
| 60 |
|
| 61 |
typedef struct ObjData { |
| 62 |
Tcl_Obj *objPtr; /* The pointer to the allocated Tcl_Obj. */ |
| 63 |
const char *file; /* The name of the source file calling this |
| 64 |
* function; used for debugging. */ |
| 65 |
int line; /* Line number in the source file; used for |
| 66 |
* debugging. */ |
| 67 |
} ObjData; |
| 68 |
#endif /* TCL_MEM_DEBUG && TCL_THREADS */ |
| 69 |
|
| 70 |
/* |
| 71 |
* All static variables used in this file are collected into a single instance |
| 72 |
* of the following structure. For multi-threaded implementations, there is |
| 73 |
* one instance of this structure for each thread. |
| 74 |
* |
| 75 |
* Notice that different structures with the same name appear in other files. |
| 76 |
* The structure defined below is used in this file only. |
| 77 |
*/ |
| 78 |
|
| 79 |
typedef struct ThreadSpecificData { |
| 80 |
Tcl_HashTable *lineCLPtr; /* This table remembers for each Tcl_Obj |
| 81 |
* generated by a call to the function |
| 82 |
* TclSubstTokens() from a literal text |
| 83 |
* where bs+nl sequences occurred in it, if |
| 84 |
* any. I.e. this table keeps track of |
| 85 |
* invisible and stripped continuation lines. |
| 86 |
* Its keys are Tcl_Obj pointers, the values |
| 87 |
* are ContLineLoc pointers. See the file |
| 88 |
* tclCompile.h for the definition of this |
| 89 |
* structure, and for references to all |
| 90 |
* related places in the core. */ |
| 91 |
#if defined(TCL_MEM_DEBUG) && defined(TCL_THREADS) |
| 92 |
Tcl_HashTable *objThreadMap;/* Thread local table that is used to check |
| 93 |
* that a Tcl_Obj was not allocated by some |
| 94 |
* other thread. */ |
| 95 |
#endif /* TCL_MEM_DEBUG && TCL_THREADS */ |
| 96 |
} ThreadSpecificData; |
| 97 |
|
| 98 |
static Tcl_ThreadDataKey dataKey; |
| 99 |
|
| 100 |
static void TclThreadFinalizeContLines(ClientData clientData); |
| 101 |
static ThreadSpecificData *TclGetContLineTable(void); |
| 102 |
|
| 103 |
/* |
| 104 |
* Nested Tcl_Obj deletion management support |
| 105 |
* |
| 106 |
* All context references used in the object freeing code are pointers to this |
| 107 |
* structure; every thread will have its own structure instance. The purpose |
| 108 |
* of this structure is to allow deeply nested collections of Tcl_Objs to be |
| 109 |
* freed without taking a vast depth of C stack (which could cause all sorts |
| 110 |
* of breakage.) |
| 111 |
*/ |
| 112 |
|
| 113 |
typedef struct PendingObjData { |
| 114 |
int deletionCount; /* Count of the number of invocations of |
| 115 |
* TclFreeObj() are on the stack (at least |
| 116 |
* conceptually; many are actually expanded |
| 117 |
* macros). */ |
| 118 |
Tcl_Obj *deletionStack; /* Stack of objects that have had TclFreeObj() |
| 119 |
* invoked upon them but which can't be |
| 120 |
* deleted yet because they are in a nested |
| 121 |
* invocation of TclFreeObj(). By postponing |
| 122 |
* this way, we limit the maximum overall C |
| 123 |
* stack depth when deleting a complex object. |
| 124 |
* The down-side is that we alter the overall |
| 125 |
* behaviour by altering the order in which |
| 126 |
* objects are deleted, and we change the |
| 127 |
* order in which the string rep and the |
| 128 |
* internal rep of an object are deleted. Note |
| 129 |
* that code which assumes the previous |
| 130 |
* behaviour in either of these respects is |
| 131 |
* unsafe anyway; it was never documented as |
| 132 |
* to exactly what would happen in these |
| 133 |
* cases, and the overall contract of a |
| 134 |
* user-level Tcl_DecrRefCount() is still |
| 135 |
* preserved (assuming that a particular T_DRC |
| 136 |
* would delete an object is not very |
| 137 |
* safe). */ |
| 138 |
} PendingObjData; |
| 139 |
|
| 140 |
/* |
| 141 |
* These are separated out so that some semantic content is attached |
| 142 |
* to them. |
| 143 |
*/ |
| 144 |
#define ObjDeletionLock(contextPtr) ((contextPtr)->deletionCount++) |
| 145 |
#define ObjDeletionUnlock(contextPtr) ((contextPtr)->deletionCount--) |
| 146 |
#define ObjDeletePending(contextPtr) ((contextPtr)->deletionCount > 0) |
| 147 |
#define ObjOnStack(contextPtr) ((contextPtr)->deletionStack != NULL) |
| 148 |
#define PushObjToDelete(contextPtr,objPtr) \ |
| 149 |
/* The string rep is already invalidated so we can use the bytes value \ |
| 150 |
* for our pointer chain: push onto the head of the stack. */ \ |
| 151 |
(objPtr)->bytes = (char *) ((contextPtr)->deletionStack); \ |
| 152 |
(contextPtr)->deletionStack = (objPtr) |
| 153 |
#define PopObjToDelete(contextPtr,objPtrVar) \ |
| 154 |
(objPtrVar) = (contextPtr)->deletionStack; \ |
| 155 |
(contextPtr)->deletionStack = (Tcl_Obj *) (objPtrVar)->bytes |
| 156 |
|
| 157 |
/* |
| 158 |
* Macro to set up the local reference to the deletion context. |
| 159 |
*/ |
| 160 |
#ifndef TCL_THREADS |
| 161 |
static PendingObjData pendingObjData; |
| 162 |
#define ObjInitDeletionContext(contextPtr) \ |
| 163 |
PendingObjData *const contextPtr = &pendingObjData |
| 164 |
#elif defined(HAVE_FAST_TSD) |
| 165 |
static __thread PendingObjData pendingObjData; |
| 166 |
#define ObjInitDeletionContext(contextPtr) \ |
| 167 |
PendingObjData *const contextPtr = &pendingObjData |
| 168 |
#else |
| 169 |
static Tcl_ThreadDataKey pendingObjDataKey; |
| 170 |
#define ObjInitDeletionContext(contextPtr) \ |
| 171 |
PendingObjData *const contextPtr = \ |
| 172 |
(PendingObjData *)Tcl_GetThreadData(&pendingObjDataKey, sizeof(PendingObjData)) |
| 173 |
#endif |
| 174 |
|
| 175 |
/* |
| 176 |
* Macros to pack/unpack a bignum's fields in a Tcl_Obj internal rep |
| 177 |
*/ |
| 178 |
|
| 179 |
#define PACK_BIGNUM(bignum, objPtr) \ |
| 180 |
if ((bignum).used > 0x7FFF) { \ |
| 181 |
mp_int *temp = (mp_int *)ckalloc(sizeof(mp_int)); \ |
| 182 |
*temp = bignum; \ |
| 183 |
(objPtr)->internalRep.twoPtrValue.ptr1 = temp; \ |
| 184 |
(objPtr)->internalRep.twoPtrValue.ptr2 = INT2PTR(-1); \ |
| 185 |
} else { \ |
| 186 |
if ((bignum).alloc > 0x7FFF) { \ |
| 187 |
mp_shrink(&(bignum)); \ |
| 188 |
} \ |
| 189 |
(objPtr)->internalRep.twoPtrValue.ptr1 = (void *)(bignum).dp; \ |
| 190 |
(objPtr)->internalRep.twoPtrValue.ptr2 = INT2PTR((mp_isneg(&(bignum)) << 30) \ |
| 191 |
| ((bignum).alloc << 15) | ((bignum).used)); \ |
| 192 |
} |
| 193 |
|
| 194 |
#define UNPACK_BIGNUM(objPtr, bignum) \ |
| 195 |
if ((objPtr)->internalRep.twoPtrValue.ptr2 == INT2PTR(-1)) { \ |
| 196 |
(bignum) = *((mp_int *) ((objPtr)->internalRep.twoPtrValue.ptr1)); \ |
| 197 |
} else { \ |
| 198 |
(bignum).dp = (mp_digit *)(objPtr)->internalRep.twoPtrValue.ptr1; \ |
| 199 |
(bignum).sign = PTR2INT((objPtr)->internalRep.twoPtrValue.ptr2) >> 30; \ |
| 200 |
(bignum).alloc = \ |
| 201 |
(PTR2INT((objPtr)->internalRep.twoPtrValue.ptr2) >> 15) & 0x7FFF; \ |
| 202 |
(bignum).used = PTR2INT((objPtr)->internalRep.twoPtrValue.ptr2) & 0x7FFF; \ |
| 203 |
} |
| 204 |
|
| 205 |
/* |
| 206 |
* Prototypes for functions defined later in this file: |
| 207 |
*/ |
| 208 |
|
| 209 |
static int ParseBoolean(Tcl_Obj *objPtr); |
| 210 |
static int SetDoubleFromAny(Tcl_Interp *interp, Tcl_Obj *objPtr); |
| 211 |
static int SetIntFromAny(Tcl_Interp *interp, Tcl_Obj *objPtr); |
| 212 |
static void UpdateStringOfDouble(Tcl_Obj *objPtr); |
| 213 |
static void UpdateStringOfInt(Tcl_Obj *objPtr); |
| 214 |
#ifndef TCL_WIDE_INT_IS_LONG |
| 215 |
static void UpdateStringOfWideInt(Tcl_Obj *objPtr); |
| 216 |
static int SetWideIntFromAny(Tcl_Interp *interp, Tcl_Obj *objPtr); |
| 217 |
#endif |
| 218 |
static void FreeBignum(Tcl_Obj *objPtr); |
| 219 |
static void DupBignum(Tcl_Obj *objPtr, Tcl_Obj *copyPtr); |
| 220 |
static void UpdateStringOfBignum(Tcl_Obj *objPtr); |
| 221 |
static int GetBignumFromObj(Tcl_Interp *interp, Tcl_Obj *objPtr, |
| 222 |
int copy, mp_int *bignumValue); |
| 223 |
|
| 224 |
/* |
| 225 |
* Prototypes for the array hash key methods. |
| 226 |
*/ |
| 227 |
|
| 228 |
static Tcl_HashEntry * AllocObjEntry(Tcl_HashTable *tablePtr, void *keyPtr); |
| 229 |
|
| 230 |
/* |
| 231 |
* Prototypes for the CommandName object type. |
| 232 |
*/ |
| 233 |
|
| 234 |
static void DupCmdNameInternalRep(Tcl_Obj *objPtr, |
| 235 |
Tcl_Obj *copyPtr); |
| 236 |
static void FreeCmdNameInternalRep(Tcl_Obj *objPtr); |
| 237 |
static int SetCmdNameFromAny(Tcl_Interp *interp, Tcl_Obj *objPtr); |
| 238 |
|
| 239 |
/* |
| 240 |
* The structures below defines the Tcl object types defined in this file by |
| 241 |
* means of functions that can be invoked by generic object code. See also |
| 242 |
* tclStringObj.c, tclListObj.c, tclByteCode.c for other type manager |
| 243 |
* implementations. |
| 244 |
*/ |
| 245 |
|
| 246 |
static const Tcl_ObjType oldBooleanType = { |
| 247 |
"boolean", /* name */ |
| 248 |
NULL, /* freeIntRepProc */ |
| 249 |
NULL, /* dupIntRepProc */ |
| 250 |
NULL, /* updateStringProc */ |
| 251 |
TclSetBooleanFromAny /* setFromAnyProc */ |
| 252 |
}; |
| 253 |
const Tcl_ObjType tclBooleanType = { |
| 254 |
"booleanString", /* name */ |
| 255 |
NULL, /* freeIntRepProc */ |
| 256 |
NULL, /* dupIntRepProc */ |
| 257 |
NULL, /* updateStringProc */ |
| 258 |
TclSetBooleanFromAny /* setFromAnyProc */ |
| 259 |
}; |
| 260 |
const Tcl_ObjType tclDoubleType = { |
| 261 |
"double", /* name */ |
| 262 |
NULL, /* freeIntRepProc */ |
| 263 |
NULL, /* dupIntRepProc */ |
| 264 |
UpdateStringOfDouble, /* updateStringProc */ |
| 265 |
SetDoubleFromAny /* setFromAnyProc */ |
| 266 |
}; |
| 267 |
const Tcl_ObjType tclIntType = { |
| 268 |
"int", /* name */ |
| 269 |
NULL, /* freeIntRepProc */ |
| 270 |
NULL, /* dupIntRepProc */ |
| 271 |
UpdateStringOfInt, /* updateStringProc */ |
| 272 |
SetIntFromAny /* setFromAnyProc */ |
| 273 |
}; |
| 274 |
#ifndef TCL_WIDE_INT_IS_LONG |
| 275 |
const Tcl_ObjType tclWideIntType = { |
| 276 |
"wideInt", /* name */ |
| 277 |
NULL, /* freeIntRepProc */ |
| 278 |
NULL, /* dupIntRepProc */ |
| 279 |
UpdateStringOfWideInt, /* updateStringProc */ |
| 280 |
SetWideIntFromAny /* setFromAnyProc */ |
| 281 |
}; |
| 282 |
#endif |
| 283 |
const Tcl_ObjType tclBignumType = { |
| 284 |
"bignum", /* name */ |
| 285 |
FreeBignum, /* freeIntRepProc */ |
| 286 |
DupBignum, /* dupIntRepProc */ |
| 287 |
UpdateStringOfBignum, /* updateStringProc */ |
| 288 |
NULL /* setFromAnyProc */ |
| 289 |
}; |
| 290 |
|
| 291 |
/* |
| 292 |
* The structure below defines the Tcl obj hash key type. |
| 293 |
*/ |
| 294 |
|
| 295 |
const Tcl_HashKeyType tclObjHashKeyType = { |
| 296 |
TCL_HASH_KEY_TYPE_VERSION, /* version */ |
| 297 |
TCL_HASH_KEY_DIRECT_COMPARE,/* allows compare keys by pointers */ |
| 298 |
TclHashObjKey, /* hashKeyProc */ |
| 299 |
TclCompareObjKeys, /* compareKeysProc */ |
| 300 |
AllocObjEntry, /* allocEntryProc */ |
| 301 |
TclFreeObjEntry /* freeEntryProc */ |
| 302 |
}; |
| 303 |
|
| 304 |
/* |
| 305 |
* The structure below defines the command name Tcl object type by means of |
| 306 |
* functions that can be invoked by generic object code. Objects of this type |
| 307 |
* cache the Command pointer that results from looking up command names in the |
| 308 |
* command hashtable. Such objects appear as the zeroth ("command name") |
| 309 |
* argument in a Tcl command. |
| 310 |
* |
| 311 |
* NOTE: the ResolvedCmdName that gets cached is stored in the |
| 312 |
* twoPtrValue.ptr1 field, and the twoPtrValue.ptr2 field is unused. You might |
| 313 |
* think you could use the simpler otherValuePtr field to store the single |
| 314 |
* ResolvedCmdName pointer, but DO NOT DO THIS. It seems that some extensions |
| 315 |
* use the second internal pointer field of the twoPtrValue field for their |
| 316 |
* own purposes. |
| 317 |
* |
| 318 |
* TRICKY POINT! Some extensions update this structure! (Notably, these |
| 319 |
* include TclBlend and TCom). This is highly ill-advised on their part, but |
| 320 |
* does allow them to delete a command when references to it are gone, which |
| 321 |
* is fragile but useful given their somewhat-OO style. Because of this, this |
| 322 |
* structure MUST NOT be const so that the C compiler puts the data in |
| 323 |
* writable memory. [Bug 2558422] [Bug 07d13d99b0a9] |
| 324 |
* TODO: Provide a better API for those extensions so that they can coexist... |
| 325 |
*/ |
| 326 |
|
| 327 |
Tcl_ObjType tclCmdNameType = { |
| 328 |
"cmdName", /* name */ |
| 329 |
FreeCmdNameInternalRep, /* freeIntRepProc */ |
| 330 |
DupCmdNameInternalRep, /* dupIntRepProc */ |
| 331 |
NULL, /* updateStringProc */ |
| 332 |
SetCmdNameFromAny /* setFromAnyProc */ |
| 333 |
}; |
| 334 |
|
| 335 |
/* |
| 336 |
* Structure containing a cached pointer to a command that is the result of |
| 337 |
* resolving the command's name in some namespace. It is the internal |
| 338 |
* representation for a cmdName object. It contains the pointer along with |
| 339 |
* some information that is used to check the pointer's validity. |
| 340 |
*/ |
| 341 |
|
| 342 |
typedef struct ResolvedCmdName { |
| 343 |
Command *cmdPtr; /* A cached Command pointer. */ |
| 344 |
Namespace *refNsPtr; /* Points to the namespace containing the |
| 345 |
* reference (not the namespace that contains |
| 346 |
* the referenced command). NULL if the name |
| 347 |
* is fully qualified.*/ |
| 348 |
long refNsId; /* refNsPtr's unique namespace id. Used to |
| 349 |
* verify that refNsPtr is still valid (e.g., |
| 350 |
* it's possible that the cmd's containing |
| 351 |
* namespace was deleted and a new one created |
| 352 |
* at the same address). */ |
| 353 |
int refNsCmdEpoch; /* Value of the referencing namespace's |
| 354 |
* cmdRefEpoch when the pointer was cached. |
| 355 |
* Before using the cached pointer, we check |
| 356 |
* if the namespace's epoch was incremented; |
| 357 |
* if so, this cached pointer is invalid. */ |
| 358 |
int cmdEpoch; /* Value of the command's cmdEpoch when this |
| 359 |
* pointer was cached. Before using the cached |
| 360 |
* pointer, we check if the cmd's epoch was |
| 361 |
* incremented; if so, the cmd was renamed, |
| 362 |
* deleted, hidden, or exposed, and so the |
| 363 |
* pointer is invalid. */ |
| 364 |
int refCount; /* Reference count: 1 for each cmdName object |
| 365 |
* that has a pointer to this ResolvedCmdName |
| 366 |
* structure as its internal rep. This |
| 367 |
* structure can be freed when refCount |
| 368 |
* becomes zero. */ |
| 369 |
} ResolvedCmdName; |
| 370 |
|
| 371 |
/* |
| 372 |
*------------------------------------------------------------------------- |
| 373 |
* |
| 374 |
* TclInitObjectSubsystem -- |
| 375 |
* |
| 376 |
* This function is invoked to perform once-only initialization of the |
| 377 |
* type table. It also registers the object types defined in this file. |
| 378 |
* |
| 379 |
* Results: |
| 380 |
* None. |
| 381 |
* |
| 382 |
* Side effects: |
| 383 |
* Initializes the table of defined object types "typeTable" with builtin |
| 384 |
* object types defined in this file. |
| 385 |
* |
| 386 |
*------------------------------------------------------------------------- |
| 387 |
*/ |
| 388 |
|
| 389 |
void |
| 390 |
TclInitObjSubsystem(void) |
| 391 |
{ |
| 392 |
Tcl_MutexLock(&tableMutex); |
| 393 |
typeTableInitialized = 1; |
| 394 |
Tcl_InitHashTable(&typeTable, TCL_STRING_KEYS); |
| 395 |
Tcl_MutexUnlock(&tableMutex); |
| 396 |
|
| 397 |
Tcl_RegisterObjType(&tclArraySearchType); |
| 398 |
Tcl_RegisterObjType(&tclByteArrayType); |
| 399 |
Tcl_RegisterObjType(&tclByteCodeType); |
| 400 |
Tcl_RegisterObjType(&tclCmdNameType); |
| 401 |
Tcl_RegisterObjType(&tclDictType); |
| 402 |
Tcl_RegisterObjType(&tclDoubleType); |
| 403 |
Tcl_RegisterObjType(&tclEndOffsetType); |
| 404 |
Tcl_RegisterObjType(&tclIntType); |
| 405 |
Tcl_RegisterObjType(&tclListType); |
| 406 |
Tcl_RegisterObjType(&tclProcBodyType); |
| 407 |
Tcl_RegisterObjType(&tclRegexpType); |
| 408 |
Tcl_RegisterObjType(&tclStringType); |
| 409 |
|
| 410 |
/* For backward compatibility only ... */ |
| 411 |
Tcl_RegisterObjType(&oldBooleanType); |
| 412 |
#ifndef TCL_WIDE_INT_IS_LONG |
| 413 |
Tcl_RegisterObjType(&tclWideIntType); |
| 414 |
#endif |
| 415 |
|
| 416 |
#ifdef TCL_COMPILE_STATS |
| 417 |
Tcl_MutexLock(&tclObjMutex); |
| 418 |
tclObjsAlloced = 0; |
| 419 |
tclObjsFreed = 0; |
| 420 |
{ |
| 421 |
int i; |
| 422 |
|
| 423 |
for (i=0 ; i<TCL_MAX_SHARED_OBJ_STATS ; i++) { |
| 424 |
tclObjsShared[i] = 0; |
| 425 |
} |
| 426 |
} |
| 427 |
Tcl_MutexUnlock(&tclObjMutex); |
| 428 |
#endif |
| 429 |
} |
| 430 |
|
| 431 |
/* |
| 432 |
*---------------------------------------------------------------------- |
| 433 |
* |
| 434 |
* TclFinalizeThreadObjects -- |
| 435 |
* |
| 436 |
* This function is called by Tcl_FinalizeThread to clean up thread |
| 437 |
* specific Tcl_Obj information. |
| 438 |
* |
| 439 |
* Results: |
| 440 |
* None. |
| 441 |
* |
| 442 |
* Side effects: |
| 443 |
* None. |
| 444 |
* |
| 445 |
*---------------------------------------------------------------------- |
| 446 |
*/ |
| 447 |
|
| 448 |
void |
| 449 |
TclFinalizeThreadObjects(void) |
| 450 |
{ |
| 451 |
#if defined(TCL_MEM_DEBUG) && defined(TCL_THREADS) |
| 452 |
Tcl_HashEntry *hPtr; |
| 453 |
Tcl_HashSearch hSearch; |
| 454 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 455 |
Tcl_HashTable *tablePtr = tsdPtr->objThreadMap; |
| 456 |
|
| 457 |
if (tablePtr != NULL) { |
| 458 |
for (hPtr = Tcl_FirstHashEntry(tablePtr, &hSearch); |
| 459 |
hPtr != NULL; hPtr = Tcl_NextHashEntry(&hSearch)) { |
| 460 |
ObjData *objData = (ObjData *)Tcl_GetHashValue(hPtr); |
| 461 |
|
| 462 |
if (objData != NULL) { |
| 463 |
ckfree(objData); |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
Tcl_DeleteHashTable(tablePtr); |
| 468 |
ckfree(tablePtr); |
| 469 |
tsdPtr->objThreadMap = NULL; |
| 470 |
} |
| 471 |
#endif |
| 472 |
} |
| 473 |
|
| 474 |
/* |
| 475 |
*---------------------------------------------------------------------- |
| 476 |
* |
| 477 |
* TclFinalizeObjects -- |
| 478 |
* |
| 479 |
* This function is called by Tcl_Finalize to clean up all registered |
| 480 |
* Tcl_ObjType's and to reset the tclFreeObjList. |
| 481 |
* |
| 482 |
* Results: |
| 483 |
* None. |
| 484 |
* |
| 485 |
* Side effects: |
| 486 |
* None. |
| 487 |
* |
| 488 |
*---------------------------------------------------------------------- |
| 489 |
*/ |
| 490 |
|
| 491 |
void |
| 492 |
TclFinalizeObjects(void) |
| 493 |
{ |
| 494 |
Tcl_MutexLock(&tableMutex); |
| 495 |
if (typeTableInitialized) { |
| 496 |
Tcl_DeleteHashTable(&typeTable); |
| 497 |
typeTableInitialized = 0; |
| 498 |
} |
| 499 |
Tcl_MutexUnlock(&tableMutex); |
| 500 |
|
| 501 |
/* |
| 502 |
* All we do here is reset the head pointer of the linked list of free |
| 503 |
* Tcl_Obj's to NULL; the memory finalization will take care of releasing |
| 504 |
* memory for us. |
| 505 |
*/ |
| 506 |
Tcl_MutexLock(&tclObjMutex); |
| 507 |
tclFreeObjList = NULL; |
| 508 |
Tcl_MutexUnlock(&tclObjMutex); |
| 509 |
} |
| 510 |
|
| 511 |
/* |
| 512 |
*---------------------------------------------------------------------- |
| 513 |
* |
| 514 |
* TclGetContLineTable -- |
| 515 |
* |
| 516 |
* This procedure is a helper which returns the thread-specific |
| 517 |
* hash-table used to track continuation line information associated with |
| 518 |
* Tcl_Obj*, and the objThreadMap, etc. |
| 519 |
* |
| 520 |
* Results: |
| 521 |
* A reference to the thread-data. |
| 522 |
* |
| 523 |
* Side effects: |
| 524 |
* May allocate memory for the thread-data. |
| 525 |
* |
| 526 |
* TIP #280 |
| 527 |
*---------------------------------------------------------------------- |
| 528 |
*/ |
| 529 |
|
| 530 |
static ThreadSpecificData * |
| 531 |
TclGetContLineTable(void) |
| 532 |
{ |
| 533 |
/* |
| 534 |
* Initialize the hashtable tracking invisible continuation lines. For |
| 535 |
* the release we use a thread exit handler to ensure that this is done |
| 536 |
* before TSD blocks are made invalid. The TclFinalizeObjects() which |
| 537 |
* would be the natural place for this is invoked afterwards, meaning that |
| 538 |
* we try to operate on a data structure already gone. |
| 539 |
*/ |
| 540 |
|
| 541 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 542 |
|
| 543 |
if (!tsdPtr->lineCLPtr) { |
| 544 |
tsdPtr->lineCLPtr = (Tcl_HashTable *)ckalloc(sizeof(Tcl_HashTable)); |
| 545 |
Tcl_InitHashTable(tsdPtr->lineCLPtr, TCL_ONE_WORD_KEYS); |
| 546 |
Tcl_CreateThreadExitHandler(TclThreadFinalizeContLines,NULL); |
| 547 |
} |
| 548 |
return tsdPtr; |
| 549 |
} |
| 550 |
|
| 551 |
/* |
| 552 |
*---------------------------------------------------------------------- |
| 553 |
* |
| 554 |
* TclContinuationsEnter -- |
| 555 |
* |
| 556 |
* This procedure is a helper which saves the continuation line |
| 557 |
* information associated with a Tcl_Obj*. |
| 558 |
* |
| 559 |
* Results: |
| 560 |
* A reference to the newly created continuation line location table. |
| 561 |
* |
| 562 |
* Side effects: |
| 563 |
* Allocates memory for the table of continuation line locations. |
| 564 |
* |
| 565 |
* TIP #280 |
| 566 |
*---------------------------------------------------------------------- |
| 567 |
*/ |
| 568 |
|
| 569 |
ContLineLoc * |
| 570 |
TclContinuationsEnter( |
| 571 |
Tcl_Obj *objPtr, |
| 572 |
int num, |
| 573 |
int *loc) |
| 574 |
{ |
| 575 |
int newEntry; |
| 576 |
ThreadSpecificData *tsdPtr = TclGetContLineTable(); |
| 577 |
Tcl_HashEntry *hPtr = |
| 578 |
Tcl_CreateHashEntry(tsdPtr->lineCLPtr, objPtr, &newEntry); |
| 579 |
ContLineLoc *clLocPtr = (ContLineLoc *)ckalloc(TclOffset(ContLineLoc, loc) + (num + 1U) *sizeof(int)); |
| 580 |
|
| 581 |
if (!newEntry) { |
| 582 |
/* |
| 583 |
* We're entering ContLineLoc data for the same value more than one |
| 584 |
* time. Taking care not to leak the old entry. |
| 585 |
* |
| 586 |
* This can happen when literals in a proc body are shared. See for |
| 587 |
* example test info-30.19 where the action (code) for all branches of |
| 588 |
* the switch command is identical, mapping them all to the same |
| 589 |
* literal. An interesting result of this is that the number and |
| 590 |
* locations (offset) of invisible continuation lines in the literal |
| 591 |
* are the same for all occurrences. |
| 592 |
* |
| 593 |
* Note that while reusing the existing entry is possible it requires |
| 594 |
* the same actions as for a new entry because we have to copy the |
| 595 |
* incoming num/loc data even so. Because we are called from |
| 596 |
* TclContinuationsEnterDerived for this case, which modified the |
| 597 |
* stored locations (Rebased to the proper relative offset). Just |
| 598 |
* returning the stored entry would rebase them a second time, or |
| 599 |
* more, hosing the data. It is easier to simply replace, as we are |
| 600 |
* doing. |
| 601 |
*/ |
| 602 |
|
| 603 |
ckfree(Tcl_GetHashValue(hPtr)); |
| 604 |
} |
| 605 |
|
| 606 |
clLocPtr->num = num; |
| 607 |
memcpy(&clLocPtr->loc, loc, num*sizeof(int)); |
| 608 |
clLocPtr->loc[num] = CLL_END; /* Sentinel */ |
| 609 |
Tcl_SetHashValue(hPtr, clLocPtr); |
| 610 |
|
| 611 |
return clLocPtr; |
| 612 |
} |
| 613 |
|
| 614 |
/* |
| 615 |
*---------------------------------------------------------------------- |
| 616 |
* |
| 617 |
* TclContinuationsEnterDerived -- |
| 618 |
* |
| 619 |
* This procedure is a helper which computes the continuation line |
| 620 |
* information associated with a Tcl_Obj* cut from the middle of a |
| 621 |
* script. |
| 622 |
* |
| 623 |
* Results: |
| 624 |
* None. |
| 625 |
* |
| 626 |
* Side effects: |
| 627 |
* Allocates memory for the table of continuation line locations. |
| 628 |
* |
| 629 |
* TIP #280 |
| 630 |
*---------------------------------------------------------------------- |
| 631 |
*/ |
| 632 |
|
| 633 |
void |
| 634 |
TclContinuationsEnterDerived( |
| 635 |
Tcl_Obj *objPtr, |
| 636 |
int start, |
| 637 |
int *clNext) |
| 638 |
{ |
| 639 |
int length; |
| 640 |
int end, num; |
| 641 |
int *wordCLLast = clNext; |
| 642 |
|
| 643 |
/* |
| 644 |
* We have to handle invisible continuations lines here as well, despite |
| 645 |
* the code we have in TclSubstTokens (TST) for that. Why ? Nesting. If |
| 646 |
* our script is the sole argument to an 'eval' command, for example, the |
| 647 |
* scriptCLLocPtr we are using was generated by a previous call to TST, |
| 648 |
* and while the words we have here may contain continuation lines they |
| 649 |
* are invisible already, and the inner call to TST had no bs+nl sequences |
| 650 |
* to trigger its code. |
| 651 |
* |
| 652 |
* Luckily for us, the table we have to create here for the current word |
| 653 |
* has to be a slice of the table currently in use, with the locations |
| 654 |
* suitably modified to be relative to the start of the word instead of |
| 655 |
* relative to the script. |
| 656 |
* |
| 657 |
* That is what we are doing now. Determine the slice we need, and if not |
| 658 |
* empty, wrap it into a new table, and save the result into our |
| 659 |
* thread-global hashtable, as usual. |
| 660 |
*/ |
| 661 |
|
| 662 |
/* |
| 663 |
* First compute the range of the word within the script. (Is there a |
| 664 |
* better way which doesn't shimmer?) |
| 665 |
*/ |
| 666 |
|
| 667 |
TclGetStringFromObj(objPtr, &length); |
| 668 |
end = start + length; /* First char after the word */ |
| 669 |
|
| 670 |
/* |
| 671 |
* Then compute the table slice covering the range of the word. |
| 672 |
*/ |
| 673 |
|
| 674 |
while (*wordCLLast >= 0 && *wordCLLast < end) { |
| 675 |
wordCLLast++; |
| 676 |
} |
| 677 |
|
| 678 |
/* |
| 679 |
* And generate the table from the slice, if it was not empty. |
| 680 |
*/ |
| 681 |
|
| 682 |
num = wordCLLast - clNext; |
| 683 |
if (num) { |
| 684 |
int i; |
| 685 |
ContLineLoc *clLocPtr = TclContinuationsEnter(objPtr, num, clNext); |
| 686 |
|
| 687 |
/* |
| 688 |
* Re-base the locations. |
| 689 |
*/ |
| 690 |
|
| 691 |
for (i=0 ; i<num ; i++) { |
| 692 |
clLocPtr->loc[i] -= start; |
| 693 |
|
| 694 |
/* |
| 695 |
* Continuation lines coming before the string and affecting us |
| 696 |
* should not happen, due to the proper maintenance of clNext |
| 697 |
* during compilation. |
| 698 |
*/ |
| 699 |
|
| 700 |
if (clLocPtr->loc[i] < 0) { |
| 701 |
Tcl_Panic("Derived ICL data for object using offsets from before the script"); |
| 702 |
} |
| 703 |
} |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
/* |
| 708 |
*---------------------------------------------------------------------- |
| 709 |
* |
| 710 |
* TclContinuationsCopy -- |
| 711 |
* |
| 712 |
* This procedure is a helper which copies the continuation line |
| 713 |
* information associated with a Tcl_Obj* to another Tcl_Obj*. It is |
| 714 |
* assumed that both contain the same string/script. Use this when a |
| 715 |
* script is duplicated because it was shared. |
| 716 |
* |
| 717 |
* Results: |
| 718 |
* None. |
| 719 |
* |
| 720 |
* Side effects: |
| 721 |
* Allocates memory for the table of continuation line locations. |
| 722 |
* |
| 723 |
* TIP #280 |
| 724 |
*---------------------------------------------------------------------- |
| 725 |
*/ |
| 726 |
|
| 727 |
void |
| 728 |
TclContinuationsCopy( |
| 729 |
Tcl_Obj *objPtr, |
| 730 |
Tcl_Obj *originObjPtr) |
| 731 |
{ |
| 732 |
ThreadSpecificData *tsdPtr = TclGetContLineTable(); |
| 733 |
Tcl_HashEntry *hPtr = |
| 734 |
Tcl_FindHashEntry(tsdPtr->lineCLPtr, originObjPtr); |
| 735 |
|
| 736 |
if (hPtr) { |
| 737 |
ContLineLoc *clLocPtr = (ContLineLoc *)Tcl_GetHashValue(hPtr); |
| 738 |
|
| 739 |
TclContinuationsEnter(objPtr, clLocPtr->num, clLocPtr->loc); |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
/* |
| 744 |
*---------------------------------------------------------------------- |
| 745 |
* |
| 746 |
* TclContinuationsGet -- |
| 747 |
* |
| 748 |
* This procedure is a helper which retrieves the continuation line |
| 749 |
* information associated with a Tcl_Obj*, if it has any. |
| 750 |
* |
| 751 |
* Results: |
| 752 |
* A reference to the continuation line location table, or NULL if the |
| 753 |
* Tcl_Obj* has no such information associated with it. |
| 754 |
* |
| 755 |
* Side effects: |
| 756 |
* None. |
| 757 |
* |
| 758 |
* TIP #280 |
| 759 |
*---------------------------------------------------------------------- |
| 760 |
*/ |
| 761 |
|
| 762 |
ContLineLoc * |
| 763 |
TclContinuationsGet( |
| 764 |
Tcl_Obj *objPtr) |
| 765 |
{ |
| 766 |
ThreadSpecificData *tsdPtr = TclGetContLineTable(); |
| 767 |
Tcl_HashEntry *hPtr = |
| 768 |
Tcl_FindHashEntry(tsdPtr->lineCLPtr, objPtr); |
| 769 |
|
| 770 |
if (!hPtr) { |
| 771 |
return NULL; |
| 772 |
} |
| 773 |
return (ContLineLoc *)Tcl_GetHashValue(hPtr); |
| 774 |
} |
| 775 |
|
| 776 |
/* |
| 777 |
*---------------------------------------------------------------------- |
| 778 |
* |
| 779 |
* TclThreadFinalizeContLines -- |
| 780 |
* |
| 781 |
* This procedure is a helper which releases all continuation line |
| 782 |
* information currently known. It is run as a thread exit handler. |
| 783 |
* |
| 784 |
* Results: |
| 785 |
* None. |
| 786 |
* |
| 787 |
* Side effects: |
| 788 |
* Releases memory. |
| 789 |
* |
| 790 |
* TIP #280 |
| 791 |
*---------------------------------------------------------------------- |
| 792 |
*/ |
| 793 |
|
| 794 |
static void |
| 795 |
TclThreadFinalizeContLines( |
| 796 |
ClientData clientData) |
| 797 |
{ |
| 798 |
/* |
| 799 |
* Release the hashtable tracking invisible continuation lines. |
| 800 |
*/ |
| 801 |
|
| 802 |
ThreadSpecificData *tsdPtr = TclGetContLineTable(); |
| 803 |
Tcl_HashEntry *hPtr; |
| 804 |
Tcl_HashSearch hSearch; |
| 805 |
|
| 806 |
for (hPtr = Tcl_FirstHashEntry(tsdPtr->lineCLPtr, &hSearch); |
| 807 |
hPtr != NULL; hPtr = Tcl_NextHashEntry(&hSearch)) { |
| 808 |
ckfree(Tcl_GetHashValue(hPtr)); |
| 809 |
Tcl_DeleteHashEntry(hPtr); |
| 810 |
} |
| 811 |
Tcl_DeleteHashTable(tsdPtr->lineCLPtr); |
| 812 |
ckfree(tsdPtr->lineCLPtr); |
| 813 |
tsdPtr->lineCLPtr = NULL; |
| 814 |
} |
| 815 |
|
| 816 |
/* |
| 817 |
*-------------------------------------------------------------- |
| 818 |
* |
| 819 |
* Tcl_RegisterObjType -- |
| 820 |
* |
| 821 |
* This function is called to register a new Tcl object type in the table |
| 822 |
* of all object types supported by Tcl. |
| 823 |
* |
| 824 |
* Results: |
| 825 |
* None. |
| 826 |
* |
| 827 |
* Side effects: |
| 828 |
* The type is registered in the Tcl type table. If there was already a |
| 829 |
* type with the same name as in typePtr, it is replaced with the new |
| 830 |
* type. |
| 831 |
* |
| 832 |
*-------------------------------------------------------------- |
| 833 |
*/ |
| 834 |
|
| 835 |
void |
| 836 |
Tcl_RegisterObjType( |
| 837 |
const Tcl_ObjType *typePtr) /* Information about object type; storage must |
| 838 |
* be statically allocated (must live |
| 839 |
* forever). */ |
| 840 |
{ |
| 841 |
int isNew; |
| 842 |
|
| 843 |
Tcl_MutexLock(&tableMutex); |
| 844 |
Tcl_SetHashValue( |
| 845 |
Tcl_CreateHashEntry(&typeTable, typePtr->name, &isNew), typePtr); |
| 846 |
Tcl_MutexUnlock(&tableMutex); |
| 847 |
} |
| 848 |
|
| 849 |
/* |
| 850 |
*---------------------------------------------------------------------- |
| 851 |
* |
| 852 |
* Tcl_AppendAllObjTypes -- |
| 853 |
* |
| 854 |
* This function appends onto the argument object the name of each object |
| 855 |
* type as a list element. This includes the builtin object types (e.g. |
| 856 |
* int, list) as well as those added using Tcl_NewObj. These names can be |
| 857 |
* used, for example, with Tcl_GetObjType to get pointers to the |
| 858 |
* corresponding Tcl_ObjType structures. |
| 859 |
* |
| 860 |
* Results: |
| 861 |
* The return value is normally TCL_OK; in this case the object |
| 862 |
* referenced by objPtr has each type name appended to it. If an error |
| 863 |
* occurs, TCL_ERROR is returned and the interpreter's result holds an |
| 864 |
* error message. |
| 865 |
* |
| 866 |
* Side effects: |
| 867 |
* If necessary, the object referenced by objPtr is converted into a list |
| 868 |
* object. |
| 869 |
* |
| 870 |
*---------------------------------------------------------------------- |
| 871 |
*/ |
| 872 |
|
| 873 |
int |
| 874 |
Tcl_AppendAllObjTypes( |
| 875 |
Tcl_Interp *interp, /* Interpreter used for error reporting. */ |
| 876 |
Tcl_Obj *objPtr) /* Points to the Tcl object onto which the |
| 877 |
* name of each registered type is appended as |
| 878 |
* a list element. */ |
| 879 |
{ |
| 880 |
Tcl_HashEntry *hPtr; |
| 881 |
Tcl_HashSearch search; |
| 882 |
int numElems; |
| 883 |
|
| 884 |
/* |
| 885 |
* Get the test for a valid list out of the way first. |
| 886 |
*/ |
| 887 |
|
| 888 |
if (TclListObjLength(interp, objPtr, &numElems) != TCL_OK) { |
| 889 |
return TCL_ERROR; |
| 890 |
} |
| 891 |
|
| 892 |
/* |
| 893 |
* Type names are NUL-terminated, not counted strings. This code relies on |
| 894 |
* that. |
| 895 |
*/ |
| 896 |
|
| 897 |
Tcl_MutexLock(&tableMutex); |
| 898 |
for (hPtr = Tcl_FirstHashEntry(&typeTable, &search); |
| 899 |
hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) { |
| 900 |
Tcl_ListObjAppendElement(NULL, objPtr, |
| 901 |
Tcl_NewStringObj((char *)Tcl_GetHashKey(&typeTable, hPtr), -1)); |
| 902 |
} |
| 903 |
Tcl_MutexUnlock(&tableMutex); |
| 904 |
return TCL_OK; |
| 905 |
} |
| 906 |
|
| 907 |
/* |
| 908 |
*---------------------------------------------------------------------- |
| 909 |
* |
| 910 |
* Tcl_GetObjType -- |
| 911 |
* |
| 912 |
* This function looks up an object type by name. |
| 913 |
* |
| 914 |
* Results: |
| 915 |
* If an object type with name matching "typeName" is found, a pointer to |
| 916 |
* its Tcl_ObjType structure is returned; otherwise, NULL is returned. |
| 917 |
* |
| 918 |
* Side effects: |
| 919 |
* None. |
| 920 |
* |
| 921 |
*---------------------------------------------------------------------- |
| 922 |
*/ |
| 923 |
|
| 924 |
const Tcl_ObjType * |
| 925 |
Tcl_GetObjType( |
| 926 |
const char *typeName) /* Name of Tcl object type to look up. */ |
| 927 |
{ |
| 928 |
Tcl_HashEntry *hPtr; |
| 929 |
const Tcl_ObjType *typePtr = NULL; |
| 930 |
|
| 931 |
Tcl_MutexLock(&tableMutex); |
| 932 |
hPtr = Tcl_FindHashEntry(&typeTable, typeName); |
| 933 |
if (hPtr != NULL) { |
| 934 |
typePtr = (const Tcl_ObjType *)Tcl_GetHashValue(hPtr); |
| 935 |
} |
| 936 |
Tcl_MutexUnlock(&tableMutex); |
| 937 |
return typePtr; |
| 938 |
} |
| 939 |
|
| 940 |
/* |
| 941 |
*---------------------------------------------------------------------- |
| 942 |
* |
| 943 |
* Tcl_ConvertToType -- |
| 944 |
* |
| 945 |
* Convert the Tcl object "objPtr" to have type "typePtr" if possible. |
| 946 |
* |
| 947 |
* Results: |
| 948 |
* The return value is TCL_OK on success and TCL_ERROR on failure. If |
| 949 |
* TCL_ERROR is returned, then the interpreter's result contains an error |
| 950 |
* message unless "interp" is NULL. Passing a NULL "interp" allows this |
| 951 |
* function to be used as a test whether the conversion could be done |
| 952 |
* (and in fact was done). |
| 953 |
* |
| 954 |
* Side effects: |
| 955 |
* Any internal representation for the old type is freed. |
| 956 |
* |
| 957 |
*---------------------------------------------------------------------- |
| 958 |
*/ |
| 959 |
|
| 960 |
int |
| 961 |
Tcl_ConvertToType( |
| 962 |
Tcl_Interp *interp, /* Used for error reporting if not NULL. */ |
| 963 |
Tcl_Obj *objPtr, /* The object to convert. */ |
| 964 |
const Tcl_ObjType *typePtr) /* The target type. */ |
| 965 |
{ |
| 966 |
if (objPtr->typePtr == typePtr) { |
| 967 |
return TCL_OK; |
| 968 |
} |
| 969 |
|
| 970 |
/* |
| 971 |
* Use the target type's Tcl_SetFromAnyProc to set "objPtr"s internal form |
| 972 |
* as appropriate for the target type. This frees the old internal |
| 973 |
* representation. |
| 974 |
*/ |
| 975 |
|
| 976 |
if (typePtr->setFromAnyProc == NULL) { |
| 977 |
if (interp) { |
| 978 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 979 |
"can't convert value to type %s", typePtr->name)); |
| 980 |
Tcl_SetErrorCode(interp, "TCL", "API_ABUSE", (char *)NULL); |
| 981 |
} |
| 982 |
return TCL_ERROR; |
| 983 |
} |
| 984 |
|
| 985 |
return typePtr->setFromAnyProc(interp, objPtr); |
| 986 |
} |
| 987 |
|
| 988 |
/* |
| 989 |
*-------------------------------------------------------------- |
| 990 |
* |
| 991 |
* TclDbDumpActiveObjects -- |
| 992 |
* |
| 993 |
* This function is called to dump all of the active Tcl_Obj structs this |
| 994 |
* allocator knows about. |
| 995 |
* |
| 996 |
* Results: |
| 997 |
* None. |
| 998 |
* |
| 999 |
* Side effects: |
| 1000 |
* None. |
| 1001 |
* |
| 1002 |
*-------------------------------------------------------------- |
| 1003 |
*/ |
| 1004 |
|
| 1005 |
void |
| 1006 |
TclDbDumpActiveObjects( |
| 1007 |
FILE *outFile) |
| 1008 |
{ |
| 1009 |
#if defined(TCL_MEM_DEBUG) && defined(TCL_THREADS) |
| 1010 |
Tcl_HashSearch hSearch; |
| 1011 |
Tcl_HashEntry *hPtr; |
| 1012 |
Tcl_HashTable *tablePtr; |
| 1013 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1014 |
|
| 1015 |
tablePtr = tsdPtr->objThreadMap; |
| 1016 |
|
| 1017 |
if (tablePtr != NULL) { |
| 1018 |
fprintf(outFile, "total objects: %d\n", tablePtr->numEntries); |
| 1019 |
for (hPtr = Tcl_FirstHashEntry(tablePtr, &hSearch); hPtr != NULL; |
| 1020 |
hPtr = Tcl_NextHashEntry(&hSearch)) { |
| 1021 |
ObjData *objData = (ObjData *)Tcl_GetHashValue(hPtr); |
| 1022 |
|
| 1023 |
if (objData != NULL) { |
| 1024 |
fprintf(outFile, |
| 1025 |
"key = 0x%p, objPtr = 0x%p, file = %s, line = %d\n", |
| 1026 |
Tcl_GetHashKey(tablePtr, hPtr), objData->objPtr, |
| 1027 |
objData->file, objData->line); |
| 1028 |
} else { |
| 1029 |
fprintf(outFile, "key = 0x%p\n", |
| 1030 |
Tcl_GetHashKey(tablePtr, hPtr)); |
| 1031 |
} |
| 1032 |
} |
| 1033 |
} |
| 1034 |
#endif |
| 1035 |
} |
| 1036 |
|
| 1037 |
/* |
| 1038 |
*---------------------------------------------------------------------- |
| 1039 |
* |
| 1040 |
* TclDbInitNewObj -- |
| 1041 |
* |
| 1042 |
* Called via the TclNewObj or TclDbNewObj macros when TCL_MEM_DEBUG is |
| 1043 |
* enabled. This function will initialize the members of a Tcl_Obj |
| 1044 |
* struct. Initialization would be done inline via the TclNewObj macro |
| 1045 |
* when compiling without TCL_MEM_DEBUG. |
| 1046 |
* |
| 1047 |
* Results: |
| 1048 |
* The Tcl_Obj struct members are initialized. |
| 1049 |
* |
| 1050 |
* Side effects: |
| 1051 |
* None. |
| 1052 |
*---------------------------------------------------------------------- |
| 1053 |
*/ |
| 1054 |
|
| 1055 |
#ifdef TCL_MEM_DEBUG |
| 1056 |
void |
| 1057 |
TclDbInitNewObj( |
| 1058 |
Tcl_Obj *objPtr, |
| 1059 |
const char *file, /* The name of the source file calling this |
| 1060 |
* function; used for debugging. */ |
| 1061 |
int line) /* Line number in the source file; used for |
| 1062 |
* debugging. */ |
| 1063 |
{ |
| 1064 |
objPtr->refCount = 0; |
| 1065 |
objPtr->bytes = tclEmptyStringRep; |
| 1066 |
objPtr->length = 0; |
| 1067 |
objPtr->typePtr = NULL; |
| 1068 |
|
| 1069 |
#ifdef TCL_THREADS |
| 1070 |
/* |
| 1071 |
* Add entry to a thread local map used to check if a Tcl_Obj was |
| 1072 |
* allocated by the currently executing thread. |
| 1073 |
*/ |
| 1074 |
|
| 1075 |
if (!TclInExit()) { |
| 1076 |
Tcl_HashEntry *hPtr; |
| 1077 |
Tcl_HashTable *tablePtr; |
| 1078 |
int isNew; |
| 1079 |
ObjData *objData; |
| 1080 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1081 |
|
| 1082 |
if (tsdPtr->objThreadMap == NULL) { |
| 1083 |
tsdPtr->objThreadMap = (Tcl_HashTable *)ckalloc(sizeof(Tcl_HashTable)); |
| 1084 |
Tcl_InitHashTable(tsdPtr->objThreadMap, TCL_ONE_WORD_KEYS); |
| 1085 |
} |
| 1086 |
tablePtr = tsdPtr->objThreadMap; |
| 1087 |
hPtr = Tcl_CreateHashEntry(tablePtr, objPtr, &isNew); |
| 1088 |
if (!isNew) { |
| 1089 |
Tcl_Panic("expected to create new entry for object map"); |
| 1090 |
} |
| 1091 |
|
| 1092 |
/* |
| 1093 |
* Record the debugging information. |
| 1094 |
*/ |
| 1095 |
|
| 1096 |
objData = (ObjData *)ckalloc(sizeof(ObjData)); |
| 1097 |
objData->objPtr = objPtr; |
| 1098 |
objData->file = file; |
| 1099 |
objData->line = line; |
| 1100 |
Tcl_SetHashValue(hPtr, objData); |
| 1101 |
} |
| 1102 |
#endif /* TCL_THREADS */ |
| 1103 |
} |
| 1104 |
#endif /* TCL_MEM_DEBUG */ |
| 1105 |
|
| 1106 |
/* |
| 1107 |
*---------------------------------------------------------------------- |
| 1108 |
* |
| 1109 |
* Tcl_NewObj -- |
| 1110 |
* |
| 1111 |
* This function is normally called when not debugging: i.e., when |
| 1112 |
* TCL_MEM_DEBUG is not defined. It creates new Tcl objects that denote |
| 1113 |
* the empty string. These objects have a NULL object type and NULL |
| 1114 |
* string representation byte pointer. Type managers call this routine to |
| 1115 |
* allocate new objects that they further initialize. |
| 1116 |
* |
| 1117 |
* When TCL_MEM_DEBUG is defined, this function just returns the result |
| 1118 |
* of calling the debugging version Tcl_DbNewObj. |
| 1119 |
* |
| 1120 |
* Results: |
| 1121 |
* The result is a newly allocated object that represents the empty |
| 1122 |
* string. The new object's typePtr is set NULL and its ref count is set |
| 1123 |
* to 0. |
| 1124 |
* |
| 1125 |
* Side effects: |
| 1126 |
* If compiling with TCL_COMPILE_STATS, this function increments the |
| 1127 |
* global count of allocated objects (tclObjsAlloced). |
| 1128 |
* |
| 1129 |
*---------------------------------------------------------------------- |
| 1130 |
*/ |
| 1131 |
|
| 1132 |
#ifdef TCL_MEM_DEBUG |
| 1133 |
#undef Tcl_NewObj |
| 1134 |
|
| 1135 |
Tcl_Obj * |
| 1136 |
Tcl_NewObj(void) |
| 1137 |
{ |
| 1138 |
return Tcl_DbNewObj("unknown", 0); |
| 1139 |
} |
| 1140 |
|
| 1141 |
#else /* if not TCL_MEM_DEBUG */ |
| 1142 |
|
| 1143 |
Tcl_Obj * |
| 1144 |
Tcl_NewObj(void) |
| 1145 |
{ |
| 1146 |
Tcl_Obj *objPtr; |
| 1147 |
|
| 1148 |
/* |
| 1149 |
* Use the macro defined in tclInt.h - it will use the correct allocator. |
| 1150 |
*/ |
| 1151 |
|
| 1152 |
TclNewObj(objPtr); |
| 1153 |
return objPtr; |
| 1154 |
} |
| 1155 |
#endif /* TCL_MEM_DEBUG */ |
| 1156 |
|
| 1157 |
/* |
| 1158 |
*---------------------------------------------------------------------- |
| 1159 |
* |
| 1160 |
* Tcl_DbNewObj -- |
| 1161 |
* |
| 1162 |
* This function is normally called when debugging: i.e., when |
| 1163 |
* TCL_MEM_DEBUG is defined. It creates new Tcl objects that denote the |
| 1164 |
* empty string. It is the same as the Tcl_NewObj function above except |
| 1165 |
* that it calls Tcl_DbCkalloc directly with the file name and line |
| 1166 |
* number from its caller. This simplifies debugging since then the |
| 1167 |
* [memory active] command will report the correct file name and line |
| 1168 |
* number when reporting objects that haven't been freed. |
| 1169 |
* |
| 1170 |
* When TCL_MEM_DEBUG is not defined, this function just returns the |
| 1171 |
* result of calling Tcl_NewObj. |
| 1172 |
* |
| 1173 |
* Results: |
| 1174 |
* The result is a newly allocated that represents the empty string. The |
| 1175 |
* new object's typePtr is set NULL and its ref count is set to 0. |
| 1176 |
* |
| 1177 |
* Side effects: |
| 1178 |
* If compiling with TCL_COMPILE_STATS, this function increments the |
| 1179 |
* global count of allocated objects (tclObjsAlloced). |
| 1180 |
* |
| 1181 |
*---------------------------------------------------------------------- |
| 1182 |
*/ |
| 1183 |
|
| 1184 |
#ifdef TCL_MEM_DEBUG |
| 1185 |
|
| 1186 |
Tcl_Obj * |
| 1187 |
Tcl_DbNewObj( |
| 1188 |
const char *file, /* The name of the source file calling this |
| 1189 |
* function; used for debugging. */ |
| 1190 |
int line) /* Line number in the source file; used for |
| 1191 |
* debugging. */ |
| 1192 |
{ |
| 1193 |
Tcl_Obj *objPtr; |
| 1194 |
|
| 1195 |
/* |
| 1196 |
* Use the macro defined in tclInt.h - it will use the correct allocator. |
| 1197 |
*/ |
| 1198 |
|
| 1199 |
TclDbNewObj(objPtr, file, line); |
| 1200 |
return objPtr; |
| 1201 |
} |
| 1202 |
#else /* if not TCL_MEM_DEBUG */ |
| 1203 |
|
| 1204 |
Tcl_Obj * |
| 1205 |
Tcl_DbNewObj( |
| 1206 |
const char *file, /* The name of the source file calling this |
| 1207 |
* function; used for debugging. */ |
| 1208 |
int line) /* Line number in the source file; used for |
| 1209 |
* debugging. */ |
| 1210 |
{ |
| 1211 |
return Tcl_NewObj(); |
| 1212 |
} |
| 1213 |
#endif /* TCL_MEM_DEBUG */ |
| 1214 |
|
| 1215 |
/* |
| 1216 |
*---------------------------------------------------------------------- |
| 1217 |
* |
| 1218 |
* TclAllocateFreeObjects -- |
| 1219 |
* |
| 1220 |
* Function to allocate a number of free Tcl_Objs. This is done using a |
| 1221 |
* single ckalloc to reduce the overhead for Tcl_Obj allocation. |
| 1222 |
* |
| 1223 |
* Assumes mutex is held. |
| 1224 |
* |
| 1225 |
* Results: |
| 1226 |
* None. |
| 1227 |
* |
| 1228 |
* Side effects: |
| 1229 |
* tclFreeObjList, the head of the list of free Tcl_Objs, is set to the |
| 1230 |
* first of a number of free Tcl_Obj's linked together by their |
| 1231 |
* internalRep.twoPtrValue.ptr1's. |
| 1232 |
* |
| 1233 |
*---------------------------------------------------------------------- |
| 1234 |
*/ |
| 1235 |
|
| 1236 |
#define OBJS_TO_ALLOC_EACH_TIME 100 |
| 1237 |
|
| 1238 |
void |
| 1239 |
TclAllocateFreeObjects(void) |
| 1240 |
{ |
| 1241 |
size_t bytesToAlloc = (OBJS_TO_ALLOC_EACH_TIME * sizeof(Tcl_Obj)); |
| 1242 |
char *basePtr; |
| 1243 |
Tcl_Obj *prevPtr, *objPtr; |
| 1244 |
int i; |
| 1245 |
|
| 1246 |
/* |
| 1247 |
* This has been noted by Purify to be a potential leak. The problem is |
| 1248 |
* that Tcl, when not TCL_MEM_DEBUG compiled, keeps around all allocated |
| 1249 |
* Tcl_Obj's, pointed to by tclFreeObjList, when freed instead of actually |
| 1250 |
* freeing the memory. TclFinalizeObjects() does not ckfree() this memory, |
| 1251 |
* but leaves it to Tcl's memory subsystem finalization to release it. |
| 1252 |
* Purify apparently can't figure that out, and fires a false alarm. |
| 1253 |
*/ |
| 1254 |
|
| 1255 |
basePtr = (char *)ckalloc(bytesToAlloc); |
| 1256 |
|
| 1257 |
prevPtr = NULL; |
| 1258 |
objPtr = (Tcl_Obj *) basePtr; |
| 1259 |
for (i = 0; i < OBJS_TO_ALLOC_EACH_TIME; i++) { |
| 1260 |
objPtr->internalRep.twoPtrValue.ptr1 = prevPtr; |
| 1261 |
prevPtr = objPtr; |
| 1262 |
objPtr++; |
| 1263 |
} |
| 1264 |
tclFreeObjList = prevPtr; |
| 1265 |
} |
| 1266 |
#undef OBJS_TO_ALLOC_EACH_TIME |
| 1267 |
|
| 1268 |
/* |
| 1269 |
*---------------------------------------------------------------------- |
| 1270 |
* |
| 1271 |
* TclFreeObj -- |
| 1272 |
* |
| 1273 |
* This function frees the memory associated with the argument object. |
| 1274 |
* It is called by the tcl.h macro Tcl_DecrRefCount when an object's ref |
| 1275 |
* count is zero. It is only "public" since it must be callable by that |
| 1276 |
* macro wherever the macro is used. It should not be directly called by |
| 1277 |
* clients. |
| 1278 |
* |
| 1279 |
* Results: |
| 1280 |
* None. |
| 1281 |
* |
| 1282 |
* Side effects: |
| 1283 |
* Deallocates the storage for the object's Tcl_Obj structure after |
| 1284 |
* deallocating the string representation and calling the type-specific |
| 1285 |
* Tcl_FreeInternalRepProc to deallocate the object's internal |
| 1286 |
* representation. If compiling with TCL_COMPILE_STATS, this function |
| 1287 |
* increments the global count of freed objects (tclObjsFreed). |
| 1288 |
* |
| 1289 |
*---------------------------------------------------------------------- |
| 1290 |
*/ |
| 1291 |
|
| 1292 |
#ifdef TCL_MEM_DEBUG |
| 1293 |
void |
| 1294 |
TclFreeObj( |
| 1295 |
Tcl_Obj *objPtr) /* The object to be freed. */ |
| 1296 |
{ |
| 1297 |
const Tcl_ObjType *typePtr = objPtr->typePtr; |
| 1298 |
|
| 1299 |
/* |
| 1300 |
* This macro declares a variable, so must come here... |
| 1301 |
*/ |
| 1302 |
|
| 1303 |
ObjInitDeletionContext(context); |
| 1304 |
|
| 1305 |
# ifdef TCL_THREADS |
| 1306 |
/* |
| 1307 |
* Check to make sure that the Tcl_Obj was allocated by the current |
| 1308 |
* thread. Don't do this check when shutting down since thread local |
| 1309 |
* storage can be finalized before the last Tcl_Obj is freed. |
| 1310 |
*/ |
| 1311 |
|
| 1312 |
if (!TclInExit()) { |
| 1313 |
Tcl_HashTable *tablePtr; |
| 1314 |
Tcl_HashEntry *hPtr; |
| 1315 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1316 |
|
| 1317 |
tablePtr = tsdPtr->objThreadMap; |
| 1318 |
if (!tablePtr) { |
| 1319 |
Tcl_Panic("TclFreeObj: object table not initialized"); |
| 1320 |
} |
| 1321 |
hPtr = Tcl_FindHashEntry(tablePtr, (char *)objPtr); |
| 1322 |
if (hPtr) { |
| 1323 |
/* |
| 1324 |
* As the Tcl_Obj is going to be deleted we remove the entry. |
| 1325 |
*/ |
| 1326 |
|
| 1327 |
ObjData *objData = (ObjData *)Tcl_GetHashValue(hPtr); |
| 1328 |
|
| 1329 |
if (objData != NULL) { |
| 1330 |
ckfree(objData); |
| 1331 |
} |
| 1332 |
|
| 1333 |
Tcl_DeleteHashEntry(hPtr); |
| 1334 |
} |
| 1335 |
} |
| 1336 |
# endif |
| 1337 |
|
| 1338 |
/* |
| 1339 |
* Check for a double free of the same value. This is slightly tricky |
| 1340 |
* because it is customary to free a Tcl_Obj when its refcount falls |
| 1341 |
* either from 1 to 0, or from 0 to -1. Falling from -1 to -2, though, |
| 1342 |
* and so on, is always a sign of a botch in the caller. |
| 1343 |
*/ |
| 1344 |
if (objPtr->refCount < -1) { |
| 1345 |
Tcl_Panic("Reference count for %p was negative", objPtr); |
| 1346 |
} |
| 1347 |
/* |
| 1348 |
* Now, in case we just approved drop from 1 to 0 as acceptable, make |
| 1349 |
* sure we do not accept a second free when falling from 0 to -1. |
| 1350 |
* Skip that possibility so any double free will trigger the panic. |
| 1351 |
*/ |
| 1352 |
objPtr->refCount = -1; |
| 1353 |
|
| 1354 |
/* |
| 1355 |
* Invalidate the string rep first so we can use the bytes value for our |
| 1356 |
* pointer chain, and signal an obj deletion (as opposed to shimmering) |
| 1357 |
* with 'length == -1'. |
| 1358 |
*/ |
| 1359 |
|
| 1360 |
TclInvalidateStringRep(objPtr); |
| 1361 |
objPtr->length = -1; |
| 1362 |
|
| 1363 |
if (ObjDeletePending(context)) { |
| 1364 |
PushObjToDelete(context, objPtr); |
| 1365 |
} else { |
| 1366 |
TCL_DTRACE_OBJ_FREE(objPtr); |
| 1367 |
if ((typePtr != NULL) && (typePtr->freeIntRepProc != NULL)) { |
| 1368 |
ObjDeletionLock(context); |
| 1369 |
typePtr->freeIntRepProc(objPtr); |
| 1370 |
ObjDeletionUnlock(context); |
| 1371 |
} |
| 1372 |
|
| 1373 |
Tcl_MutexLock(&tclObjMutex); |
| 1374 |
ckfree(objPtr); |
| 1375 |
Tcl_MutexUnlock(&tclObjMutex); |
| 1376 |
TclIncrObjsFreed(); |
| 1377 |
ObjDeletionLock(context); |
| 1378 |
while (ObjOnStack(context)) { |
| 1379 |
Tcl_Obj *objToFree; |
| 1380 |
|
| 1381 |
PopObjToDelete(context, objToFree); |
| 1382 |
TCL_DTRACE_OBJ_FREE(objToFree); |
| 1383 |
TclFreeIntRep(objToFree); |
| 1384 |
|
| 1385 |
Tcl_MutexLock(&tclObjMutex); |
| 1386 |
ckfree(objToFree); |
| 1387 |
Tcl_MutexUnlock(&tclObjMutex); |
| 1388 |
TclIncrObjsFreed(); |
| 1389 |
} |
| 1390 |
ObjDeletionUnlock(context); |
| 1391 |
} |
| 1392 |
|
| 1393 |
/* |
| 1394 |
* We cannot use TclGetContinuationTable() here, because that may |
| 1395 |
* re-initialize the thread-data for calls coming after the finalization. |
| 1396 |
* We have to access it using the low-level call and then check for |
| 1397 |
* validity. This function can be called after TclFinalizeThreadData() has |
| 1398 |
* already killed the thread-global data structures. Performing |
| 1399 |
* TCL_TSD_INIT will leave us with an uninitialized memory block upon |
| 1400 |
* which we crash (if we where to access the uninitialized hashtable). |
| 1401 |
*/ |
| 1402 |
|
| 1403 |
{ |
| 1404 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1405 |
Tcl_HashEntry *hPtr; |
| 1406 |
|
| 1407 |
if (tsdPtr->lineCLPtr) { |
| 1408 |
hPtr = Tcl_FindHashEntry(tsdPtr->lineCLPtr, objPtr); |
| 1409 |
if (hPtr) { |
| 1410 |
ckfree(Tcl_GetHashValue(hPtr)); |
| 1411 |
Tcl_DeleteHashEntry(hPtr); |
| 1412 |
} |
| 1413 |
} |
| 1414 |
} |
| 1415 |
} |
| 1416 |
#else /* TCL_MEM_DEBUG */ |
| 1417 |
|
| 1418 |
void |
| 1419 |
TclFreeObj( |
| 1420 |
Tcl_Obj *objPtr) /* The object to be freed. */ |
| 1421 |
{ |
| 1422 |
/* |
| 1423 |
* Invalidate the string rep first so we can use the bytes value for our |
| 1424 |
* pointer chain, and signal an obj deletion (as opposed to shimmering) |
| 1425 |
* with 'length == -1'. |
| 1426 |
*/ |
| 1427 |
|
| 1428 |
TclInvalidateStringRep(objPtr); |
| 1429 |
objPtr->length = -1; |
| 1430 |
|
| 1431 |
if (!objPtr->typePtr || !objPtr->typePtr->freeIntRepProc) { |
| 1432 |
/* |
| 1433 |
* objPtr can be freed safely, as it will not attempt to free any |
| 1434 |
* other objects: it will not cause recursive calls to this function. |
| 1435 |
*/ |
| 1436 |
|
| 1437 |
TCL_DTRACE_OBJ_FREE(objPtr); |
| 1438 |
TclFreeObjStorage(objPtr); |
| 1439 |
TclIncrObjsFreed(); |
| 1440 |
} else { |
| 1441 |
/* |
| 1442 |
* This macro declares a variable, so must come here... |
| 1443 |
*/ |
| 1444 |
|
| 1445 |
ObjInitDeletionContext(context); |
| 1446 |
|
| 1447 |
if (ObjDeletePending(context)) { |
| 1448 |
PushObjToDelete(context, objPtr); |
| 1449 |
} else { |
| 1450 |
/* |
| 1451 |
* Note that the contents of the while loop assume that the string |
| 1452 |
* rep has already been freed and we don't want to do anything |
| 1453 |
* fancy with adding to the queue inside ourselves. Must take care |
| 1454 |
* to unstack the object first since freeing the internal rep can |
| 1455 |
* add further objects to the stack. The code assumes that it is |
| 1456 |
* the first thing in a block; all current usages in the core |
| 1457 |
* satisfy this. |
| 1458 |
*/ |
| 1459 |
|
| 1460 |
TCL_DTRACE_OBJ_FREE(objPtr); |
| 1461 |
ObjDeletionLock(context); |
| 1462 |
objPtr->typePtr->freeIntRepProc(objPtr); |
| 1463 |
ObjDeletionUnlock(context); |
| 1464 |
|
| 1465 |
TclFreeObjStorage(objPtr); |
| 1466 |
TclIncrObjsFreed(); |
| 1467 |
ObjDeletionLock(context); |
| 1468 |
while (ObjOnStack(context)) { |
| 1469 |
Tcl_Obj *objToFree; |
| 1470 |
|
| 1471 |
PopObjToDelete(context, objToFree); |
| 1472 |
TCL_DTRACE_OBJ_FREE(objToFree); |
| 1473 |
if ((objToFree->typePtr != NULL) |
| 1474 |
&& (objToFree->typePtr->freeIntRepProc != NULL)) { |
| 1475 |
objToFree->typePtr->freeIntRepProc(objToFree); |
| 1476 |
} |
| 1477 |
TclFreeObjStorage(objToFree); |
| 1478 |
TclIncrObjsFreed(); |
| 1479 |
} |
| 1480 |
ObjDeletionUnlock(context); |
| 1481 |
} |
| 1482 |
} |
| 1483 |
|
| 1484 |
/* |
| 1485 |
* We cannot use TclGetContinuationTable() here, because that may |
| 1486 |
* re-initialize the thread-data for calls coming after the finalization. |
| 1487 |
* We have to access it using the low-level call and then check for |
| 1488 |
* validity. This function can be called after TclFinalizeThreadData() has |
| 1489 |
* already killed the thread-global data structures. Performing |
| 1490 |
* TCL_TSD_INIT will leave us with an uninitialized memory block upon |
| 1491 |
* which we crash (if we where to access the uninitialized hashtable). |
| 1492 |
*/ |
| 1493 |
|
| 1494 |
{ |
| 1495 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1496 |
Tcl_HashEntry *hPtr; |
| 1497 |
|
| 1498 |
if (tsdPtr->lineCLPtr) { |
| 1499 |
hPtr = Tcl_FindHashEntry(tsdPtr->lineCLPtr, objPtr); |
| 1500 |
if (hPtr) { |
| 1501 |
ckfree(Tcl_GetHashValue(hPtr)); |
| 1502 |
Tcl_DeleteHashEntry(hPtr); |
| 1503 |
} |
| 1504 |
} |
| 1505 |
} |
| 1506 |
} |
| 1507 |
#endif /* TCL_MEM_DEBUG */ |
| 1508 |
|
| 1509 |
/* |
| 1510 |
*---------------------------------------------------------------------- |
| 1511 |
* |
| 1512 |
* TclObjBeingDeleted -- |
| 1513 |
* |
| 1514 |
* This function returns 1 when the Tcl_Obj is being deleted. It is |
| 1515 |
* provided for the rare cases where the reason for the loss of an |
| 1516 |
* internal rep might be relevant. [FR 1512138] |
| 1517 |
* |
| 1518 |
* Results: |
| 1519 |
* 1 if being deleted, 0 otherwise. |
| 1520 |
* |
| 1521 |
* Side effects: |
| 1522 |
* None. |
| 1523 |
* |
| 1524 |
*---------------------------------------------------------------------- |
| 1525 |
*/ |
| 1526 |
|
| 1527 |
int |
| 1528 |
TclObjBeingDeleted( |
| 1529 |
Tcl_Obj *objPtr) |
| 1530 |
{ |
| 1531 |
return (objPtr->length == -1); |
| 1532 |
} |
| 1533 |
|
| 1534 |
/* |
| 1535 |
*---------------------------------------------------------------------- |
| 1536 |
* |
| 1537 |
* Tcl_DuplicateObj -- |
| 1538 |
* |
| 1539 |
* Create and return a new object that is a duplicate of the argument |
| 1540 |
* object. |
| 1541 |
* |
| 1542 |
* Results: |
| 1543 |
* The return value is a pointer to a newly created Tcl_Obj. This object |
| 1544 |
* has reference count 0 and the same type, if any, as the source object |
| 1545 |
* objPtr. Also: |
| 1546 |
* 1) If the source object has a valid string rep, we copy it; |
| 1547 |
* otherwise, the duplicate's string rep is set NULL to mark it |
| 1548 |
* invalid. |
| 1549 |
* 2) If the source object has an internal representation (i.e. its |
| 1550 |
* typePtr is non-NULL), the new object's internal rep is set to a |
| 1551 |
* copy; otherwise the new internal rep is marked invalid. |
| 1552 |
* |
| 1553 |
* Side effects: |
| 1554 |
* What constitutes "copying" the internal representation depends on the |
| 1555 |
* type. For example, if the argument object is a list, the element |
| 1556 |
* objects it points to will not actually be copied but will be shared |
| 1557 |
* with the duplicate list. That is, the ref counts of the element |
| 1558 |
* objects will be incremented. |
| 1559 |
* |
| 1560 |
*---------------------------------------------------------------------- |
| 1561 |
*/ |
| 1562 |
|
| 1563 |
#define SetDuplicateObj(dupPtr, objPtr) \ |
| 1564 |
{ \ |
| 1565 |
const Tcl_ObjType *typePtr = (objPtr)->typePtr; \ |
| 1566 |
const char *bytes = (objPtr)->bytes; \ |
| 1567 |
if (bytes) { \ |
| 1568 |
TclInitStringRep((dupPtr), bytes, (objPtr)->length); \ |
| 1569 |
} else { \ |
| 1570 |
(dupPtr)->bytes = NULL; \ |
| 1571 |
} \ |
| 1572 |
if (typePtr) { \ |
| 1573 |
if (typePtr->dupIntRepProc) { \ |
| 1574 |
typePtr->dupIntRepProc((objPtr), (dupPtr)); \ |
| 1575 |
} else { \ |
| 1576 |
(dupPtr)->internalRep = (objPtr)->internalRep; \ |
| 1577 |
(dupPtr)->typePtr = typePtr; \ |
| 1578 |
} \ |
| 1579 |
} \ |
| 1580 |
} |
| 1581 |
|
| 1582 |
Tcl_Obj * |
| 1583 |
Tcl_DuplicateObj( |
| 1584 |
Tcl_Obj *objPtr) /* The object to duplicate. */ |
| 1585 |
{ |
| 1586 |
Tcl_Obj *dupPtr; |
| 1587 |
|
| 1588 |
TclNewObj(dupPtr); |
| 1589 |
SetDuplicateObj(dupPtr, objPtr); |
| 1590 |
return dupPtr; |
| 1591 |
} |
| 1592 |
|
| 1593 |
void |
| 1594 |
TclSetDuplicateObj( |
| 1595 |
Tcl_Obj *dupPtr, |
| 1596 |
Tcl_Obj *objPtr) |
| 1597 |
{ |
| 1598 |
if (Tcl_IsShared(dupPtr)) { |
| 1599 |
Tcl_Panic("%s called with shared object", "TclSetDuplicateObj"); |
| 1600 |
} |
| 1601 |
TclInvalidateStringRep(dupPtr); |
| 1602 |
TclFreeIntRep(dupPtr); |
| 1603 |
SetDuplicateObj(dupPtr, objPtr); |
| 1604 |
} |
| 1605 |
|
| 1606 |
/* |
| 1607 |
*---------------------------------------------------------------------- |
| 1608 |
* |
| 1609 |
* Tcl_GetString -- |
| 1610 |
* |
| 1611 |
* Returns the string representation byte array pointer for an object. |
| 1612 |
* |
| 1613 |
* Results: |
| 1614 |
* Returns a pointer to the string representation of objPtr. The byte |
| 1615 |
* array referenced by the returned pointer must not be modified by the |
| 1616 |
* caller. Furthermore, the caller must copy the bytes if they need to |
| 1617 |
* retain them since the object's string rep can change as a result of |
| 1618 |
* other operations. |
| 1619 |
* |
| 1620 |
* Side effects: |
| 1621 |
* May call the object's updateStringProc to update the string |
| 1622 |
* representation from the internal representation. |
| 1623 |
* |
| 1624 |
*---------------------------------------------------------------------- |
| 1625 |
*/ |
| 1626 |
|
| 1627 |
char * |
| 1628 |
Tcl_GetString( |
| 1629 |
Tcl_Obj *objPtr) /* Object whose string rep byte pointer should |
| 1630 |
* be returned. */ |
| 1631 |
{ |
| 1632 |
if (objPtr->bytes != NULL) { |
| 1633 |
return objPtr->bytes; |
| 1634 |
} |
| 1635 |
|
| 1636 |
/* |
| 1637 |
* Note we do not check for objPtr->typePtr == NULL. An invariant of |
| 1638 |
* a properly maintained Tcl_Obj is that at least one of objPtr->bytes |
| 1639 |
* and objPtr->typePtr must not be NULL. If broken extensions fail to |
| 1640 |
* maintain that invariant, we can crash here. |
| 1641 |
*/ |
| 1642 |
|
| 1643 |
if (objPtr->typePtr->updateStringProc == NULL) { |
| 1644 |
/* |
| 1645 |
* Those Tcl_ObjTypes which choose not to define an updateStringProc |
| 1646 |
* must be written in such a way that (objPtr->bytes) never becomes |
| 1647 |
* NULL. This panic was added in Tcl 8.1. |
| 1648 |
*/ |
| 1649 |
|
| 1650 |
Tcl_Panic("UpdateStringProc should not be invoked for type %s", |
| 1651 |
objPtr->typePtr->name); |
| 1652 |
} |
| 1653 |
objPtr->typePtr->updateStringProc(objPtr); |
| 1654 |
if (objPtr->bytes == NULL || objPtr->length < 0 |
| 1655 |
|| objPtr->bytes[objPtr->length] != '\0') { |
| 1656 |
Tcl_Panic("UpdateStringProc for type '%s' " |
| 1657 |
"failed to create a valid string rep", objPtr->typePtr->name); |
| 1658 |
} |
| 1659 |
return objPtr->bytes; |
| 1660 |
} |
| 1661 |
|
| 1662 |
/* |
| 1663 |
*---------------------------------------------------------------------- |
| 1664 |
* |
| 1665 |
* Tcl_GetStringFromObj -- |
| 1666 |
* |
| 1667 |
* Returns the string representation's byte array pointer and length for |
| 1668 |
* an object. |
| 1669 |
* |
| 1670 |
* Results: |
| 1671 |
* Returns a pointer to the string representation of objPtr. If lengthPtr |
| 1672 |
* isn't NULL, the length of the string representation is stored at |
| 1673 |
* *lengthPtr. The byte array referenced by the returned pointer must not |
| 1674 |
* be modified by the caller. Furthermore, the caller must copy the bytes |
| 1675 |
* if they need to retain them since the object's string rep can change |
| 1676 |
* as a result of other operations. |
| 1677 |
* |
| 1678 |
* Side effects: |
| 1679 |
* May call the object's updateStringProc to update the string |
| 1680 |
* representation from the internal representation. |
| 1681 |
* |
| 1682 |
*---------------------------------------------------------------------- |
| 1683 |
*/ |
| 1684 |
|
| 1685 |
char * |
| 1686 |
Tcl_GetStringFromObj( |
| 1687 |
Tcl_Obj *objPtr, /* Object whose string rep byte pointer should |
| 1688 |
* be returned. */ |
| 1689 |
int *lengthPtr) /* If non-NULL, the location where the string |
| 1690 |
* rep's byte array length should * be stored. |
| 1691 |
* If NULL, no length is stored. */ |
| 1692 |
{ |
| 1693 |
(void) TclGetString(objPtr); |
| 1694 |
|
| 1695 |
if (lengthPtr != NULL) { |
| 1696 |
*lengthPtr = objPtr->length; |
| 1697 |
} |
| 1698 |
return objPtr->bytes; |
| 1699 |
} |
| 1700 |
|
| 1701 |
/* |
| 1702 |
*---------------------------------------------------------------------- |
| 1703 |
* |
| 1704 |
* Tcl_InvalidateStringRep -- |
| 1705 |
* |
| 1706 |
* This function is called to invalidate an object's string |
| 1707 |
* representation. |
| 1708 |
* |
| 1709 |
* Results: |
| 1710 |
* None. |
| 1711 |
* |
| 1712 |
* Side effects: |
| 1713 |
* Deallocates the storage for any old string representation, then sets |
| 1714 |
* the string representation NULL to mark it invalid. |
| 1715 |
* |
| 1716 |
*---------------------------------------------------------------------- |
| 1717 |
*/ |
| 1718 |
|
| 1719 |
void |
| 1720 |
Tcl_InvalidateStringRep( |
| 1721 |
Tcl_Obj *objPtr) /* Object whose string rep byte pointer should |
| 1722 |
* be freed. */ |
| 1723 |
{ |
| 1724 |
TclInvalidateStringRep(objPtr); |
| 1725 |
} |
| 1726 |
|
| 1727 |
/* |
| 1728 |
*---------------------------------------------------------------------- |
| 1729 |
* |
| 1730 |
* Tcl_NewBooleanObj -- |
| 1731 |
* |
| 1732 |
* This function is normally called when not debugging: i.e., when |
| 1733 |
* TCL_MEM_DEBUG is not defined. It creates a new Tcl_Obj and |
| 1734 |
* initializes it from the argument boolean value. A nonzero "intValue" |
| 1735 |
* is coerced to 1. |
| 1736 |
* |
| 1737 |
* When TCL_MEM_DEBUG is defined, this function just returns the result |
| 1738 |
* of calling the debugging version Tcl_DbNewBooleanObj. |
| 1739 |
* |
| 1740 |
* Results: |
| 1741 |
* The newly created object is returned. This object will have an invalid |
| 1742 |
* string representation. The returned object has ref count 0. |
| 1743 |
* |
| 1744 |
* Side effects: |
| 1745 |
* None. |
| 1746 |
* |
| 1747 |
*---------------------------------------------------------------------- |
| 1748 |
*/ |
| 1749 |
|
| 1750 |
#undef Tcl_NewBooleanObj |
| 1751 |
#ifdef TCL_MEM_DEBUG |
| 1752 |
|
| 1753 |
Tcl_Obj * |
| 1754 |
Tcl_NewBooleanObj( |
| 1755 |
int intValue) /* Boolean used to initialize new object. */ |
| 1756 |
{ |
| 1757 |
return Tcl_DbNewBooleanObj(intValue, "unknown", 0); |
| 1758 |
} |
| 1759 |
|
| 1760 |
#else /* if not TCL_MEM_DEBUG */ |
| 1761 |
|
| 1762 |
Tcl_Obj * |
| 1763 |
Tcl_NewBooleanObj( |
| 1764 |
int intValue) /* Boolean used to initialize new object. */ |
| 1765 |
{ |
| 1766 |
Tcl_Obj *objPtr; |
| 1767 |
|
| 1768 |
TclNewBooleanObj(objPtr, intValue); |
| 1769 |
return objPtr; |
| 1770 |
} |
| 1771 |
#endif /* TCL_MEM_DEBUG */ |
| 1772 |
|
| 1773 |
/* |
| 1774 |
*---------------------------------------------------------------------- |
| 1775 |
* |
| 1776 |
* Tcl_DbNewBooleanObj -- |
| 1777 |
* |
| 1778 |
* This function is normally called when debugging: i.e., when |
| 1779 |
* TCL_MEM_DEBUG is defined. It creates new boolean objects. It is the |
| 1780 |
* same as the Tcl_NewBooleanObj function above except that it calls |
| 1781 |
* Tcl_DbCkalloc directly with the file name and line number from its |
| 1782 |
* caller. This simplifies debugging since then the [memory active] |
| 1783 |
* command will report the correct file name and line number when |
| 1784 |
* reporting objects that haven't been freed. |
| 1785 |
* |
| 1786 |
* When TCL_MEM_DEBUG is not defined, this function just returns the |
| 1787 |
* result of calling Tcl_NewBooleanObj. |
| 1788 |
* |
| 1789 |
* Results: |
| 1790 |
* The newly created object is returned. This object will have an invalid |
| 1791 |
* string representation. The returned object has ref count 0. |
| 1792 |
* |
| 1793 |
* Side effects: |
| 1794 |
* None. |
| 1795 |
* |
| 1796 |
*---------------------------------------------------------------------- |
| 1797 |
*/ |
| 1798 |
|
| 1799 |
#undef Tcl_DbNewBooleanObj |
| 1800 |
#ifdef TCL_MEM_DEBUG |
| 1801 |
|
| 1802 |
Tcl_Obj * |
| 1803 |
Tcl_DbNewBooleanObj( |
| 1804 |
int intValue, /* Boolean used to initialize new object. */ |
| 1805 |
const char *file, /* The name of the source file calling this |
| 1806 |
* function; used for debugging. */ |
| 1807 |
int line) /* Line number in the source file; used for |
| 1808 |
* debugging. */ |
| 1809 |
{ |
| 1810 |
Tcl_Obj *objPtr; |
| 1811 |
|
| 1812 |
TclDbNewObj(objPtr, file, line); |
| 1813 |
objPtr->bytes = NULL; |
| 1814 |
|
| 1815 |
objPtr->internalRep.longValue = (intValue? 1 : 0); |
| 1816 |
objPtr->typePtr = &tclIntType; |
| 1817 |
return objPtr; |
| 1818 |
} |
| 1819 |
|
| 1820 |
#else /* if not TCL_MEM_DEBUG */ |
| 1821 |
|
| 1822 |
Tcl_Obj * |
| 1823 |
Tcl_DbNewBooleanObj( |
| 1824 |
int intValue, /* Boolean used to initialize new object. */ |
| 1825 |
const char *file, /* The name of the source file calling this |
| 1826 |
* function; used for debugging. */ |
| 1827 |
int line) /* Line number in the source file; used for |
| 1828 |
* debugging. */ |
| 1829 |
{ |
| 1830 |
return Tcl_NewBooleanObj(intValue); |
| 1831 |
} |
| 1832 |
#endif /* TCL_MEM_DEBUG */ |
| 1833 |
|
| 1834 |
/* |
| 1835 |
*---------------------------------------------------------------------- |
| 1836 |
* |
| 1837 |
* Tcl_SetBooleanObj -- |
| 1838 |
* |
| 1839 |
* Modify an object to be a boolean object and to have the specified |
| 1840 |
* boolean value. A nonzero "intValue" is coerced to 1. |
| 1841 |
* |
| 1842 |
* Results: |
| 1843 |
* None. |
| 1844 |
* |
| 1845 |
* Side effects: |
| 1846 |
* The object's old string rep, if any, is freed. Also, any old internal |
| 1847 |
* rep is freed. |
| 1848 |
* |
| 1849 |
*---------------------------------------------------------------------- |
| 1850 |
*/ |
| 1851 |
|
| 1852 |
#undef Tcl_SetBooleanObj |
| 1853 |
void |
| 1854 |
Tcl_SetBooleanObj( |
| 1855 |
Tcl_Obj *objPtr, /* Object whose internal rep to init. */ |
| 1856 |
int intValue) /* Boolean used to set object's value. */ |
| 1857 |
{ |
| 1858 |
if (Tcl_IsShared(objPtr)) { |
| 1859 |
Tcl_Panic("%s called with shared object", "Tcl_SetBooleanObj"); |
| 1860 |
} |
| 1861 |
|
| 1862 |
TclSetLongObj(objPtr, (intValue)!=0); |
| 1863 |
} |
| 1864 |
|
| 1865 |
/* |
| 1866 |
*---------------------------------------------------------------------- |
| 1867 |
* |
| 1868 |
* Tcl_GetBooleanFromObj -- |
| 1869 |
* |
| 1870 |
* Attempt to return a boolean from the Tcl object "objPtr". This |
| 1871 |
* includes conversion from any of Tcl's numeric types. |
| 1872 |
* |
| 1873 |
* Results: |
| 1874 |
* The return value is a standard Tcl object result. If an error occurs |
| 1875 |
* during conversion, an error message is left in the interpreter's |
| 1876 |
* result unless "interp" is NULL. |
| 1877 |
* |
| 1878 |
* Side effects: |
| 1879 |
* The internalrep of *objPtr may be changed. |
| 1880 |
* |
| 1881 |
*---------------------------------------------------------------------- |
| 1882 |
*/ |
| 1883 |
|
| 1884 |
int |
| 1885 |
Tcl_GetBooleanFromObj( |
| 1886 |
Tcl_Interp *interp, /* Used for error reporting if not NULL. */ |
| 1887 |
Tcl_Obj *objPtr, /* The object from which to get boolean. */ |
| 1888 |
int *intPtr) /* Place to store resulting boolean. */ |
| 1889 |
{ |
| 1890 |
do { |
| 1891 |
if (objPtr->typePtr == &tclIntType) { |
| 1892 |
*intPtr = (objPtr->internalRep.longValue != 0); |
| 1893 |
return TCL_OK; |
| 1894 |
} |
| 1895 |
if (objPtr->typePtr == &tclBooleanType) { |
| 1896 |
*intPtr = (int) objPtr->internalRep.longValue; |
| 1897 |
return TCL_OK; |
| 1898 |
} |
| 1899 |
if (objPtr->typePtr == &tclDoubleType) { |
| 1900 |
/* |
| 1901 |
* Caution: Don't be tempted to check directly for the "double" |
| 1902 |
* Tcl_ObjType and then compare the internalrep to 0.0. This isn't |
| 1903 |
* reliable because a "double" Tcl_ObjType can hold the NaN value. |
| 1904 |
* Use the API Tcl_GetDoubleFromObj, which does the checking and |
| 1905 |
* sets the proper error message for us. |
| 1906 |
*/ |
| 1907 |
|
| 1908 |
double d; |
| 1909 |
|
| 1910 |
if (Tcl_GetDoubleFromObj(interp, objPtr, &d) != TCL_OK) { |
| 1911 |
return TCL_ERROR; |
| 1912 |
} |
| 1913 |
*intPtr = (d != 0.0); |
| 1914 |
return TCL_OK; |
| 1915 |
} |
| 1916 |
if (objPtr->typePtr == &tclBignumType) { |
| 1917 |
*intPtr = 1; |
| 1918 |
return TCL_OK; |
| 1919 |
} |
| 1920 |
#ifndef TCL_WIDE_INT_IS_LONG |
| 1921 |
if (objPtr->typePtr == &tclWideIntType) { |
| 1922 |
*intPtr = (objPtr->internalRep.wideValue != 0); |
| 1923 |
return TCL_OK; |
| 1924 |
} |
| 1925 |
#endif |
| 1926 |
} while ((ParseBoolean(objPtr) == TCL_OK) || (TCL_OK == |
| 1927 |
TclParseNumber(interp, objPtr, "boolean value", NULL,-1,NULL,0))); |
| 1928 |
return TCL_ERROR; |
| 1929 |
} |
| 1930 |
|
| 1931 |
/* |
| 1932 |
*---------------------------------------------------------------------- |
| 1933 |
* |
| 1934 |
* TclSetBooleanFromAny -- |
| 1935 |
* |
| 1936 |
* Attempt to generate a boolean internal form for the Tcl object |
| 1937 |
* "objPtr". |
| 1938 |
* |
| 1939 |
* Results: |
| 1940 |
* The return value is a standard Tcl result. If an error occurs during |
| 1941 |
* conversion, an error message is left in the interpreter's result |
| 1942 |
* unless "interp" is NULL. |
| 1943 |
* |
| 1944 |
* Side effects: |
| 1945 |
* If no error occurs, an integer 1 or 0 is stored as "objPtr"s internal |
| 1946 |
* representation and the type of "objPtr" is set to boolean. |
| 1947 |
* |
| 1948 |
*---------------------------------------------------------------------- |
| 1949 |
*/ |
| 1950 |
|
| 1951 |
int |
| 1952 |
TclSetBooleanFromAny( |
| 1953 |
Tcl_Interp *interp, /* Used for error reporting if not NULL. */ |
| 1954 |
Tcl_Obj *objPtr) /* The object to convert. */ |
| 1955 |
{ |
| 1956 |
/* |
| 1957 |
* For some "pure" numeric Tcl_ObjTypes (no string rep), we can determine |
| 1958 |
* whether a boolean conversion is possible without generating the string |
| 1959 |
* rep. |
| 1960 |
*/ |
| 1961 |
|
| 1962 |
if (objPtr->bytes == NULL) { |
| 1963 |
if (objPtr->typePtr == &tclIntType) { |
| 1964 |
switch (objPtr->internalRep.longValue) { |
| 1965 |
case 0L: case 1L: |
| 1966 |
return TCL_OK; |
| 1967 |
} |
| 1968 |
goto badBoolean; |
| 1969 |
} |
| 1970 |
|
| 1971 |
if (objPtr->typePtr == &tclBignumType) { |
| 1972 |
goto badBoolean; |
| 1973 |
} |
| 1974 |
|
| 1975 |
#ifndef TCL_WIDE_INT_IS_LONG |
| 1976 |
if (objPtr->typePtr == &tclWideIntType) { |
| 1977 |
goto badBoolean; |
| 1978 |
} |
| 1979 |
#endif |
| 1980 |
|
| 1981 |
if (objPtr->typePtr == &tclDoubleType) { |
| 1982 |
goto badBoolean; |
| 1983 |
} |
| 1984 |
} |
| 1985 |
|
| 1986 |
if (ParseBoolean(objPtr) == TCL_OK) { |
| 1987 |
return TCL_OK; |
| 1988 |
} |
| 1989 |
|
| 1990 |
badBoolean: |
| 1991 |
if (interp != NULL) { |
| 1992 |
int length; |
| 1993 |
const char *str = TclGetStringFromObj(objPtr, &length); |
| 1994 |
Tcl_Obj *msg; |
| 1995 |
|
| 1996 |
TclNewLiteralStringObj(msg, "expected boolean value but got \""); |
| 1997 |
Tcl_AppendLimitedToObj(msg, str, length, 50, ""); |
| 1998 |
Tcl_AppendToObj(msg, "\"", -1); |
| 1999 |
Tcl_SetObjResult(interp, msg); |
| 2000 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "BOOLEAN", (char *)NULL); |
| 2001 |
} |
| 2002 |
return TCL_ERROR; |
| 2003 |
} |
| 2004 |
|
| 2005 |
static int |
| 2006 |
ParseBoolean( |
| 2007 |
Tcl_Obj *objPtr) /* The object to parse/convert. */ |
| 2008 |
{ |
| 2009 |
int newBool; |
| 2010 |
char lowerCase[6]; |
| 2011 |
int i, length; |
| 2012 |
const char *str = TclGetStringFromObj(objPtr, &length); |
| 2013 |
|
| 2014 |
if ((length < 1) || (length > 5)) { |
| 2015 |
/* |
| 2016 |
* Longest valid boolean string rep. is "false". |
| 2017 |
*/ |
| 2018 |
|
| 2019 |
return TCL_ERROR; |
| 2020 |
} |
| 2021 |
|
| 2022 |
switch (str[0]) { |
| 2023 |
case '0': |
| 2024 |
if (length == 1) { |
| 2025 |
newBool = 0; |
| 2026 |
goto numericBoolean; |
| 2027 |
} |
| 2028 |
return TCL_ERROR; |
| 2029 |
case '1': |
| 2030 |
if (length == 1) { |
| 2031 |
newBool = 1; |
| 2032 |
goto numericBoolean; |
| 2033 |
} |
| 2034 |
return TCL_ERROR; |
| 2035 |
} |
| 2036 |
|
| 2037 |
/* |
| 2038 |
* Force to lower case for case-insensitive detection. Filter out known |
| 2039 |
* invalid characters at the same time. |
| 2040 |
*/ |
| 2041 |
|
| 2042 |
for (i=0; i < length; i++) { |
| 2043 |
char c = str[i]; |
| 2044 |
|
| 2045 |
switch (c) { |
| 2046 |
case 'A': case 'E': case 'F': case 'L': case 'N': |
| 2047 |
case 'O': case 'R': case 'S': case 'T': case 'U': case 'Y': |
| 2048 |
lowerCase[i] = c + (char) ('a' - 'A'); |
| 2049 |
break; |
| 2050 |
case 'a': case 'e': case 'f': case 'l': case 'n': |
| 2051 |
case 'o': case 'r': case 's': case 't': case 'u': case 'y': |
| 2052 |
lowerCase[i] = c; |
| 2053 |
break; |
| 2054 |
default: |
| 2055 |
return TCL_ERROR; |
| 2056 |
} |
| 2057 |
} |
| 2058 |
lowerCase[length] = 0; |
| 2059 |
switch (lowerCase[0]) { |
| 2060 |
case 'y': |
| 2061 |
/* |
| 2062 |
* Checking the 'y' is redundant, but makes the code clearer. |
| 2063 |
*/ |
| 2064 |
if (strncmp(lowerCase, "yes", length) == 0) { |
| 2065 |
newBool = 1; |
| 2066 |
goto goodBoolean; |
| 2067 |
} |
| 2068 |
return TCL_ERROR; |
| 2069 |
case 'n': |
| 2070 |
if (strncmp(lowerCase, "no", length) == 0) { |
| 2071 |
newBool = 0; |
| 2072 |
goto goodBoolean; |
| 2073 |
} |
| 2074 |
return TCL_ERROR; |
| 2075 |
case 't': |
| 2076 |
if (strncmp(lowerCase, "true", length) == 0) { |
| 2077 |
newBool = 1; |
| 2078 |
goto goodBoolean; |
| 2079 |
} |
| 2080 |
return TCL_ERROR; |
| 2081 |
case 'f': |
| 2082 |
if (strncmp(lowerCase, "false", length) == 0) { |
| 2083 |
newBool = 0; |
| 2084 |
goto goodBoolean; |
| 2085 |
} |
| 2086 |
return TCL_ERROR; |
| 2087 |
case 'o': |
| 2088 |
if (length < 2) { |
| 2089 |
return TCL_ERROR; |
| 2090 |
} |
| 2091 |
if (strncmp(lowerCase, "on", length) == 0) { |
| 2092 |
newBool = 1; |
| 2093 |
goto goodBoolean; |
| 2094 |
} else if (strncmp(lowerCase, "off", length) == 0) { |
| 2095 |
newBool = 0; |
| 2096 |
goto goodBoolean; |
| 2097 |
} |
| 2098 |
return TCL_ERROR; |
| 2099 |
default: |
| 2100 |
return TCL_ERROR; |
| 2101 |
} |
| 2102 |
|
| 2103 |
/* |
| 2104 |
* Free the old internalRep before setting the new one. We do this as late |
| 2105 |
* as possible to allow the conversion code, in particular |
| 2106 |
* Tcl_GetStringFromObj, to use that old internalRep. |
| 2107 |
*/ |
| 2108 |
|
| 2109 |
goodBoolean: |
| 2110 |
TclFreeIntRep(objPtr); |
| 2111 |
objPtr->internalRep.longValue = newBool; |
| 2112 |
objPtr->typePtr = &tclBooleanType; |
| 2113 |
return TCL_OK; |
| 2114 |
|
| 2115 |
numericBoolean: |
| 2116 |
TclFreeIntRep(objPtr); |
| 2117 |
objPtr->internalRep.longValue = newBool; |
| 2118 |
objPtr->typePtr = &tclIntType; |
| 2119 |
return TCL_OK; |
| 2120 |
} |
| 2121 |
|
| 2122 |
/* |
| 2123 |
*---------------------------------------------------------------------- |
| 2124 |
* |
| 2125 |
* Tcl_NewDoubleObj -- |
| 2126 |
* |
| 2127 |
* This function is normally called when not debugging: i.e., when |
| 2128 |
* TCL_MEM_DEBUG is not defined. It creates a new double object and |
| 2129 |
* initializes it from the argument double value. |
| 2130 |
* |
| 2131 |
* When TCL_MEM_DEBUG is defined, this function just returns the result |
| 2132 |
* of calling the debugging version Tcl_DbNewDoubleObj. |
| 2133 |
* |
| 2134 |
* Results: |
| 2135 |
* The newly created object is returned. This object will have an |
| 2136 |
* invalid string representation. The returned object has ref count 0. |
| 2137 |
* |
| 2138 |
* Side effects: |
| 2139 |
* None. |
| 2140 |
* |
| 2141 |
*---------------------------------------------------------------------- |
| 2142 |
*/ |
| 2143 |
|
| 2144 |
#ifdef TCL_MEM_DEBUG |
| 2145 |
#undef Tcl_NewDoubleObj |
| 2146 |
|
| 2147 |
Tcl_Obj * |
| 2148 |
Tcl_NewDoubleObj( |
| 2149 |
double dblValue) /* Double used to initialize the object. */ |
| 2150 |
{ |
| 2151 |
return Tcl_DbNewDoubleObj(dblValue, "unknown", 0); |
| 2152 |
} |
| 2153 |
|
| 2154 |
#else /* if not TCL_MEM_DEBUG */ |
| 2155 |
|
| 2156 |
Tcl_Obj * |
| 2157 |
Tcl_NewDoubleObj( |
| 2158 |
double dblValue) /* Double used to initialize the object. */ |
| 2159 |
{ |
| 2160 |
Tcl_Obj *objPtr; |
| 2161 |
|
| 2162 |
TclNewDoubleObj(objPtr, dblValue); |
| 2163 |
return objPtr; |
| 2164 |
} |
| 2165 |
#endif /* if TCL_MEM_DEBUG */ |
| 2166 |
|
| 2167 |
/* |
| 2168 |
*---------------------------------------------------------------------- |
| 2169 |
* |
| 2170 |
* Tcl_DbNewDoubleObj -- |
| 2171 |
* |
| 2172 |
* This function is normally called when debugging: i.e., when |
| 2173 |
* TCL_MEM_DEBUG is defined. It creates new double objects. It is the |
| 2174 |
* same as the Tcl_NewDoubleObj function above except that it calls |
| 2175 |
* Tcl_DbCkalloc directly with the file name and line number from its |
| 2176 |
* caller. This simplifies debugging since then the [memory active] |
| 2177 |
* command will report the correct file name and line number when |
| 2178 |
* reporting objects that haven't been freed. |
| 2179 |
* |
| 2180 |
* When TCL_MEM_DEBUG is not defined, this function just returns the |
| 2181 |
* result of calling Tcl_NewDoubleObj. |
| 2182 |
* |
| 2183 |
* Results: |
| 2184 |
* The newly created object is returned. This object will have an invalid |
| 2185 |
* string representation. The returned object has ref count 0. |
| 2186 |
* |
| 2187 |
* Side effects: |
| 2188 |
* None. |
| 2189 |
* |
| 2190 |
*---------------------------------------------------------------------- |
| 2191 |
*/ |
| 2192 |
|
| 2193 |
#ifdef TCL_MEM_DEBUG |
| 2194 |
|
| 2195 |
Tcl_Obj * |
| 2196 |
Tcl_DbNewDoubleObj( |
| 2197 |
double dblValue, /* Double used to initialize the object. */ |
| 2198 |
const char *file, /* The name of the source file calling this |
| 2199 |
* function; used for debugging. */ |
| 2200 |
int line) /* Line number in the source file; used for |
| 2201 |
* debugging. */ |
| 2202 |
{ |
| 2203 |
Tcl_Obj *objPtr; |
| 2204 |
|
| 2205 |
TclDbNewObj(objPtr, file, line); |
| 2206 |
objPtr->bytes = NULL; |
| 2207 |
|
| 2208 |
objPtr->internalRep.doubleValue = dblValue; |
| 2209 |
objPtr->typePtr = &tclDoubleType; |
| 2210 |
return objPtr; |
| 2211 |
} |
| 2212 |
|
| 2213 |
#else /* if not TCL_MEM_DEBUG */ |
| 2214 |
|
| 2215 |
Tcl_Obj * |
| 2216 |
Tcl_DbNewDoubleObj( |
| 2217 |
double dblValue, /* Double used to initialize the object. */ |
| 2218 |
const char *file, /* The name of the source file calling this |
| 2219 |
* function; used for debugging. */ |
| 2220 |
int line) /* Line number in the source file; used for |
| 2221 |
* debugging. */ |
| 2222 |
{ |
| 2223 |
return Tcl_NewDoubleObj(dblValue); |
| 2224 |
} |
| 2225 |
#endif /* TCL_MEM_DEBUG */ |
| 2226 |
|
| 2227 |
/* |
| 2228 |
*---------------------------------------------------------------------- |
| 2229 |
* |
| 2230 |
* Tcl_SetDoubleObj -- |
| 2231 |
* |
| 2232 |
* Modify an object to be a double object and to have the specified |
| 2233 |
* double value. |
| 2234 |
* |
| 2235 |
* Results: |
| 2236 |
* None. |
| 2237 |
* |
| 2238 |
* Side effects: |
| 2239 |
* The object's old string rep, if any, is freed. Also, any old internal |
| 2240 |
* rep is freed. |
| 2241 |
* |
| 2242 |
*---------------------------------------------------------------------- |
| 2243 |
*/ |
| 2244 |
|
| 2245 |
void |
| 2246 |
Tcl_SetDoubleObj( |
| 2247 |
Tcl_Obj *objPtr, /* Object whose internal rep to init. */ |
| 2248 |
double dblValue) /* Double used to set the object's value. */ |
| 2249 |
{ |
| 2250 |
if (Tcl_IsShared(objPtr)) { |
| 2251 |
Tcl_Panic("%s called with shared object", "Tcl_SetDoubleObj"); |
| 2252 |
} |
| 2253 |
|
| 2254 |
TclSetDoubleObj(objPtr, dblValue); |
| 2255 |
} |
| 2256 |
|
| 2257 |
/* |
| 2258 |
*---------------------------------------------------------------------- |
| 2259 |
* |
| 2260 |
* Tcl_GetDoubleFromObj -- |
| 2261 |
* |
| 2262 |
* Attempt to return a double from the Tcl object "objPtr". If the object |
| 2263 |
* is not already a double, an attempt will be made to convert it to one. |
| 2264 |
* |
| 2265 |
* Results: |
| 2266 |
* The return value is a standard Tcl object result. If an error occurs |
| 2267 |
* during conversion, an error message is left in the interpreter's |
| 2268 |
* result unless "interp" is NULL. |
| 2269 |
* |
| 2270 |
* Side effects: |
| 2271 |
* If the object is not already a double, the conversion will free any |
| 2272 |
* old internal representation. |
| 2273 |
* |
| 2274 |
*---------------------------------------------------------------------- |
| 2275 |
*/ |
| 2276 |
|
| 2277 |
int |
| 2278 |
Tcl_GetDoubleFromObj( |
| 2279 |
Tcl_Interp *interp, /* Used for error reporting if not NULL. */ |
| 2280 |
Tcl_Obj *objPtr, /* The object from which to get a double. */ |
| 2281 |
double *dblPtr) /* Place to store resulting double. */ |
| 2282 |
{ |
| 2283 |
do { |
| 2284 |
if (objPtr->typePtr == &tclDoubleType) { |
| 2285 |
if (TclIsNaN(objPtr->internalRep.doubleValue)) { |
| 2286 |
if (interp != NULL) { |
| 2287 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 2288 |
"floating point value is Not a Number", -1)); |
| 2289 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "DOUBLE", "NAN", |
| 2290 |
(char *)NULL); |
| 2291 |
} |
| 2292 |
return TCL_ERROR; |
| 2293 |
} |
| 2294 |
*dblPtr = (double) objPtr->internalRep.doubleValue; |
| 2295 |
return TCL_OK; |
| 2296 |
} |
| 2297 |
if (objPtr->typePtr == &tclIntType) { |
| 2298 |
*dblPtr = objPtr->internalRep.longValue; |
| 2299 |
return TCL_OK; |
| 2300 |
} |
| 2301 |
if (objPtr->typePtr == &tclBignumType) { |
| 2302 |
mp_int big; |
| 2303 |
|
| 2304 |
UNPACK_BIGNUM(objPtr, big); |
| 2305 |
*dblPtr = TclBignumToDouble(&big); |
| 2306 |
return TCL_OK; |
| 2307 |
} |
| 2308 |
#ifndef TCL_WIDE_INT_IS_LONG |
| 2309 |
if (objPtr->typePtr == &tclWideIntType) { |
| 2310 |
*dblPtr = (double) objPtr->internalRep.wideValue; |
| 2311 |
return TCL_OK; |
| 2312 |
} |
| 2313 |
#endif |
| 2314 |
} while (SetDoubleFromAny(interp, objPtr) == TCL_OK); |
| 2315 |
return TCL_ERROR; |
| 2316 |
} |
| 2317 |
|
| 2318 |
/* |
| 2319 |
*---------------------------------------------------------------------- |
| 2320 |
* |
| 2321 |
* SetDoubleFromAny -- |
| 2322 |
* |
| 2323 |
* Attempt to generate an double-precision floating point internal form |
| 2324 |
* for the Tcl object "objPtr". |
| 2325 |
* |
| 2326 |
* Results: |
| 2327 |
* The return value is a standard Tcl object result. If an error occurs |
| 2328 |
* during conversion, an error message is left in the interpreter's |
| 2329 |
* result unless "interp" is NULL. |
| 2330 |
* |
| 2331 |
* Side effects: |
| 2332 |
* If no error occurs, a double is stored as "objPtr"s internal |
| 2333 |
* representation. |
| 2334 |
* |
| 2335 |
*---------------------------------------------------------------------- |
| 2336 |
*/ |
| 2337 |
|
| 2338 |
static int |
| 2339 |
SetDoubleFromAny( |
| 2340 |
Tcl_Interp *interp, /* Used for error reporting if not NULL. */ |
| 2341 |
Tcl_Obj *objPtr) /* The object to convert. */ |
| 2342 |
{ |
| 2343 |
return TclParseNumber(interp, objPtr, "floating-point number", NULL, -1, |
| 2344 |
NULL, 0); |
| 2345 |
} |
| 2346 |
|
| 2347 |
/* |
| 2348 |
*---------------------------------------------------------------------- |
| 2349 |
* |
| 2350 |
* UpdateStringOfDouble -- |
| 2351 |
* |
| 2352 |
* Update the string representation for a double-precision floating point |
| 2353 |
* object. This must obey the current tcl_precision value for |
| 2354 |
* double-to-string conversions. Note: This function does not free an |
| 2355 |
* existing old string rep so storage will be lost if this has not |
| 2356 |
* already been done. |
| 2357 |
* |
| 2358 |
* Results: |
| 2359 |
* None. |
| 2360 |
* |
| 2361 |
* Side effects: |
| 2362 |
* The object's string is set to a valid string that results from the |
| 2363 |
* double-to-string conversion. |
| 2364 |
* |
| 2365 |
*---------------------------------------------------------------------- |
| 2366 |
*/ |
| 2367 |
|
| 2368 |
static void |
| 2369 |
UpdateStringOfDouble( |
| 2370 |
Tcl_Obj *objPtr) /* Double obj with string rep to update. */ |
| 2371 |
{ |
| 2372 |
char buffer[TCL_DOUBLE_SPACE]; |
| 2373 |
int len; |
| 2374 |
|
| 2375 |
Tcl_PrintDouble(NULL, objPtr->internalRep.doubleValue, buffer); |
| 2376 |
len = strlen(buffer); |
| 2377 |
|
| 2378 |
objPtr->bytes = (char *)ckalloc(len + 1); |
| 2379 |
memcpy(objPtr->bytes, buffer, len + 1); |
| 2380 |
objPtr->length = len; |
| 2381 |
} |
| 2382 |
|
| 2383 |
/* |
| 2384 |
*---------------------------------------------------------------------- |
| 2385 |
* |
| 2386 |
* Tcl_NewIntObj -- |
| 2387 |
* |
| 2388 |
* If a client is compiled with TCL_MEM_DEBUG defined, calls to |
| 2389 |
* Tcl_NewIntObj to create a new integer object end up calling the |
| 2390 |
* debugging function Tcl_DbNewLongObj instead. |
| 2391 |
* |
| 2392 |
* Otherwise, if the client is compiled without TCL_MEM_DEBUG defined, |
| 2393 |
* calls to Tcl_NewIntObj result in a call to one of the two |
| 2394 |
* Tcl_NewIntObj implementations below. We provide two implementations so |
| 2395 |
* that the Tcl core can be compiled to do memory debugging of the core |
| 2396 |
* even if a client does not request it for itself. |
| 2397 |
* |
| 2398 |
* Integer and long integer objects share the same "integer" type |
| 2399 |
* implementation. We store all integers as longs and Tcl_GetIntFromObj |
| 2400 |
* checks whether the current value of the long can be represented by an |
| 2401 |
* int. |
| 2402 |
* |
| 2403 |
* Results: |
| 2404 |
* The newly created object is returned. This object will have an invalid |
| 2405 |
* string representation. The returned object has ref count 0. |
| 2406 |
* |
| 2407 |
* Side effects: |
| 2408 |
* None. |
| 2409 |
* |
| 2410 |
*---------------------------------------------------------------------- |
| 2411 |
*/ |
| 2412 |
|
| 2413 |
#undef Tcl_NewIntObj |
| 2414 |
#ifdef TCL_MEM_DEBUG |
| 2415 |
|
| 2416 |
Tcl_Obj * |
| 2417 |
Tcl_NewIntObj( |
| 2418 |
int intValue) /* Int used to initialize the new object. */ |
| 2419 |
{ |
| 2420 |
return Tcl_DbNewLongObj((long)intValue, "unknown", 0); |
| 2421 |
} |
| 2422 |
|
| 2423 |
#else /* if not TCL_MEM_DEBUG */ |
| 2424 |
|
| 2425 |
Tcl_Obj * |
| 2426 |
Tcl_NewIntObj( |
| 2427 |
int intValue) /* Int used to initialize the new object. */ |
| 2428 |
{ |
| 2429 |
Tcl_Obj *objPtr; |
| 2430 |
|
| 2431 |
TclNewIntObj(objPtr, intValue); |
| 2432 |
return objPtr; |
| 2433 |
} |
| 2434 |
#endif /* if TCL_MEM_DEBUG */ |
| 2435 |
|
| 2436 |
/* |
| 2437 |
*---------------------------------------------------------------------- |
| 2438 |
* |
| 2439 |
* Tcl_SetIntObj -- |
| 2440 |
* |
| 2441 |
* Modify an object to be an integer and to have the specified integer |
| 2442 |
* value. |
| 2443 |
* |
| 2444 |
* Results: |
| 2445 |
* None. |
| 2446 |
* |
| 2447 |
* Side effects: |
| 2448 |
* The object's old string rep, if any, is freed. Also, any old internal |
| 2449 |
* rep is freed. |
| 2450 |
* |
| 2451 |
*---------------------------------------------------------------------- |
| 2452 |
*/ |
| 2453 |
|
| 2454 |
#undef Tcl_SetIntObj |
| 2455 |
void |
| 2456 |
Tcl_SetIntObj( |
| 2457 |
Tcl_Obj *objPtr, /* Object whose internal rep to init. */ |
| 2458 |
int intValue) /* Integer used to set object's value. */ |
| 2459 |
{ |
| 2460 |
if (Tcl_IsShared(objPtr)) { |
| 2461 |
Tcl_Panic("%s called with shared object", "Tcl_SetIntObj"); |
| 2462 |
} |
| 2463 |
|
| 2464 |
TclSetIntObj(objPtr, intValue); |
| 2465 |
} |
| 2466 |
|
| 2467 |
/* |
| 2468 |
*---------------------------------------------------------------------- |
| 2469 |
* |
| 2470 |
* Tcl_GetIntFromObj -- |
| 2471 |
* |
| 2472 |
* Retrieve the integer value of 'objPtr'. |
| 2473 |
* |
| 2474 |
* Value |
| 2475 |
* |
| 2476 |
* TCL_OK |
| 2477 |
* |
| 2478 |
* Success. |
| 2479 |
* |
| 2480 |
* TCL_ERROR |
| 2481 |
* |
| 2482 |
* An error occurred during conversion or the integral value can not |
| 2483 |
* be represented as an integer (it might be too large). An error |
| 2484 |
* message is left in the interpreter's result if 'interp' is not |
| 2485 |
* NULL. |
| 2486 |
* |
| 2487 |
* Effect |
| 2488 |
* |
| 2489 |
* 'objPtr' is converted to an integer if necessary if it is not one |
| 2490 |
* already. The conversion frees any previously-existing internal |
| 2491 |
* representation. |
| 2492 |
* |
| 2493 |
*---------------------------------------------------------------------- |
| 2494 |
*/ |
| 2495 |
|
| 2496 |
int |
| 2497 |
Tcl_GetIntFromObj( |
| 2498 |
Tcl_Interp *interp, /* Used for error reporting if not NULL. */ |
| 2499 |
Tcl_Obj *objPtr, /* The object from which to get a int. */ |
| 2500 |
int *intPtr) /* Place to store resulting int. */ |
| 2501 |
{ |
| 2502 |
#if (LONG_MAX == INT_MAX) |
| 2503 |
return TclGetLongFromObj(interp, objPtr, (long *) intPtr); |
| 2504 |
#else |
| 2505 |
void *p; |
| 2506 |
int type; |
| 2507 |
|
| 2508 |
if ((TclGetNumberFromObj(NULL, objPtr, &p, &type) != TCL_OK) |
| 2509 |
|| (type == TCL_NUMBER_DOUBLE)) { |
| 2510 |
if (interp != NULL) { |
| 2511 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 2512 |
"expected integer but got \"%s\"", Tcl_GetString(objPtr))); |
| 2513 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "INTEGER", (char *)NULL); |
| 2514 |
} |
| 2515 |
return TCL_ERROR; |
| 2516 |
} |
| 2517 |
if ((type != TCL_NUMBER_LONG) || ((ULONG_MAX > UINT_MAX) |
| 2518 |
&& ((*(long *)p > UINT_MAX) || (*(long *)p < -(long)UINT_MAX)))) { |
| 2519 |
if (interp != NULL) { |
| 2520 |
const char *s = |
| 2521 |
"integer value too large to represent"; |
| 2522 |
Tcl_SetObjResult(interp, Tcl_NewStringObj(s, -1)); |
| 2523 |
Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW", s, (char *)NULL); |
| 2524 |
} |
| 2525 |
return TCL_ERROR; |
| 2526 |
} |
| 2527 |
*intPtr = (int)*(long *)p; |
| 2528 |
return TCL_OK; |
| 2529 |
#endif |
| 2530 |
} |
| 2531 |
|
| 2532 |
/* |
| 2533 |
*---------------------------------------------------------------------- |
| 2534 |
* |
| 2535 |
* SetIntFromAny -- |
| 2536 |
* |
| 2537 |
* Attempts to force the internal representation for a Tcl object to |
| 2538 |
* tclIntType, specifically. |
| 2539 |
* |
| 2540 |
* Results: |
| 2541 |
* The return value is a standard object Tcl result. If an error occurs |
| 2542 |
* during conversion, an error message is left in the interpreter's |
| 2543 |
* result unless "interp" is NULL. |
| 2544 |
* |
| 2545 |
*---------------------------------------------------------------------- |
| 2546 |
*/ |
| 2547 |
|
| 2548 |
static int |
| 2549 |
SetIntFromAny( |
| 2550 |
Tcl_Interp *interp, /* Tcl interpreter */ |
| 2551 |
Tcl_Obj *objPtr) /* Pointer to the object to convert */ |
| 2552 |
{ |
| 2553 |
long l; |
| 2554 |
|
| 2555 |
return TclGetLongFromObj(interp, objPtr, &l); |
| 2556 |
} |
| 2557 |
|
| 2558 |
/* |
| 2559 |
*---------------------------------------------------------------------- |
| 2560 |
* |
| 2561 |
* UpdateStringOfInt -- |
| 2562 |
* |
| 2563 |
* Update the string representation for an integer object. Note: This |
| 2564 |
* function does not free an existing old string rep so storage will be |
| 2565 |
* lost if this has not already been done. |
| 2566 |
* |
| 2567 |
* Results: |
| 2568 |
* None. |
| 2569 |
* |
| 2570 |
* Side effects: |
| 2571 |
* The object's string is set to a valid string that results from the |
| 2572 |
* int-to-string conversion. |
| 2573 |
* |
| 2574 |
*---------------------------------------------------------------------- |
| 2575 |
*/ |
| 2576 |
|
| 2577 |
static void |
| 2578 |
UpdateStringOfInt( |
| 2579 |
Tcl_Obj *objPtr) /* Int object whose string rep to update. */ |
| 2580 |
{ |
| 2581 |
char buffer[TCL_INTEGER_SPACE]; |
| 2582 |
int len; |
| 2583 |
|
| 2584 |
len = TclFormatInt(buffer, objPtr->internalRep.longValue); |
| 2585 |
|
| 2586 |
objPtr->bytes = (char *)ckalloc(len + 1); |
| 2587 |
memcpy(objPtr->bytes, buffer, len + 1); |
| 2588 |
objPtr->length = len; |
| 2589 |
} |
| 2590 |
|
| 2591 |
/* |
| 2592 |
*---------------------------------------------------------------------- |
| 2593 |
* |
| 2594 |
* Tcl_NewLongObj -- |
| 2595 |
* |
| 2596 |
* If a client is compiled with TCL_MEM_DEBUG defined, calls to |
| 2597 |
* Tcl_NewLongObj to create a new long integer object end up calling the |
| 2598 |
* debugging function Tcl_DbNewLongObj instead. |
| 2599 |
* |
| 2600 |
* Otherwise, if the client is compiled without TCL_MEM_DEBUG defined, |
| 2601 |
* calls to Tcl_NewLongObj result in a call to one of the two |
| 2602 |
* Tcl_NewLongObj implementations below. We provide two implementations |
| 2603 |
* so that the Tcl core can be compiled to do memory debugging of the |
| 2604 |
* core even if a client does not request it for itself. |
| 2605 |
* |
| 2606 |
* Integer and long integer objects share the same "integer" type |
| 2607 |
* implementation. We store all integers as longs and Tcl_GetIntFromObj |
| 2608 |
* checks whether the current value of the long can be represented by an |
| 2609 |
* int. |
| 2610 |
* |
| 2611 |
* Results: |
| 2612 |
* The newly created object is returned. This object will have an invalid |
| 2613 |
* string representation. The returned object has ref count 0. |
| 2614 |
* |
| 2615 |
* Side effects: |
| 2616 |
* None. |
| 2617 |
* |
| 2618 |
*---------------------------------------------------------------------- |
| 2619 |
*/ |
| 2620 |
|
| 2621 |
#ifdef TCL_MEM_DEBUG |
| 2622 |
#undef Tcl_NewLongObj |
| 2623 |
|
| 2624 |
Tcl_Obj * |
| 2625 |
Tcl_NewLongObj( |
| 2626 |
long longValue) /* Long integer used to initialize the |
| 2627 |
* new object. */ |
| 2628 |
{ |
| 2629 |
return Tcl_DbNewLongObj(longValue, "unknown", 0); |
| 2630 |
} |
| 2631 |
|
| 2632 |
#else /* if not TCL_MEM_DEBUG */ |
| 2633 |
|
| 2634 |
Tcl_Obj * |
| 2635 |
Tcl_NewLongObj( |
| 2636 |
long longValue) /* Long integer used to initialize the |
| 2637 |
* new object. */ |
| 2638 |
{ |
| 2639 |
Tcl_Obj *objPtr; |
| 2640 |
|
| 2641 |
TclNewLongObj(objPtr, longValue); |
| 2642 |
return objPtr; |
| 2643 |
} |
| 2644 |
#endif /* if TCL_MEM_DEBUG */ |
| 2645 |
|
| 2646 |
/* |
| 2647 |
*---------------------------------------------------------------------- |
| 2648 |
* |
| 2649 |
* Tcl_DbNewLongObj -- |
| 2650 |
* |
| 2651 |
* If a client is compiled with TCL_MEM_DEBUG defined, calls to |
| 2652 |
* Tcl_NewIntObj and Tcl_NewLongObj to create new integer or long integer |
| 2653 |
* objects end up calling the debugging function Tcl_DbNewLongObj |
| 2654 |
* instead. We provide two implementations of Tcl_DbNewLongObj so that |
| 2655 |
* whether the Tcl core is compiled to do memory debugging of the core is |
| 2656 |
* independent of whether a client requests debugging for itself. |
| 2657 |
* |
| 2658 |
* When the core is compiled with TCL_MEM_DEBUG defined, Tcl_DbNewLongObj |
| 2659 |
* calls Tcl_DbCkalloc directly with the file name and line number from |
| 2660 |
* its caller. This simplifies debugging since then the [memory active] |
| 2661 |
* command will report the caller's file name and line number when |
| 2662 |
* reporting objects that haven't been freed. |
| 2663 |
* |
| 2664 |
* Otherwise, when the core is compiled without TCL_MEM_DEBUG defined, |
| 2665 |
* this function just returns the result of calling Tcl_NewLongObj. |
| 2666 |
* |
| 2667 |
* Results: |
| 2668 |
* The newly created long integer object is returned. This object will |
| 2669 |
* have an invalid string representation. The returned object has ref |
| 2670 |
* count 0. |
| 2671 |
* |
| 2672 |
* Side effects: |
| 2673 |
* Allocates memory. |
| 2674 |
* |
| 2675 |
*---------------------------------------------------------------------- |
| 2676 |
*/ |
| 2677 |
|
| 2678 |
#ifdef TCL_MEM_DEBUG |
| 2679 |
|
| 2680 |
Tcl_Obj * |
| 2681 |
Tcl_DbNewLongObj( |
| 2682 |
long longValue, /* Long integer used to initialize the new |
| 2683 |
* object. */ |
| 2684 |
const char *file, /* The name of the source file calling this |
| 2685 |
* function; used for debugging. */ |
| 2686 |
int line) /* Line number in the source file; used for |
| 2687 |
* debugging. */ |
| 2688 |
{ |
| 2689 |
Tcl_Obj *objPtr; |
| 2690 |
|
| 2691 |
TclDbNewObj(objPtr, file, line); |
| 2692 |
objPtr->bytes = NULL; |
| 2693 |
|
| 2694 |
objPtr->internalRep.longValue = longValue; |
| 2695 |
objPtr->typePtr = &tclIntType; |
| 2696 |
return objPtr; |
| 2697 |
} |
| 2698 |
|
| 2699 |
#else /* if not TCL_MEM_DEBUG */ |
| 2700 |
|
| 2701 |
Tcl_Obj * |
| 2702 |
Tcl_DbNewLongObj( |
| 2703 |
long longValue, /* Long integer used to initialize the new |
| 2704 |
* object. */ |
| 2705 |
const char *file, /* The name of the source file calling this |
| 2706 |
* function; used for debugging. */ |
| 2707 |
int line) /* Line number in the source file; used for |
| 2708 |
* debugging. */ |
| 2709 |
{ |
| 2710 |
return Tcl_NewLongObj(longValue); |
| 2711 |
} |
| 2712 |
#endif /* TCL_MEM_DEBUG */ |
| 2713 |
|
| 2714 |
/* |
| 2715 |
*---------------------------------------------------------------------- |
| 2716 |
* |
| 2717 |
* Tcl_SetLongObj -- |
| 2718 |
* |
| 2719 |
* Modify an object to be an integer object and to have the specified |
| 2720 |
* long integer value. |
| 2721 |
* |
| 2722 |
* Results: |
| 2723 |
* None. |
| 2724 |
* |
| 2725 |
* Side effects: |
| 2726 |
* The object's old string rep, if any, is freed. Also, any old internal |
| 2727 |
* rep is freed. |
| 2728 |
* |
| 2729 |
*---------------------------------------------------------------------- |
| 2730 |
*/ |
| 2731 |
|
| 2732 |
void |
| 2733 |
Tcl_SetLongObj( |
| 2734 |
Tcl_Obj *objPtr, /* Object whose internal rep to init. */ |
| 2735 |
long longValue) /* Long integer used to initialize the |
| 2736 |
* object's value. */ |
| 2737 |
{ |
| 2738 |
if (Tcl_IsShared(objPtr)) { |
| 2739 |
Tcl_Panic("%s called with shared object", "Tcl_SetLongObj"); |
| 2740 |
} |
| 2741 |
|
| 2742 |
TclSetLongObj(objPtr, longValue); |
| 2743 |
} |
| 2744 |
|
| 2745 |
/* |
| 2746 |
*---------------------------------------------------------------------- |
| 2747 |
* |
| 2748 |
* Tcl_GetLongFromObj -- |
| 2749 |
* |
| 2750 |
* Attempt to return an long integer from the Tcl object "objPtr". If the |
| 2751 |
* object is not already an int object, an attempt will be made to |
| 2752 |
* convert it to one. |
| 2753 |
* |
| 2754 |
* Results: |
| 2755 |
* The return value is a standard Tcl object result. If an error occurs |
| 2756 |
* during conversion, an error message is left in the interpreter's |
| 2757 |
* result unless "interp" is NULL. |
| 2758 |
* |
| 2759 |
* Side effects: |
| 2760 |
* If the object is not already an int object, the conversion will free |
| 2761 |
* any old internal representation. |
| 2762 |
* |
| 2763 |
*---------------------------------------------------------------------- |
| 2764 |
*/ |
| 2765 |
|
| 2766 |
int |
| 2767 |
Tcl_GetLongFromObj( |
| 2768 |
Tcl_Interp *interp, /* Used for error reporting if not NULL. */ |
| 2769 |
Tcl_Obj *objPtr, /* The object from which to get a long. */ |
| 2770 |
long *longPtr) /* Place to store resulting long. */ |
| 2771 |
{ |
| 2772 |
do { |
| 2773 |
if (objPtr->typePtr == &tclIntType) { |
| 2774 |
*longPtr = objPtr->internalRep.longValue; |
| 2775 |
return TCL_OK; |
| 2776 |
} |
| 2777 |
#ifndef TCL_WIDE_INT_IS_LONG |
| 2778 |
if (objPtr->typePtr == &tclWideIntType) { |
| 2779 |
/* |
| 2780 |
* We return any integer in the range -ULONG_MAX to ULONG_MAX |
| 2781 |
* converted to a long, ignoring overflow. The rule preserves |
| 2782 |
* existing semantics for conversion of integers on input, but |
| 2783 |
* avoids inadvertent demotion of wide integers to 32-bit ones in |
| 2784 |
* the internal rep. |
| 2785 |
*/ |
| 2786 |
|
| 2787 |
Tcl_WideInt w = objPtr->internalRep.wideValue; |
| 2788 |
|
| 2789 |
if (w >= -(Tcl_WideInt)(ULONG_MAX) |
| 2790 |
&& w <= (Tcl_WideInt)(ULONG_MAX)) { |
| 2791 |
*longPtr = Tcl_WideAsLong(w); |
| 2792 |
return TCL_OK; |
| 2793 |
} |
| 2794 |
goto tooLarge; |
| 2795 |
} |
| 2796 |
#endif |
| 2797 |
if (objPtr->typePtr == &tclDoubleType) { |
| 2798 |
if (interp != NULL) { |
| 2799 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 2800 |
"expected integer but got \"%s\"", |
| 2801 |
TclGetString(objPtr))); |
| 2802 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "INTEGER", (char *)NULL); |
| 2803 |
} |
| 2804 |
return TCL_ERROR; |
| 2805 |
} |
| 2806 |
if (objPtr->typePtr == &tclBignumType) { |
| 2807 |
/* |
| 2808 |
* Must check for those bignum values that can fit in a long, even |
| 2809 |
* when auto-narrowing is enabled. Only those values in the signed |
| 2810 |
* long range get auto-narrowed to tclIntType, while all the |
| 2811 |
* values in the unsigned long range will fit in a long. |
| 2812 |
*/ |
| 2813 |
|
| 2814 |
mp_int big; |
| 2815 |
|
| 2816 |
UNPACK_BIGNUM(objPtr, big); |
| 2817 |
if ((size_t) big.used <= (CHAR_BIT * sizeof(long) + MP_DIGIT_BIT - 1) |
| 2818 |
/ MP_DIGIT_BIT) { |
| 2819 |
unsigned long value = 0; |
| 2820 |
size_t numBytes; |
| 2821 |
long scratch; |
| 2822 |
unsigned char *bytes = (unsigned char *) &scratch; |
| 2823 |
|
| 2824 |
if (mp_to_ubin(&big, bytes, sizeof(long), &numBytes) == MP_OKAY) { |
| 2825 |
while (numBytes-- > 0) { |
| 2826 |
value = (value << CHAR_BIT) | *bytes++; |
| 2827 |
} |
| 2828 |
if (mp_isneg(&big)) { |
| 2829 |
*longPtr = (long)(-value); |
| 2830 |
} else { |
| 2831 |
*longPtr = (long)value; |
| 2832 |
} |
| 2833 |
return TCL_OK; |
| 2834 |
} |
| 2835 |
} |
| 2836 |
#ifndef TCL_WIDE_INT_IS_LONG |
| 2837 |
tooLarge: |
| 2838 |
#endif |
| 2839 |
if (interp != NULL) { |
| 2840 |
const char *s = "integer value too large to represent"; |
| 2841 |
Tcl_Obj *msg = Tcl_NewStringObj(s, -1); |
| 2842 |
|
| 2843 |
Tcl_SetObjResult(interp, msg); |
| 2844 |
Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW", s, (char *)NULL); |
| 2845 |
} |
| 2846 |
return TCL_ERROR; |
| 2847 |
} |
| 2848 |
} while (TclParseNumber(interp, objPtr, "integer", NULL, -1, NULL, |
| 2849 |
TCL_PARSE_INTEGER_ONLY)==TCL_OK); |
| 2850 |
return TCL_ERROR; |
| 2851 |
} |
| 2852 |
#ifndef TCL_WIDE_INT_IS_LONG |
| 2853 |
|
| 2854 |
/* |
| 2855 |
*---------------------------------------------------------------------- |
| 2856 |
* |
| 2857 |
* UpdateStringOfWideInt -- |
| 2858 |
* |
| 2859 |
* Update the string representation for a wide integer object. Note: this |
| 2860 |
* function does not free an existing old string rep so storage will be |
| 2861 |
* lost if this has not already been done. |
| 2862 |
* |
| 2863 |
* Results: |
| 2864 |
* None. |
| 2865 |
* |
| 2866 |
* Side effects: |
| 2867 |
* The object's string is set to a valid string that results from the |
| 2868 |
* wideInt-to-string conversion. |
| 2869 |
* |
| 2870 |
*---------------------------------------------------------------------- |
| 2871 |
*/ |
| 2872 |
|
| 2873 |
static void |
| 2874 |
UpdateStringOfWideInt( |
| 2875 |
Tcl_Obj *objPtr) /* Int object whose string rep to update. */ |
| 2876 |
{ |
| 2877 |
char buffer[TCL_INTEGER_SPACE+2]; |
| 2878 |
unsigned len; |
| 2879 |
Tcl_WideInt wideVal = objPtr->internalRep.wideValue; |
| 2880 |
|
| 2881 |
/* |
| 2882 |
* Note that snprintf will generate a compiler warning under Mingw claiming |
| 2883 |
* %I64 is an unknown format specifier. Just ignore this warning. We can't |
| 2884 |
* use %L as the format specifier since that gets printed as a 32 bit |
| 2885 |
* value. |
| 2886 |
*/ |
| 2887 |
|
| 2888 |
snprintf(buffer, sizeof(buffer), "%" TCL_LL_MODIFIER "d", wideVal); |
| 2889 |
len = strlen(buffer); |
| 2890 |
objPtr->bytes = (char *)ckalloc(len + 1); |
| 2891 |
memcpy(objPtr->bytes, buffer, len + 1); |
| 2892 |
objPtr->length = len; |
| 2893 |
} |
| 2894 |
#endif /* !TCL_WIDE_INT_IS_LONG */ |
| 2895 |
|
| 2896 |
/* |
| 2897 |
*---------------------------------------------------------------------- |
| 2898 |
* |
| 2899 |
* Tcl_NewWideIntObj -- |
| 2900 |
* |
| 2901 |
* If a client is compiled with TCL_MEM_DEBUG defined, calls to |
| 2902 |
* Tcl_NewWideIntObj to create a new 64-bit integer object end up calling |
| 2903 |
* the debugging function Tcl_DbNewWideIntObj instead. |
| 2904 |
* |
| 2905 |
* Otherwise, if the client is compiled without TCL_MEM_DEBUG defined, |
| 2906 |
* calls to Tcl_NewWideIntObj result in a call to one of the two |
| 2907 |
* Tcl_NewWideIntObj implementations below. We provide two |
| 2908 |
* implementations so that the Tcl core can be compiled to do memory |
| 2909 |
* debugging of the core even if a client does not request it for itself. |
| 2910 |
* |
| 2911 |
* Results: |
| 2912 |
* The newly created object is returned. This object will have an invalid |
| 2913 |
* string representation. The returned object has ref count 0. |
| 2914 |
* |
| 2915 |
* Side effects: |
| 2916 |
* None. |
| 2917 |
* |
| 2918 |
*---------------------------------------------------------------------- |
| 2919 |
*/ |
| 2920 |
|
| 2921 |
#ifdef TCL_MEM_DEBUG |
| 2922 |
#undef Tcl_NewWideIntObj |
| 2923 |
|
| 2924 |
Tcl_Obj * |
| 2925 |
Tcl_NewWideIntObj( |
| 2926 |
Tcl_WideInt wideValue) |
| 2927 |
/* Wide integer used to initialize the new |
| 2928 |
* object. */ |
| 2929 |
{ |
| 2930 |
return Tcl_DbNewWideIntObj(wideValue, "unknown", 0); |
| 2931 |
} |
| 2932 |
|
| 2933 |
#else /* if not TCL_MEM_DEBUG */ |
| 2934 |
|
| 2935 |
Tcl_Obj * |
| 2936 |
Tcl_NewWideIntObj( |
| 2937 |
Tcl_WideInt wideValue) |
| 2938 |
/* Wide integer used to initialize the new |
| 2939 |
* object. */ |
| 2940 |
{ |
| 2941 |
Tcl_Obj *objPtr; |
| 2942 |
|
| 2943 |
TclNewObj(objPtr); |
| 2944 |
Tcl_SetWideIntObj(objPtr, wideValue); |
| 2945 |
return objPtr; |
| 2946 |
} |
| 2947 |
#endif /* if TCL_MEM_DEBUG */ |
| 2948 |
|
| 2949 |
/* |
| 2950 |
*---------------------------------------------------------------------- |
| 2951 |
* |
| 2952 |
* Tcl_DbNewWideIntObj -- |
| 2953 |
* |
| 2954 |
* If a client is compiled with TCL_MEM_DEBUG defined, calls to |
| 2955 |
* Tcl_NewWideIntObj to create new wide integer end up calling the |
| 2956 |
* debugging function Tcl_DbNewWideIntObj instead. We provide two |
| 2957 |
* implementations of Tcl_DbNewWideIntObj so that whether the Tcl core is |
| 2958 |
* compiled to do memory debugging of the core is independent of whether |
| 2959 |
* a client requests debugging for itself. |
| 2960 |
* |
| 2961 |
* When the core is compiled with TCL_MEM_DEBUG defined, |
| 2962 |
* Tcl_DbNewWideIntObj calls Tcl_DbCkalloc directly with the file name |
| 2963 |
* and line number from its caller. This simplifies debugging since then |
| 2964 |
* the checkmem command will report the caller's file name and line |
| 2965 |
* number when reporting objects that haven't been freed. |
| 2966 |
* |
| 2967 |
* Otherwise, when the core is compiled without TCL_MEM_DEBUG defined, |
| 2968 |
* this function just returns the result of calling Tcl_NewWideIntObj. |
| 2969 |
* |
| 2970 |
* Results: |
| 2971 |
* The newly created wide integer object is returned. This object will |
| 2972 |
* have an invalid string representation. The returned object has ref |
| 2973 |
* count 0. |
| 2974 |
* |
| 2975 |
* Side effects: |
| 2976 |
* Allocates memory. |
| 2977 |
* |
| 2978 |
*---------------------------------------------------------------------- |
| 2979 |
*/ |
| 2980 |
|
| 2981 |
#ifdef TCL_MEM_DEBUG |
| 2982 |
|
| 2983 |
Tcl_Obj * |
| 2984 |
Tcl_DbNewWideIntObj( |
| 2985 |
Tcl_WideInt wideValue, |
| 2986 |
/* Wide integer used to initialize the new |
| 2987 |
* object. */ |
| 2988 |
const char *file, /* The name of the source file calling this |
| 2989 |
* function; used for debugging. */ |
| 2990 |
int line) /* Line number in the source file; used for |
| 2991 |
* debugging. */ |
| 2992 |
{ |
| 2993 |
Tcl_Obj *objPtr; |
| 2994 |
|
| 2995 |
TclDbNewObj(objPtr, file, line); |
| 2996 |
Tcl_SetWideIntObj(objPtr, wideValue); |
| 2997 |
return objPtr; |
| 2998 |
} |
| 2999 |
|
| 3000 |
#else /* if not TCL_MEM_DEBUG */ |
| 3001 |
|
| 3002 |
Tcl_Obj * |
| 3003 |
Tcl_DbNewWideIntObj( |
| 3004 |
Tcl_WideInt wideValue, |
| 3005 |
/* Long integer used to initialize the new |
| 3006 |
* object. */ |
| 3007 |
const char *file, /* The name of the source file calling this |
| 3008 |
* function; used for debugging. */ |
| 3009 |
int line) /* Line number in the source file; used for |
| 3010 |
* debugging. */ |
| 3011 |
{ |
| 3012 |
return Tcl_NewWideIntObj(wideValue); |
| 3013 |
} |
| 3014 |
#endif /* TCL_MEM_DEBUG */ |
| 3015 |
|
| 3016 |
/* |
| 3017 |
*---------------------------------------------------------------------- |
| 3018 |
* |
| 3019 |
* Tcl_SetWideIntObj -- |
| 3020 |
* |
| 3021 |
* Modify an object to be a wide integer object and to have the specified |
| 3022 |
* wide integer value. |
| 3023 |
* |
| 3024 |
* Results: |
| 3025 |
* None. |
| 3026 |
* |
| 3027 |
* Side effects: |
| 3028 |
* The object's old string rep, if any, is freed. Also, any old internal |
| 3029 |
* rep is freed. |
| 3030 |
* |
| 3031 |
*---------------------------------------------------------------------- |
| 3032 |
*/ |
| 3033 |
|
| 3034 |
void |
| 3035 |
Tcl_SetWideIntObj( |
| 3036 |
Tcl_Obj *objPtr, /* Object w. internal rep to init. */ |
| 3037 |
Tcl_WideInt wideValue) |
| 3038 |
/* Wide integer used to initialize the |
| 3039 |
* object's value. */ |
| 3040 |
{ |
| 3041 |
if (Tcl_IsShared(objPtr)) { |
| 3042 |
Tcl_Panic("%s called with shared object", "Tcl_SetWideIntObj"); |
| 3043 |
} |
| 3044 |
|
| 3045 |
#ifndef TCL_WIDE_INT_IS_LONG |
| 3046 |
if ((wideValue < (Tcl_WideInt)LONG_MIN) |
| 3047 |
|| (wideValue > (Tcl_WideInt)LONG_MAX)) { |
| 3048 |
TclSetWideIntObj(objPtr, wideValue); |
| 3049 |
} else |
| 3050 |
#endif |
| 3051 |
TclSetLongObj(objPtr, (long) wideValue); |
| 3052 |
} |
| 3053 |
|
| 3054 |
/* |
| 3055 |
*---------------------------------------------------------------------- |
| 3056 |
* |
| 3057 |
* Tcl_GetWideIntFromObj -- |
| 3058 |
* |
| 3059 |
* Attempt to return a wide integer from the Tcl object "objPtr". If the |
| 3060 |
* object is not already a wide int object, an attempt will be made to |
| 3061 |
* convert it to one. |
| 3062 |
* |
| 3063 |
* Results: |
| 3064 |
* The return value is a standard Tcl object result. If an error occurs |
| 3065 |
* during conversion, an error message is left in the interpreter's |
| 3066 |
* result unless "interp" is NULL. |
| 3067 |
* |
| 3068 |
* Side effects: |
| 3069 |
* If the object is not already an int object, the conversion will free |
| 3070 |
* any old internal representation. |
| 3071 |
* |
| 3072 |
*---------------------------------------------------------------------- |
| 3073 |
*/ |
| 3074 |
|
| 3075 |
int |
| 3076 |
Tcl_GetWideIntFromObj( |
| 3077 |
Tcl_Interp *interp, /* Used for error reporting if not NULL. */ |
| 3078 |
Tcl_Obj *objPtr, /* Object from which to get a wide int. */ |
| 3079 |
Tcl_WideInt *wideIntPtr) |
| 3080 |
/* Place to store resulting long. */ |
| 3081 |
{ |
| 3082 |
do { |
| 3083 |
#ifndef TCL_WIDE_INT_IS_LONG |
| 3084 |
if (objPtr->typePtr == &tclWideIntType) { |
| 3085 |
*wideIntPtr = objPtr->internalRep.wideValue; |
| 3086 |
return TCL_OK; |
| 3087 |
} |
| 3088 |
#endif |
| 3089 |
if (objPtr->typePtr == &tclIntType) { |
| 3090 |
*wideIntPtr = (Tcl_WideInt)objPtr->internalRep.longValue; |
| 3091 |
return TCL_OK; |
| 3092 |
} |
| 3093 |
if (objPtr->typePtr == &tclDoubleType) { |
| 3094 |
if (interp != NULL) { |
| 3095 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 3096 |
"expected integer but got \"%s\"", |
| 3097 |
TclGetString(objPtr))); |
| 3098 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "INTEGER", (char *)NULL); |
| 3099 |
} |
| 3100 |
return TCL_ERROR; |
| 3101 |
} |
| 3102 |
if (objPtr->typePtr == &tclBignumType) { |
| 3103 |
/* |
| 3104 |
* Must check for those bignum values that can fit in a |
| 3105 |
* Tcl_WideInt, even when auto-narrowing is enabled. |
| 3106 |
*/ |
| 3107 |
|
| 3108 |
mp_int big; |
| 3109 |
|
| 3110 |
UNPACK_BIGNUM(objPtr, big); |
| 3111 |
if ((size_t) big.used <= (CHAR_BIT * sizeof(Tcl_WideInt) |
| 3112 |
+ MP_DIGIT_BIT - 1) / MP_DIGIT_BIT) { |
| 3113 |
Tcl_WideUInt value = 0; |
| 3114 |
size_t numBytes; |
| 3115 |
Tcl_WideInt scratch; |
| 3116 |
unsigned char *bytes = (unsigned char *) &scratch; |
| 3117 |
|
| 3118 |
if (mp_to_ubin(&big, bytes, sizeof(Tcl_WideInt), &numBytes) == MP_OKAY) { |
| 3119 |
while (numBytes-- > 0) { |
| 3120 |
value = (value << CHAR_BIT) | *bytes++; |
| 3121 |
} |
| 3122 |
if (mp_isneg(&big)) { |
| 3123 |
*wideIntPtr = (Tcl_WideInt)(-value); |
| 3124 |
} else { |
| 3125 |
*wideIntPtr = (Tcl_WideInt)value; |
| 3126 |
} |
| 3127 |
return TCL_OK; |
| 3128 |
} |
| 3129 |
} |
| 3130 |
if (interp != NULL) { |
| 3131 |
const char *s = "integer value too large to represent"; |
| 3132 |
Tcl_Obj *msg = Tcl_NewStringObj(s, -1); |
| 3133 |
|
| 3134 |
Tcl_SetObjResult(interp, msg); |
| 3135 |
Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW", s, (char *)NULL); |
| 3136 |
} |
| 3137 |
return TCL_ERROR; |
| 3138 |
} |
| 3139 |
} while (TclParseNumber(interp, objPtr, "integer", NULL, -1, NULL, |
| 3140 |
TCL_PARSE_INTEGER_ONLY)==TCL_OK); |
| 3141 |
return TCL_ERROR; |
| 3142 |
} |
| 3143 |
#ifndef TCL_WIDE_INT_IS_LONG |
| 3144 |
|
| 3145 |
/* |
| 3146 |
*---------------------------------------------------------------------- |
| 3147 |
* |
| 3148 |
* SetWideIntFromAny -- |
| 3149 |
* |
| 3150 |
* Attempts to force the internal representation for a Tcl object to |
| 3151 |
* tclWideIntType, specifically. |
| 3152 |
* |
| 3153 |
* Results: |
| 3154 |
* The return value is a standard object Tcl result. If an error occurs |
| 3155 |
* during conversion, an error message is left in the interpreter's |
| 3156 |
* result unless "interp" is NULL. |
| 3157 |
* |
| 3158 |
*---------------------------------------------------------------------- |
| 3159 |
*/ |
| 3160 |
|
| 3161 |
static int |
| 3162 |
SetWideIntFromAny( |
| 3163 |
Tcl_Interp *interp, /* Tcl interpreter */ |
| 3164 |
Tcl_Obj *objPtr) /* Pointer to the object to convert */ |
| 3165 |
{ |
| 3166 |
Tcl_WideInt w; |
| 3167 |
return Tcl_GetWideIntFromObj(interp, objPtr, &w); |
| 3168 |
} |
| 3169 |
#endif /* !TCL_WIDE_INT_IS_LONG */ |
| 3170 |
|
| 3171 |
/* |
| 3172 |
*---------------------------------------------------------------------- |
| 3173 |
* |
| 3174 |
* FreeBignum -- |
| 3175 |
* |
| 3176 |
* This function frees the internal rep of a bignum. |
| 3177 |
* |
| 3178 |
* Results: |
| 3179 |
* None. |
| 3180 |
* |
| 3181 |
*---------------------------------------------------------------------- |
| 3182 |
*/ |
| 3183 |
|
| 3184 |
static void |
| 3185 |
FreeBignum( |
| 3186 |
Tcl_Obj *objPtr) |
| 3187 |
{ |
| 3188 |
mp_int toFree; /* Bignum to free */ |
| 3189 |
|
| 3190 |
UNPACK_BIGNUM(objPtr, toFree); |
| 3191 |
mp_clear(&toFree); |
| 3192 |
if (PTR2INT(objPtr->internalRep.twoPtrValue.ptr2) < 0) { |
| 3193 |
ckfree(objPtr->internalRep.twoPtrValue.ptr1); |
| 3194 |
} |
| 3195 |
objPtr->typePtr = NULL; |
| 3196 |
} |
| 3197 |
|
| 3198 |
/* |
| 3199 |
*---------------------------------------------------------------------- |
| 3200 |
* |
| 3201 |
* DupBignum -- |
| 3202 |
* |
| 3203 |
* This function duplicates the internal rep of a bignum. |
| 3204 |
* |
| 3205 |
* Results: |
| 3206 |
* None. |
| 3207 |
* |
| 3208 |
* Side effects: |
| 3209 |
* The destination object receives a copy of the source object |
| 3210 |
* |
| 3211 |
*---------------------------------------------------------------------- |
| 3212 |
*/ |
| 3213 |
|
| 3214 |
static void |
| 3215 |
DupBignum( |
| 3216 |
Tcl_Obj *srcPtr, |
| 3217 |
Tcl_Obj *copyPtr) |
| 3218 |
{ |
| 3219 |
mp_int bignumVal; |
| 3220 |
mp_int bignumCopy; |
| 3221 |
|
| 3222 |
copyPtr->typePtr = &tclBignumType; |
| 3223 |
UNPACK_BIGNUM(srcPtr, bignumVal); |
| 3224 |
if (mp_init_copy(&bignumCopy, &bignumVal) != MP_OKAY) { |
| 3225 |
Tcl_Panic("initialization failure in DupBignum"); |
| 3226 |
} |
| 3227 |
PACK_BIGNUM(bignumCopy, copyPtr); |
| 3228 |
} |
| 3229 |
|
| 3230 |
/* |
| 3231 |
*---------------------------------------------------------------------- |
| 3232 |
* |
| 3233 |
* UpdateStringOfBignum -- |
| 3234 |
* |
| 3235 |
* This function updates the string representation of a bignum object. |
| 3236 |
* |
| 3237 |
* Results: |
| 3238 |
* None. |
| 3239 |
* |
| 3240 |
* Side effects: |
| 3241 |
* The object's string is set to whatever results from the bignum- |
| 3242 |
* to-string conversion. |
| 3243 |
* |
| 3244 |
* The object's existing string representation is NOT freed; memory will leak |
| 3245 |
* if the string rep is still valid at the time this function is called. |
| 3246 |
* |
| 3247 |
*---------------------------------------------------------------------- |
| 3248 |
*/ |
| 3249 |
|
| 3250 |
static void |
| 3251 |
UpdateStringOfBignum( |
| 3252 |
Tcl_Obj *objPtr) |
| 3253 |
{ |
| 3254 |
mp_int bignumVal; |
| 3255 |
int size; |
| 3256 |
int status; |
| 3257 |
char *stringVal; |
| 3258 |
|
| 3259 |
UNPACK_BIGNUM(objPtr, bignumVal); |
| 3260 |
status = mp_radix_size(&bignumVal, 10, &size); |
| 3261 |
if (status != MP_OKAY) { |
| 3262 |
Tcl_Panic("radix size failure in UpdateStringOfBignum"); |
| 3263 |
} |
| 3264 |
if (size < 2) { |
| 3265 |
/* |
| 3266 |
* mp_radix_size() returns < 2 when more than INT_MAX bytes would be |
| 3267 |
* needed to hold the string rep (because mp_radix_size ignores |
| 3268 |
* integer overflow issues). |
| 3269 |
* |
| 3270 |
* Note that so long as we enforce our bignums to the size that fits |
| 3271 |
* in a packed bignum, this branch will never be taken. |
| 3272 |
*/ |
| 3273 |
|
| 3274 |
Tcl_Panic("UpdateStringOfBignum: string length limit exceeded"); |
| 3275 |
} |
| 3276 |
stringVal = (char *)ckalloc(size); |
| 3277 |
status = mp_to_radix(&bignumVal, stringVal, size, NULL, 10); |
| 3278 |
if (status != MP_OKAY) { |
| 3279 |
Tcl_Panic("conversion failure in UpdateStringOfBignum"); |
| 3280 |
} |
| 3281 |
objPtr->bytes = stringVal; |
| 3282 |
objPtr->length = size - 1; /* size includes a trailing NUL byte. */ |
| 3283 |
} |
| 3284 |
|
| 3285 |
/* |
| 3286 |
*---------------------------------------------------------------------- |
| 3287 |
* |
| 3288 |
* Tcl_NewBignumObj -- |
| 3289 |
* |
| 3290 |
* Creates and initializes a bignum object. |
| 3291 |
* |
| 3292 |
* Results: |
| 3293 |
* Returns the newly created object. |
| 3294 |
* |
| 3295 |
* Side effects: |
| 3296 |
* The bignum value is cleared, since ownership has transferred to Tcl. |
| 3297 |
* |
| 3298 |
*---------------------------------------------------------------------- |
| 3299 |
*/ |
| 3300 |
|
| 3301 |
#ifdef TCL_MEM_DEBUG |
| 3302 |
#undef Tcl_NewBignumObj |
| 3303 |
|
| 3304 |
Tcl_Obj * |
| 3305 |
Tcl_NewBignumObj( |
| 3306 |
mp_int *bignumValue) |
| 3307 |
{ |
| 3308 |
return Tcl_DbNewBignumObj(bignumValue, "unknown", 0); |
| 3309 |
} |
| 3310 |
#else |
| 3311 |
Tcl_Obj * |
| 3312 |
Tcl_NewBignumObj( |
| 3313 |
mp_int *bignumValue) |
| 3314 |
{ |
| 3315 |
Tcl_Obj *objPtr; |
| 3316 |
|
| 3317 |
TclNewObj(objPtr); |
| 3318 |
Tcl_SetBignumObj(objPtr, bignumValue); |
| 3319 |
return objPtr; |
| 3320 |
} |
| 3321 |
#endif |
| 3322 |
|
| 3323 |
/* |
| 3324 |
*---------------------------------------------------------------------- |
| 3325 |
* |
| 3326 |
* Tcl_DbNewBignumObj -- |
| 3327 |
* |
| 3328 |
* This function is normally called when debugging: that is, when |
| 3329 |
* TCL_MEM_DEBUG is defined. It constructs a bignum object, recording the |
| 3330 |
* creation point so that [memory active] can report it. |
| 3331 |
* |
| 3332 |
* Results: |
| 3333 |
* Returns the newly created object. |
| 3334 |
* |
| 3335 |
* Side effects: |
| 3336 |
* The bignum value is cleared, since ownership has transferred to Tcl. |
| 3337 |
* |
| 3338 |
*---------------------------------------------------------------------- |
| 3339 |
*/ |
| 3340 |
|
| 3341 |
#ifdef TCL_MEM_DEBUG |
| 3342 |
Tcl_Obj * |
| 3343 |
Tcl_DbNewBignumObj( |
| 3344 |
mp_int *bignumValue, |
| 3345 |
const char *file, |
| 3346 |
int line) |
| 3347 |
{ |
| 3348 |
Tcl_Obj *objPtr; |
| 3349 |
|
| 3350 |
TclDbNewObj(objPtr, file, line); |
| 3351 |
Tcl_SetBignumObj(objPtr, bignumValue); |
| 3352 |
return objPtr; |
| 3353 |
} |
| 3354 |
#else |
| 3355 |
Tcl_Obj * |
| 3356 |
Tcl_DbNewBignumObj( |
| 3357 |
mp_int *bignumValue, |
| 3358 |
const char *file, |
| 3359 |
int line) |
| 3360 |
{ |
| 3361 |
return Tcl_NewBignumObj(bignumValue); |
| 3362 |
} |
| 3363 |
#endif |
| 3364 |
|
| 3365 |
/* |
| 3366 |
*---------------------------------------------------------------------- |
| 3367 |
* |
| 3368 |
* GetBignumFromObj -- |
| 3369 |
* |
| 3370 |
* This function retrieves a 'bignum' value from a Tcl object, converting |
| 3371 |
* the object if necessary. Either copies or transfers the mp_int value |
| 3372 |
* depending on the copy flag value passed in. |
| 3373 |
* |
| 3374 |
* Results: |
| 3375 |
* Returns TCL_OK if the conversion is successful, TCL_ERROR otherwise. |
| 3376 |
* |
| 3377 |
* Side effects: |
| 3378 |
* A copy of bignum is stored in *bignumValue, which is expected to be |
| 3379 |
* uninitialized or cleared. If conversion fails, and the 'interp' |
| 3380 |
* argument is not NULL, an error message is stored in the interpreter |
| 3381 |
* result. |
| 3382 |
* |
| 3383 |
*---------------------------------------------------------------------- |
| 3384 |
*/ |
| 3385 |
|
| 3386 |
static int |
| 3387 |
GetBignumFromObj( |
| 3388 |
Tcl_Interp *interp, /* Tcl interpreter for error reporting */ |
| 3389 |
Tcl_Obj *objPtr, /* Object to read */ |
| 3390 |
int copy, /* Whether to copy the returned bignum value */ |
| 3391 |
mp_int *bignumValue) /* Returned bignum value. */ |
| 3392 |
{ |
| 3393 |
do { |
| 3394 |
if (objPtr->typePtr == &tclBignumType) { |
| 3395 |
if (copy || Tcl_IsShared(objPtr)) { |
| 3396 |
mp_int temp; |
| 3397 |
|
| 3398 |
UNPACK_BIGNUM(objPtr, temp); |
| 3399 |
if (mp_init_copy(bignumValue, &temp) != MP_OKAY) { |
| 3400 |
if (interp != NULL) { |
| 3401 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3402 |
"insufficient memory to unpack bignum", -1)); |
| 3403 |
Tcl_SetErrorCode(interp, "TCL", "MEMORY", (char *)NULL); |
| 3404 |
} |
| 3405 |
return TCL_ERROR; |
| 3406 |
} |
| 3407 |
} else { |
| 3408 |
UNPACK_BIGNUM(objPtr, *bignumValue); |
| 3409 |
objPtr->internalRep.twoPtrValue.ptr1 = NULL; |
| 3410 |
objPtr->internalRep.twoPtrValue.ptr2 = NULL; |
| 3411 |
objPtr->typePtr = NULL; |
| 3412 |
if (objPtr->bytes == NULL) { |
| 3413 |
TclInitStringRep(objPtr, tclEmptyStringRep, 0); |
| 3414 |
} |
| 3415 |
} |
| 3416 |
return TCL_OK; |
| 3417 |
} |
| 3418 |
if (objPtr->typePtr == &tclIntType) { |
| 3419 |
TclBNInitBignumFromLong(bignumValue, objPtr->internalRep.longValue); |
| 3420 |
return TCL_OK; |
| 3421 |
} |
| 3422 |
#ifndef TCL_WIDE_INT_IS_LONG |
| 3423 |
if (objPtr->typePtr == &tclWideIntType) { |
| 3424 |
TclBNInitBignumFromWideInt(bignumValue, |
| 3425 |
objPtr->internalRep.wideValue); |
| 3426 |
return TCL_OK; |
| 3427 |
} |
| 3428 |
#endif |
| 3429 |
if (objPtr->typePtr == &tclDoubleType) { |
| 3430 |
if (interp != NULL) { |
| 3431 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 3432 |
"expected integer but got \"%s\"", |
| 3433 |
TclGetString(objPtr))); |
| 3434 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "INTEGER", (char *)NULL); |
| 3435 |
} |
| 3436 |
return TCL_ERROR; |
| 3437 |
} |
| 3438 |
} while (TclParseNumber(interp, objPtr, "integer", NULL, -1, NULL, |
| 3439 |
TCL_PARSE_INTEGER_ONLY)==TCL_OK); |
| 3440 |
return TCL_ERROR; |
| 3441 |
} |
| 3442 |
|
| 3443 |
/* |
| 3444 |
*---------------------------------------------------------------------- |
| 3445 |
* |
| 3446 |
* Tcl_GetBignumFromObj -- |
| 3447 |
* |
| 3448 |
* This function retrieves a 'bignum' value from a Tcl object, converting |
| 3449 |
* the object if necessary. |
| 3450 |
* |
| 3451 |
* Results: |
| 3452 |
* Returns TCL_OK if the conversion is successful, TCL_ERROR otherwise. |
| 3453 |
* |
| 3454 |
* Side effects: |
| 3455 |
* A copy of bignum is stored in *bignumValue, which is expected to be |
| 3456 |
* uninitialized or cleared. If conversion fails, an the 'interp' |
| 3457 |
* argument is not NULL, an error message is stored in the interpreter |
| 3458 |
* result. |
| 3459 |
* |
| 3460 |
* It is expected that the caller will NOT have invoked mp_init on the |
| 3461 |
* bignum value before passing it in. Tcl will initialize the mp_int as |
| 3462 |
* it sets the value. The value is a copy of the value in objPtr, so it |
| 3463 |
* becomes the responsibility of the caller to call mp_clear on it. |
| 3464 |
* |
| 3465 |
*---------------------------------------------------------------------- |
| 3466 |
*/ |
| 3467 |
|
| 3468 |
int |
| 3469 |
Tcl_GetBignumFromObj( |
| 3470 |
Tcl_Interp *interp, /* Tcl interpreter for error reporting */ |
| 3471 |
Tcl_Obj *objPtr, /* Object to read */ |
| 3472 |
mp_int *bignumValue) /* Returned bignum value. */ |
| 3473 |
{ |
| 3474 |
return GetBignumFromObj(interp, objPtr, 1, bignumValue); |
| 3475 |
} |
| 3476 |
|
| 3477 |
/* |
| 3478 |
*---------------------------------------------------------------------- |
| 3479 |
* |
| 3480 |
* Tcl_TakeBignumFromObj -- |
| 3481 |
* |
| 3482 |
* This function retrieves a 'bignum' value from a Tcl object, converting |
| 3483 |
* the object if necessary. |
| 3484 |
* |
| 3485 |
* Results: |
| 3486 |
* Returns TCL_OK if the conversion is successful, TCL_ERROR otherwise. |
| 3487 |
* |
| 3488 |
* Side effects: |
| 3489 |
* A copy of bignum is stored in *bignumValue, which is expected to be |
| 3490 |
* uninitialized or cleared. If conversion fails and the 'interp' |
| 3491 |
* argument is not NULL, an error message is stored in the interpreter |
| 3492 |
* result. |
| 3493 |
* |
| 3494 |
* It is expected that the caller will NOT have invoked mp_init on the |
| 3495 |
* bignum value before passing it in. Tcl will initialize the mp_int as |
| 3496 |
* it sets the value. The value is transferred from the internals of |
| 3497 |
* objPtr to the caller, passing responsibility of the caller to call |
| 3498 |
* mp_clear on it. The objPtr is cleared to hold an empty value. |
| 3499 |
* |
| 3500 |
*---------------------------------------------------------------------- |
| 3501 |
*/ |
| 3502 |
|
| 3503 |
int |
| 3504 |
Tcl_TakeBignumFromObj( |
| 3505 |
Tcl_Interp *interp, /* Tcl interpreter for error reporting */ |
| 3506 |
Tcl_Obj *objPtr, /* Object to read */ |
| 3507 |
mp_int *bignumValue) /* Returned bignum value. */ |
| 3508 |
{ |
| 3509 |
return GetBignumFromObj(interp, objPtr, 0, bignumValue); |
| 3510 |
} |
| 3511 |
|
| 3512 |
/* |
| 3513 |
*---------------------------------------------------------------------- |
| 3514 |
* |
| 3515 |
* Tcl_SetBignumObj -- |
| 3516 |
* |
| 3517 |
* This function sets the value of a Tcl_Obj to a large integer. |
| 3518 |
* |
| 3519 |
* Results: |
| 3520 |
* None. |
| 3521 |
* |
| 3522 |
* Side effects: |
| 3523 |
* Object value is stored. The bignum value is cleared, since ownership |
| 3524 |
* has transferred to Tcl. |
| 3525 |
* |
| 3526 |
*---------------------------------------------------------------------- |
| 3527 |
*/ |
| 3528 |
|
| 3529 |
void |
| 3530 |
Tcl_SetBignumObj( |
| 3531 |
Tcl_Obj *objPtr, /* Object to set */ |
| 3532 |
mp_int *bignumValue) /* Value to store */ |
| 3533 |
{ |
| 3534 |
if (Tcl_IsShared(objPtr)) { |
| 3535 |
Tcl_Panic("%s called with shared object", "Tcl_SetBignumObj"); |
| 3536 |
} |
| 3537 |
if ((size_t) bignumValue->used |
| 3538 |
<= (CHAR_BIT * sizeof(long) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT) { |
| 3539 |
unsigned long value = 0; |
| 3540 |
size_t numBytes; |
| 3541 |
long scratch; |
| 3542 |
unsigned char *bytes = (unsigned char *) &scratch; |
| 3543 |
|
| 3544 |
if (mp_to_ubin(bignumValue, bytes, sizeof(long), &numBytes) != MP_OKAY) { |
| 3545 |
goto tooLargeForLong; |
| 3546 |
} |
| 3547 |
while (numBytes-- > 0) { |
| 3548 |
value = (value << CHAR_BIT) | *bytes++; |
| 3549 |
} |
| 3550 |
if (value > (((~(unsigned long)0) >> 1) + mp_isneg(bignumValue))) { |
| 3551 |
goto tooLargeForLong; |
| 3552 |
} |
| 3553 |
if (mp_isneg(bignumValue)) { |
| 3554 |
TclSetLongObj(objPtr, (long)(-value)); |
| 3555 |
} else { |
| 3556 |
TclSetLongObj(objPtr, (long)value); |
| 3557 |
} |
| 3558 |
mp_clear(bignumValue); |
| 3559 |
return; |
| 3560 |
} |
| 3561 |
tooLargeForLong: |
| 3562 |
#ifndef TCL_WIDE_INT_IS_LONG |
| 3563 |
if ((size_t) bignumValue->used |
| 3564 |
<= (CHAR_BIT * sizeof(Tcl_WideInt) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT) { |
| 3565 |
Tcl_WideUInt value = 0; |
| 3566 |
size_t numBytes; |
| 3567 |
Tcl_WideInt scratch; |
| 3568 |
unsigned char *bytes = (unsigned char *)&scratch; |
| 3569 |
|
| 3570 |
if (mp_to_ubin(bignumValue, bytes, sizeof(Tcl_WideInt), &numBytes) != MP_OKAY) { |
| 3571 |
goto tooLargeForWide; |
| 3572 |
} |
| 3573 |
while (numBytes-- > 0) { |
| 3574 |
value = (value << CHAR_BIT) | *bytes++; |
| 3575 |
} |
| 3576 |
if (value > ((UWIDE_MAX >> 1) + mp_isneg(bignumValue))) { |
| 3577 |
goto tooLargeForWide; |
| 3578 |
} |
| 3579 |
if (mp_isneg(bignumValue)) { |
| 3580 |
TclSetWideIntObj(objPtr, (Tcl_WideInt)(-value)); |
| 3581 |
} else { |
| 3582 |
TclSetWideIntObj(objPtr, (Tcl_WideInt)value); |
| 3583 |
} |
| 3584 |
mp_clear(bignumValue); |
| 3585 |
return; |
| 3586 |
} |
| 3587 |
tooLargeForWide: |
| 3588 |
#endif |
| 3589 |
TclInvalidateStringRep(objPtr); |
| 3590 |
TclFreeIntRep(objPtr); |
| 3591 |
TclSetBignumInternalRep(objPtr, bignumValue); |
| 3592 |
} |
| 3593 |
|
| 3594 |
/* |
| 3595 |
*---------------------------------------------------------------------- |
| 3596 |
* |
| 3597 |
* TclSetBignumInternalRep -- |
| 3598 |
* |
| 3599 |
* Install a bignum into the internal representation of an object. |
| 3600 |
* |
| 3601 |
* Results: |
| 3602 |
* None. |
| 3603 |
* |
| 3604 |
* Side effects: |
| 3605 |
* Object internal representation is updated and object type is set. The |
| 3606 |
* bignum value is cleared, since ownership has transferred to the |
| 3607 |
* object. |
| 3608 |
* |
| 3609 |
*---------------------------------------------------------------------- |
| 3610 |
*/ |
| 3611 |
|
| 3612 |
void |
| 3613 |
TclSetBignumInternalRep( |
| 3614 |
Tcl_Obj *objPtr, |
| 3615 |
mp_int *bignumValue) |
| 3616 |
{ |
| 3617 |
objPtr->typePtr = &tclBignumType; |
| 3618 |
PACK_BIGNUM(*bignumValue, objPtr); |
| 3619 |
|
| 3620 |
/* |
| 3621 |
* Clear the mp_int value. |
| 3622 |
* |
| 3623 |
* Don't call mp_clear() because it would free the digit array we just |
| 3624 |
* packed into the Tcl_Obj. |
| 3625 |
*/ |
| 3626 |
|
| 3627 |
bignumValue->dp = NULL; |
| 3628 |
bignumValue->alloc = bignumValue->used = 0; |
| 3629 |
bignumValue->sign = MP_NEG; |
| 3630 |
} |
| 3631 |
|
| 3632 |
/* |
| 3633 |
*---------------------------------------------------------------------- |
| 3634 |
* |
| 3635 |
* TclGetNumberFromObj -- |
| 3636 |
* |
| 3637 |
* Extracts a number (of any possible numeric type) from an object. |
| 3638 |
* |
| 3639 |
* Results: |
| 3640 |
* Whether the extraction worked. The type is stored in the variable |
| 3641 |
* referred to by the typePtr argument, and a pointer to the |
| 3642 |
* representation is stored in the variable referred to by the |
| 3643 |
* clientDataPtr. |
| 3644 |
* |
| 3645 |
* Side effects: |
| 3646 |
* Can allocate thread-specific data for handling the copy-out space for |
| 3647 |
* bignums; this space is shared within a thread. |
| 3648 |
* |
| 3649 |
*---------------------------------------------------------------------- |
| 3650 |
*/ |
| 3651 |
|
| 3652 |
int |
| 3653 |
TclGetNumberFromObj( |
| 3654 |
Tcl_Interp *interp, |
| 3655 |
Tcl_Obj *objPtr, |
| 3656 |
ClientData *clientDataPtr, |
| 3657 |
int *typePtr) |
| 3658 |
{ |
| 3659 |
do { |
| 3660 |
if (objPtr->typePtr == &tclDoubleType) { |
| 3661 |
if (TclIsNaN(objPtr->internalRep.doubleValue)) { |
| 3662 |
*typePtr = TCL_NUMBER_NAN; |
| 3663 |
} else { |
| 3664 |
*typePtr = TCL_NUMBER_DOUBLE; |
| 3665 |
} |
| 3666 |
*clientDataPtr = &objPtr->internalRep.doubleValue; |
| 3667 |
return TCL_OK; |
| 3668 |
} |
| 3669 |
if (objPtr->typePtr == &tclIntType) { |
| 3670 |
*typePtr = TCL_NUMBER_LONG; |
| 3671 |
*clientDataPtr = &objPtr->internalRep.longValue; |
| 3672 |
return TCL_OK; |
| 3673 |
} |
| 3674 |
#ifndef TCL_WIDE_INT_IS_LONG |
| 3675 |
if (objPtr->typePtr == &tclWideIntType) { |
| 3676 |
*typePtr = TCL_NUMBER_WIDE; |
| 3677 |
*clientDataPtr = &objPtr->internalRep.wideValue; |
| 3678 |
return TCL_OK; |
| 3679 |
} |
| 3680 |
#endif |
| 3681 |
if (objPtr->typePtr == &tclBignumType) { |
| 3682 |
static Tcl_ThreadDataKey bignumKey; |
| 3683 |
mp_int *bigPtr = (mp_int *)Tcl_GetThreadData(&bignumKey, |
| 3684 |
(int)sizeof(mp_int)); |
| 3685 |
|
| 3686 |
UNPACK_BIGNUM(objPtr, *bigPtr); |
| 3687 |
*typePtr = TCL_NUMBER_BIG; |
| 3688 |
*clientDataPtr = bigPtr; |
| 3689 |
return TCL_OK; |
| 3690 |
} |
| 3691 |
} while (TCL_OK == |
| 3692 |
TclParseNumber(interp, objPtr, "number", NULL, -1, NULL, 0)); |
| 3693 |
return TCL_ERROR; |
| 3694 |
} |
| 3695 |
|
| 3696 |
/* |
| 3697 |
*---------------------------------------------------------------------- |
| 3698 |
* |
| 3699 |
* Tcl_DbIncrRefCount -- |
| 3700 |
* |
| 3701 |
* This function is normally called when debugging: i.e., when |
| 3702 |
* TCL_MEM_DEBUG is defined. This checks to see whether or not the memory |
| 3703 |
* has been freed before incrementing the ref count. |
| 3704 |
* |
| 3705 |
* When TCL_MEM_DEBUG is not defined, this function just increments the |
| 3706 |
* reference count of the object. |
| 3707 |
* |
| 3708 |
* Results: |
| 3709 |
* None. |
| 3710 |
* |
| 3711 |
* Side effects: |
| 3712 |
* The object's ref count is incremented. |
| 3713 |
* |
| 3714 |
*---------------------------------------------------------------------- |
| 3715 |
*/ |
| 3716 |
|
| 3717 |
void |
| 3718 |
Tcl_DbIncrRefCount( |
| 3719 |
Tcl_Obj *objPtr, /* The object we are registering a reference |
| 3720 |
* to. */ |
| 3721 |
const char *file, /* The name of the source file calling this |
| 3722 |
* function; used for debugging. */ |
| 3723 |
int line) /* Line number in the source file; used for |
| 3724 |
* debugging. */ |
| 3725 |
{ |
| 3726 |
#ifdef TCL_MEM_DEBUG |
| 3727 |
if (objPtr->refCount == 0x61616161) { |
| 3728 |
fprintf(stderr, "file = %s, line = %d\n", file, line); |
| 3729 |
fflush(stderr); |
| 3730 |
Tcl_Panic("incrementing refCount of previously disposed object"); |
| 3731 |
} |
| 3732 |
|
| 3733 |
# ifdef TCL_THREADS |
| 3734 |
/* |
| 3735 |
* Check to make sure that the Tcl_Obj was allocated by the current |
| 3736 |
* thread. Don't do this check when shutting down since thread local |
| 3737 |
* storage can be finalized before the last Tcl_Obj is freed. |
| 3738 |
*/ |
| 3739 |
|
| 3740 |
if (!TclInExit()) { |
| 3741 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 3742 |
Tcl_HashTable *tablePtr = tsdPtr->objThreadMap; |
| 3743 |
Tcl_HashEntry *hPtr; |
| 3744 |
|
| 3745 |
if (!tablePtr) { |
| 3746 |
Tcl_Panic("object table not initialized"); |
| 3747 |
} |
| 3748 |
hPtr = Tcl_FindHashEntry(tablePtr, objPtr); |
| 3749 |
if (!hPtr) { |
| 3750 |
Tcl_Panic("Trying to %s of Tcl_Obj allocated in another thread", |
| 3751 |
"incr ref count"); |
| 3752 |
} |
| 3753 |
} |
| 3754 |
# endif /* TCL_THREADS */ |
| 3755 |
#endif /* TCL_MEM_DEBUG */ |
| 3756 |
++(objPtr)->refCount; |
| 3757 |
} |
| 3758 |
|
| 3759 |
/* |
| 3760 |
*---------------------------------------------------------------------- |
| 3761 |
* |
| 3762 |
* Tcl_DbDecrRefCount -- |
| 3763 |
* |
| 3764 |
* This function is normally called when debugging: i.e., when |
| 3765 |
* TCL_MEM_DEBUG is defined. This checks to see whether or not the memory |
| 3766 |
* has been freed before decrementing the ref count. |
| 3767 |
* |
| 3768 |
* When TCL_MEM_DEBUG is not defined, this function just decrements the |
| 3769 |
* reference count of the object. |
| 3770 |
* |
| 3771 |
* Results: |
| 3772 |
* None. |
| 3773 |
* |
| 3774 |
* Side effects: |
| 3775 |
* The object's ref count is incremented. |
| 3776 |
* |
| 3777 |
*---------------------------------------------------------------------- |
| 3778 |
*/ |
| 3779 |
|
| 3780 |
void |
| 3781 |
Tcl_DbDecrRefCount( |
| 3782 |
Tcl_Obj *objPtr, /* The object we are releasing a reference |
| 3783 |
* to. */ |
| 3784 |
const char *file, /* The name of the source file calling this |
| 3785 |
* function; used for debugging. */ |
| 3786 |
int line) /* Line number in the source file; used for |
| 3787 |
* debugging. */ |
| 3788 |
{ |
| 3789 |
#ifdef TCL_MEM_DEBUG |
| 3790 |
if (objPtr->refCount == 0x61616161) { |
| 3791 |
fprintf(stderr, "file = %s, line = %d\n", file, line); |
| 3792 |
fflush(stderr); |
| 3793 |
Tcl_Panic("decrementing refCount of previously disposed object"); |
| 3794 |
} |
| 3795 |
|
| 3796 |
# ifdef TCL_THREADS |
| 3797 |
/* |
| 3798 |
* Check to make sure that the Tcl_Obj was allocated by the current |
| 3799 |
* thread. Don't do this check when shutting down since thread local |
| 3800 |
* storage can be finalized before the last Tcl_Obj is freed. |
| 3801 |
*/ |
| 3802 |
|
| 3803 |
if (!TclInExit()) { |
| 3804 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 3805 |
Tcl_HashTable *tablePtr = tsdPtr->objThreadMap; |
| 3806 |
Tcl_HashEntry *hPtr; |
| 3807 |
|
| 3808 |
if (!tablePtr) { |
| 3809 |
Tcl_Panic("object table not initialized"); |
| 3810 |
} |
| 3811 |
hPtr = Tcl_FindHashEntry(tablePtr, objPtr); |
| 3812 |
if (!hPtr) { |
| 3813 |
Tcl_Panic("Trying to %s of Tcl_Obj allocated in another thread", |
| 3814 |
"decr ref count"); |
| 3815 |
} |
| 3816 |
} |
| 3817 |
# endif /* TCL_THREADS */ |
| 3818 |
#endif /* TCL_MEM_DEBUG */ |
| 3819 |
|
| 3820 |
if (objPtr->refCount-- <= 1) { |
| 3821 |
TclFreeObj(objPtr); |
| 3822 |
} |
| 3823 |
} |
| 3824 |
|
| 3825 |
/* |
| 3826 |
*---------------------------------------------------------------------- |
| 3827 |
* |
| 3828 |
* Tcl_DbIsShared -- |
| 3829 |
* |
| 3830 |
* This function is normally called when debugging: i.e., when |
| 3831 |
* TCL_MEM_DEBUG is defined. It tests whether the object has a ref count |
| 3832 |
* greater than one. |
| 3833 |
* |
| 3834 |
* When TCL_MEM_DEBUG is not defined, this function just tests if the |
| 3835 |
* object has a ref count greater than one. |
| 3836 |
* |
| 3837 |
* Results: |
| 3838 |
* None. |
| 3839 |
* |
| 3840 |
* Side effects: |
| 3841 |
* None. |
| 3842 |
* |
| 3843 |
*---------------------------------------------------------------------- |
| 3844 |
*/ |
| 3845 |
|
| 3846 |
int |
| 3847 |
Tcl_DbIsShared( |
| 3848 |
Tcl_Obj *objPtr, /* The object to test for being shared. */ |
| 3849 |
const char *file, /* The name of the source file calling this |
| 3850 |
* function; used for debugging. */ |
| 3851 |
int line) /* Line number in the source file; used for |
| 3852 |
* debugging. */ |
| 3853 |
{ |
| 3854 |
#ifdef TCL_MEM_DEBUG |
| 3855 |
if (objPtr->refCount == 0x61616161) { |
| 3856 |
fprintf(stderr, "file = %s, line = %d\n", file, line); |
| 3857 |
fflush(stderr); |
| 3858 |
Tcl_Panic("checking whether previously disposed object is shared"); |
| 3859 |
} |
| 3860 |
|
| 3861 |
# ifdef TCL_THREADS |
| 3862 |
/* |
| 3863 |
* Check to make sure that the Tcl_Obj was allocated by the current |
| 3864 |
* thread. Don't do this check when shutting down since thread local |
| 3865 |
* storage can be finalized before the last Tcl_Obj is freed. |
| 3866 |
*/ |
| 3867 |
|
| 3868 |
if (!TclInExit()) { |
| 3869 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 3870 |
Tcl_HashTable *tablePtr = tsdPtr->objThreadMap; |
| 3871 |
Tcl_HashEntry *hPtr; |
| 3872 |
|
| 3873 |
if (!tablePtr) { |
| 3874 |
Tcl_Panic("object table not initialized"); |
| 3875 |
} |
| 3876 |
hPtr = Tcl_FindHashEntry(tablePtr, objPtr); |
| 3877 |
if (!hPtr) { |
| 3878 |
Tcl_Panic("Trying to %s of Tcl_Obj allocated in another thread", |
| 3879 |
"check shared status"); |
| 3880 |
} |
| 3881 |
} |
| 3882 |
# endif /* TCL_THREADS */ |
| 3883 |
#endif /* TCL_MEM_DEBUG */ |
| 3884 |
|
| 3885 |
#ifdef TCL_COMPILE_STATS |
| 3886 |
Tcl_MutexLock(&tclObjMutex); |
| 3887 |
if ((objPtr)->refCount <= 1) { |
| 3888 |
tclObjsShared[1]++; |
| 3889 |
} else if ((objPtr)->refCount < TCL_MAX_SHARED_OBJ_STATS) { |
| 3890 |
tclObjsShared[(objPtr)->refCount]++; |
| 3891 |
} else { |
| 3892 |
tclObjsShared[0]++; |
| 3893 |
} |
| 3894 |
Tcl_MutexUnlock(&tclObjMutex); |
| 3895 |
#endif /* TCL_COMPILE_STATS */ |
| 3896 |
|
| 3897 |
return ((objPtr)->refCount > 1); |
| 3898 |
} |
| 3899 |
|
| 3900 |
/* |
| 3901 |
*---------------------------------------------------------------------- |
| 3902 |
* |
| 3903 |
* Tcl_InitObjHashTable -- |
| 3904 |
* |
| 3905 |
* Given storage for a hash table, set up the fields to prepare the hash |
| 3906 |
* table for use, the keys are Tcl_Obj *. |
| 3907 |
* |
| 3908 |
* Results: |
| 3909 |
* None. |
| 3910 |
* |
| 3911 |
* Side effects: |
| 3912 |
* TablePtr is now ready to be passed to Tcl_FindHashEntry and |
| 3913 |
* Tcl_CreateHashEntry. |
| 3914 |
* |
| 3915 |
*---------------------------------------------------------------------- |
| 3916 |
*/ |
| 3917 |
|
| 3918 |
void |
| 3919 |
Tcl_InitObjHashTable( |
| 3920 |
Tcl_HashTable *tablePtr) |
| 3921 |
/* Pointer to table record, which is supplied |
| 3922 |
* by the caller. */ |
| 3923 |
{ |
| 3924 |
Tcl_InitCustomHashTable(tablePtr, TCL_CUSTOM_PTR_KEYS, |
| 3925 |
&tclObjHashKeyType); |
| 3926 |
} |
| 3927 |
|
| 3928 |
/* |
| 3929 |
*---------------------------------------------------------------------- |
| 3930 |
* |
| 3931 |
* AllocObjEntry -- |
| 3932 |
* |
| 3933 |
* Allocate space for a Tcl_HashEntry containing the Tcl_Obj * key. |
| 3934 |
* |
| 3935 |
* Results: |
| 3936 |
* The return value is a pointer to the created entry. |
| 3937 |
* |
| 3938 |
* Side effects: |
| 3939 |
* Increments the reference count on the object. |
| 3940 |
* |
| 3941 |
*---------------------------------------------------------------------- |
| 3942 |
*/ |
| 3943 |
|
| 3944 |
static Tcl_HashEntry * |
| 3945 |
AllocObjEntry( |
| 3946 |
Tcl_HashTable *tablePtr, /* Hash table. */ |
| 3947 |
void *keyPtr) /* Key to store in the hash table entry. */ |
| 3948 |
{ |
| 3949 |
Tcl_Obj *objPtr = (Tcl_Obj *)keyPtr; |
| 3950 |
Tcl_HashEntry *hPtr = (Tcl_HashEntry *)ckalloc(sizeof(Tcl_HashEntry)); |
| 3951 |
|
| 3952 |
hPtr->key.objPtr = objPtr; |
| 3953 |
Tcl_IncrRefCount(objPtr); |
| 3954 |
hPtr->clientData = NULL; |
| 3955 |
|
| 3956 |
return hPtr; |
| 3957 |
} |
| 3958 |
|
| 3959 |
/* |
| 3960 |
*---------------------------------------------------------------------- |
| 3961 |
* |
| 3962 |
* TclCompareObjKeys -- |
| 3963 |
* |
| 3964 |
* Compares two Tcl_Obj * keys. |
| 3965 |
* |
| 3966 |
* Results: |
| 3967 |
* The return value is 0 if they are different and 1 if they are the |
| 3968 |
* same. |
| 3969 |
* |
| 3970 |
* Side effects: |
| 3971 |
* None. |
| 3972 |
* |
| 3973 |
*---------------------------------------------------------------------- |
| 3974 |
*/ |
| 3975 |
|
| 3976 |
int |
| 3977 |
TclCompareObjKeys( |
| 3978 |
void *keyPtr, /* New key to compare. */ |
| 3979 |
Tcl_HashEntry *hPtr) /* Existing key to compare. */ |
| 3980 |
{ |
| 3981 |
Tcl_Obj *objPtr1 = (Tcl_Obj *)keyPtr; |
| 3982 |
Tcl_Obj *objPtr2 = (Tcl_Obj *)hPtr->key.oneWordValue; |
| 3983 |
const char *p1, *p2; |
| 3984 |
size_t l1, l2; |
| 3985 |
|
| 3986 |
/* |
| 3987 |
* If the object pointers are the same then they match. |
| 3988 |
* OPT: this comparison was moved to the caller |
| 3989 |
|
| 3990 |
if (objPtr1 == objPtr2) { |
| 3991 |
return 1; |
| 3992 |
} |
| 3993 |
*/ |
| 3994 |
|
| 3995 |
/* |
| 3996 |
* Don't use Tcl_GetStringFromObj as it would prevent l1 and l2 being |
| 3997 |
* in a register. |
| 3998 |
*/ |
| 3999 |
|
| 4000 |
p1 = TclGetString(objPtr1); |
| 4001 |
l1 = objPtr1->length; |
| 4002 |
p2 = TclGetString(objPtr2); |
| 4003 |
l2 = objPtr2->length; |
| 4004 |
|
| 4005 |
/* |
| 4006 |
* Only compare if the string representations are of the same length. |
| 4007 |
*/ |
| 4008 |
|
| 4009 |
if (l1 == l2) { |
| 4010 |
for (;; p1++, p2++, l1--) { |
| 4011 |
if (*p1 != *p2) { |
| 4012 |
break; |
| 4013 |
} |
| 4014 |
if (l1 == 0) { |
| 4015 |
return 1; |
| 4016 |
} |
| 4017 |
} |
| 4018 |
} |
| 4019 |
|
| 4020 |
return 0; |
| 4021 |
} |
| 4022 |
|
| 4023 |
/* |
| 4024 |
*---------------------------------------------------------------------- |
| 4025 |
* |
| 4026 |
* TclFreeObjEntry -- |
| 4027 |
* |
| 4028 |
* Frees space for a Tcl_HashEntry containing the Tcl_Obj * key. |
| 4029 |
* |
| 4030 |
* Results: |
| 4031 |
* The return value is a pointer to the created entry. |
| 4032 |
* |
| 4033 |
* Side effects: |
| 4034 |
* Decrements the reference count of the object. |
| 4035 |
* |
| 4036 |
*---------------------------------------------------------------------- |
| 4037 |
*/ |
| 4038 |
|
| 4039 |
void |
| 4040 |
TclFreeObjEntry( |
| 4041 |
Tcl_HashEntry *hPtr) /* Hash entry to free. */ |
| 4042 |
{ |
| 4043 |
Tcl_Obj *objPtr = (Tcl_Obj *) hPtr->key.oneWordValue; |
| 4044 |
|
| 4045 |
Tcl_DecrRefCount(objPtr); |
| 4046 |
ckfree(hPtr); |
| 4047 |
} |
| 4048 |
|
| 4049 |
/* |
| 4050 |
*---------------------------------------------------------------------- |
| 4051 |
* |
| 4052 |
* TclHashObjKey -- |
| 4053 |
* |
| 4054 |
* Compute a one-word summary of the string representation of the |
| 4055 |
* Tcl_Obj, which can be used to generate a hash index. |
| 4056 |
* |
| 4057 |
* Results: |
| 4058 |
* The return value is a one-word summary of the information in the |
| 4059 |
* string representation of the Tcl_Obj. |
| 4060 |
* |
| 4061 |
* Side effects: |
| 4062 |
* None. |
| 4063 |
* |
| 4064 |
*---------------------------------------------------------------------- |
| 4065 |
*/ |
| 4066 |
|
| 4067 |
unsigned int |
| 4068 |
TclHashObjKey( |
| 4069 |
Tcl_HashTable *tablePtr, /* Hash table. */ |
| 4070 |
void *keyPtr) /* Key from which to compute hash value. */ |
| 4071 |
{ |
| 4072 |
Tcl_Obj *objPtr = (Tcl_Obj *)keyPtr; |
| 4073 |
int length; |
| 4074 |
const char *string = TclGetStringFromObj(objPtr, &length); |
| 4075 |
unsigned int result = 0; |
| 4076 |
|
| 4077 |
/* |
| 4078 |
* I tried a zillion different hash functions and asked many other people |
| 4079 |
* for advice. Many people had their own favorite functions, all |
| 4080 |
* different, but no-one had much idea why they were good ones. I chose |
| 4081 |
* the one below (multiply by 9 and add new character) because of the |
| 4082 |
* following reasons: |
| 4083 |
* |
| 4084 |
* 1. Multiplying by 10 is perfect for keys that are decimal strings, and |
| 4085 |
* multiplying by 9 is just about as good. |
| 4086 |
* 2. Times-9 is (shift-left-3) plus (old). This means that each |
| 4087 |
* character's bits hang around in the low-order bits of the hash value |
| 4088 |
* for ever, plus they spread fairly rapidly up to the high-order bits |
| 4089 |
* to fill out the hash value. This seems works well both for decimal |
| 4090 |
* and non-decimal strings. |
| 4091 |
* |
| 4092 |
* Note that this function is very weak against malicious strings; it's |
| 4093 |
* very easy to generate multiple keys that have the same hashcode. On the |
| 4094 |
* other hand, that hardly ever actually occurs and this function *is* |
| 4095 |
* very cheap, even by comparison with industry-standard hashes like FNV. |
| 4096 |
* If real strength of hash is required though, use a custom hash based on |
| 4097 |
* Bob Jenkins's lookup3(), but be aware that it's significantly slower. |
| 4098 |
* Tcl does not use that level of strength because it typically does not |
| 4099 |
* need it (and some of the aspects of that strength are genuinely |
| 4100 |
* unnecessary given the rest of Tcl's hash machinery, and the fact that |
| 4101 |
* we do not either transfer hashes to another machine, use them as a true |
| 4102 |
* substitute for equality, or attempt to minimize work in rebuilding the |
| 4103 |
* hash table). |
| 4104 |
* |
| 4105 |
* See also HashStringKey in tclHash.c. |
| 4106 |
* See also HashString in tclLiteral.c. |
| 4107 |
* |
| 4108 |
* See [tcl-Feature Request #2958832] |
| 4109 |
*/ |
| 4110 |
|
| 4111 |
if (length > 0) { |
| 4112 |
result = UCHAR(*string); |
| 4113 |
while (--length) { |
| 4114 |
result += (result << 3) + UCHAR(*++string); |
| 4115 |
} |
| 4116 |
} |
| 4117 |
return result; |
| 4118 |
} |
| 4119 |
|
| 4120 |
/* |
| 4121 |
*---------------------------------------------------------------------- |
| 4122 |
* |
| 4123 |
* Tcl_GetCommandFromObj -- |
| 4124 |
* |
| 4125 |
* Returns the command specified by the name in a Tcl_Obj. |
| 4126 |
* |
| 4127 |
* Results: |
| 4128 |
* Returns a token for the command if it is found. Otherwise, if it can't |
| 4129 |
* be found or there is an error, returns NULL. |
| 4130 |
* |
| 4131 |
* Side effects: |
| 4132 |
* May update the internal representation for the object, caching the |
| 4133 |
* command reference so that the next time this function is called with |
| 4134 |
* the same object, the command can be found quickly. |
| 4135 |
* |
| 4136 |
*---------------------------------------------------------------------- |
| 4137 |
*/ |
| 4138 |
|
| 4139 |
Tcl_Command |
| 4140 |
Tcl_GetCommandFromObj( |
| 4141 |
Tcl_Interp *interp, /* The interpreter in which to resolve the |
| 4142 |
* command and to report errors. */ |
| 4143 |
Tcl_Obj *objPtr) /* The object containing the command's name. |
| 4144 |
* If the name starts with "::", will be |
| 4145 |
* looked up in global namespace. Else, looked |
| 4146 |
* up first in the current namespace, then in |
| 4147 |
* global namespace. */ |
| 4148 |
{ |
| 4149 |
ResolvedCmdName *resPtr; |
| 4150 |
|
| 4151 |
/* |
| 4152 |
* Get the internal representation, converting to a command type if |
| 4153 |
* needed. The internal representation is a ResolvedCmdName that points to |
| 4154 |
* the actual command. |
| 4155 |
* |
| 4156 |
* Check the context namespace and the namespace epoch of the resolved |
| 4157 |
* symbol to make sure that it is fresh. Note that we verify that the |
| 4158 |
* namespace id of the context namespace is the same as the one we cached; |
| 4159 |
* this insures that the namespace wasn't deleted and a new one created at |
| 4160 |
* the same address with the same command epoch. Note that fully qualified |
| 4161 |
* names have a NULL refNsPtr, these checks needn't be made. |
| 4162 |
* |
| 4163 |
* Check also that the command's epoch is up to date, and that the command |
| 4164 |
* is not deleted. |
| 4165 |
* |
| 4166 |
* If any check fails, then force another conversion to the command type, |
| 4167 |
* to discard the old rep and create a new one. |
| 4168 |
*/ |
| 4169 |
|
| 4170 |
resPtr = (ResolvedCmdName *)objPtr->internalRep.twoPtrValue.ptr1; |
| 4171 |
if ((objPtr->typePtr == &tclCmdNameType) && (resPtr != NULL)) { |
| 4172 |
Command *cmdPtr = resPtr->cmdPtr; |
| 4173 |
|
| 4174 |
if ((cmdPtr->cmdEpoch == resPtr->cmdEpoch) |
| 4175 |
&& !(cmdPtr->flags & CMD_IS_DELETED) |
| 4176 |
&& (interp == cmdPtr->nsPtr->interp) |
| 4177 |
&& !(cmdPtr->nsPtr->flags & NS_DYING)) { |
| 4178 |
Namespace *refNsPtr = (Namespace *) |
| 4179 |
TclGetCurrentNamespace(interp); |
| 4180 |
|
| 4181 |
if ((resPtr->refNsPtr == NULL) |
| 4182 |
|| ((refNsPtr == resPtr->refNsPtr) |
| 4183 |
&& (resPtr->refNsId == refNsPtr->nsId) |
| 4184 |
&& (resPtr->refNsCmdEpoch == refNsPtr->cmdRefEpoch))) { |
| 4185 |
return (Tcl_Command) cmdPtr; |
| 4186 |
} |
| 4187 |
} |
| 4188 |
} |
| 4189 |
|
| 4190 |
/* |
| 4191 |
* OK, must create a new internal representation (or fail) as any cache we |
| 4192 |
* had is invalid one way or another. |
| 4193 |
*/ |
| 4194 |
|
| 4195 |
/* See [07d13d99b0a9] why we cannot call SetCmdNameFromAny() directly here. */ |
| 4196 |
if (tclCmdNameType.setFromAnyProc(interp, objPtr) != TCL_OK) { |
| 4197 |
return NULL; |
| 4198 |
} |
| 4199 |
resPtr = (ResolvedCmdName *)objPtr->internalRep.twoPtrValue.ptr1; |
| 4200 |
return (Tcl_Command) (resPtr ? resPtr->cmdPtr : NULL); |
| 4201 |
} |
| 4202 |
|
| 4203 |
/* |
| 4204 |
*---------------------------------------------------------------------- |
| 4205 |
* |
| 4206 |
* TclSetCmdNameObj -- |
| 4207 |
* |
| 4208 |
* Modify an object to be an CmdName object that refers to the argument |
| 4209 |
* Command structure. |
| 4210 |
* |
| 4211 |
* Results: |
| 4212 |
* None. |
| 4213 |
* |
| 4214 |
* Side effects: |
| 4215 |
* The object's old internal rep is freed. Its string rep is not |
| 4216 |
* changed. The refcount in the Command structure is incremented to keep |
| 4217 |
* it from being freed if the command is later deleted until |
| 4218 |
* TclNRExecuteByteCode has a chance to recognize that it was deleted. |
| 4219 |
* |
| 4220 |
*---------------------------------------------------------------------- |
| 4221 |
*/ |
| 4222 |
|
| 4223 |
void |
| 4224 |
TclSetCmdNameObj( |
| 4225 |
Tcl_Interp *interp, /* Points to interpreter containing command |
| 4226 |
* that should be cached in objPtr. */ |
| 4227 |
Tcl_Obj *objPtr, /* Points to Tcl object to be changed to a |
| 4228 |
* CmdName object. */ |
| 4229 |
Command *cmdPtr) /* Points to Command structure that the |
| 4230 |
* CmdName object should refer to. */ |
| 4231 |
{ |
| 4232 |
Interp *iPtr = (Interp *)interp; |
| 4233 |
ResolvedCmdName *resPtr; |
| 4234 |
Namespace *currNsPtr; |
| 4235 |
const char *name; |
| 4236 |
|
| 4237 |
if (objPtr->typePtr == &tclCmdNameType) { |
| 4238 |
resPtr = (ResolvedCmdName *)objPtr->internalRep.twoPtrValue.ptr1; |
| 4239 |
if (resPtr != NULL && resPtr->cmdPtr == cmdPtr) { |
| 4240 |
return; |
| 4241 |
} |
| 4242 |
} |
| 4243 |
|
| 4244 |
cmdPtr->refCount++; |
| 4245 |
resPtr = (ResolvedCmdName *)ckalloc(sizeof(ResolvedCmdName)); |
| 4246 |
resPtr->cmdPtr = cmdPtr; |
| 4247 |
resPtr->cmdEpoch = cmdPtr->cmdEpoch; |
| 4248 |
resPtr->refCount = 1; |
| 4249 |
|
| 4250 |
name = TclGetString(objPtr); |
| 4251 |
if ((*name++ == ':') && (*name == ':')) { |
| 4252 |
/* |
| 4253 |
* The name is fully qualified: set the referring namespace to |
| 4254 |
* NULL. |
| 4255 |
*/ |
| 4256 |
|
| 4257 |
resPtr->refNsPtr = NULL; |
| 4258 |
} else { |
| 4259 |
/* |
| 4260 |
* Get the current namespace. |
| 4261 |
*/ |
| 4262 |
|
| 4263 |
currNsPtr = iPtr->varFramePtr->nsPtr; |
| 4264 |
|
| 4265 |
resPtr->refNsPtr = currNsPtr; |
| 4266 |
resPtr->refNsId = currNsPtr->nsId; |
| 4267 |
resPtr->refNsCmdEpoch = currNsPtr->cmdRefEpoch; |
| 4268 |
} |
| 4269 |
|
| 4270 |
TclFreeIntRep(objPtr); |
| 4271 |
objPtr->internalRep.twoPtrValue.ptr1 = resPtr; |
| 4272 |
objPtr->internalRep.twoPtrValue.ptr2 = NULL; |
| 4273 |
objPtr->typePtr = &tclCmdNameType; |
| 4274 |
} |
| 4275 |
|
| 4276 |
/* |
| 4277 |
*---------------------------------------------------------------------- |
| 4278 |
* |
| 4279 |
* FreeCmdNameInternalRep -- |
| 4280 |
* |
| 4281 |
* Frees the resources associated with a cmdName object's internal |
| 4282 |
* representation. |
| 4283 |
* |
| 4284 |
* Results: |
| 4285 |
* None. |
| 4286 |
* |
| 4287 |
* Side effects: |
| 4288 |
* Decrements the ref count of any cached ResolvedCmdName structure |
| 4289 |
* pointed to by the cmdName's internal representation. If this is the |
| 4290 |
* last use of the ResolvedCmdName, it is freed. This in turn decrements |
| 4291 |
* the ref count of the Command structure pointed to by the |
| 4292 |
* ResolvedSymbol, which may free the Command structure. |
| 4293 |
* |
| 4294 |
*---------------------------------------------------------------------- |
| 4295 |
*/ |
| 4296 |
|
| 4297 |
static void |
| 4298 |
FreeCmdNameInternalRep( |
| 4299 |
Tcl_Obj *objPtr) /* CmdName object with internal |
| 4300 |
* representation to free. */ |
| 4301 |
{ |
| 4302 |
ResolvedCmdName *resPtr = (ResolvedCmdName *)objPtr->internalRep.twoPtrValue.ptr1; |
| 4303 |
|
| 4304 |
if (resPtr != (ResolvedCmdName *)NULL) { |
| 4305 |
/* |
| 4306 |
* Decrement the reference count of the ResolvedCmdName structure. If |
| 4307 |
* there are no more uses, free the ResolvedCmdName structure. |
| 4308 |
*/ |
| 4309 |
|
| 4310 |
if (resPtr->refCount-- == 1) { |
| 4311 |
/* |
| 4312 |
* Now free the cached command, unless it is still in its hash |
| 4313 |
* table or if there are other references to it from other cmdName |
| 4314 |
* objects. |
| 4315 |
*/ |
| 4316 |
|
| 4317 |
Command *cmdPtr = resPtr->cmdPtr; |
| 4318 |
|
| 4319 |
TclCleanupCommandMacro(cmdPtr); |
| 4320 |
ckfree(resPtr); |
| 4321 |
} |
| 4322 |
} |
| 4323 |
objPtr->typePtr = NULL; |
| 4324 |
} |
| 4325 |
|
| 4326 |
/* |
| 4327 |
*---------------------------------------------------------------------- |
| 4328 |
* |
| 4329 |
* DupCmdNameInternalRep -- |
| 4330 |
* |
| 4331 |
* Initialize the internal representation of an cmdName Tcl_Obj to a copy |
| 4332 |
* of the internal representation of an existing cmdName object. |
| 4333 |
* |
| 4334 |
* Results: |
| 4335 |
* None. |
| 4336 |
* |
| 4337 |
* Side effects: |
| 4338 |
* "copyPtr"s internal rep is set to point to the ResolvedCmdName |
| 4339 |
* structure corresponding to "srcPtr"s internal rep. Increments the ref |
| 4340 |
* count of the ResolvedCmdName structure pointed to by the cmdName's |
| 4341 |
* internal representation. |
| 4342 |
* |
| 4343 |
*---------------------------------------------------------------------- |
| 4344 |
*/ |
| 4345 |
|
| 4346 |
static void |
| 4347 |
DupCmdNameInternalRep( |
| 4348 |
Tcl_Obj *srcPtr, /* Object with internal rep to copy. */ |
| 4349 |
Tcl_Obj *copyPtr) /* Object with internal rep to set. */ |
| 4350 |
{ |
| 4351 |
ResolvedCmdName *resPtr = (ResolvedCmdName *)srcPtr->internalRep.twoPtrValue.ptr1; |
| 4352 |
|
| 4353 |
copyPtr->internalRep.twoPtrValue.ptr1 = resPtr; |
| 4354 |
copyPtr->internalRep.twoPtrValue.ptr2 = NULL; |
| 4355 |
if (resPtr != NULL) { |
| 4356 |
resPtr->refCount++; |
| 4357 |
} |
| 4358 |
copyPtr->typePtr = &tclCmdNameType; |
| 4359 |
} |
| 4360 |
|
| 4361 |
/* |
| 4362 |
*---------------------------------------------------------------------- |
| 4363 |
* |
| 4364 |
* SetCmdNameFromAny -- |
| 4365 |
* |
| 4366 |
* Generate an cmdName internal form for the Tcl object "objPtr". |
| 4367 |
* |
| 4368 |
* Results: |
| 4369 |
* The return value is a standard Tcl result. The conversion always |
| 4370 |
* succeeds and TCL_OK is returned. |
| 4371 |
* |
| 4372 |
* Side effects: |
| 4373 |
* A pointer to a ResolvedCmdName structure that holds a cached pointer |
| 4374 |
* to the command with a name that matches objPtr's string rep is stored |
| 4375 |
* as objPtr's internal representation. This ResolvedCmdName pointer will |
| 4376 |
* be NULL if no matching command was found. The ref count of the cached |
| 4377 |
* Command's structure (if any) is also incremented. |
| 4378 |
* |
| 4379 |
*---------------------------------------------------------------------- |
| 4380 |
*/ |
| 4381 |
|
| 4382 |
static int |
| 4383 |
SetCmdNameFromAny( |
| 4384 |
Tcl_Interp *interp, /* Used for error reporting if not NULL. */ |
| 4385 |
Tcl_Obj *objPtr) /* The object to convert. */ |
| 4386 |
{ |
| 4387 |
Interp *iPtr = (Interp *)interp; |
| 4388 |
const char *name; |
| 4389 |
Command *cmdPtr; |
| 4390 |
Namespace *currNsPtr; |
| 4391 |
ResolvedCmdName *resPtr; |
| 4392 |
|
| 4393 |
if (interp == NULL) { |
| 4394 |
return TCL_ERROR; |
| 4395 |
} |
| 4396 |
|
| 4397 |
/* |
| 4398 |
* Find the Command structure, if any, that describes the command called |
| 4399 |
* "name". Build a ResolvedCmdName that holds a cached pointer to this |
| 4400 |
* Command, and bump the reference count in the referenced Command |
| 4401 |
* structure. A Command structure will not be deleted as long as it is |
| 4402 |
* referenced from a CmdName object. |
| 4403 |
*/ |
| 4404 |
|
| 4405 |
name = TclGetString(objPtr); |
| 4406 |
cmdPtr = (Command *) |
| 4407 |
Tcl_FindCommand(interp, name, /*ns*/ NULL, /*flags*/ 0); |
| 4408 |
|
| 4409 |
/* |
| 4410 |
* Free the old internalRep before setting the new one. Do this after |
| 4411 |
* getting the string rep to allow the conversion code (in particular, |
| 4412 |
* Tcl_GetStringFromObj) to use that old internalRep. |
| 4413 |
*/ |
| 4414 |
|
| 4415 |
if (cmdPtr) { |
| 4416 |
cmdPtr->refCount++; |
| 4417 |
resPtr = (ResolvedCmdName *)objPtr->internalRep.twoPtrValue.ptr1; |
| 4418 |
if ((objPtr->typePtr == &tclCmdNameType) |
| 4419 |
&& resPtr && (resPtr->refCount == 1)) { |
| 4420 |
/* |
| 4421 |
* Reuse the old ResolvedCmdName struct instead of freeing it |
| 4422 |
*/ |
| 4423 |
|
| 4424 |
Command *oldCmdPtr = resPtr->cmdPtr; |
| 4425 |
|
| 4426 |
if (--oldCmdPtr->refCount == 0) { |
| 4427 |
TclCleanupCommandMacro(oldCmdPtr); |
| 4428 |
} |
| 4429 |
} else { |
| 4430 |
TclFreeIntRep(objPtr); |
| 4431 |
resPtr = (ResolvedCmdName *)ckalloc(sizeof(ResolvedCmdName)); |
| 4432 |
resPtr->refCount = 1; |
| 4433 |
objPtr->internalRep.twoPtrValue.ptr1 = resPtr; |
| 4434 |
objPtr->internalRep.twoPtrValue.ptr2 = NULL; |
| 4435 |
objPtr->typePtr = &tclCmdNameType; |
| 4436 |
} |
| 4437 |
resPtr->cmdPtr = cmdPtr; |
| 4438 |
resPtr->cmdEpoch = cmdPtr->cmdEpoch; |
| 4439 |
if ((*name++ == ':') && (*name == ':')) { |
| 4440 |
/* |
| 4441 |
* The name is fully qualified: set the referring namespace to |
| 4442 |
* NULL. |
| 4443 |
*/ |
| 4444 |
|
| 4445 |
resPtr->refNsPtr = NULL; |
| 4446 |
} else { |
| 4447 |
/* |
| 4448 |
* Get the current namespace. |
| 4449 |
*/ |
| 4450 |
|
| 4451 |
currNsPtr = iPtr->varFramePtr->nsPtr; |
| 4452 |
|
| 4453 |
resPtr->refNsPtr = currNsPtr; |
| 4454 |
resPtr->refNsId = currNsPtr->nsId; |
| 4455 |
resPtr->refNsCmdEpoch = currNsPtr->cmdRefEpoch; |
| 4456 |
} |
| 4457 |
} else { |
| 4458 |
TclFreeIntRep(objPtr); |
| 4459 |
objPtr->internalRep.twoPtrValue.ptr1 = NULL; |
| 4460 |
objPtr->internalRep.twoPtrValue.ptr2 = NULL; |
| 4461 |
objPtr->typePtr = &tclCmdNameType; |
| 4462 |
} |
| 4463 |
return TCL_OK; |
| 4464 |
} |
| 4465 |
|
| 4466 |
/* |
| 4467 |
*---------------------------------------------------------------------- |
| 4468 |
* |
| 4469 |
* Tcl_RepresentationCmd -- |
| 4470 |
* |
| 4471 |
* Implementation of the "tcl::unsupported::representation" command. |
| 4472 |
* |
| 4473 |
* Results: |
| 4474 |
* Reports the current representation (Tcl_Obj type) of its argument. |
| 4475 |
* |
| 4476 |
* Side effects: |
| 4477 |
* None. |
| 4478 |
* |
| 4479 |
*---------------------------------------------------------------------- |
| 4480 |
*/ |
| 4481 |
|
| 4482 |
int |
| 4483 |
Tcl_RepresentationCmd( |
| 4484 |
ClientData clientData, |
| 4485 |
Tcl_Interp *interp, |
| 4486 |
int objc, |
| 4487 |
Tcl_Obj *const objv[]) |
| 4488 |
{ |
| 4489 |
char ptrBuffer[2*TCL_INTEGER_SPACE+6]; |
| 4490 |
Tcl_Obj *descObj; |
| 4491 |
|
| 4492 |
if (objc != 2) { |
| 4493 |
Tcl_WrongNumArgs(interp, 1, objv, "value"); |
| 4494 |
return TCL_ERROR; |
| 4495 |
} |
| 4496 |
|
| 4497 |
/* |
| 4498 |
* Value is a bignum with a refcount of 14, object pointer at 0x12345678, |
| 4499 |
* internal representation 0x45671234:0x98765432, string representation |
| 4500 |
* "1872361827361287" |
| 4501 |
*/ |
| 4502 |
|
| 4503 |
snprintf(ptrBuffer, sizeof(ptrBuffer), "%p", (void *) objv[1]); |
| 4504 |
descObj = Tcl_ObjPrintf("value is a %s with a refcount of %d," |
| 4505 |
" object pointer at %s", |
| 4506 |
objv[1]->typePtr ? objv[1]->typePtr->name : "pure string", |
| 4507 |
objv[1]->refCount, ptrBuffer); |
| 4508 |
|
| 4509 |
/* |
| 4510 |
* This is a workaround to silence reports from `make valgrind` |
| 4511 |
* on 64-bit systems. The problem is that the test suite |
| 4512 |
* includes calling the [representation] command on values of |
| 4513 |
* &tclDoubleType. When these values are created, the "doubleValue" |
| 4514 |
* is set, but when the "twoPtrValue" is examined, its "ptr2" |
| 4515 |
* field has never been initialized. Since [representation] |
| 4516 |
* presents the value of the ptr2 value in its output, valgrind |
| 4517 |
* alerts about the read of uninitialized memory. |
| 4518 |
* |
| 4519 |
* The general problem with [representation], that it can read |
| 4520 |
* and report uninitialized fields, is still present. This is |
| 4521 |
* just the minimal workaround to silence one particular test. |
| 4522 |
*/ |
| 4523 |
|
| 4524 |
if ((sizeof(void *) > 4) && objv[1]->typePtr == &tclDoubleType) { |
| 4525 |
objv[1]->internalRep.twoPtrValue.ptr2 = NULL; |
| 4526 |
} |
| 4527 |
if (objv[1]->typePtr) { |
| 4528 |
snprintf(ptrBuffer, sizeof(ptrBuffer), "%p:%p", |
| 4529 |
(void *) objv[1]->internalRep.twoPtrValue.ptr1, |
| 4530 |
(void *) objv[1]->internalRep.twoPtrValue.ptr2); |
| 4531 |
Tcl_AppendPrintfToObj(descObj, ", internal representation %s", |
| 4532 |
ptrBuffer); |
| 4533 |
} |
| 4534 |
|
| 4535 |
if (objv[1]->bytes) { |
| 4536 |
Tcl_AppendToObj(descObj, ", string representation \"", -1); |
| 4537 |
Tcl_AppendLimitedToObj(descObj, objv[1]->bytes, objv[1]->length, |
| 4538 |
16, "..."); |
| 4539 |
Tcl_AppendToObj(descObj, "\"", -1); |
| 4540 |
} else { |
| 4541 |
Tcl_AppendToObj(descObj, ", no string representation", -1); |
| 4542 |
} |
| 4543 |
|
| 4544 |
Tcl_SetObjResult(interp, descObj); |
| 4545 |
return TCL_OK; |
| 4546 |
} |
| 4547 |
|
| 4548 |
/* |
| 4549 |
* Local Variables: |
| 4550 |
* mode: c |
| 4551 |
* c-basic-offset: 4 |
| 4552 |
* fill-column: 78 |
| 4553 |
* tab-width: 8 |
| 4554 |
* indent-tabs-mode: nil |
| 4555 |
* End: |
| 4556 |
*/ |