40
40
*/
41
41
package com .oracle .graal .python .builtins .modules .ctypes ;
42
42
43
- import static com .oracle .graal .python .builtins .modules .ctypes .CtypesModuleBuiltins .DICTFLAG_FINAL ;
43
+ import static com .oracle .graal .python .builtins .modules .ctypes .StgDictObject .DICTFLAG_FINAL ;
44
44
import static com .oracle .graal .python .nodes .ErrorMessages .BUFFER_SIZE_TOO_SMALL_D_INSTEAD_OF_AT_LEAST_D_BYTES ;
45
45
import static com .oracle .graal .python .nodes .ErrorMessages .CTYPES_OBJECT_STRUCTURE_TOO_DEEP ;
46
46
import static com .oracle .graal .python .nodes .ErrorMessages .EXPECTED_P_INSTANCE_GOT_P ;
@@ -271,6 +271,7 @@ Object CDataType_from_buffer_copy(Object type, Object buffer, int offset,
271
271
@ CachedLibrary ("buffer" ) PythonBufferAccessLibrary bufferLib ,
272
272
@ Cached PointerNodes .WriteBytesNode writeBytesNode ,
273
273
@ Cached AuditNode auditNode ,
274
+ @ Cached CtypesNodes .GenericPyCDataNewNode pyCDataNewNode ,
274
275
@ Cached PyTypeStgDictNode pyTypeStgDictNode ) {
275
276
try {
276
277
StgDictObject dict = pyTypeStgDictNode .checkAbstractClass (type , getRaiseNode ());
@@ -288,8 +289,7 @@ Object CDataType_from_buffer_copy(Object type, Object buffer, int offset,
288
289
// This prints the raw pointer in C, so just print 0
289
290
auditNode .audit ("ctypes.cdata/buffer" , 0 , bufferLen , offset );
290
291
291
- CDataObject result = factory ().createCDataObject (type );
292
- GenericPyCDataNew (dict , result );
292
+ CDataObject result = pyCDataNewNode .execute (inliningTarget , type , dict );
293
293
byte [] slice = new byte [dict .size ];
294
294
bufferLib .readIntoByteArray (buffer , offset , slice , 0 , dict .size );
295
295
writeBytesNode .execute (inliningTarget , result .b_ptr , slice );
@@ -349,49 +349,48 @@ protected abstract static class PyCDataAtAddress extends PNodeWithRaise {
349
349
*/
350
350
@ Specialization
351
351
CDataObject PyCData_AtAddress (Object type , Pointer pointer ,
352
+ @ Bind ("this" ) Node inliningTarget ,
352
353
@ Cached PyTypeCheck pyTypeCheck ,
353
354
@ Cached PyTypeStgDictNode pyTypeStgDictNode ,
354
- @ Cached PythonObjectFactory factory ) {
355
+ @ Cached CtypesNodes . CreateCDataObjectNode createCDataObjectNode ) {
355
356
// auditNode.audit("ctypes.cdata", buf);
356
357
// assert(PyType_Check(type));
357
358
StgDictObject stgdict = pyTypeStgDictNode .checkAbstractClass (type , getRaiseNode ());
358
359
stgdict .flags |= DICTFLAG_FINAL ;
359
360
360
- CDataObject pd = factory . createCDataObject ( type );
361
+ CDataObject pd = createCDataObjectNode . execute ( inliningTarget , type , pointer , stgdict . size , false );
361
362
assert pyTypeCheck .isCDataObject (pd );
362
- pd .b_ptr = pointer ;
363
363
pd .b_length = stgdict .length ;
364
- pd .b_size = stgdict .size ;
365
364
return pd ;
366
365
}
367
366
}
368
367
369
368
// corresponds to PyCData_get
370
369
@ ImportStatic (FieldGet .class )
371
370
protected abstract static class PyCDataGetNode extends PNodeWithRaise {
372
- protected abstract Object execute (Object type , FieldGet getfunc , Object src , int index , int size , Pointer adr );
371
+ protected abstract Object execute (Object type , FieldGet getfunc , CDataObject src , int index , int size , Pointer adr );
373
372
374
373
@ Specialization (guards = "getfunc != nil" )
375
374
@ SuppressWarnings ("unused" )
376
- Object withFunc (Object type , FieldGet getfunc , Object src , int index , int size , Pointer adr ,
375
+ Object withFunc (Object type , FieldGet getfunc , CDataObject src , int index , int size , Pointer adr ,
377
376
@ Cached GetFuncNode getFuncNode ) {
378
377
return getFuncNode .execute (getfunc , adr , size );
379
378
}
380
379
381
380
@ Specialization (guards = "getfunc == nil" )
382
- Object WithoutFunc (Object type , @ SuppressWarnings ("unused" ) FieldGet getfunc , Object src , int index , int size , Pointer adr ,
383
- @ Cached PythonObjectFactory factory ,
381
+ Object withoutFunc (Object type , @ SuppressWarnings ("unused" ) FieldGet getfunc , CDataObject src , int index , int size , Pointer adr ,
384
382
@ Bind ("this" ) Node inliningTarget ,
385
383
@ Cached PyTypeCheck pyTypeCheck ,
386
384
@ Cached InlinedIsSameTypeNode isSameTypeNode ,
387
385
@ Cached GetBaseClassNode getBaseClassNode ,
388
386
@ Cached GetFuncNode getFuncNode ,
389
- @ Cached PyTypeStgDictNode pyTypeStgDictNode ) {
387
+ @ Cached PyTypeStgDictNode pyTypeStgDictNode ,
388
+ @ Cached CtypesNodes .PyCDataFromBaseObjNode fromBaseObjNode ) {
390
389
StgDictObject dict = pyTypeStgDictNode .execute (type );
391
390
if (dict != null && dict .getfunc != FieldGet .nil && !pyTypeCheck .ctypesSimpleInstance (inliningTarget , type , getBaseClassNode , isSameTypeNode )) {
392
391
return getFuncNode .execute (dict .getfunc , adr , size );
393
392
}
394
- return PyCData_FromBaseObj ( type , src , index , adr , factory , getRaiseNode (), pyTypeStgDictNode );
393
+ return fromBaseObjNode . execute ( inliningTarget , type , src , index , adr );
395
394
}
396
395
}
397
396
@@ -612,45 +611,4 @@ static TruffleString unique_key(CDataObject cdata, int index,
612
611
private static String toHex (int value ) {
613
612
return Integer .toHexString (value );
614
613
}
615
-
616
- static void PyCData_MallocBuffer (CDataObject obj , StgDictObject dict ) {
617
- obj .b_ptr = dict .size > 0 ? Pointer .allocate (dict .ffi_type_pointer , dict .size ) : Pointer .NULL ;
618
- obj .b_size = dict .size ;
619
- obj .b_needsfree = true ;
620
- }
621
-
622
- static CDataObject PyCData_FromBaseObj (Object type , Object base , int index , Pointer adr ,
623
- PythonObjectFactory factory ,
624
- PRaiseNode raiseNode ,
625
- PyTypeStgDictNode pyTypeStgDictNode ) {
626
- // assert(PyType_Check(type));
627
- StgDictObject dict = pyTypeStgDictNode .checkAbstractClass (type , raiseNode );
628
- dict .flags |= DICTFLAG_FINAL ;
629
- CDataObject cmem = factory .createCDataObject (type );
630
-
631
- cmem .b_length = dict .length ;
632
- cmem .b_size = dict .size ;
633
- if (base != null ) { /* use base's buffer */
634
- cmem .b_ptr = adr ;
635
- cmem .b_needsfree = false ;
636
- cmem .b_base = (CDataObject ) base ;
637
- } else { /* copy contents of adr */
638
- PyCData_MallocBuffer (cmem , dict );
639
- // memcpy(cmem.b_ptr, adr, dict.size); TODO
640
- cmem .b_ptr = adr ;
641
- }
642
- cmem .b_index = index ;
643
- return cmem ;
644
- }
645
-
646
- // corresponds to GenericPyCData_new
647
- protected static CDataObject GenericPyCDataNew (StgDictObject dict , CDataObject obj ) {
648
- dict .flags |= DICTFLAG_FINAL ;
649
- obj .b_base = null ;
650
- obj .b_index = 0 ;
651
- obj .b_objects = null ;
652
- obj .b_length = dict .length ;
653
- PyCData_MallocBuffer (obj , dict );
654
- return obj ;
655
- }
656
614
}
0 commit comments