Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to define a type like size_t?? #39372

Closed
JsouLiang opened this issue Nov 13, 2019 · 5 comments
Closed

How to define a type like size_t?? #39372

JsouLiang opened this issue Nov 13, 2019 · 5 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-ffi

Comments

@JsouLiang
Copy link

JavaScriptCore has a API is JSStringGetLength it need a JSStringRef and return a size_t type value.

size_t : if the compiler is 32 bit then it is simply a typedef(i.e., alias) for unsigned int but if the compiler is 64 bit then it would be a typedef for unsigned long long. The size_t data type is never negative.

dart.ffi has ffi.Uint32, ffi.Uint64, witch type should I use to map size_t? If I use ffi.Uint64 it has a out of bound problem is 32bit machine. I think this problem because in 32bit matchine C API return a 32bit size value, but ffi.Pointer it will find UInt64 size value. If I use Uint32 in 64bit matchine when the return value is so large may be out of bound.
How to define a type in 32bit matchine is Uint32 and 64bit is Uint64 like size_t??

@ds84182
Copy link
Contributor

ds84182 commented Nov 13, 2019

You can use ffi.IntPtr for this. However, I think it gets sign extended on both 32-bit and 64-bit machines.

@keertip keertip added area-sdk Use area-sdk for general purpose SDK issues (packaging, distribution, …). library-ffi area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. and removed area-sdk Use area-sdk for general purpose SDK issues (packaging, distribution, …). labels Nov 13, 2019
@JsouLiang
Copy link
Author

I find some package is use Int32 like this: tflite_native, method: TfLiteTensorByteSize
and some package is use IntPtr like this: malloc, malloc : void* malloc(size_t __byte_count)
I want know what different of them?? Thanks😁

@ds84182
Copy link
Contributor

ds84182 commented Nov 16, 2019

@x-liang The TFLite one seems like a bug. It should be IntPtr, otherwise if the size is greater than 2gb the return result will be incorrect.

@dcharkes
Copy link
Contributor

It's not possible to define your own native type that way.

We keep track of a list of requested types here: #36140. Though, we can only add those types if they are well-defined across a range of compilers/platforms.

@dcharkes
Copy link
Contributor

dcharkes commented Sep 7, 2020

We've filed an issue for implementing a generic solution for this #42563.

@dcharkes dcharkes closed this as completed Sep 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-ffi
Projects
None yet
Development

No branches or pull requests

4 participants