aboutsummaryrefslogtreecommitdiff
path: root/reftable/blocksource.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2026-04-02 09:31:18 +0200
committerJunio C Hamano <gitster@pobox.com>2026-04-02 10:45:44 -0700
commit87e4eee3f94ec261a92a76d06261b227b00de461 (patch)
tree4e2baee0836ac16f4813ba107630a785683b3d55 /reftable/blocksource.c
parentcb0882de1979522b2fc3dc4c3064b0ad21d50b06 (diff)
downloadgit-87e4eee3f94ec261a92a76d06261b227b00de461.tar.xz
reftable/system: add abstraction to mmap files
In our codebase we have a couple of wrappers around mmap(3p) that allow us to reimplement the syscall on platforms that don't have it natively, like for example Windows. Other projects that embed the reftable library may have a different infra though to hook up mmap wrappers, but these are currently hard to integrate. Provide the infrastructure to let projects easily define the mmap interface with a custom struct and custom functions. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'reftable/blocksource.c')
-rw-r--r--reftable/blocksource.c19
1 files changed, 7 insertions, 12 deletions
diff --git a/reftable/blocksource.c b/reftable/blocksource.c
index 573c81287f..7f7441f751 100644
--- a/reftable/blocksource.c
+++ b/reftable/blocksource.c
@@ -93,13 +93,12 @@ void block_source_from_buf(struct reftable_block_source *bs,
}
struct file_block_source {
- uint64_t size;
- unsigned char *data;
+ struct reftable_mmap mmap;
};
static uint64_t file_size(void *b)
{
- return ((struct file_block_source *)b)->size;
+ return ((struct file_block_source *)b)->mmap.size;
}
static void file_release_data(void *b REFTABLE_UNUSED, struct reftable_block_data *dest REFTABLE_UNUSED)
@@ -109,7 +108,7 @@ static void file_release_data(void *b REFTABLE_UNUSED, struct reftable_block_dat
static void file_close(void *v)
{
struct file_block_source *b = v;
- munmap(b->data, b->size);
+ reftable_munmap(&b->mmap);
reftable_free(b);
}
@@ -117,8 +116,8 @@ static ssize_t file_read_data(void *v, struct reftable_block_data *dest, uint64_
uint32_t size)
{
struct file_block_source *b = v;
- assert(off + size <= b->size);
- dest->data = b->data + off;
+ assert(off + size <= b->mmap.size);
+ dest->data = (unsigned char *) b->mmap.data + off;
dest->len = size;
return size;
}
@@ -156,13 +155,9 @@ int reftable_block_source_from_file(struct reftable_block_source *bs,
goto out;
}
- p->size = st.st_size;
- p->data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
- if (p->data == MAP_FAILED) {
- err = REFTABLE_IO_ERROR;
- p->data = NULL;
+ err = reftable_mmap(&p->mmap, fd, st.st_size);
+ if (err < 0)
goto out;
- }
assert(!bs->ops);
bs->ops = &file_vtable;