Method: ConcurrentSHM::Region.map

Defined in:
ext/concurrent-shm/posix.c

.map(file, off, len) ⇒ Region

Memory-map a region of a file or shared memory space. The region is read/write and shared.

Parameters:

  • file (IO|SharedMemory)

    the file or shared memory space

  • off (Integer)

    the offset of the region

  • len (Integer)

    the length of the region

Returns:

  • (Region)

    the mapped region

Raises:

  • (Errno::EACCES)

    if the file is not a regular file, or the file is not open for reading, not open for writing, or append-only

  • (Errno::EAGAIN)

    if the file is locked or too much memory has been locked

  • (Errno::EBADF)

    if file.lineno is not a valid file descriptor

  • (Errno::EINVAL)

    if the length or offset are too large or the length is 0

  • (Errno::ENFILE)

    if the system-wide limit on the total number of open files has been reached

  • (Errno::ENODEV)

    if the underlying filesystem of the specified file does not support memory mapping

  • (Errno::ENOMEM)

    if no memory is available, or the process’s number of mappings limit has been reached

  • (Errno::EPERM)

    if the operation was prevented by a file seal



328
329
330
331
332
333
334
335
336
337
338
339
# File 'ext/concurrent-shm/posix.c', line 328

static VALUE region_map(VALUE self, VALUE file, VALUE off, VALUE len)
{
    UNUSED(self);

    int fd = FIX2INT(rb_funcall(file, rb_intern("fileno"), 0));
    void * data = mmap(NULL, FIX2INT(len), PROT_READ | PROT_WRITE, MAP_SHARED, fd, FIX2INT(off));
    if (data == MAP_FAILED) {
        rb_syserr_fail_strf(errno, "mmap(fd=%d, len=%d)", fd, FIX2INT(len));
    }

    return __new_region(data, FIX2INT(len), 1, file);
}