Skip to content

Commit

Permalink
Simplify implemmentation and invocation of block_is_zeros() function.
Browse files Browse the repository at this point in the history
  • Loading branch information
archiecobbs committed May 23, 2022
1 parent 618bfa1 commit 3e83d48
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion http_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -1796,7 +1796,7 @@ http_io_write_block(struct s3backer_store *const s3b, s3b_block_t block_num, con
return EINVAL;

// Detect zero blocks (if not done already by upper layer)
if (src != NULL && block_is_zeros(src, config->block_size))
if (src != NULL && block_is_zeros(src))
src = NULL;

// Don't write zero blocks when bitmap indicates empty until non-zero content is written
Expand Down
4 changes: 2 additions & 2 deletions s3b_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1711,8 +1711,8 @@ validate_config(int parse_only)
config.num_blocks = (s3b_block_t)big_num_blocks;

// Allocate zero block
if ((zero_block = calloc(1, config.block_size)) == NULL) {
warn("calloc");
if (init_zero_block(config.block_size) == -1) {
warn("init_zero_block");
return -1;
}

Expand Down
2 changes: 1 addition & 1 deletion test_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ test_io_write_block(struct s3backer_store *const s3b, s3b_block_t block_num, con
int r;

// Check for zero block
if (src != NULL && memcmp(src, zero_block, config->block_size) == 0)
if (src != NULL && block_is_zeros(src))
src = NULL;

// Compute MD5
Expand Down
27 changes: 14 additions & 13 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ int log_enable_debug;

// A block's worth of data containing only zero bytes
const void *zero_block;
static size_t zero_block_size;

// stderr logging mutex
static pthread_mutex_t stderr_log_mutex = PTHREAD_MUTEX_INITIALIZER;
Expand Down Expand Up @@ -259,21 +260,21 @@ bitmap_not(bitmap_t *bitmap, s3b_block_t num_blocks)
}

int
block_is_zeros(const void *data, u_int block_size)
init_zero_block(u_int block_size)
{
static const u_long zero;
const u_int *ptr;
int i;
assert(zero_block == NULL);
if ((zero_block = calloc(1, block_size)) == NULL)
return -1;
zero_block_size = block_size;
return 0;
}

if (block_size <= sizeof(zero))
return memcmp(data, &zero, block_size) == 0;
assert(block_size % sizeof(*ptr) == 0);
ptr = (const u_int *)data;
for (i = 0; i < block_size / sizeof(*ptr); i++) {
if (*ptr++ != 0)
return 0;
}
return 1;
int
block_is_zeros(const void *data)
{
assert(zero_block != NULL);
assert(zero_block_size > 0);
return memcmp(data, zero_block, zero_block_size) == 0;
}

void
Expand Down
3 changes: 2 additions & 1 deletion util.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,14 @@ extern void describe_size(char *buf, int bmax, uintmax_t value);
extern void syslog_logger(int level, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
extern void stderr_logger(int level, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
extern int find_string_in_table(const char *const *table, const char *value);
extern int block_is_zeros(const void *data, u_int block_size);
extern int block_is_zeros(const void *data);
extern int snvprintf(char *buf, int bufsize, const char *format, ...) __attribute__ ((__format__ (__printf__, 3, 4)));
extern char *prefix_log_format(int level, const char *fmt);
extern void calculate_boundary_info(struct boundary_info *info, u_int block_size, const void *buf, size_t size, off_t offset);
extern pid_t fork_off(const char *executable, char **argv);
extern int add_string(struct string_array *array, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
extern void free_strings(struct string_array *array);
extern int init_zero_block(u_int block_size);

// Bitmaps
extern bitmap_t *bitmap_init(s3b_block_t num_blocks, int value);
Expand Down
5 changes: 2 additions & 3 deletions zero_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ zero_cache_read_block(struct s3backer_store *const s3b, s3b_block_t block_num, v

// Update cache - if read was successful (or EEXIST case)
if (r == 0 || (expect_etag != NULL && !strict && r == EEXIST && memcmp(expect_etag, zero_etag, MD5_DIGEST_LENGTH) == 0)) {
const int zero = block_is_zeros(dest, config->block_size);
const int zero = block_is_zeros(dest);
pthread_mutex_lock(&priv->mutex);
zero_cache_update_block(priv, block_num, zero);
CHECK_RETURN(pthread_mutex_unlock(&priv->mutex));
Expand All @@ -382,12 +382,11 @@ zero_cache_write_block(struct s3backer_store *const s3b, s3b_block_t block_num,
check_cancel_t *check_cancel, void *check_cancel_arg)
{
struct zero_cache_private *const priv = s3b->data;
struct zero_cache_conf *const config = priv->config;
int known_zero;
int r;

// Detect zero blocks
if (src != NULL && block_is_zeros(src, config->block_size))
if (src != NULL && block_is_zeros(src))
src = NULL;
known_zero = src == NULL;

Expand Down

0 comments on commit 3e83d48

Please sign in to comment.