matz / zetabits.com (Yukihiro Matsumoto) wrote:
> Hi,
> 
> In message "[ruby-talk:7776] Re: String as IO and/or File object?"
>     on 00/12/21, ts <decoux / moulon.inra.fr> writes:
> 
> |Y> |mf = open("bigimage", "r").to_mmap("r")
> |Y> |mf.scan(/.../)...
> |
> |Y> Pretty interesting.  I wish to see the extension which implements this.
> |Y> I prefer just 'mmap' over 'to_mmap' though.
> |
> | It's not better to have String.mmap("path", "mode") ?
> 
> Probably.  It will be proved by the implementation.

It wouldn't be String.mmap() because that's not how the real mmap() works.  
The real mmap uses a file descriptor (that is, an open file) as the source:

     void *
     mmap(void * addr, size_t len, int prot, int flags, int fd, off_t offset)

Soooo....  the Ruby mmap would probably map to:

File#mmap(len = self.stat.size, prot = "r", flags = MAP_SHARED, offset = 0)

and return a MmapString < String.  The problem with implementation is thus:  
it will be hard.  There are LOTS of corner cases to consider.  A file isn't 
a memory location, but may be treated in a "somewhat" similar way -- it will 
have to be ftruncate()d/write()d to extend it if the string is to grow.

The biggest problem would be that it would have to either be a new type or a 
flag in T_STRING type... the flag would be okay but to do things cleanly 
without GCing the open fd, the object would have to reference the source 
file descriptor (err, File object), and there is no room left without 
bumping the size of RStructures in general.

If it were to be a new type, all checks for T_STRING would have to be 
changed to check for T_STRING or T_MMAPSTRING... all in all, I have no idea 
how I would properly implement it, but had I any good idea how I would do it 
:)

--
 Brian Fundakowski Feldman           \  FreeBSD: The Power to Serve!  /
 green / FreeBSD.org                    `------------------------------'