[Previous] [Contents] [Next]

mem_offset(), mem_offset64()

Get the offset of a mapped typed memory block

Synopsis:

#include <sys/mman.h>

int mem_offset( const void * addr,
                int fd,
                size_t length,
                off_t * offset,
                size_t * contig_len );

int mem_offset64( const void * addr,
                  int fd,
                  size_t length,
                  off64_t * offset,
                  size_t * contig_len );

Library:

libc

Description:

The mem_offset() and mem_offset64() functions set the variable pointed to by offset to the offset (or location), within a typed memory object, of the memory block currently mapped at addr.

The typed memory object is identified by fd and must be the descriptor used (via mmap()) to establish the mapping that contains addr.

The length argument specifies the length of the block of memory you want the offset for. On return, the value pointed to by contig_len is either length, or the length of the largest contiguous block of typed memory that's currently mapped to the calling process starting at addr, whichever is smaller.

If the offset and contig_len values obtained from calling mem_offset() are used in a call to mmap() with a file descriptor that refers to the same memory pool as fd (either through the same port or through a different port), the memory region that's mapped must be exactly the same region that was mapped at addr in the address space of the process that called mem_offset().

QNX OS extension

If fd is specified as NOFD, offset is the offset into /dev/mem of addr (i.e. its physical address). If the memory object specified by fd isn't a typed memory object, or specified as NOFD, the call is failed.

Returns:

0
Success.
-1
An error occurred (errno is set).

Errors:

EACCES
The process hasn't mapped memory at the given address addr.
EBADF
Invalid open file descriptor fildes.
EINVAL
The file descriptor fildes doesn't correspond to the memory object mapped at addr.
ENODEV
The file descriptor fildes isn't connected to a memory object supported by this function.
ENOSYS
The mem_offset() function isn't supported by this implementation.

Examples:

#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/mman.h>

paddr_t mphys(void *addr) {
        off64_t                 offset;

        if(mem_offset64(addr, NOFD, 1, &offset, 0) == -1) {
                return -1;
        }
        return offset;
}

Classification:

mem_offset() is POSIX 1003.1j (draft); mem_offset64() is for large-file support

Safety:
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes

See also:

mmap(), posix_mem_offset(), posix_mem_offset64()


[Previous] [Contents] [Next]