istream-mmap.c revision dd62b77c932d1b518f2a3e4bf80e36542becc256
/* Copyright (c) 2002-2003 Timo Sirainen */
#include "lib.h"
#include "mmap-util.h"
#include "istream-internal.h"
#include <unistd.h>
#include <sys/stat.h>
struct mmap_istream {
struct _istream istream;
int fd;
void *mmap_base;
off_t mmap_offset;
size_t mmap_block_size;
uoff_t v_size;
unsigned int autoclose_fd:1;
};
static size_t mmap_pagesize = 0;
static size_t mmap_pagemask = 0;
static void _close(struct _iostream *stream)
{
struct mmap_istream *mstream = (struct mmap_istream *) stream;
if (mstream->autoclose_fd && mstream->fd != -1) {
if (close(mstream->fd) < 0)
i_error("mmap_istream.close() failed: %m");
mstream->fd = -1;
}
}
static void i_stream_munmap(struct mmap_istream *mstream)
{
struct _istream *_stream = &mstream->istream;
if (_stream->buffer != NULL) {
if (munmap(mstream->mmap_base, _stream->buffer_size) < 0)
i_error("mmap_istream.munmap() failed: %m");
mstream->mmap_base = NULL;
_stream->buffer = NULL;
_stream->buffer_size = 0;
mstream->mmap_offset = 0;
}
}
static void _destroy(struct _iostream *stream)
{
struct mmap_istream *mstream = (struct mmap_istream *) stream;
i_stream_munmap(mstream);
}
static void _set_max_buffer_size(struct _iostream *stream, size_t max_size)
{
struct mmap_istream *mstream = (struct mmap_istream *) stream;
/* allow only full page sizes */
if (max_size < mmap_pagesize)
mstream->mmap_block_size = mmap_pagesize;
else {
if (max_size % mmap_pagesize != 0)
max_size += mmap_pagesize - (max_size % mmap_pagesize);
mstream->mmap_block_size = max_size;
}
}
static void _set_blocking(struct _iostream *stream __attr_unused__,
int timeout_msecs __attr_unused__,
void (*timeout_cb)(void *) __attr_unused__,
void *context __attr_unused__)
{
/* we never block */
}
static ssize_t _read(struct _istream *stream)
{
struct mmap_istream *mstream = (struct mmap_istream *) stream;
size_t aligned_skip;
uoff_t top;
stream->istream.stream_errno = 0;
if (stream->pos < stream->buffer_size) {
/* more bytes available without needing to mmap() */
stream->pos = stream->buffer_size;
return stream->pos - stream->skip;
}
if (stream->istream.v_offset >= mstream->v_size) {
stream->istream.eof = TRUE;
return -1;
}
aligned_skip = stream->skip & ~mmap_pagemask;
if (aligned_skip == 0 && mstream->mmap_base != NULL) {
/* didn't skip enough bytes */
return -2;
}
stream->skip -= aligned_skip;
mstream->mmap_offset += aligned_skip;
if (mstream->mmap_base != NULL) {
if (munmap(mstream->mmap_base, stream->buffer_size) < 0)
i_error("io_stream_read_mmaped(): munmap() failed: %m");
}
top = stream->abs_start_offset + mstream->v_size - mstream->mmap_offset;
stream->buffer_size = I_MIN(top, mstream->mmap_block_size);
i_assert((uoff_t)mstream->mmap_offset + stream->buffer_size <=
stream->abs_start_offset + mstream->v_size);
mstream->mmap_base = mmap(NULL, stream->buffer_size,
PROT_READ, MAP_PRIVATE,
mstream->fd, mstream->mmap_offset);
if (mstream->mmap_base == MAP_FAILED) {
stream->istream.stream_errno = errno;
mstream->mmap_base = NULL;
stream->buffer = NULL;
stream->buffer_size = 0;
stream->skip = stream->pos = 0;
i_error("mmap_istream.mmap() failed: %m");
return -1;
}
stream->buffer = mstream->mmap_base;
if (stream->buffer_size > mmap_pagesize) {
if (madvise(mstream->mmap_base, stream->buffer_size,
MADV_SEQUENTIAL) < 0)
i_error("mmap_istream.madvise(): %m");
}
stream->pos = stream->buffer_size;
return stream->pos - stream->skip;
}
static void _seek(struct _istream *stream, uoff_t v_offset)
{
struct mmap_istream *mstream = (struct mmap_istream *) stream;
uoff_t abs_offset;
abs_offset = stream->abs_start_offset + v_offset;
if (stream->buffer_size != 0 &&
(uoff_t)mstream->mmap_offset <= abs_offset &&
(uoff_t)mstream->mmap_offset + stream->buffer_size > abs_offset) {
/* already mmaped */
stream->skip = stream->pos = abs_offset - mstream->mmap_offset;
} else {
/* force reading next time */
i_stream_munmap(mstream);
stream->skip = stream->pos = abs_offset;
}
stream->istream.v_offset = v_offset;
}
static uoff_t _get_size(struct _istream *stream)
{
struct mmap_istream *mstream = (struct mmap_istream *) stream;
return mstream->v_size;
}
struct istream *i_stream_create_mmap(int fd, pool_t pool, size_t block_size,
uoff_t start_offset, uoff_t v_size,
int autoclose_fd)
{
struct mmap_istream *mstream;
struct istream *istream;
struct stat st;
if (mmap_pagesize == 0) {
mmap_pagesize = getpagesize();
mmap_pagemask = mmap_pagesize-1;
}
if (v_size == 0) {
if (fstat(fd, &st) < 0)
i_error("i_stream_create_mmap(): fstat() failed: %m");
else {
v_size = st.st_size;
if (start_offset > v_size)
start_offset = v_size;
v_size -= start_offset;
}
}
mstream = p_new(pool, struct mmap_istream, 1);
mstream->fd = fd;
_set_max_buffer_size(&mstream->istream.iostream, block_size);
mstream->autoclose_fd = autoclose_fd;
mstream->v_size = v_size;
mstream->istream.iostream.close = _close;
mstream->istream.iostream.destroy = _destroy;
mstream->istream.iostream.set_max_buffer_size = _set_max_buffer_size;
mstream->istream.iostream.set_blocking = _set_blocking;
mstream->istream.read = _read;
mstream->istream.seek = _seek;
mstream->istream.get_size = _get_size;
istream = _i_stream_create(&mstream->istream, pool, fd, start_offset);
istream->mmaped = TRUE;
return istream;
}