view src/lib/mmap-util.c @ 221:ed0d5b17c7a4 HEAD

Added extra functions for easier printing of error messages. Moved file_set_size() to generic function in lib. If there's no space to build hash file, it builds itself in anon-mmaped memory and stays there.
author Timo Sirainen <tss@iki.fi>
date Fri, 13 Sep 2002 03:01:23 +0300
parents 4a7ab9e94f25
children cf4d065f2f85
line wrap: on
line source

/*
 mmap-util.c - Memory mapping utilities

    Copyright (c) 2002 Timo Sirainen

    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

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

#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
#  define MAP_ANONYMOUS MAP_ANON
#endif

static void *mmap_file(int fd, size_t *length, int access)
{
	off_t size;

	size = lseek(fd, 0, SEEK_END);
	if (size == -1)
		return MAP_FAILED;

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

	*length = (size_t)size;
	if (*length == 0)
		return NULL;

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

	return mmap(NULL, *length, access, 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);
}

void *mmap_aligned(int fd, int access, off_t offset, size_t length,
		   void **data_start, size_t *mmap_length)
{
	void *mmap_base;
	static int pagemask = 0;

	if (pagemask == 0) {
		pagemask = getpagesize();
		i_assert(pagemask > 0);
		pagemask--;
	}

	*mmap_length = length + (offset & pagemask);

	mmap_base = mmap(NULL, *mmap_length, access, MAP_SHARED,
			 fd, offset & ~pagemask);
	*data_start = mmap_base == MAP_FAILED || mmap_base == NULL ? NULL :
		(char *) mmap_base + (offset & pagemask);

	return mmap_base;
}

void *mmap_anonymous(size_t length)
{
	return mmap(NULL, length, PROT_READ | PROT_WRITE,
		    MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
}

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