aboutsummaryrefslogtreecommitdiffstats
path: root/reftable/record.c
diff options
context:
space:
mode:
Diffstat (limited to 'reftable/record.c')
-rw-r--r--reftable/record.c125
1 files changed, 59 insertions, 66 deletions
diff --git a/reftable/record.c b/reftable/record.c
index 04429d23fe..8919df8a4d 100644
--- a/reftable/record.c
+++ b/reftable/record.c
@@ -21,47 +21,49 @@ static void *reftable_record_data(struct reftable_record *rec);
int get_var_int(uint64_t *dest, struct string_view *in)
{
- int ptr = 0;
+ const unsigned char *buf = in->buf;
+ unsigned char c;
uint64_t val;
- if (in->len == 0)
+ if (!in->len)
return -1;
- val = in->buf[ptr] & 0x7f;
-
- while (in->buf[ptr] & 0x80) {
- ptr++;
- if (ptr > in->len) {
+ c = *buf++;
+ val = c & 0x7f;
+
+ while (c & 0x80) {
+ /*
+ * We use a micro-optimization here: whenever we see that the
+ * 0x80 bit is set, we know that the remainder of the value
+ * cannot be 0. The zero-values thus doesn't need to be encoded
+ * at all, which is why we subtract 1 when encoding and add 1
+ * when decoding.
+ *
+ * This allows us to save a byte in some edge cases.
+ */
+ val += 1;
+ if (!val || (val & (uint64_t)(~0ULL << (64 - 7))))
+ return -1; /* overflow */
+ if (buf >= in->buf + in->len)
return -1;
- }
- val = (val + 1) << 7 | (uint64_t)(in->buf[ptr] & 0x7f);
+ c = *buf++;
+ val = (val << 7) + (c & 0x7f);
}
*dest = val;
- return ptr + 1;
+ return buf - in->buf;
}
-int put_var_int(struct string_view *dest, uint64_t val)
+int put_var_int(struct string_view *dest, uint64_t value)
{
- uint8_t buf[10] = { 0 };
- int i = 9;
- int n = 0;
- buf[i] = (uint8_t)(val & 0x7f);
- i--;
- while (1) {
- val >>= 7;
- if (!val) {
- break;
- }
- val--;
- buf[i] = 0x80 | (uint8_t)(val & 0x7f);
- i--;
- }
-
- n = sizeof(buf) - i - 1;
- if (dest->len < n)
+ unsigned char varint[10];
+ unsigned pos = sizeof(varint) - 1;
+ varint[pos] = value & 0x7f;
+ while (value >>= 7)
+ varint[--pos] = 0x80 | (--value & 0x7f);
+ if (dest->len < sizeof(varint) - pos)
return -1;
- memcpy(dest->buf, &buf[i + 1], n);
- return n;
+ memcpy(dest->buf, varint + pos, sizeof(varint) - pos);
+ return sizeof(varint) - pos;
}
int reftable_is_block_type(uint8_t typ)
@@ -124,7 +126,7 @@ static int decode_string(struct reftable_buf *dest, struct string_view in)
static int encode_string(const char *str, struct string_view s)
{
struct string_view start = s;
- int l = strlen(str);
+ size_t l = strlen(str);
int n = put_var_int(&s, l);
if (n < 0)
return -1;
@@ -142,9 +144,9 @@ int reftable_encode_key(int *restart, struct string_view dest,
uint8_t extra)
{
struct string_view start = dest;
- int prefix_len = common_prefix_size(&prev_key, &key);
+ size_t prefix_len = common_prefix_size(&prev_key, &key);
uint64_t suffix_len = key.len - prefix_len;
- int n = put_var_int(&dest, (uint64_t)prefix_len);
+ int n = put_var_int(&dest, prefix_len);
if (n < 0)
return -1;
string_view_consume(&dest, n);
@@ -227,7 +229,7 @@ static int reftable_ref_record_key(const void *r, struct reftable_buf *dest)
}
static int reftable_ref_record_copy_from(void *rec, const void *src_rec,
- int hash_size)
+ uint32_t hash_size)
{
struct reftable_ref_record *ref = rec;
const struct reftable_ref_record *src = src_rec;
@@ -235,8 +237,6 @@ static int reftable_ref_record_copy_from(void *rec, const void *src_rec,
size_t refname_cap = 0;
int err;
- assert(hash_size > 0);
-
SWAP(refname, ref->refname);
SWAP(refname_cap, ref->refname_cap);
reftable_ref_record_release(ref);
@@ -317,13 +317,12 @@ static uint8_t reftable_ref_record_val_type(const void *rec)
}
static int reftable_ref_record_encode(const void *rec, struct string_view s,
- int hash_size)
+ uint32_t hash_size)
{
const struct reftable_ref_record *r =
(const struct reftable_ref_record *)rec;
struct string_view start = s;
int n = put_var_int(&s, r->update_index);
- assert(hash_size > 0);
if (n < 0)
return -1;
string_view_consume(&s, n);
@@ -363,7 +362,7 @@ static int reftable_ref_record_encode(const void *rec, struct string_view s,
static int reftable_ref_record_decode(void *rec, struct reftable_buf key,
uint8_t val_type, struct string_view in,
- int hash_size, struct reftable_buf *scratch)
+ uint32_t hash_size, struct reftable_buf *scratch)
{
struct reftable_ref_record *r = rec;
struct string_view start = in;
@@ -372,8 +371,6 @@ static int reftable_ref_record_decode(void *rec, struct reftable_buf key,
size_t refname_cap = 0;
int n, err;
- assert(hash_size > 0);
-
n = get_var_int(&update_index, &in);
if (n < 0)
return n;
@@ -449,7 +446,7 @@ static int reftable_ref_record_is_deletion_void(const void *p)
}
static int reftable_ref_record_equal_void(const void *a,
- const void *b, int hash_size)
+ const void *b, uint32_t hash_size)
{
struct reftable_ref_record *ra = (struct reftable_ref_record *) a;
struct reftable_ref_record *rb = (struct reftable_ref_record *) b;
@@ -493,7 +490,7 @@ static void reftable_obj_record_release(void *rec)
}
static int reftable_obj_record_copy_from(void *rec, const void *src_rec,
- int hash_size UNUSED)
+ uint32_t hash_size UNUSED)
{
struct reftable_obj_record *obj = rec;
const struct reftable_obj_record *src = src_rec;
@@ -525,7 +522,7 @@ static uint8_t reftable_obj_record_val_type(const void *rec)
}
static int reftable_obj_record_encode(const void *rec, struct string_view s,
- int hash_size UNUSED)
+ uint32_t hash_size UNUSED)
{
const struct reftable_obj_record *r = rec;
struct string_view start = s;
@@ -560,7 +557,7 @@ static int reftable_obj_record_encode(const void *rec, struct string_view s,
static int reftable_obj_record_decode(void *rec, struct reftable_buf key,
uint8_t val_type, struct string_view in,
- int hash_size UNUSED,
+ uint32_t hash_size UNUSED,
struct reftable_buf *scratch UNUSED)
{
struct string_view start = in;
@@ -568,7 +565,6 @@ static int reftable_obj_record_decode(void *rec, struct reftable_buf key,
uint64_t count = val_type;
int n = 0;
uint64_t last;
- int j;
reftable_obj_record_release(r);
@@ -603,8 +599,7 @@ static int reftable_obj_record_decode(void *rec, struct reftable_buf key,
string_view_consume(&in, n);
last = r->offsets[0];
- j = 1;
- while (j < count) {
+ for (uint64_t j = 1; j < count; j++) {
uint64_t delta = 0;
int n = get_var_int(&delta, &in);
if (n < 0) {
@@ -613,7 +608,6 @@ static int reftable_obj_record_decode(void *rec, struct reftable_buf key,
string_view_consume(&in, n);
last = r->offsets[j] = (delta + last);
- j++;
}
return start.len - in.len;
}
@@ -624,7 +618,7 @@ static int not_a_deletion(const void *p UNUSED)
}
static int reftable_obj_record_equal_void(const void *a, const void *b,
- int hash_size UNUSED)
+ uint32_t hash_size UNUSED)
{
struct reftable_obj_record *ra = (struct reftable_obj_record *) a;
struct reftable_obj_record *rb = (struct reftable_obj_record *) b;
@@ -699,7 +693,7 @@ static int reftable_log_record_key(const void *r, struct reftable_buf *dest)
}
static int reftable_log_record_copy_from(void *rec, const void *src_rec,
- int hash_size)
+ uint32_t hash_size)
{
struct reftable_log_record *dst = rec;
const struct reftable_log_record *src =
@@ -780,7 +774,7 @@ static uint8_t reftable_log_record_val_type(const void *rec)
}
static int reftable_log_record_encode(const void *rec, struct string_view s,
- int hash_size)
+ uint32_t hash_size)
{
const struct reftable_log_record *r = rec;
struct string_view start = s;
@@ -828,7 +822,7 @@ static int reftable_log_record_encode(const void *rec, struct string_view s,
static int reftable_log_record_decode(void *rec, struct reftable_buf key,
uint8_t val_type, struct string_view in,
- int hash_size, struct reftable_buf *scratch)
+ uint32_t hash_size, struct reftable_buf *scratch)
{
struct string_view start = in;
struct reftable_log_record *r = rec;
@@ -976,7 +970,7 @@ static int null_streq(const char *a, const char *b)
}
static int reftable_log_record_equal_void(const void *a,
- const void *b, int hash_size)
+ const void *b, uint32_t hash_size)
{
return reftable_log_record_equal((struct reftable_log_record *) a,
(struct reftable_log_record *) b,
@@ -1000,7 +994,7 @@ static int reftable_log_record_cmp_void(const void *_a, const void *_b)
}
int reftable_log_record_equal(const struct reftable_log_record *a,
- const struct reftable_log_record *b, int hash_size)
+ const struct reftable_log_record *b, uint32_t hash_size)
{
if (!(null_streq(a->refname, b->refname) &&
a->update_index == b->update_index &&
@@ -1054,7 +1048,7 @@ static int reftable_index_record_key(const void *r, struct reftable_buf *dest)
}
static int reftable_index_record_copy_from(void *rec, const void *src_rec,
- int hash_size UNUSED)
+ uint32_t hash_size UNUSED)
{
struct reftable_index_record *dst = rec;
const struct reftable_index_record *src = src_rec;
@@ -1081,7 +1075,7 @@ static uint8_t reftable_index_record_val_type(const void *rec UNUSED)
}
static int reftable_index_record_encode(const void *rec, struct string_view out,
- int hash_size UNUSED)
+ uint32_t hash_size UNUSED)
{
const struct reftable_index_record *r =
(const struct reftable_index_record *)rec;
@@ -1099,7 +1093,7 @@ static int reftable_index_record_encode(const void *rec, struct string_view out,
static int reftable_index_record_decode(void *rec, struct reftable_buf key,
uint8_t val_type UNUSED,
struct string_view in,
- int hash_size UNUSED,
+ uint32_t hash_size UNUSED,
struct reftable_buf *scratch UNUSED)
{
struct string_view start = in;
@@ -1120,7 +1114,7 @@ static int reftable_index_record_decode(void *rec, struct reftable_buf key,
}
static int reftable_index_record_equal(const void *a, const void *b,
- int hash_size UNUSED)
+ uint32_t hash_size UNUSED)
{
struct reftable_index_record *ia = (struct reftable_index_record *) a;
struct reftable_index_record *ib = (struct reftable_index_record *) b;
@@ -1154,14 +1148,14 @@ int reftable_record_key(struct reftable_record *rec, struct reftable_buf *dest)
}
int reftable_record_encode(struct reftable_record *rec, struct string_view dest,
- int hash_size)
+ uint32_t hash_size)
{
return reftable_record_vtable(rec)->encode(reftable_record_data(rec),
dest, hash_size);
}
int reftable_record_copy_from(struct reftable_record *rec,
- struct reftable_record *src, int hash_size)
+ struct reftable_record *src, uint32_t hash_size)
{
assert(src->type == rec->type);
@@ -1176,7 +1170,7 @@ uint8_t reftable_record_val_type(struct reftable_record *rec)
}
int reftable_record_decode(struct reftable_record *rec, struct reftable_buf key,
- uint8_t extra, struct string_view src, int hash_size,
+ uint8_t extra, struct string_view src, uint32_t hash_size,
struct reftable_buf *scratch)
{
return reftable_record_vtable(rec)->decode(reftable_record_data(rec),
@@ -1203,7 +1197,7 @@ int reftable_record_cmp(struct reftable_record *a, struct reftable_record *b)
reftable_record_data(a), reftable_record_data(b));
}
-int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size)
+int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, uint32_t hash_size)
{
if (a->type != b->type)
return 0;
@@ -1211,7 +1205,7 @@ int reftable_record_equal(struct reftable_record *a, struct reftable_record *b,
reftable_record_data(a), reftable_record_data(b), hash_size);
}
-static int hash_equal(const unsigned char *a, const unsigned char *b, int hash_size)
+static int hash_equal(const unsigned char *a, const unsigned char *b, uint32_t hash_size)
{
if (a && b)
return !memcmp(a, b, hash_size);
@@ -1220,9 +1214,8 @@ static int hash_equal(const unsigned char *a, const unsigned char *b, int hash_s
}
int reftable_ref_record_equal(const struct reftable_ref_record *a,
- const struct reftable_ref_record *b, int hash_size)
+ const struct reftable_ref_record *b, uint32_t hash_size)
{
- assert(hash_size > 0);
if (!null_streq(a->refname, b->refname))
return 0;