Skip to content

Commit bdbd4dc

Browse files
pks-tgitster
authored andcommitted
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>
1 parent c080add commit bdbd4dc

3 files changed

Lines changed: 45 additions & 12 deletions

File tree

reftable/blocksource.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,12 @@ void block_source_from_buf(struct reftable_block_source *bs,
9393
}
9494

9595
struct file_block_source {
96-
uint64_t size;
97-
unsigned char *data;
96+
struct reftable_mmap mmap;
9897
};
9998

10099
static uint64_t file_size(void *b)
101100
{
102-
return ((struct file_block_source *)b)->size;
101+
return ((struct file_block_source *)b)->mmap.size;
103102
}
104103

105104
static void file_release_data(void *b REFTABLE_UNUSED, struct reftable_block_data *dest REFTABLE_UNUSED)
@@ -109,16 +108,16 @@ static void file_release_data(void *b REFTABLE_UNUSED, struct reftable_block_dat
109108
static void file_close(void *v)
110109
{
111110
struct file_block_source *b = v;
112-
munmap(b->data, b->size);
111+
reftable_munmap(&b->mmap);
113112
reftable_free(b);
114113
}
115114

116115
static ssize_t file_read_data(void *v, struct reftable_block_data *dest, uint64_t off,
117116
uint32_t size)
118117
{
119118
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;
122121
dest->len = size;
123122
return size;
124123
}
@@ -156,13 +155,9 @@ int reftable_block_source_from_file(struct reftable_block_source *bs,
156155
goto out;
157156
}
158157

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)
164160
goto out;
165-
}
166161

167162
assert(!bs->ops);
168163
bs->ops = &file_vtable;

reftable/system.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,23 @@ uint64_t reftable_time_ms(void)
137137
{
138138
return getnanotime() / 1000000;
139139
}
140+
141+
int reftable_mmap(struct reftable_mmap *out, int fd, size_t len)
142+
{
143+
void *data = xmmap_gently(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
144+
if (data == MAP_FAILED)
145+
return REFTABLE_IO_ERROR;
146+
147+
out->data = data;
148+
out->size = len;
149+
150+
return 0;
151+
}
152+
153+
int reftable_munmap(struct reftable_mmap *mmap)
154+
{
155+
if (munmap(mmap->data, mmap->size) < 0)
156+
return REFTABLE_IO_ERROR;
157+
memset(mmap, 0, sizeof(*mmap));
158+
return 0;
159+
}

reftable/system.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,22 @@ int flock_commit(struct reftable_flock *l);
113113
/* Report the time in milliseconds. */
114114
uint64_t reftable_time_ms(void);
115115

116+
struct reftable_mmap {
117+
void *data;
118+
size_t size;
119+
void *priv;
120+
};
121+
122+
/*
123+
* Map the file into memory. Returns 0 on success, a reftable error code on
124+
* error.
125+
*/
126+
int reftable_mmap(struct reftable_mmap *out, int fd, size_t len);
127+
128+
/*
129+
* Unmap the file from memory. Returns 0 on success, a reftable error code on
130+
* error.
131+
*/
132+
int reftable_munmap(struct reftable_mmap *mmap);
133+
116134
#endif

0 commit comments

Comments
 (0)