Subject: Re: [ruby-ffi] wiki update on pointers |
From: Wayne Meissner |
Date: 5/11/10 5:33 PM |
To: ruby-ffi@googlegroups.com |
It looks pretty good. A couple of nits: 1) The LibC module can just use: ffi_lib FFI::Library::LIBC ffi knows that means "whatever the libc on this system is supposed to be", since its a pretty common library people want to map in, and the name differs across platforms (e.g. on linux, its libc.so.6, macos is libc.dylib, windows is msvcrt.dll, aix is something weird, etc. 2) The first arg to MemoryPointer.new can in fact be either a symbol, or anything that responds to #size. e.g. class S < FFI::Struct layout :i, :int end FFI::MemoryPointer.new(S, 1, false) # Will allocate space for one instance of S, without clearing the memory first 3) The bit at the top about copying pointers is ... confusing. A pointer is merely a Fixnum that holds a native memory address, so there is no need to do anything special to "copy" a pointer - just assign it to a like you would any other Fixnum. Think of "Pointer" as "fixnum with methods to read/write the native memory at the address". The example: b = MemoryPointer.new(:pointer).write_pointer(some_pointer.read_pointer) Doesn't do what you think. It reads a pointer from the memory pointed-to by some_pointer, allocates a new chunk of memory, and writes that pointer value into that memory - it doesn't copy the value of some_pointer to a new pointer. On 12 May 2010 02:02, Chuck Remes <cremes.devlist@mac.com> wrote:
http://wiki.github.com/ffi/ffi/pointers I'd appreciate it if someone more knowledgeable would check it over (the bottom section under the sub-heading) and verify that what I wrote is correct. It seems to be working for me but I've been wrong before! :) crI just wrote an update to the wiki page on pointers at