Skip to content

Commit e5f5443

Browse files
committed
Merge branch 'ps/reftable-portability' into jch
Update reftable library part with what is used in libgit2 to improve portability to different target codebases and platforms. * ps/reftable-portability: reftable: introduce "reftable-system.h" header reftable/system: add abstraction to mmap files reftable/system: add abstraction to retrieve time in milliseconds reftable/fsck: use REFTABLE_UNUSED instead of UNUSED reftable/stack: don't call fsync(3p) unless provided reftable/system: provide `REFTABLE_INLINE()` macro
2 parents fb496d8 + 896e647 commit e5f5443

20 files changed

Lines changed: 96 additions & 61 deletions

reftable/basics.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,22 @@ char *reftable_buf_detach(struct reftable_buf *buf);
7575

7676
/* Bigendian en/decoding of integers */
7777

78-
static inline void reftable_put_be16(void *out, uint16_t i)
78+
REFTABLE_INLINE(void) reftable_put_be16(void *out, uint16_t i)
7979
{
8080
unsigned char *p = out;
8181
p[0] = (uint8_t)((i >> 8) & 0xff);
8282
p[1] = (uint8_t)((i >> 0) & 0xff);
8383
}
8484

85-
static inline void reftable_put_be24(void *out, uint32_t i)
85+
REFTABLE_INLINE(void) reftable_put_be24(void *out, uint32_t i)
8686
{
8787
unsigned char *p = out;
8888
p[0] = (uint8_t)((i >> 16) & 0xff);
8989
p[1] = (uint8_t)((i >> 8) & 0xff);
9090
p[2] = (uint8_t)((i >> 0) & 0xff);
9191
}
9292

93-
static inline void reftable_put_be32(void *out, uint32_t i)
93+
REFTABLE_INLINE(void) reftable_put_be32(void *out, uint32_t i)
9494
{
9595
unsigned char *p = out;
9696
p[0] = (uint8_t)((i >> 24) & 0xff);
@@ -99,7 +99,7 @@ static inline void reftable_put_be32(void *out, uint32_t i)
9999
p[3] = (uint8_t)((i >> 0) & 0xff);
100100
}
101101

102-
static inline void reftable_put_be64(void *out, uint64_t i)
102+
REFTABLE_INLINE(void) reftable_put_be64(void *out, uint64_t i)
103103
{
104104
unsigned char *p = out;
105105
p[0] = (uint8_t)((i >> 56) & 0xff);
@@ -112,22 +112,22 @@ static inline void reftable_put_be64(void *out, uint64_t i)
112112
p[7] = (uint8_t)((i >> 0) & 0xff);
113113
}
114114

115-
static inline uint16_t reftable_get_be16(const void *in)
115+
REFTABLE_INLINE(uint16_t) reftable_get_be16(const void *in)
116116
{
117117
const unsigned char *p = in;
118118
return (uint16_t)(p[0]) << 8 |
119119
(uint16_t)(p[1]) << 0;
120120
}
121121

122-
static inline uint32_t reftable_get_be24(const void *in)
122+
REFTABLE_INLINE(uint32_t) reftable_get_be24(const void *in)
123123
{
124124
const unsigned char *p = in;
125125
return (uint32_t)(p[0]) << 16 |
126126
(uint32_t)(p[1]) << 8 |
127127
(uint32_t)(p[2]) << 0;
128128
}
129129

130-
static inline uint32_t reftable_get_be32(const void *in)
130+
REFTABLE_INLINE(uint32_t) reftable_get_be32(const void *in)
131131
{
132132
const unsigned char *p = in;
133133
return (uint32_t)(p[0]) << 24 |
@@ -136,7 +136,7 @@ static inline uint32_t reftable_get_be32(const void *in)
136136
(uint32_t)(p[3]) << 0;
137137
}
138138

139-
static inline uint64_t reftable_get_be64(const void *in)
139+
REFTABLE_INLINE(uint64_t) reftable_get_be64(const void *in)
140140
{
141141
const unsigned char *p = in;
142142
return (uint64_t)(p[0]) << 56 |
@@ -187,7 +187,7 @@ void reftable_free(void *p);
187187
void *reftable_calloc(size_t nelem, size_t elsize);
188188
char *reftable_strdup(const char *str);
189189

190-
static inline int reftable_alloc_size(size_t nelem, size_t elsize, size_t *out)
190+
REFTABLE_INLINE(int) reftable_alloc_size(size_t nelem, size_t elsize, size_t *out)
191191
{
192192
if (nelem && elsize > SIZE_MAX / nelem)
193193
return -1;
@@ -215,7 +215,7 @@ static inline int reftable_alloc_size(size_t nelem, size_t elsize, size_t *out)
215215
} \
216216
} while (0)
217217

