@@ -231,14 +231,15 @@ DEFINE_NATIVE_ENTRY(Ffi_allocate, 1, 1) {
231
231
CheckRange (argCount, 1 , max_count, " count" );
232
232
233
233
size_t size = ffi::ElementSizeInBytes (type_cid) * count;
234
- uint8_t * memory = reinterpret_cast <uint8_t * >(malloc (size));
235
- if (memory == NULL ) {
234
+ intptr_t memory = reinterpret_cast <intptr_t >(malloc (size));
235
+ if (memory == 0 ) {
236
236
const String& error = String::Handle (String::NewFormatted (
237
237
" allocating (%" Pd " ) bytes of memory failed" , size));
238
238
Exceptions::ThrowArgumentError (error);
239
239
}
240
240
241
- RawPointer* result = Pointer::New (type_arg, memory);
241
+ RawPointer* result =
242
+ Pointer::New (type_arg, Integer::Handle (zone, Integer::New (memory)));
242
243
return result;
243
244
}
244
245
@@ -250,27 +251,27 @@ DEFINE_NATIVE_ENTRY(Ffi_fromAddress, 1, 1) {
250
251
CheckIsConcreteNativeType (native_type);
251
252
GET_NON_NULL_NATIVE_ARGUMENT (Integer, arg_ptr, arguments->NativeArgAt (0 ));
252
253
253
- uint8_t * address = reinterpret_cast <uint8_t *>(arg_ptr.AsInt64Value ());
254
- // TODO(dacoharkes): should this return NULL if addres is 0?
254
+ // TODO(dacoharkes): should this return NULL if address is 0?
255
255
// https://github.com/dart-lang/sdk/issues/35756
256
256
257
257
RawPointer* result =
258
- Pointer::New (native_type, address , type_arg.type_class_id ());
258
+ Pointer::New (native_type, arg_ptr , type_arg.type_class_id ());
259
259
return result;
260
260
}
261
261
262
262
DEFINE_NATIVE_ENTRY (Ffi_elementAt, 0 , 2 ) {
263
263
GET_NON_NULL_NATIVE_ARGUMENT (Pointer, pointer, arguments->NativeArgAt (0 ));
264
264
GET_NON_NULL_NATIVE_ARGUMENT (Integer, index, arguments->NativeArgAt (1 ));
265
265
AbstractType& pointer_type_arg =
266
- AbstractType::Handle (pointer.type_argument ());
266
+ AbstractType::Handle (zone, pointer.type_argument ());
267
267
CheckSized (pointer_type_arg);
268
268
269
269
classid_t class_id = pointer_type_arg.type_class_id ();
270
- uint8_t * address = pointer.GetCMemoryAddress ();
271
- uint8_t * address_new =
272
- address + index.AsInt64Value () * ffi::ElementSizeInBytes (class_id);
273
- RawPointer* result = Pointer::New (pointer_type_arg, address_new);
270
+ Integer& address = Integer::Handle (zone, pointer.GetCMemoryAddress ());
271
+ address =
272
+ Integer::New (address.AsInt64Value () +
273
+ index.AsInt64Value () * ffi::ElementSizeInBytes (class_id));
274
+ RawPointer* result = Pointer::New (pointer_type_arg, address);
274
275
return result;
275
276
}
276
277
@@ -280,9 +281,11 @@ DEFINE_NATIVE_ENTRY(Ffi_offsetBy, 0, 2) {
280
281
AbstractType& pointer_type_arg =
281
282
AbstractType::Handle (pointer.type_argument ());
282
283
283
- uint8_t * address = pointer.GetCMemoryAddress ();
284
- uint8_t * address_new = address + offset.AsInt64Value ();
285
- RawPointer* result = Pointer::New (pointer_type_arg, address_new);
284
+ intptr_t address =
285
+ Integer::Handle (zone, pointer.GetCMemoryAddress ()).AsInt64Value () +
286
+ offset.AsInt64Value ();
287
+ RawPointer* result = Pointer::New (
288
+ pointer_type_arg, Integer::Handle (zone, Integer::New (address)));
286
289
return result;
287
290
}
288
291
@@ -294,7 +297,7 @@ DEFINE_NATIVE_ENTRY(Ffi_cast, 1, 1) {
294
297
type_args.TypeAtNullSafe (Pointer::kNativeTypeArgPos ));
295
298
CheckIsConcreteNativeType (native_type);
296
299
297
- uint8_t * address = pointer.GetCMemoryAddress ();
300
+ const Integer& address = Integer::Handle (zone, pointer.GetCMemoryAddress () );
298
301
RawPointer* result =
299
302
Pointer::New (native_type, address, type_arg.type_class_id ());
300
303
return result;
@@ -303,35 +306,37 @@ DEFINE_NATIVE_ENTRY(Ffi_cast, 1, 1) {
303
306
DEFINE_NATIVE_ENTRY (Ffi_free, 0 , 1 ) {
304
307
GET_NON_NULL_NATIVE_ARGUMENT (Pointer, pointer, arguments->NativeArgAt (0 ));
305
308
306
- uint8_t * address = pointer.GetCMemoryAddress ();
307
- free (address);
308
- pointer.SetCMemoryAddress (0 );
309
+ const Integer& address = Integer::Handle (zone, pointer.GetCMemoryAddress () );
310
+ free (reinterpret_cast < void *>( address. AsInt64Value ()) );
311
+ pointer.SetCMemoryAddress (Integer::Handle (zone, Integer::New ( 0 )) );
309
312
return Object::null ();
310
313
}
311
314
312
315
DEFINE_NATIVE_ENTRY (Ffi_address, 0 , 1 ) {
313
316
GET_NON_NULL_NATIVE_ARGUMENT (Pointer, pointer, arguments->NativeArgAt (0 ));
314
-
315
- uint8_t * address = pointer.GetCMemoryAddress ();
316
- intptr_t int_ptr = reinterpret_cast <intptr_t >(address);
317
- return Integer::NewFromUint64 (int_ptr);
317
+ return pointer.GetCMemoryAddress ();
318
318
}
319
319
320
- static RawInstance* BoxLoadPointer (uint8_t * address,
320
+ static RawInstance* BoxLoadPointer (Zone* zone,
321
+ uint8_t * address,
321
322
const AbstractType& instance_type_arg,
322
323
intptr_t type_cid) {
323
324
// TODO(dacoharkes): should this return NULL if addres is 0?
324
325
// https://github.com/dart-lang/sdk/issues/35756
325
- if (address == 0 ) { // 0 is the c++ null pointer
326
+ if (address == nullptr ) {
326
327
return Instance::null ();
327
328
}
328
329
AbstractType& type_arg =
329
330
AbstractType::Handle (TypeArguments::Handle (instance_type_arg.arguments ())
330
331
.TypeAt (Pointer::kNativeTypeArgPos ));
331
- return Pointer::New (type_arg, address, type_cid);
332
+ return Pointer::New (
333
+ type_arg,
334
+ Integer::Handle (zone, Integer::New (reinterpret_cast <intptr_t >(address))),
335
+ type_cid);
332
336
}
333
337
334
- static RawInstance* LoadValue (uint8_t * address,
338
+ static RawInstance* LoadValue (Zone* zone,
339
+ uint8_t * address,
335
340
const AbstractType& instance_type_arg) {
336
341
classid_t type_cid = instance_type_arg.type_class_id ();
337
342
switch (type_cid) {
@@ -360,7 +365,7 @@ static RawInstance* LoadValue(uint8_t* address,
360
365
case kFfiPointerCid :
361
366
default :
362
367
ASSERT (IsPointerType (instance_type_arg));
363
- return BoxLoadPointer (*reinterpret_cast <uint8_t **>(address),
368
+ return BoxLoadPointer (zone, *reinterpret_cast <uint8_t **>(address),
364
369
instance_type_arg, type_cid);
365
370
}
366
371
}
@@ -373,14 +378,17 @@ DEFINE_NATIVE_ENTRY(Ffi_load, 1, 1) {
373
378
CheckSized (pointer_type_arg);
374
379
ASSERT (DartAndCTypeCorrespond (pointer_type_arg, type_arg));
375
380
376
- uint8_t * address = pointer.GetCMemoryAddress ();
377
- return LoadValue (address, pointer_type_arg);
381
+ uint8_t * address = reinterpret_cast <uint8_t *>(
382
+ Integer::Handle (pointer.GetCMemoryAddress ()).AsInt64Value ());
383
+ return LoadValue (zone, address, pointer_type_arg);
378
384
}
379
385
380
- static void StoreValue (const Pointer& pointer,
386
+ static void StoreValue (Zone* zone,
387
+ const Pointer& pointer,
381
388
classid_t type_cid,
382
389
const Instance& new_value) {
383
- uint8_t * address = pointer.GetCMemoryAddress ();
390
+ uint8_t * address = reinterpret_cast <uint8_t *>(
391
+ Integer::Handle (pointer.GetCMemoryAddress ()).AsInt64Value ());
384
392
AbstractType& pointer_type_arg =
385
393
AbstractType::Handle (pointer.type_argument ());
386
394
switch (type_cid) {
@@ -428,14 +436,16 @@ static void StoreValue(const Pointer& pointer,
428
436
case kFfiPointerCid :
429
437
default : {
430
438
ASSERT (IsPointerType (pointer_type_arg));
431
- uint8_t * new_value_unwrapped = nullptr ;
439
+ intptr_t new_value_unwrapped = 0 ;
432
440
if (!new_value.IsNull ()) {
433
441
ASSERT (new_value.IsPointer ());
434
- new_value_unwrapped = AsPointer (new_value).GetCMemoryAddress ();
442
+ new_value_unwrapped =
443
+ Integer::Handle (AsPointer (new_value).GetCMemoryAddress ())
444
+ .AsInt64Value ();
435
445
// TODO(dacoharkes): should this return NULL if addres is 0?
436
446
// https://github.com/dart-lang/sdk/issues/35756
437
447
}
438
- *reinterpret_cast <uint8_t * *>(address) = new_value_unwrapped;
448
+ *reinterpret_cast <intptr_t *>(address) = new_value_unwrapped;
439
449
} break ;
440
450
}
441
451
}
@@ -450,7 +460,7 @@ DEFINE_NATIVE_ENTRY(Ffi_store, 0, 2) {
450
460
ASSERT (DartAndCTypeCorrespond (pointer_type_arg, arg_type));
451
461
452
462
classid_t type_cid = pointer_type_arg.type_class_id ();
453
- StoreValue (pointer, type_cid, new_value);
463
+ StoreValue (zone, pointer, type_cid, new_value);
454
464
return Object::null ();
455
465
}
456
466
@@ -561,9 +571,7 @@ DEFINE_NATIVE_ENTRY(Ffi_asFunction, 1, 1) {
561
571
// the function so that we can reuse the function for each c function with
562
572
// the same signature.
563
573
Context& context = Context::Handle (Context::New (1 ));
564
- context.SetAt (0 ,
565
- Object::Handle (Integer::NewFromUint64 (
566
- reinterpret_cast <intptr_t >(pointer.GetCMemoryAddress ()))));
574
+ context.SetAt (0 , Integer::Handle (zone, pointer.GetCMemoryAddress ()));
567
575
568
576
RawClosure* raw_closure =
569
577
Closure::New (Object::null_type_arguments (), Object::null_type_arguments (),
@@ -623,7 +631,8 @@ DEFINE_NATIVE_ENTRY(Ffi_fromFunction, 1, 1) {
623
631
THR_Print (" Ffi_fromFunction: %p\n " , entryPoint);
624
632
THR_Print (" Ffi_fromFunction: %" Pd " \n " , code.Size ());
625
633
626
- void * address = GenerateFfiInverseTrampoline (c_signature, entryPoint);
634
+ intptr_t address = reinterpret_cast <intptr_t >(
635
+ GenerateFfiInverseTrampoline (c_signature, entryPoint));
627
636
628
637
TypeArguments& type_args = TypeArguments::Handle (zone);
629
638
type_args = TypeArguments::New (1 );
@@ -642,8 +651,8 @@ DEFINE_NATIVE_ENTRY(Ffi_fromFunction, 1, 1) {
642
651
643
652
address = 0 ; // https://github.com/dart-lang/sdk/issues/35761
644
653
645
- Pointer& result = Pointer::Handle (
646
- Pointer::New (native_function_type, reinterpret_cast < uint8_t *> (address)));
654
+ Pointer& result = Pointer::Handle (Pointer::New (
655
+ native_function_type, Integer::Handle (zone, Integer::New (address) )));
647
656
648
657
return result.raw ();
649
658
}
0 commit comments