view src/lib/mmap-util.c @ 1793:82e19ad18784 HEAD

If we don't have function foo, do it always as #define foo my_foo and create your own my_foo. Did this to pread/pwrite/madvise.
author Timo Sirainen <tss@iki.fi>
date Fri, 03 Oct 2003 17:50:48 +0300
parents 9df02b1533b3
children 7d6ca38c8c37
line wrap: on
line source

/* Copyright (c) 2002-2003 Timo Sirainen */

#include "lib.h"
#include "mmap-util.h"

#include <sys/stat.h>

void *mmap_file(int fd, size_t *length, int prot)
{
	struct stat st;

	if (fstat(fd, &st) < 0)
		return MAP_FAILED;

	if (st.st_size > SSIZE_T_MAX) {
		/* too large file to map into memory */
		errno = EFBIG;
		return MAP_FAILED;
	}

	*length = (size_t)st.st_size;
	if (*length == 0)
		return NULL;

	i_assert(*length > 0 && *length < SSIZE_T_MAX);

	return mmap(NULL, *length, prot, MAP_SHARED, fd, 0);
}

void *mmap_ro_file(int fd, size_t *length)
{
	return mmap_file(fd, length, PROT_READ);
}

void *mmap_rw_file(int fd, size_t *length)
{
	return mmap_file(fd, length, PROT_READ | PROT_WRITE);
}

#ifndef HAVE_MADVISE
int my_madvise(void *start __attr_unused__, size_t length __attr_unused__,
	       int advice __attr_unused__)
{
}
#endif