Skip to content

Commit 139ad7a

Browse files
committed
added ArrayU8/U16 and String ffi traits that use bare ffi
1 parent 31de27b commit 139ad7a

File tree

3 files changed

+701
-0
lines changed

3 files changed

+701
-0
lines changed
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
Trait {
2+
#name : #TGtWithBareArrayBoxU16,
3+
#category : #'GToolkit-Boxer-New'
4+
}
5+
6+
{ #category : #'examples - array u16' }
7+
TGtWithBareArrayBoxU16 classSide >> exampleArrayU16AsByteArray [
8+
<gtExample>
9+
| aDefaultValue anArray aByteArray|
10+
11+
aDefaultValue := 16.
12+
anArray := self uniqueInstance
13+
newArrayU16: 1
14+
withAll: aDefaultValue.
15+
16+
aByteArray := anArray asByteArray.
17+
self assert: aByteArray equals: (ByteArray withAll: { aDefaultValue . 0 }).
18+
"the returned byte array must not be pinned in memory, since it is an in-image copy"
19+
self assert: aByteArray isPinnedInMemory not.
20+
21+
^ aByteArray
22+
]
23+
24+
{ #category : #'examples - array u16' }
25+
TGtWithBareArrayBoxU16 classSide >> exampleArrayU16At [
26+
<gtExample>
27+
| anArray |
28+
29+
anArray := self exampleArrayU16NewOfSizeWithAll.
30+
self assert: (anArray at: 1) equals: self exampleArrayU16DefaultValue.
31+
^ anArray
32+
]
33+
34+
{ #category : #'examples - array u16' }
35+
TGtWithBareArrayBoxU16 classSide >> exampleArrayU16AtPut [
36+
<gtExample>
37+
| anArray aNewValue |
38+
39+
aNewValue := self exampleArrayU16DefaultValue.
40+
41+
anArray := self exampleArrayU16NewOfSizeWithAll.
42+
anArray at: 1 put: aNewValue.
43+
self assert: (anArray at: 1) equals: aNewValue.
44+
^ anArray
45+
]
46+
47+
{ #category : #'examples - array u16' }
48+
TGtWithBareArrayBoxU16 classSide >> exampleArrayU16DefaultValue [
49+
<gtExample>
50+
51+
^ 0
52+
]
53+
54+
{ #category : #'examples - array u16' }
55+
TGtWithBareArrayBoxU16 classSide >> exampleArrayU16LengthOfNull [
56+
"Make sure that the array api accepts and survives null address as an argument"
57+
<gtExample>
58+
| aLength |
59+
60+
aLength := self uniqueInstance primArrayU16GetLength: ExternalAddress null.
61+
self
62+
assert: aLength
63+
equals: 0.
64+
65+
^ aLength
66+
]
67+
68+
{ #category : #'examples - array u16' }
69+
TGtWithBareArrayBoxU16 classSide >> exampleArrayU16New [
70+
<gtExample>
71+
| anArray |
72+
73+
anArray := self uniqueInstance newArrayU16.
74+
self assert: anArray size isZero.
75+
self assert: anArray capacity isZero.
76+
self assert: anArray data isEmpty.
77+
78+
^ anArray
79+
]
80+
81+
{ #category : #'examples - array u16' }
82+
TGtWithBareArrayBoxU16 classSide >> exampleArrayU16NewOfSizeWithAll [
83+
<gtExample>
84+
| aDefaultValue anArray |
85+
86+
aDefaultValue := self exampleArrayU16DefaultValue.
87+
anArray := self uniqueInstance
88+
newArrayU16: 100
89+
withAll: aDefaultValue.
90+
91+
self assert: anArray size equals: 100.
92+
self assert: anArray capacity >= 100.
93+
self assert: anArray asArray equals: (Array new: 100 withAll: aDefaultValue).
94+
95+
^ anArray
96+
]
97+
98+
{ #category : #'examples - array u16' }
99+
TGtWithBareArrayBoxU16 classSide >> exampleArrayU16Release [
100+
<gtExample>
101+
| anArray |
102+
103+
anArray := self exampleArrayU16New.
104+
anArray release.
105+
106+
self assert: anArray size isZero.
107+
self assert: anArray capacity isZero.
108+
self assert: anArray data isEmpty.
109+
110+
^ anArray
111+
]
112+
113+
{ #category : #'api - array u16' }
114+
TGtWithBareArrayBoxU16 >> newArrayU16 [
115+
^ GtArrayBoxU16
116+
fromOwnedHandle: self primArrayU16Create
117+
library: self
118+
]
119+
120+
{ #category : #'api - array u16' }
121+
TGtWithBareArrayBoxU16 >> newArrayU16: aSize withAll: aValue [
122+
"Answer an array of type `u16` with aSize of aValue elements"
123+
124+
^ GtArrayBoxU16
125+
fromOwnedHandle: (self primArrayU16CreateNew: aSize withAll: aValue)
126+
library: self
127+
]
128+
129+
{ #category : #'api - array u16' }
130+
TGtWithBareArrayBoxU16 >> newArrayU16During: aBlock [
131+
^ GtArrayBoxU16
132+
fromOwnedHandle: self primArrayU16Create
133+
library: self
134+
during: aBlock
135+
]
136+
137+
{ #category : #'api - array u16' }
138+
TGtWithBareArrayBoxU16 >> newArrayU16FromHandle: anExternalAddress during: aBlock [
139+
^ GtArrayBoxU16
140+
fromOwnedHandle: anExternalAddress
141+
library: self
142+
during: aBlock
143+
]
144+
145+
{ #category : #'api - array u16' }
146+
TGtWithBareArrayBoxU16 >> newArrayU16WithAll: aCollectionOfNumbers during: aBlock [
147+
"Create a new u16 array containing all the elements from aCollectionOfNumbers for the duration of aBlock."
148+
149+
^ GtArrayBoxU16
150+
fromOwnedHandle: (self primArrayU16CreateNew: aCollectionOfNumbers size withAll: 0)
151+
library: self
152+
during: [ :anArrayBox |
153+
aCollectionOfNumbers withIndexDo: [ :eachNumber :eachIndex |
154+
anArrayBox at: eachIndex put: eachNumber ].
155+
aBlock value: anArrayBox ]
156+
]
157+
158+
{ #category : #'private - array u16' }
159+
TGtWithBareArrayBoxU16 >> primArrayU16: anArrayBoxU16 at: anIndex [
160+
^ self ffiBareCall: #(uint16 boxer_array_u16_at(
161+
GtArrayBoxU16 anArrayBoxU16,
162+
size_t anIndex))
163+
]
164+
165+
{ #category : #'private - array u16' }
166+
TGtWithBareArrayBoxU16 >> primArrayU16: anArrayBoxU16 at: anIndex put: anItem [
167+
^ self ffiBareCall: #(void boxer_array_u16_at_put(
168+
GtArrayBoxU16 anArrayBoxU16,
169+
size_t anIndex,
170+
uint16 anItem))
171+
]
172+
173+
{ #category : #'private - array u16' }
174+
TGtWithBareArrayBoxU16 >> primArrayU16: anArrayBoxU16 copyInto: anotherArrayBoxU16 [
175+
^ self ffiBareCall: #(void boxer_array_u16_copy_into(
176+
GtArrayBoxU16 anArrayBoxU16,
177+
GtArrayBoxU16 anotherArrayBoxU16))
178+
]
179+
180+
{ #category : #'private - array u16' }
181+
TGtWithBareArrayBoxU16 >> primArrayU16: anArrayBoxU16 copyIntoData: anExternalAddress length: aLength [
182+
^ self ffiBareCall: #(void boxer_array_u16_copy_into_data(
183+
GtArrayBoxU16 anArrayBoxU16,
184+
void* anExternalAddress,
185+
size_t aLength))
186+
]
187+
188+
{ #category : #'private - array u16' }
189+
TGtWithBareArrayBoxU16 >> primArrayU16Create [
190+
^ self ffiBareCall: #(void* boxer_array_u16_create())
191+
]
192+
193+
{ #category : #'private - array u16' }
194+
TGtWithBareArrayBoxU16 >> primArrayU16CreateFromData: anExternalAddress length: aSize [
195+
^ self ffiBareCall: #(void* boxer_array_u16_create_from_data(
196+
void* anExternalAddress,
197+
size_t aSize))
198+
]
199+
200+
{ #category : #'private - array u16' }
201+
TGtWithBareArrayBoxU16 >> primArrayU16CreateNew: aSize withAll: aValue [
202+
^ self ffiBareCall: #(void* boxer_array_u16_create_with(uint16 aValue, size_t aSize))
203+
]
204+
205+
{ #category : #'private - array u16' }
206+
TGtWithBareArrayBoxU16 >> primArrayU16GetCapacity: anArrayBoxU16 [
207+
^ self ffiBareCall: #(size_t boxer_array_u16_get_capacity(GtArrayBoxU16 anArrayBoxU16))
208+
]
209+
210+
{ #category : #'private - array u16' }
211+
TGtWithBareArrayBoxU16 >> primArrayU16GetData: anArrayBoxU16 [
212+
^ self ffiBareCall: #(void* boxer_array_u16_get_data(GtArrayBoxU16 anArrayBoxU16))
213+
]
214+
215+
{ #category : #'private - array u16' }
216+
TGtWithBareArrayBoxU16 >> primArrayU16GetLength: anArrayBoxU16 [
217+
^ self ffiBareCall: #(size_t boxer_array_u16_get_length(GtArrayBoxU16 anArrayBoxU16))
218+
]
219+
220+
{ #category : #'private - array u16' }
221+
TGtWithBareArrayBoxU16 >> primArrayU16Release: aHandle [
222+
self ffiBareCall: #(void boxer_array_u16_drop(void* aHandle))
223+
]
224+
225+
{ #category : #'api - array u16' }
226+
TGtWithBareArrayBoxU16 >> unsafeNewArrayU16FromData: anExternalAddress length: aLength [
227+
"Unsafe. Create an array from a given raw pointer to the data of given length.
228+
aLength is an amount of u16 elements in the data.
229+
Make sure that the allocated data overlives the returned array.
230+
Data will not be freed when the array is dropped."
231+
232+
^ GtArrayBoxU16
233+
fromOwnedHandle: (self primArrayU16CreateFromData: anExternalAddress length: aLength)
234+
library: self
235+
]
236+
237+
{ #category : #'api - array u16' }
238+
TGtWithBareArrayBoxU16 >> unsafeNewArrayU16FromData: anExternalAddress length: aLength during: aBlock [
239+
"Unsafe. Create an array from a given raw pointer to the data
240+
of given length for the duration of aBlock.
241+
aLength is an amount of u16 elements in the data.
242+
Make sure that the allocated data overlives the returned array.
243+
Data will not be freed when the array is dropped."
244+
245+
^ GtArrayBoxU16
246+
fromOwnedHandle: (self primArrayU16CreateFromData: anExternalAddress length: aLength)
247+
library: self
248+
during: aBlock
249+
]

0 commit comments

Comments
 (0)