-
Couldn't load subscription status.
- Fork 62
Fix FFI::Pointer handling in Image#new_from_memory and Image#new_from_memory_copy #296
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
Conversation
There seems to be an issue with older versions of FFI missing the size_limit? method. Will check if there's an alternative way to determine the size.
Perhaps use respond_to?? You could check size as well.
@jcupitt yes we could check the whether the size of the pointer equals the "magic" value. That's what size_limit? does here: https://github.com/ffi/ffi/blob/a7f4c951f5004c338fbae237bbae69fbb6ca66c6/lib/ffi/abstract_memory.rb#L41
This helper method was introduced with FFI v1.14.0 but the concept of setting size to LONG_MAX if the memory size is unknown goes way back even before v1.0.0 of FFI.
I'd suggest to define a private constant somewhere as FFI does, i.e INVALID_POINTER_SIZE = FFI::Pointer.new(1).size but where should I define this constant, would Vips::Image be a good place?
Yes, I guess Vips::Image would be OK, though I think you're probably a more experienced rubyist than me, so I'm very happy to leave the choice to you.
@jcupitt okay I added a private constant to the Vips::Image class. All CRuby specs are passing now, JRuby has a custom FFI implementation which needs some further investigation.
@jcupitt the problem seems to be in JRuby's FFI implementation when casting a MemoryPointer to a Pointer it looses the size information. I just added a call to .slice in the specs if used with JRuby.
JRuby
irb(main):002:0> memory = FFI::MemoryPointer.new(:uchar, 1024)
=> #<FFI::MemoryPointer address=0x7fb823ced800 size=1024>
irb(main):003:0> memory.size
=> 1024
irb(main):004:0> ptr = FFI::Pointer.new(memory)
=> #<FFI::Pointer address=0x7fb823ced800>
irb(main):005:0> ptr.size
=> 9223372036854775807
CRuby
irb(main):003:0> memory = FFI::MemoryPointer.new(:uchar, 1024)
=> #<FFI::MemoryPointer address=0x00007f860c902600 size=1024>
irb(main):004:0> memory.size
=> 1024
irb(main):005:0> ptr = FFI::Pointer.new(memory)
=> #<FFI::Pointer address=0x00007f860c902600 size=1024>
irb(main):006:0> ptr.size
=> 1024
Sorry @sled, I though this was still a WIP, I hadn't read your last comment carefully enough. I'll push 2.1.2 with this change.
Thank you for contributing this nice feature!
FFI::Pointerto be passed as data argument toImage#new_from_memoryandImage#new_from_memory_copy.FFI::Pointerto have a valid size..sizeonFFI::Pointer, falls back to.bytesizefor strings.See bug report: #295