Commit 20e7f426 by Shawn O. Pearce

Add a simple mmap wrapper for cross-platform mmap usage

Win32 has a variant of mmap that is harder to use than POSIX, but
to run natively and efficiently on Win32 we need some form of it.

gitfo_map_ro() provides a basic mmap function for use in locations
where we need read-only random data access to large ranges of a file,
such as a pack-*.idx.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
parent 098ac57a
#include "common.h"
#include "fileops.h"
#include <sys/mman.h>
int gitfo_open(const char *path, int flags)
{
......@@ -106,6 +107,20 @@ void gitfo_free_buf(gitfo_buf *obj)
obj->data = NULL;
}
int gitfo_map_ro(gitfo_map *out, git_file fd, off_t begin, size_t len)
{
out->data = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, begin);
if (out->data == (void*)-1)
return git_os_error();
out->len = len;
return GIT_SUCCESS;
}
void gitfo_free_map(gitfo_map *out)
{
munmap(out->data, out->len);
}
/* cached diskio */
struct gitfo_cache {
git_file fd;
......
......@@ -30,6 +30,10 @@ typedef struct { /* file io buffer */
size_t len; /* data length */
} gitfo_buf;
typedef struct { /* memory mapped buffer */
void *data; /* data bytes */
size_t len; /* data length */
} gitfo_map;
extern int gitfo_exists(const char *path);
extern int gitfo_open(const char *path, int flags);
......@@ -44,6 +48,33 @@ extern int gitfo_read_file(gitfo_buf *obj, const char *path);
extern void gitfo_free_buf(gitfo_buf *obj);
/**
* Read-only map all or part of a file into memory.
* When possible this function should favor a virtual memory
* style mapping over some form of malloc()+read(), as the
* data access will be random and is not likely to touch the
* majority of the region requested.
*
* @param out buffer to populate with the mapping information.
* @param fd open descriptor to configure the mapping from.
* @param begin first byte to map, this should be page aligned.
* @param end number of bytes to map.
* @return
* - GIT_SUCCESS on success;
* - GIT_EOSERR on an unspecified OS related error.
*/
extern int gitfo_map_ro(
gitfo_map *out,
git_file fd,
off_t begin,
size_t len);
/**
* Release the memory associated with a previous memory mapping.
* @param map the mapping description previously configured.
*/
extern void gitfo_free_map(gitfo_map *map);
/**
* Walk each directory entry, except '.' and '..', calling fn(state).
*
* @param pathbuf buffer the function reads the initial directory
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment