Created
June 8, 2018 15:03
-
-
Save king1600/af670d0573302fe09afa62c197392beb to your computer and use it in GitHub Desktop.
Unsafe abstractions for pony data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
primitive Unsafe | |
fun gc() => | |
@pony_triggergc[None](@pony_ctx[USize]()) | |
fun usize[T](value: T): USize => | |
Unsafe.ptr[T](consume value).usize() | |
fun ptr[T](value: T): Pointer[T] => | |
@memcpy[Pointer[T]](value, USize(0), USize(0)) | |
fun deref[T](pointer: USize): T^ => | |
@memcpy[T^](pointer, USize(0), USize(0)) | |
fun offset[T, R](value: T, offs: USize = 0): R^ => | |
Unsafe.deref[R](Unsafe.usize[T](consume value) + offs) | |
fun alloc[T](size: USize): T^ => | |
@pony_alloc[T^](@pony_ctx[USize](), size) | |
fun realloc[T](pointer: USize, size: USize): T^ => | |
@pony_realloc[T^](@pony_ctx[USize](), pointer, size) | |
fun istype2[T, T1, T2](): Bool => | |
Unsafe.istype[T, T1]() or Unsafe.istype[T, T2]() | |
fun istype[T1, T2](): Bool => | |
iftype T1 <: T2 | |
then true | |
else false | |
end | |
fun sizeof[T](): USize => | |
if Unsafe.istype2[T, U8, I8]() then 1 | |
elseif Unsafe.istype2[T, U16, I16]() then 2 | |
elseif Unsafe.istype2[T, U32, I32]() then 4 | |
elseif Unsafe.istype2[T, U64, I64]() then 8 | |
elseif Unsafe.istype2[T, U128, I128]() then 16 | |
elseif Unsafe.istype2[T, ULong, ILong]() then | |
ifdef ilp32 or llp64 then 4 else 8 end | |
else | |
USize.max_value().bitwidth() >> 3 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment