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>

Patrick Steinhardt committed Apr 2, 2026 at 09:31 UTC 87e4eee3f94ec261a92a76d06261b227b00de461
3 files changed +45 -12
reftable/blocksource.c
+7 -12
@@ -93,13 +93,12 @@ void block_source_from_buf(struct reftable_block_source *bs,
93 }
94
95 struct file_block_source {
96 - uint64_t size;
97 - unsigned char *data;
96 + struct reftable_mmap mmap;
97 };
98
99 static uint64_t file_size(void *b)
100 {
102 - return ((struct file_block_source *)b)->size;
101 + return ((struct file_block_source *)b)->mmap.size;
102 }
103
104 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
108 static void file_close(void *v)
109 {
110 struct file_block_source *b = v;
112 - munmap(b->data, b->size);
111 + reftable_munmap(&b->mmap);
112 reftable_free(b);
113 }
114
@@ -117,8 +116,8 @@ static ssize_t file_read_data(void *v, struct reftable_block_data *dest, uint64_
116 uint32_t size)
117 {
118 struct file_block_source *b = v;
120 - assert(off + size <= b->size);
121 - dest->data = b->data + off;
119 + assert(off + size <= b->mmap.size);
120 + dest->data = (unsigned char *) b->mmap.data + off;
121 dest->len = size;
122 return size;
123 }
@@ -156,13 +155,9 @@ int reftable_block_source_from_file(struct reftable_block_source *bs,
155 goto out;
156 }
157
159 - p->size = st.st_size;
160 - p->data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
161 - if (p->data == MAP_FAILED) {
162 - err = REFTABLE_IO_ERROR;
163 - p->data = NULL;
158 + err = reftable_mmap(&p->mmap, fd, st.st_size);
159 + if (err < 0)
160 goto out;
165 - }
161
162 assert(!bs->ops);
163 bs->ops = &file_vtable;
reftable/system.c
+20
@@ -143,3 +143,23 @@ uint64_t reftable_time_ms(void)
143 {
144 return getnanotime() / 1000000;
145 }
146 +
147 +int reftable_mmap(struct reftable_mmap *out, int fd, size_t len)
148 +{
149 + void *data = xmmap_gently(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
150 + if (data == MAP_FAILED)
151 + return REFTABLE_IO_ERROR;
152 +
153 + out->data = data;
154 + out->size = len;
155 +
156 + return 0;
157 +}
158 +
159 +int reftable_munmap(struct reftable_mmap *mmap)
160 +{
161 + if (munmap(mmap->data, mmap->size) < 0)
162 + return REFTABLE_IO_ERROR;
163 + memset(mmap, 0, sizeof(*mmap));
164 + return 0;
165 +}
reftable/system.h
+18
@@ -114,4 +114,22 @@ int flock_commit(struct reftable_flock *l);
114 /* Report the time in milliseconds. */
115 uint64_t reftable_time_ms(void);
116
117 +struct reftable_mmap {
118 + void *data;
119 + size_t size;
120 + void *priv;
121 +};
122 +
123 +/*
124 + * Map the file into memory. Returns 0 on success, a reftable error code on
125 + * error.
126 + */
127 +int reftable_mmap(struct reftable_mmap *out, int fd, size_t len);
128 +
129 +/*
130 + * Unmap the file from memory. Returns 0 on success, a reftable error code on
131 + * error.
132 + */
133 +int reftable_munmap(struct reftable_mmap *mmap);
134 +
135 #endif