]> git.ipfire.org Git - thirdparty/git.git/commitdiff
reftable/system: add abstraction to mmap files
authorPatrick Steinhardt <ps@pks.im>
Thu, 2 Apr 2026 07:31:18 +0000 (09:31 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 2 Apr 2026 17:45:44 +0000 (10:45 -0700)
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>
reftable/blocksource.c
reftable/system.c
reftable/system.h

index 573c81287fe5d8bc8565bf95fa721e6d2e3e4d39..7f7441f751342ac254871edd7b68f5cc2afa9935 100644 (file)
@@ -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;
index cd76e56be8f16f2af8d6a9a00641ea4c6fe2735a..9063641f304c1b1d0e358c2178b88f28c2545278 100644 (file)
@@ -143,3 +143,23 @@ uint64_t reftable_time_ms(void)
 {
        return getnanotime() / 1000000;
 }
+
+int reftable_mmap(struct reftable_mmap *out, int fd, size_t len)
+{
+       void *data = xmmap_gently(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
+       if (data == MAP_FAILED)
+               return REFTABLE_IO_ERROR;
+
+       out->data = data;
+       out->size = len;
+
+       return 0;
+}
+
+int reftable_munmap(struct reftable_mmap *mmap)
+{
+       if (munmap(mmap->data, mmap->size) < 0)
+               return REFTABLE_IO_ERROR;
+       memset(mmap, 0, sizeof(*mmap));
+       return 0;
+}
index 071bfa3d58962cfe2632e224d2b4657cc7997d77..c0e2cbe0ffb90cd1b8fcb29ca6ba190a90a8ad45 100644 (file)
@@ -114,4 +114,22 @@ int flock_commit(struct reftable_flock *l);
 /* Report the time in milliseconds. */
 uint64_t reftable_time_ms(void);
 
+struct reftable_mmap {
+       void *data;
+       size_t size;
+       void *priv;
+};
+
+/*
+ * Map the file into memory. Returns 0 on success, a reftable error code on
+ * error.
+ */
+int reftable_mmap(struct reftable_mmap *out, int fd, size_t len);
+
+/*
+ * Unmap the file from memory. Returns 0 on success, a reftable error code on
+ * error.
+ */
+int reftable_munmap(struct reftable_mmap *mmap);
+
 #endif