-
-
Notifications
You must be signed in to change notification settings - Fork 343
/
ExternalAddress.extension.st
558 lines (456 loc) · 17.3 KB
/
ExternalAddress.extension.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
Extension { #name : #ExternalAddress }
{ #category : #'*UnifiedFFI-private' }
ExternalAddress >> adoptAddress: anExternalAddress [
anExternalAddress = 0 ifTrue: [ ^ self beNull ].
1 to: self size do: [ :index |
self basicAt: index put: (anExternalAddress at: index) ]
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> asExternalAddress [
^ self
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> autoRelease [
^ self class finalizationRegistry add: self
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> boolean8AtOffset: zeroBasedOffset [
<primitive: 630>
^ (self integerAt: zeroBasedOffset + 1 size: 1 signed: false) ~= 0
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> boolean8AtOffset: zeroBasedOffset put: value [
<primitive: 645>
^ self integerAt: zeroBasedOffset + 1 put: (value ifTrue:[1] ifFalse:[0]) size: 1 signed: false
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> booleanAt: byteIndex [
"This method is duplicated in this subclass with the purpose of ensuring a monomorphic inline cache in the following message send."
^ self boolean8AtOffset: byteIndex - 1
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> booleanAt: byteIndex put: value [
"This method is duplicated in this subclass with the purpose of ensuring a monomorphic inline cache in the following message send."
^ self boolean8AtOffset: byteIndex - 1 put: value
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> char16AtOffset: zeroBasedOffset [
<primitive: 641>
^ (self integerAt: zeroBasedOffset + 1 size: 2 signed: false) asCharacter
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> char16AtOffset: zeroBasedOffset put: value [
<primitive: 656>
^ self integerAt: zeroBasedOffset + 1 put: value asInteger size: 2 signed: false
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> char32AtOffset: zeroBasedOffset [
<primitive: 642>
^ (self integerAt: zeroBasedOffset + 1 size: 4 signed: false) asCharacter
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> char32AtOffset: zeroBasedOffset put: value [
<primitive: 657>
^ self integerAt: zeroBasedOffset + 1 put: value asInteger size: 4 signed: false
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> char8AtOffset: zeroBasedOffset [
<primitive: 640>
^ (self integerAt: zeroBasedOffset + 1 size: 1 signed: false) asCharacter
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> char8AtOffset: zeroBasedOffset put: value [
<primitive: 655>
^ self integerAt: zeroBasedOffset + 1 put: value asInteger size: 1 signed: false
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> copyFrom: startIndex to: endIndex [
| result size |
size := endIndex - startIndex + 1.
result := ByteArray new: size.
LibC memCopy: (self + (startIndex - 1)) to: result size: size.
^ result
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> copyFromOffset: startZeroBasedOffset to: endZeroBasedOffset [
^ self copyFrom: startZeroBasedOffset + 1 to: endZeroBasedOffset + 1
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> doubleAt: byteIndex [
"This method is duplicated in this subclass with the purpose of ensuring a monomorphic inline cache in the following message send."
^ self float64AtOffset: byteIndex - 1
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> doubleAt: byteIndex put: value [
"This method is duplicated in this subclass with the purpose of ensuring a monomorphic inline cache in the following message send."
^ self float64AtOffset: byteIndex - 1 put: value
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> float32AtOffset: zeroBasedOffset [
<primitive: 643>
^ FFIBackend current on: self float32At: zeroBasedOffset + 1
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> float32AtOffset: zeroBasedOffset put: value [
<primitive: 658>
^ FFIBackend current on: self float32At: zeroBasedOffset + 1 put: value
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> float64AtOffset: zeroBasedOffset [
<primitive: 644>
^ FFIBackend current on: self float64At: zeroBasedOffset + 1
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> float64AtOffset: zeroBasedOffset put: value [
<primitive: 659>
^ FFIBackend current
on: self
float64At: zeroBasedOffset + 1
put: value
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> floatAt: byteIndex [
"This method is duplicated in this subclass with the purpose of ensuring a monomorphic inline cache in the following message send."
^ self float32AtOffset: byteIndex - 1
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> floatAt: byteIndex put: value [
"This method is duplicated in this subclass with the purpose of ensuring a monomorphic inline cache in the following message send."
^ self float32AtOffset: byteIndex - 1 put: value
]
{ #category : #'*UnifiedFFI' }
ExternalAddress class >> fromAddress: aNumber [
"Answers an external address who points to aNumber"
^ self new fromAddress: aNumber
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> fromAddress: aNumber [
^ self fromInteger: aNumber
]
{ #category : #'*UnifiedFFI' }
ExternalAddress class >> fromString: aString [
| result |
result := self allocate: aString size + 1.
(self assert: result notNil).
result writeString: aString.
result unsignedByteAt: aString size + 1 put: 0.
^ result
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> getHandle [
^ self
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> int16AtOffset: zeroBasedOffset [
<primitive: 634>
^ self integerAt: zeroBasedOffset + 1 size: 2 signed: true
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> int16AtOffset: zeroBasedOffset put: value [
<primitive: 649>
^ self integerAt: zeroBasedOffset + 1 put: value size: 2 signed: true
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> int32AtOffset: zeroBasedOffset [
<primitive: 636>
^ self integerAt: zeroBasedOffset + 1 size: 4 signed: true
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> int32AtOffset: zeroBasedOffset put: value [
<primitive: 651>
^ self integerAt: zeroBasedOffset + 1 put: value size: 4 signed: true
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> int64AtOffset: zeroBasedOffset [
<primitive: 638>
^ self integerAt: zeroBasedOffset + 1 size: 8 signed: true
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> int64AtOffset: zeroBasedOffset put: value [
<primitive: 653>
^ self integerAt: zeroBasedOffset + 1 put: value size: 8 signed: true
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> int8AtOffset: zeroBasedOffset [
<primitive: 632>
^ self integerAt: zeroBasedOffset + 1 size: 1 signed: true
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> int8AtOffset: zeroBasedOffset put: value [
<primitive: 647>
^ self integerAt: zeroBasedOffset + 1 put: value size: 1 signed: true
]
{ #category : #'*UnifiedFFI' }
ExternalAddress class >> loadSymbol: moduleSymbol [
"Load global symbol (one that you can find in current VM binary)"
^ self
loadSymbol: moduleSymbol
module: nil
]
{ #category : #'*UnifiedFFI' }
ExternalAddress class >> loadSymbol: moduleSymbol from: moduleNameOrClass [
^ self
loadSymbol: moduleSymbol
module: moduleNameOrClass asFFILibrary libraryName.
]
{ #category : #'*UnifiedFFI' }
ExternalAddress class >> newPointer [
^ self allocate: self wordSize
]
{ #category : #'*UnifiedFFI' }
ExternalAddress class >> null [
^ self new
]
{ #category : #'*UnifiedFFI-private' }
ExternalAddress >> packToArity: arity [
"This will 'pack' a pointer to a certain arity.
What does this means? Let's say I have a method who will be called with a ** arity (that's 'arity 2'):
method: aPointer
self ffiCall: #( method ( MyExternalObjectClass **aPointer) )
This usually means that method will put a pointer in the address of aPointer.
And what I actually want is this pointer. So I do Something like this:
p := MyExternalObjectClass new.
self mehod: p.
And I expect to have 'p' correctly initialised on return.
Well... tha's not so simple :)
When compiling #method:, UnifiedFFI needs to first 'pack' the pointer (which means to
take a pointer of a pointer of a pointer... as many pointers as arity solicited), and then,
after call, it needs to 'unpack' all this pointers (see #unpackFromArity: method)
"
| rolledPointer |
rolledPointer := self.
"Start in 2 because first pointer is self"
2 to: arity do: [ :index | rolledPointer := rolledPointer pointerAutoRelease ].
^ rolledPointer
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> pointer [
"Answers a pointer to the this address.
Sometimes, an ExternalAddress represents a pointer, so you do not need this, but some others
it represents just an address. Yes, I know, difference is subtle.
Take this as an example:
SQSurfaceDispatch externalNew.
This will answer an instance if FFIExternalStructure with an ExternalAddress as handle, but
this handle WILL NOT represent a pointer address, just an address.
This is why, later in a call where I use the surface:
AthensCairoSurface>>#initializeForNewSession
... I need to keep a pointer to the surface, just the external address is not enough."
^ (ExternalAddress allocate: self size)
pointerAt: 1 put: self;
yourself
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> pointerAt: byteIndex [
"This method is duplicated in this subclass with the purpose of ensuring a monomorphic inline cache in the following message send."
^ self pointerAtOffset: byteIndex - 1
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> pointerAt: byteIndex put: value [
"This method is duplicated in this subclass with the purpose of ensuring a monomorphic inline cache in the following message send."
^ self pointerAtOffset: byteIndex - 1 put: value
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> pointerAtOffset: zeroBasedOffset [
<primitive: 639>
"Answer a pointer object stored at the given byte address"
| addr |
addr := ExternalAddress new.
1 to: ExternalAddress wordSize do:
[:i|
addr basicAt: i put: (self unsignedByteAt: zeroBasedOffset+i)].
^addr
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> pointerAtOffset: zeroBasedOffset put: value [
<primitive: 654>
"Store a pointer object at the given byte address"
value isExternalAddress ifFalse:
[^self error:'Only external addresses can be stored'].
1 to: ExternalAddress wordSize do:
[:i|
self unsignedByteAt: zeroBasedOffset+i put: (value basicAt: i)].
^value
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> pointerAutoRelease [
"Same as #pointer (see its comment for detals), but contents are garbage collected automatically"
^ self pointer autoRelease
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> readArrayOf: aType until: aBlock [
"Reads an array of aType until aBlock returns true.
this is an util to extract arrays from answers style char ** or int*, etc.
Example:
someAddress readArrayOf: #uint32 until: [ :each | each isZero ].
someAddress readArrayOf: #'void *' until: [ :each | each isNull ].
"
| externalType |
"resolve type if needed"
externalType := aType isString
ifTrue: [ FFIExternalType resolveType: aType ]
ifFalse: [ aType ].
"then build the array"
^ Array
streamContents: [ :array |
| address last count |
address := self.
count := 0.
[ address isNull
or: [ last := externalType handle: address at: 1.
count := count + 1.
aBlock cull: last cull: count ] ]
whileFalse: [ array nextPut: last.
address := address + externalType typeSize ] ]
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> replaceFrom: start to: stop with: replacement startingAt: repStart [
| dstAddress srcAddress repSize |
self flag: #pharoTodo. "Maybe replace it with a primitive (like the one at Alien)?"
repSize := stop - start + 1.
dstAddress := start = 1
ifTrue: [ self ]
ifFalse: [ self + (start - 1) ].
srcAddress := repStart = 1
ifTrue: [ replacement ]
ifFalse: [ replacement copyFrom: repStart to: repStart + repSize ].
LibC memCopy: srcAddress to: dstAddress size: repSize
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> signedCharAt: byteOffset [
^ self char8AtOffset: byteOffset - 1
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> signedCharAt: byteIndex put: aCharacter [
^ self char8AtOffset: byteIndex - 1 put: aCharacter
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> signedLongAt: byteIndex [
^ self int32AtOffset: byteIndex - 1
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> signedLongAt: byteIndex put: value [
^ self int32AtOffset: byteIndex - 1 put: value
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> signedLongLongAt: byteIndex [
^ self int64AtOffset: byteIndex - 1
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> signedLongLongAt: byteIndex put: value [
^ self int64AtOffset: byteIndex - 1 put: value
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> signedShortAt: byteIndex [
^ self int16AtOffset: byteIndex - 1
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> signedShortAt: byteIndex put: value [
^ self int16AtOffset: byteIndex - 1 put: value
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> uint16AtOffset: zeroBasedOffset [
<primitive: 633>
^ self integerAt: zeroBasedOffset + 1 size: 2 signed: false
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> uint16AtOffset: zeroBasedOffset put: value [
<primitive: 648>
^ self integerAt: zeroBasedOffset + 1 put: value size: 2 signed: false
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> uint32AtOffset: zeroBasedOffset [
<primitive: 635>
^ self integerAt: zeroBasedOffset + 1 size: 4 signed: false
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> uint32AtOffset: zeroBasedOffset put: value [
<primitive: 650>
^ self integerAt: zeroBasedOffset + 1 put: value size: 4 signed: false
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> uint64AtOffset: zeroBasedOffset [
<primitive: 637>
^ self integerAt: zeroBasedOffset + 1 size: 8 signed: false
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> uint64AtOffset: zeroBasedOffset put: value [
<primitive: 652>
^ self integerAt: zeroBasedOffset + 1 put: value size: 8 signed: false
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> uint8AtOffset: zeroBasedOffset [
<primitive: 631>
^ self integerAt: zeroBasedOffset + 1 size: 1 signed: false
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> uint8AtOffset: zeroBasedOffset put: value [
<primitive: 646>
^ self integerAt: zeroBasedOffset + 1 put: value size: 1 signed: false
]
{ #category : #'*UnifiedFFI-private' }
ExternalAddress >> unpackFromArity: arity [
"This will 'unpack' a pointer from a certain arity. See #unpackToArity: for a better explanation."
| rolledPointer |
rolledPointer := self.
"Start in 2 because first pointer is self"
2 to: arity do: [ :index | rolledPointer := rolledPointer pointerAt: 1 ].
^ rolledPointer
]
{ #category : #'*UnifiedFFI-private' }
ExternalAddress >> unpackHandleFromArity: arity [
^ (self unpackFromArity: arity) unsignedLongAt: 1
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> unsignedByteAt: byteIndex [
"This method is duplicated in this subclass with the purpose of ensuring a monomorphic inline cache in the following message send."
^ self uint8AtOffset: byteIndex - 1
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> unsignedByteAt: byteIndex put: value [
"This method is duplicated in this subclass with the purpose of ensuring a monomorphic inline cache in the following message send."
^ self uint8AtOffset: byteIndex - 1 put: value
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> unsignedCharAt: byteIndex [
"This method is duplicated in this subclass with the purpose of ensuring a monomorphic inline cache in the following message send."
^ self char8AtOffset: byteIndex - 1
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> unsignedCharAt: byteIndex put: aCharacter [
"This method is duplicated in this subclass with the purpose of ensuring a monomorphic inline cache in the following message send."
^ self char8AtOffset: byteIndex - 1 put: aCharacter
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> unsignedLongAt: byteIndex [
"This method is duplicated in this subclass with the purpose of ensuring a monomorphic inline cache in the following message send."
^ self uint32AtOffset: byteIndex - 1
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> unsignedLongAt: byteIndex put: value [
"This method is duplicated in this subclass with the purpose of ensuring a monomorphic inline cache in the following message send."
^ self uint32AtOffset: byteIndex - 1 put: value
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> unsignedLongLongAt: byteIndex [
"This method is duplicated in this subclass with the purpose of ensuring a monomorphic inline cache in the following message send."
^ self uint64AtOffset: byteIndex - 1
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> unsignedLongLongAt: byteIndex put: value [
"This method is duplicated in this subclass with the purpose of ensuring a monomorphic inline cache in the following message send."
^ self uint64AtOffset: byteIndex - 1 put: value
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> unsignedShortAt: byteIndex [
"This method is duplicated in this subclass with the purpose of ensuring a monomorphic inline cache in the following message send."
^ self uint16AtOffset: byteIndex - 1
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> unsignedShortAt: byteIndex put: value [
"This method is duplicated in this subclass with the purpose of ensuring a monomorphic inline cache in the following message send."
^ self uint16AtOffset: byteIndex - 1 put: value
]
{ #category : #'*UnifiedFFI' }
ExternalAddress >> value [
^ self asInteger
]