OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [tcl/] [doc/] [Object.3] - Blame information for rev 1771

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.