[Win32utils-devel] Need some help with MMap#[] and MMap#[]=

Daniel Berger djberg96 at gmail.com
Sun Oct 22 21:34:36 EDT 2006


Hi all,

At RubyConf this year I talked about win32-mmap with Patrick Hurley and 
Ara Howard.  Ara mentioned that Guy's version of mmap (for Unix) 
contains some string-like methods that let you access pieces of the view 
like a string.  Patrick and I came up with this (which requires the 
latest windows-pr in cvs so that we can use a more flexible version of 
memcpy, as well as some constants for VirtualAlloc):

def [](offset, length)
   buf = 0.chr * length
   memcpy(buf, @address + offset, length)
   buf
end
      
def []=(offset, length, string)
   memcpy(@address + offset, string, string.length)
end

This works as is but Ara also mentioned that some methods, such as 
MMap#[]=, will autoexpand the allocation size if the offset + length is 
greater than the initial allocation size.  With the above implementation 
the string gets chopped:

mmap = MMap.new(:file => 'test.txt', :size => 3)
mmap[0,6] = "foobar" # only 'foo' gets written
mmap.close

I thought this was doable with VirtualAlloc, but I can't seem to get it 
to work:

def []=(offset, length, string)
   if offset + length > @size
      diff = offset + length - size
      addr = [@address + diff + 1].pack('L')
      if VirtualAlloc(addr, diff, MEM_COMMIT|MEM_RESERVE, 
PAGE_READWRITE) == 0
         raise Error, get_last_error
      end
   end
   memcpy(@address + offset, string, string.length)
end

When I try this, however, I get:

C:/ruby/lib/ruby/site_ruby/1.8/windows/memory.rb:82:in `call': NULL 
pointer given (ArgumentError)
        from C:/ruby/lib/ruby/site_ruby/1.8/windows/memory.rb:82:in 
`VirtualAlloc'
        from C:/Documents and 
Settings/djberge/workspace/win32-mmap/lib/win32/mmap.rb:309:in `[]='
        from test.rb:8

Am I using the right function? Do I have it declared correctly in 
windows-pr?  BTW, I also tried changed the function prototype for 
VirtualAlloc from 'PLLL' to just 'LLLL' and using the numeric address 
directly, but that didn't seem to help.

Any ideas?

Thanks,

Dan

PS - It was great to see the guys from win32utils-devel who showed up at 
RubyConf!


More information about the win32utils-devel mailing list