file-cache.c revision 77717f1e9e8442fd2cdbca87cbc45c03d0b5fcae
/* Copyright (c) 2004 Timo Sirainen */
#include "lib.h"
#include "buffer.h"
#include "mmap-util.h"
#include "file-cache.h"
struct file_cache {
int fd;
void *mmap_base;
};
{
struct file_cache *cache;
return cache;
}
{
i_error("munmap_anon() failed: %m");
}
}
{
}
{
if (size > SSIZE_T_MAX) {
/* make sure our calculations won't overflow. most likely
we'll be reading less data, but allow it anyway so caller
doesn't have to deal with any extra checks. */
size = SSIZE_T_MAX;
}
/* growing more than a megabyte, make sure that the
file is large enough so we don't allocate memory
more than needed */
i_error("fstat(file_cache) failed: %m");
return -1;
}
return 0;
}
}
/* grow mmaping */
return -1;
}
} else {
return -1;
}
}
}
while (psize > 0) {
/* page is already in cache */
dest_offset += page_size;
continue;
}
if (ret <= 0) {
if (ret < 0)
return -1;
/* EOF */
/* FIXME: we should mark the last block cached and
invalidate it only when trying to read past the
file */
return dest_offset <= offset ? 0 :
}
dest_offset += ret;
/* partial read - probably EOF but make sure. */
continue;
}
}
return size;
}
{
}
{
unsigned int i;
return;
if (size != 1) {
/* tell operating system that we don't need the memory anymore
and it may free it. don't bother to do it for single pages,
there's a good chance that they get re-read back
immediately. */
}
/* set the first byte */
mask |= 1 << i;
size--;
}
/* set the middle bytes */
/* set the last byte */
if (size > 0) {
mask = 0;
mask |= 1 << i;
}
}