|
1 " |
|
2 COPYRIGHT (c) 1995 by Claus Gittinger |
|
3 All Rights Reserved |
|
4 |
|
5 This software is furnished under a license and may be used |
|
6 only in accordance with the terms of that license and with the |
|
7 inclusion of the above copyright notice. This software may not |
|
8 be provided or otherwise made available to, or used by, any |
|
9 other person. No title to or ownership of the software is |
|
10 hereby transferred. |
|
11 " |
|
12 |
|
13 Object subclass:#ExternalAddress |
|
14 instanceVariableNames:'address*' |
|
15 classVariableNames:'' |
|
16 poolDictionaries:'' |
|
17 category:'System-Support' |
|
18 ! |
|
19 |
|
20 !ExternalAddress class methodsFor:'documentation'! |
|
21 |
|
22 copyright |
|
23 " |
|
24 COPYRIGHT (c) 1995 by Claus Gittinger |
|
25 All Rights Reserved |
|
26 |
|
27 This software is furnished under a license and may be used |
|
28 only in accordance with the terms of that license and with the |
|
29 inclusion of the above copyright notice. This software may not |
|
30 be provided or otherwise made available to, or used by, any |
|
31 other person. No title to or ownership of the software is |
|
32 hereby transferred. |
|
33 " |
|
34 ! |
|
35 |
|
36 documentation |
|
37 " |
|
38 Instances of this class represent external (non-Smalltalk) addresses. |
|
39 They are only useful to represent handles as returned by C functions |
|
40 as smalltalk objects. For example, Window- or WidgetIDs (which are actually |
|
41 32 bit pointers) should be represented this way. |
|
42 (you should not use SmallIntegers for this, since they can only represent 31 |
|
43 bits; LargeIntegers could be used in theory, but it is not a very good style |
|
44 to do so, since it makes things a bit cryptic - having ExternalAddresses |
|
45 around makes things pretty clear in inspectors etc.). |
|
46 |
|
47 There is not much you can do with ExternalAddresses on the smalltalk level; |
|
48 creation/use is mostly in primitive C-code via |
|
49 __MKEXTERNALADDRESS(voidPtr) and __ExternalAddressVal(obj). |
|
50 |
|
51 ExternallAddresses are much like ExternalBytes - however, the latter |
|
52 allow you to access bytes via indexed at:/at:put: messages. |
|
53 ExternallAddresses do not allow such accesses (they are meant to remain |
|
54 anonymous). |
|
55 " |
|
56 ! |
|
57 |
|
58 examples |
|
59 " |
|
60 To pass some C-pointer from primitive code to smalltalk: |
|
61 |
|
62 ... |
|
63 RETURN (__MKEXTERNALADDRESS(foo)); |
|
64 |
|
65 pass it back to C and use it there: |
|
66 |
|
67 ... |
|
68 if (__isExternalAddress(anExternalAddress)) { |
|
69 ptr = __externalAddressVal(anExternalAddress)); |
|
70 } |
|
71 ... |
|
72 |
|
73 concrete example: |
|
74 |
|
75 test1 |
|
76 %{ |
|
77 static char foo[] = {'h', 'e' ,'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '\n'}; |
|
78 |
|
79 RETURN (__MKEXTERNALADDRESS(foo)); |
|
80 %} |
|
81 |
|
82 test2:anExternalAddress |
|
83 %{ |
|
84 if (__isExternalAddress(anExternalAddress)) { |
|
85 printf(__externalAddressVal(anExternalAddress)); |
|
86 RETURN (self); |
|
87 } |
|
88 %} |
|
89 " |
|
90 ! ! |
|
91 |
|
92 !ExternalAddress class methodsFor:'queries'! |
|
93 |
|
94 isBuiltInClass |
|
95 "known by the runtime system" |
|
96 |
|
97 ^ true |
|
98 ! ! |
|
99 |
|
100 !ExternalAddress methodsFor:'accessing'! |
|
101 |
|
102 address |
|
103 "return the start address as an integer" |
|
104 |
|
105 %{ /* NOCONTEXT */ |
|
106 |
|
107 unsigned addr; |
|
108 |
|
109 addr = (unsigned)_INST(address_); |
|
110 RETURN ( __MKUINT(addr)); |
|
111 %} |
|
112 ! ! |
|
113 |
|
114 !ExternalAddress methodsFor:'comparing'! |
|
115 |
|
116 = anExternalAddress |
|
117 "return true, if two externalAddress instance represent the same |
|
118 C pointer" |
|
119 |
|
120 %{ /* NOCONTEXT */ |
|
121 if (__isExternalAddress(anExternalAddress) |
|
122 && (__externalAddressVal(self) == __externalAddressVal(anExternalAddress))) { |
|
123 RETURN (true); |
|
124 } |
|
125 %}. |
|
126 ^ false |
|
127 ! |
|
128 |
|
129 hash |
|
130 "return a number useful for hashing" |
|
131 |
|
132 %{ /* NOCONTEXT */ |
|
133 unsigned addr = (unsigned) __externalAddressVal(self); |
|
134 |
|
135 RETURN (__MKSMALLINT(addr >> 2)); |
|
136 %} |
|
137 ! ! |
|
138 |
|
139 !ExternalAddress methodsFor:'converting'! |
|
140 |
|
141 asExternalBytes |
|
142 "return an ExternalBytes object pointing to where the receiver points to. |
|
143 Use of this is not recommended; primitives which return externalAddresses |
|
144 dont think that access to the memory is required/useful, while primitives |
|
145 which do think so should return an externalBytes instance right away." |
|
146 |
|
147 ^ ExternalBytes address:(self address) |
|
148 ! ! |
|
149 |
|
150 !ExternalAddress methodsFor:'printing & storing'! |
|
151 |
|
152 displayString |
|
153 ^ 'ExternalAddress at:' , (self address printStringRadix:16) |
|
154 ! ! |
|
155 |
|
156 !ExternalAddress class methodsFor:'documentation'! |
|
157 |
|
158 version |
|
159 ^ '$Header: /cvs/stx/stx/libbasic/ExternalAddress.st,v 1.1 1996-01-11 13:18:04 cg Exp $' |
|
160 ! ! |