Skip to content

Conversation

sled
Copy link
Contributor

@sled sled commented Apr 1, 2021

  • Allow FFI::Pointer to be passed as data argument to Image#new_from_memory and Image#new_from_memory_copy.
  • Requires the FFI::Pointer to have a valid size.
  • Calculates the size using .size on FFI::Pointer, falls back to .bytesize for strings.

See bug report: #295

@sled
Copy link
Contributor Author

sled commented Apr 1, 2021

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.

@jcupitt
Copy link
Member

jcupitt commented Apr 1, 2021

Perhaps use respond_to?? You could check size as well.

@sled
Copy link
Contributor Author

sled commented Apr 2, 2021

@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?

@jcupitt
Copy link
Member

jcupitt commented Apr 4, 2021

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.

@sled
Copy link
Contributor Author

sled commented Apr 6, 2021

@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.

@sled
Copy link
Contributor Author

sled commented Apr 6, 2021

@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

@jcupitt jcupitt merged commit c9c7b56 into libvips:master May 6, 2021
@jcupitt
Copy link
Member

jcupitt commented May 6, 2021

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants