| 1 |
'\" |
| 2 |
'\" Copyright (c) 1996-1997 Sun Microsystems, Inc. |
| 3 |
'\" |
| 4 |
'\" See the file "license.terms" for information on usage and redistribution |
| 5 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 6 |
'\" |
| 7 |
.TH Tcl_Obj 3 8.5 Tcl "Tcl Library Procedures" |
| 8 |
.so man.macros |
| 9 |
.BS |
| 10 |
.SH NAME |
| 11 |
Tcl_NewObj, Tcl_DuplicateObj, Tcl_IncrRefCount, Tcl_DecrRefCount, Tcl_IsShared, Tcl_InvalidateStringRep \- manipulate Tcl values |
| 12 |
.SH SYNOPSIS |
| 13 |
.nf |
| 14 |
\fB#include <tcl.h>\fR |
| 15 |
.sp |
| 16 |
Tcl_Obj * |
| 17 |
\fBTcl_NewObj\fR() |
| 18 |
.sp |
| 19 |
Tcl_Obj * |
| 20 |
\fBTcl_DuplicateObj\fR(\fIobjPtr\fR) |
| 21 |
.sp |
| 22 |
\fBTcl_IncrRefCount\fR(\fIobjPtr\fR) |
| 23 |
.sp |
| 24 |
\fBTcl_DecrRefCount\fR(\fIobjPtr\fR) |
| 25 |
.sp |
| 26 |
int |
| 27 |
\fBTcl_IsShared\fR(\fIobjPtr\fR) |
| 28 |
.sp |
| 29 |
\fBTcl_InvalidateStringRep\fR(\fIobjPtr\fR) |
| 30 |
.SH ARGUMENTS |
| 31 |
.AS Tcl_Obj *objPtr |
| 32 |
.AP Tcl_Obj *objPtr in |
| 33 |
Points to a value; |
| 34 |
must have been the result of a previous call to \fBTcl_NewObj\fR. |
| 35 |
.BE |
| 36 |
.SH INTRODUCTION |
| 37 |
.PP |
| 38 |
This man page presents an overview of Tcl values (called \fBTcl_Obj\fRs for |
| 39 |
historical reasons) and how they are used. |
| 40 |
It also describes generic procedures for managing Tcl values. |
| 41 |
These procedures are used to create and copy values, |
| 42 |
and increment and decrement the count of references (pointers) to values. |
| 43 |
The procedures are used in conjunction with ones |
| 44 |
that operate on specific types of values such as |
| 45 |
\fBTcl_GetIntFromObj\fR and \fBTcl_ListObjAppendElement\fR. |
| 46 |
The individual procedures are described along with the data structures |
| 47 |
they manipulate. |
| 48 |
.PP |
| 49 |
Tcl's \fIdual-ported\fR values provide a general-purpose mechanism |
| 50 |
for storing and exchanging Tcl values. |
| 51 |
They largely replace the use of strings in Tcl. |
| 52 |
For example, they are used to store variable values, |
| 53 |
command arguments, command results, and scripts. |
| 54 |
Tcl values behave like strings but also hold an internal representation |
| 55 |
that can be manipulated more efficiently. |
| 56 |
For example, a Tcl list is now represented as a value |
| 57 |
that holds the list's string representation |
| 58 |
as well as an array of pointers to the values for each list element. |
| 59 |
Dual-ported values avoid most runtime type conversions. |
| 60 |
They also improve the speed of many operations |
| 61 |
since an appropriate representation is immediately available. |
| 62 |
The compiler itself uses Tcl values to |
| 63 |
cache the instruction bytecodes resulting from compiling scripts. |
| 64 |
.PP |
| 65 |
The two representations are a cache of each other and are computed lazily. |
| 66 |
That is, each representation is only computed when necessary, |
| 67 |
it is computed from the other representation, |
| 68 |
and, once computed, it is saved. |
| 69 |
In addition, a change in one representation invalidates the other one. |
| 70 |
As an example, a Tcl program doing integer calculations can |
| 71 |
operate directly on a variable's internal machine integer |
| 72 |
representation without having to constantly convert |
| 73 |
between integers and strings. |
| 74 |
Only when it needs a string representing the variable's value, |
| 75 |
say to print it, |
| 76 |
will the program regenerate the string representation from the integer. |
| 77 |
Although values contain an internal representation, |
| 78 |
their semantics are defined in terms of strings: |
| 79 |
an up-to-date string can always be obtained, |
| 80 |
and any change to the value will be reflected in that string |
| 81 |
when the value's string representation is fetched. |
| 82 |
Because of this representation invalidation and regeneration, |
| 83 |
it is dangerous for extension writers to access |
| 84 |
\fBTcl_Obj\fR fields directly. |
| 85 |
It is better to access Tcl_Obj information using |
| 86 |
procedures like \fBTcl_GetStringFromObj\fR and \fBTcl_GetString\fR. |
| 87 |
.PP |
| 88 |
Values are allocated on the heap |
| 89 |
and are referenced using a pointer to their \fBTcl_Obj\fR structure. |
| 90 |
Values are shared as much as possible. |
| 91 |
This significantly reduces storage requirements |
| 92 |
because some values such as long lists are very large. |
| 93 |
Also, most Tcl values are only read and never modified. |
| 94 |
This is especially true for procedure arguments, |
| 95 |
which can be shared between the caller and the called procedure. |
| 96 |
Assignment and argument binding is done by |
| 97 |
simply assigning a pointer to the value. |
| 98 |
Reference counting is used to determine when it is safe to |
| 99 |
reclaim a value's storage. |
| 100 |
.PP |
| 101 |
Tcl values are typed. |
| 102 |
A value's internal representation is controlled by its type. |
| 103 |
Several types are predefined in the Tcl core |
| 104 |
including integer, double, list, and bytecode. |
| 105 |
Extension writers can extend the set of types |
| 106 |
by defining their own \fBTcl_ObjType\fR structs. |
| 107 |
.SH "THE TCL_OBJ STRUCTURE" |
| 108 |
.PP |
| 109 |
Each Tcl value is represented by a \fBTcl_Obj\fR structure |
| 110 |
which is defined as follows. |
| 111 |
.PP |
| 112 |
.CS |
| 113 |
typedef struct Tcl_Obj { |
| 114 |
int \fIrefCount\fR; |
| 115 |
char *\fIbytes\fR; |
| 116 |
int \fIlength\fR; |
| 117 |
const Tcl_ObjType *\fItypePtr\fR; |
| 118 |
union { |
| 119 |
long \fIlongValue\fR; |
| 120 |
double \fIdoubleValue\fR; |
| 121 |
void *\fIotherValuePtr\fR; |
| 122 |
Tcl_WideInt \fIwideValue\fR; |
| 123 |
struct { |
| 124 |
void *\fIptr1\fR; |
| 125 |
void *\fIptr2\fR; |
| 126 |
} \fItwoPtrValue\fR; |
| 127 |
struct { |
| 128 |
void *\fIptr\fR; |
| 129 |
unsigned long \fIvalue\fR; |
| 130 |
} \fIptrAndLongRep\fR; |
| 131 |
} \fIinternalRep\fR; |
| 132 |
} \fBTcl_Obj\fR; |
| 133 |
.CE |
| 134 |
.PP |
| 135 |
The \fIbytes\fR and the \fIlength\fR members together hold |
| 136 |
a value's UTF-8 string representation, |
| 137 |
which is a \fIcounted string\fR not containing null bytes (UTF-8 null |
| 138 |
characters should be encoded as a two byte sequence: 192, 128.) |
| 139 |
\fIbytes\fR points to the first byte of the string representation. |
| 140 |
The \fIlength\fR member gives the number of bytes. |
| 141 |
The byte array must always have a null byte after the last data byte, |
| 142 |
at offset \fIlength\fR; |
| 143 |
this allows string representations |
| 144 |
to be treated as conventional null-terminated C strings. |
| 145 |
C programs use \fBTcl_GetStringFromObj\fR and \fBTcl_GetString\fR to get |
| 146 |
a value's string representation. |
| 147 |
If \fIbytes\fR is NULL, |
| 148 |
the string representation is invalid. |
| 149 |
.PP |
| 150 |
A value's type manages its internal representation. |
| 151 |
The member \fItypePtr\fR points to the Tcl_ObjType structure |
| 152 |
that describes the type. |
| 153 |
If \fItypePtr\fR is NULL, |
| 154 |
the internal representation is invalid. |
| 155 |
.PP |
| 156 |
The \fIinternalRep\fR union member holds |
| 157 |
a value's internal representation. |
| 158 |
This is either a (long) integer, a double-precision floating-point number, |
| 159 |
a pointer to a value containing additional information |
| 160 |
needed by the value's type to represent the value, a Tcl_WideInt |
| 161 |
integer, two arbitrary pointers, or a pair made up of an unsigned long |
| 162 |
integer and a pointer. |
| 163 |
.PP |
| 164 |
The \fIrefCount\fR member is used to tell when it is safe to free |
| 165 |
a value's storage. |
| 166 |
It holds the count of active references to the value. |
| 167 |
Maintaining the correct reference count is a key responsibility |
| 168 |
of extension writers. |
| 169 |
Reference counting is discussed below |
| 170 |
in the section \fBSTORAGE MANAGEMENT OF VALUES\fR. |
| 171 |
.PP |
| 172 |
Although extension writers can directly access |
| 173 |
the members of a Tcl_Obj structure, |
| 174 |
it is much better to use the appropriate procedures and macros. |
| 175 |
For example, extension writers should never |
| 176 |
read or update \fIrefCount\fR directly; |
| 177 |
they should use macros such as |
| 178 |
\fBTcl_IncrRefCount\fR and \fBTcl_IsShared\fR instead. |
| 179 |
.PP |
| 180 |
A key property of Tcl values is that they hold two representations. |
| 181 |
A value typically starts out containing only a string representation: |
| 182 |
it is untyped and has a NULL \fItypePtr\fR. |
| 183 |
A value containing an empty string or a copy of a specified string |
| 184 |
is created using \fBTcl_NewObj\fR or \fBTcl_NewStringObj\fR respectively. |
| 185 |
A value's string value is gotten with |
| 186 |
\fBTcl_GetStringFromObj\fR or \fBTcl_GetString\fR |
| 187 |
and changed with \fBTcl_SetStringObj\fR. |
| 188 |
If the value is later passed to a procedure like \fBTcl_GetIntFromObj\fR |
| 189 |
that requires a specific internal representation, |
| 190 |
the procedure will create one and set the value's \fItypePtr\fR. |
| 191 |
The internal representation is computed from the string representation. |
| 192 |
A value's two representations are duals of each other: |
| 193 |
changes made to one are reflected in the other. |
| 194 |
For example, \fBTcl_ListObjReplace\fR will modify a value's |
| 195 |
internal representation and the next call to \fBTcl_GetStringFromObj\fR |
| 196 |
or \fBTcl_GetString\fR will reflect that change. |
| 197 |
.PP |
| 198 |
Representations are recomputed lazily for efficiency. |
| 199 |
A change to one representation made by a procedure |
| 200 |
such as \fBTcl_ListObjReplace\fR is not reflected immediately |
| 201 |
in the other representation. |
| 202 |
Instead, the other representation is marked invalid |
| 203 |
so that it is only regenerated if it is needed later. |
| 204 |
Most C programmers never have to be concerned with how this is done |
| 205 |
and simply use procedures such as \fBTcl_GetBooleanFromObj\fR or |
| 206 |
\fBTcl_ListObjIndex\fR. |
| 207 |
Programmers that implement their own value types |
| 208 |
must check for invalid representations |
| 209 |
and mark representations invalid when necessary. |
| 210 |
The procedure \fBTcl_InvalidateStringRep\fR is used |
| 211 |
to mark a value's string representation invalid and to |
| 212 |
free any storage associated with the old string representation. |
| 213 |
.PP |
| 214 |
Values usually remain one type over their life, |
| 215 |
but occasionally a value must be converted from one type to another. |
| 216 |
For example, a C program might build up a string in a value |
| 217 |
with repeated calls to \fBTcl_AppendToObj\fR, |
| 218 |
and then call \fBTcl_ListObjIndex\fR to extract a list element from |
| 219 |
the value. |
| 220 |
The same value holding the same string value |
| 221 |
can have several different internal representations |
| 222 |
at different times. |
| 223 |
Extension writers can also force a value to be converted from one type |
| 224 |
to another using the \fBTcl_ConvertToType\fR procedure. |
| 225 |
Only programmers that create new value types need to be concerned |
| 226 |
about how this is done. |
| 227 |
A procedure defined as part of the value type's implementation |
| 228 |
creates a new internal representation for a value |
| 229 |
and changes its \fItypePtr\fR. |
| 230 |
See the man page for \fBTcl_RegisterObjType\fR |
| 231 |
to see how to create a new value type. |
| 232 |
.SH "EXAMPLE OF THE LIFETIME OF A VALUE" |
| 233 |
.PP |
| 234 |
As an example of the lifetime of a value, |
| 235 |
consider the following sequence of commands: |
| 236 |
.PP |
| 237 |
.CS |
| 238 |
\fBset x 123\fR |
| 239 |
.CE |
| 240 |
.PP |
| 241 |
This assigns to \fIx\fR an untyped value whose |
| 242 |
\fIbytes\fR member points to \fB123\fR and \fIlength\fR member contains 3. |
| 243 |
The value's \fItypePtr\fR member is NULL. |
| 244 |
.PP |
| 245 |
.CS |
| 246 |
\fBputs "x is $x"\fR |
| 247 |
.CE |
| 248 |
.PP |
| 249 |
\fIx\fR's string representation is valid (since \fIbytes\fR is non-NULL) |
| 250 |
and is fetched for the command. |
| 251 |
.PP |
| 252 |
.CS |
| 253 |
\fBincr x\fR |
| 254 |
.CE |
| 255 |
.PP |
| 256 |
The \fBincr\fR command first gets an integer from \fIx\fR's value |
| 257 |
by calling \fBTcl_GetIntFromObj\fR. |
| 258 |
This procedure checks whether the value is already an integer value. |
| 259 |
Since it is not, it converts the value |
| 260 |
by setting the value's internal representation |
| 261 |
to the integer \fB123\fR |
| 262 |
and setting the value's \fItypePtr\fR |
| 263 |
to point to the integer Tcl_ObjType structure. |
| 264 |
Both representations are now valid. |
| 265 |
\fBincr\fR increments the value's integer internal representation |
| 266 |
then invalidates its string representation |
| 267 |
(by calling \fBTcl_InvalidateStringRep\fR) |
| 268 |
since the string representation |
| 269 |
no longer corresponds to the internal representation. |
| 270 |
.PP |
| 271 |
.CS |
| 272 |
\fBputs "x is now $x"\fR |
| 273 |
.CE |
| 274 |
.PP |
| 275 |
The string representation of \fIx\fR's value is needed |
| 276 |
and is recomputed. |
| 277 |
The string representation is now \fB124\fR |
| 278 |
and both representations are again valid. |
| 279 |
.SH "STORAGE MANAGEMENT OF VALUES" |
| 280 |
.PP |
| 281 |
Tcl values are allocated on the heap and are shared as much as possible |
| 282 |
to reduce storage requirements. |
| 283 |
Reference counting is used to determine when a value is |
| 284 |
no longer needed and can safely be freed. |
| 285 |
A value just created by \fBTcl_NewObj\fR or \fBTcl_NewStringObj\fR |
| 286 |
has \fIrefCount\fR 0. |
| 287 |
The macro \fBTcl_IncrRefCount\fR increments the reference count |
| 288 |
when a new reference to the value is created. |
| 289 |
The macro \fBTcl_DecrRefCount\fR decrements the count |
| 290 |
when a reference is no longer needed and, |
| 291 |
if the value's reference count drops to zero, frees its storage. |
| 292 |
A value shared by different code or data structures has |
| 293 |
\fIrefCount\fR greater than 1. |
| 294 |
Incrementing a value's reference count ensures that |
| 295 |
it will not be freed too early or have its value change accidentally. |
| 296 |
.PP |
| 297 |
As an example, the bytecode interpreter shares argument values |
| 298 |
between calling and called Tcl procedures to avoid having to copy values. |
| 299 |
It assigns the call's argument values to the procedure's |
| 300 |
formal parameter variables. |
| 301 |
In doing so, it calls \fBTcl_IncrRefCount\fR to increment |
| 302 |
the reference count of each argument since there is now a new |
| 303 |
reference to it from the formal parameter. |
| 304 |
When the called procedure returns, |
| 305 |
the interpreter calls \fBTcl_DecrRefCount\fR to decrement |
| 306 |
each argument's reference count. |
| 307 |
When a value's reference count drops less than or equal to zero, |
| 308 |
\fBTcl_DecrRefCount\fR reclaims its storage. |
| 309 |
Most command procedures do not have to be concerned about |
| 310 |
reference counting since they use a value's value immediately |
| 311 |
and do not retain a pointer to the value after they return. |
| 312 |
However, if they do retain a pointer to a value in a data structure, |
| 313 |
they must be careful to increment its reference count |
| 314 |
since the retained pointer is a new reference. |
| 315 |
.PP |
| 316 |
Command procedures that directly modify values |
| 317 |
such as those for \fBlappend\fR and \fBlinsert\fR must be careful to |
| 318 |
copy a shared value before changing it. |
| 319 |
They must first check whether the value is shared |
| 320 |
by calling \fBTcl_IsShared\fR. |
| 321 |
If the value is shared they must copy the value |
| 322 |
by using \fBTcl_DuplicateObj\fR; |
| 323 |
this returns a new duplicate of the original value |
| 324 |
that has \fIrefCount\fR 0. |
| 325 |
If the value is not shared, |
| 326 |
the command procedure |
| 327 |
.QW "owns" |
| 328 |
the value and can safely modify it directly. |
| 329 |
For example, the following code appears in the command procedure |
| 330 |
that implements \fBlinsert\fR. |
| 331 |
This procedure modifies the list value passed to it in \fIobjv[1]\fR |
| 332 |
by inserting \fIobjc-3\fR new elements before \fIindex\fR. |
| 333 |
.PP |
| 334 |
.CS |
| 335 |
listPtr = objv[1]; |
| 336 |
if (\fBTcl_IsShared\fR(listPtr)) { |
| 337 |
listPtr = \fBTcl_DuplicateObj\fR(listPtr); |
| 338 |
} |
| 339 |
result = Tcl_ListObjReplace(interp, listPtr, index, 0, |
| 340 |
(objc-3), &(objv[3])); |
| 341 |
.CE |
| 342 |
.PP |
| 343 |
As another example, \fBincr\fR's command procedure |
| 344 |
must check whether the variable's value is shared before |
| 345 |
incrementing the integer in its internal representation. |
| 346 |
If it is shared, it needs to duplicate the value |
| 347 |
in order to avoid accidentally changing values in other data structures. |
| 348 |
.SH "SEE ALSO" |
| 349 |
Tcl_ConvertToType(3), Tcl_GetIntFromObj(3), Tcl_ListObjAppendElement(3), Tcl_ListObjIndex(3), Tcl_ListObjReplace(3), Tcl_RegisterObjType(3) |
| 350 |
.SH KEYWORDS |
| 351 |
internal representation, value, value creation, value type, |
| 352 |
reference counting, string representation, type conversion |