Skip to content

Commit e201d56

Browse files
authored
add ffi_cast_ptr2addr and ffi_cast_addr2ptr (#57)
1 parent 422dce1 commit e201d56

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

kphp_polyfills.php

+23
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,29 @@ function ffi_memcpy_string(\FFI\CData $dst, string $src, int $size) {
11791179
\FFI::memcpy($dst, $src, $size);
11801180
}
11811181

1182+
/**
1183+
* identical to FFI::cast('uintptr_t', $ptr)
1184+
* the addr obtained from this function can be used with ffi_cast_addr2ptr
1185+
* @param ffi_cdata<C, void*> $ptr
1186+
* @return int
1187+
*/
1188+
function ffi_cast_ptr2addr($ptr) {
1189+
// For some reason, PHP doesn't like void* cast to scalar.
1190+
// As a temporary (?) workaround, cast void* to some sized pointer first.
1191+
$as_sized_ptr = \FFI::cast('uint8_t*', $ptr);
1192+
return \FFI::cast('uintptr_t', $as_sized_ptr)->cdata;
1193+
}
1194+
1195+
/**
1196+
* identical to FFI::cast('void*', $addr)
1197+
* $addr should be obtained from ffi_cast_ptr2addr
1198+
* @param int $addr
1199+
* @return ffi_cdata<C, void*>
1200+
*/
1201+
function ffi_cast_addr2ptr($addr) {
1202+
return \FFI::cast('void*', $addr);
1203+
}
1204+
11821205
/**
11831206
* ffi_array_set implements array or pointer update operation: $arr[$index] = $value
11841207
* For CData arrays, a bound check if performed: PHP throws and KPHP triggers

0 commit comments

Comments
 (0)