Skip to content

Commit

Permalink
bupsplit: expose functionality in the header file
Browse files Browse the repository at this point in the history
This will be used later by the HashSplitter and RecordHashSplitter
classes.

Since I now declared inlines in the header file, drop the
C++ guards, we don't use C++ anyway.

Signed-off-by: Johannes Berg <[email protected]>
  • Loading branch information
jmberg authored and rlbdv committed Nov 12, 2022
1 parent a49b687 commit 8cc0615
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 54 deletions.
47 changes: 0 additions & 47 deletions lib/bup/bupsplit.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,53 +34,6 @@
#include <stdlib.h>
#include <stdio.h>

// According to librsync/rollsum.h:
// "We should make this something other than zero to improve the
// checksum algorithm: tridge suggests a prime number."
// apenwarr: I unscientifically tried 0 and 7919, and they both ended up
// slightly worse than the librsync value of 31 for my arbitrary test data.
#define ROLLSUM_CHAR_OFFSET 31

typedef struct {
unsigned s1, s2;
uint8_t window[BUP_WINDOWSIZE];
int wofs;
} Rollsum;


// These formulas are based on rollsum.h in the librsync project.
static void rollsum_add(Rollsum *r, uint8_t drop, uint8_t add)
{
r->s1 += add - drop;
r->s2 += r->s1 - (BUP_WINDOWSIZE * (drop + ROLLSUM_CHAR_OFFSET));
}


static void rollsum_init(Rollsum *r)
{
r->s1 = BUP_WINDOWSIZE * ROLLSUM_CHAR_OFFSET;
r->s2 = BUP_WINDOWSIZE * (BUP_WINDOWSIZE-1) * ROLLSUM_CHAR_OFFSET;
r->wofs = 0;
memset(r->window, 0, BUP_WINDOWSIZE);
}


// For some reason, gcc 4.3 (at least) optimizes badly if find_ofs()
// is static and rollsum_roll is an inline function. Let's use a macro
// here instead to help out the optimizer.
#define rollsum_roll(r, ch) do { \
rollsum_add((r), (r)->window[(r)->wofs], (ch)); \
(r)->window[(r)->wofs] = (ch); \
(r)->wofs = ((r)->wofs + 1) % BUP_WINDOWSIZE; \
} while (0)


static uint32_t rollsum_digest(Rollsum *r)
{
return (r->s1 << 16) | (r->s2 & 0xffff);
}


static uint32_t rollsum_sum(uint8_t *buf, size_t ofs, size_t len)
{
size_t count;
Expand Down
50 changes: 43 additions & 7 deletions lib/bup/bupsplit.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,57 @@
*/
#ifndef __BUPSPLIT_H
#define __BUPSPLIT_H
#include <string.h>
#include <stdint.h>

#define BUP_BLOBBITS (13)
#define BUP_BLOBSIZE (1<<BUP_BLOBBITS)
#define BUP_WINDOWBITS (6)
#define BUP_WINDOWSIZE (1<<BUP_WINDOWBITS)

#ifdef __cplusplus
extern "C" {
#endif
// According to librsync/rollsum.h:
// "We should make this something other than zero to improve the
// checksum algorithm: tridge suggests a prime number."
// apenwarr: I unscientifically tried 0 and 7919, and they both ended up
// slightly worse than the librsync value of 31 for my arbitrary test data.
#define ROLLSUM_CHAR_OFFSET 31

typedef struct {
unsigned s1, s2;
uint8_t window[BUP_WINDOWSIZE];
int wofs;
} Rollsum;

static inline void rollsum_init(Rollsum *r)
{
r->s1 = BUP_WINDOWSIZE * ROLLSUM_CHAR_OFFSET;
r->s2 = BUP_WINDOWSIZE * (BUP_WINDOWSIZE-1) * ROLLSUM_CHAR_OFFSET;
r->wofs = 0;
memset(r->window, 0, BUP_WINDOWSIZE);
}

// These formulas are based on rollsum.h in the librsync project.
static inline void rollsum_add(Rollsum *r, uint8_t drop, uint8_t add)
{
r->s1 += add - drop;
r->s2 += r->s1 - (BUP_WINDOWSIZE * (drop + ROLLSUM_CHAR_OFFSET));
}

// For some reason, gcc 4.3 (at least) optimizes badly if find_ofs()
// is static and rollsum_roll is an inline function. Let's use a macro
// here instead to help out the optimizer.
#define rollsum_roll(r, ch) do { \
rollsum_add((r), (r)->window[(r)->wofs], ch); \
(r)->window[(r)->wofs] = ch; \
(r)->wofs = ((r)->wofs + 1) % BUP_WINDOWSIZE; \
} while (0)

static inline uint32_t rollsum_digest(Rollsum *r)
{
return (r->s1 << 16) | (r->s2 & 0xffff);
}

int bupsplit_find_ofs(const unsigned char *buf, int len, int *bits);
int bupsplit_selftest(void);

#ifdef __cplusplus
}
#endif

#endif /* __BUPSPLIT_H */

0 comments on commit 8cc0615

Please sign in to comment.