218-
static inline void *reftable_alloc_grow(void *p, size_t nelem, size_t elsize,
218+
REFTABLE_INLINE(void) *reftable_alloc_grow(void *p, size_t nelem, size_t elsize,
219219
size_t *allocp)
220220
{
221221
void *new_p;

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/fsck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static int table_check_name(struct reftable_table *table,
6363

6464
static int table_checks(struct reftable_table *table,
6565
reftable_fsck_report_fn report_fn,
66-
reftable_fsck_verbose_fn verbose_fn UNUSED,
66+
reftable_fsck_verbose_fn verbose_fn REFTABLE_UNUSED,
6767
void *cb_data)
6868
{
6969
table_check_fn table_check_fns[] = {

reftable/pq.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ int merged_iter_pqueue_add(struct merged_iter_pqueue *pq, const struct pq_entry
2727
void merged_iter_pqueue_release(struct merged_iter_pqueue *pq);
2828
int pq_less(struct pq_entry *a, struct pq_entry *b);
2929

30-
static inline struct pq_entry merged_iter_pqueue_top(struct merged_iter_pqueue pq)
30+
REFTABLE_INLINE(struct) pq_entry merged_iter_pqueue_top(struct merged_iter_pqueue pq)
3131
{
3232
return pq.heap[0];
3333
}
3434

35-
static inline int merged_iter_pqueue_is_empty(struct merged_iter_pqueue pq)
35+
REFTABLE_INLINE(int) merged_iter_pqueue_is_empty(struct merged_iter_pqueue pq)
3636
{
3737
return pq.len == 0;
3838
}

reftable/record.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct string_view {
2626
};
2727

2828
/* Advance `s.buf` by `n`, and decrease length. */
29-
static inline void string_view_consume(struct string_view *s, int n)
29+
REFTABLE_INLINE(void) string_view_consume(struct string_view *s, int n)
3030
{
3131
s->buf += n;
3232
s->len -= n;
@@ -147,7 +147,7 @@ int reftable_record_decode(struct reftable_record *rec, struct reftable_buf key,
147147
uint32_t hash_size, struct reftable_buf *scratch);
148148
int reftable_record_is_deletion(struct reftable_record *rec);
149149

150-
static inline uint8_t reftable_record_type(struct reftable_record *rec)
150+
REFTABLE_INLINE(uint8_t) reftable_record_type(struct reftable_record *rec)
151151
{
152152
return rec->type;
153153
}

reftable/reftable-basics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#ifndef REFTABLE_BASICS_H
1010
#define REFTABLE_BASICS_H
1111

12-
#include <stddef.h>
12+
#include "reftable-system.h"
1313

1414
/* A buffer that contains arbitrary byte slices. */
1515
struct reftable_buf {

reftable/reftable-block.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
#ifndef REFTABLE_BLOCK_H
1010
#define REFTABLE_BLOCK_H
1111

12-
#include <stdint.h>
13-
12+
#include "reftable-system.h"
1413
#include "reftable-basics.h"
1514
#include "reftable-blocksource.h"
1615
#include "reftable-iterator.h"

reftable/reftable-blocksource.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#ifndef REFTABLE_BLOCKSOURCE_H
1010
#define REFTABLE_BLOCKSOURCE_H
1111

12-
#include <stdint.h>
12+
#include "reftable-system.h"
1313

1414
/*
1515
* Generic wrapper for a seekable readable file.

reftable/reftable-error.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef REFTABLE_ERROR_H
1010
#define REFTABLE_ERROR_H
1111

12+
#include "reftable-system.h"
13+
1214
/*
1315
* Errors in reftable calls are signaled with negative integer return values. 0
1416
* means success.

reftable/reftable-fsck.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef REFTABLE_FSCK_H
22
#define REFTABLE_FSCK_H
33

4+
#include "reftable-system.h"
45
#include "reftable-stack.h"
56

67
enum reftable_fsck_error {

0 commit comments

Comments
 (0)