Skip to content

Commit

Permalink
A lot of style fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Kenrick <github@jack.fr.eu.org>
  • Loading branch information
Jack Kenrick committed Mar 2, 2015
1 parent 2258622 commit 9e4b2dc
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 79 deletions.
50 changes: 25 additions & 25 deletions bloom.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@
#include "bloom.h"
#include "csum.h"

static int bloom_check_add(struct bloom * bloom,
void * buffer, int len, int add)
static int bloom_check_add(struct bloom *bloom,
void *buffer, int len, int add)
{
if (bloom->ready == 0) {
fprintf(stderr, "bloom is not initialized!\n");
return -1;
}

int hits = 0;

unsigned char digest[DIGEST_LEN_MAX];

checksum_block(buffer, len, digest);

register unsigned int a = ((unsigned int*)digest)[0];
register unsigned int b = ((unsigned int*)digest)[1];
register unsigned int a = ((unsigned int *)digest)[0];
register unsigned int b = ((unsigned int *)digest)[1];

register unsigned int x;
register unsigned int i;
Expand All @@ -56,70 +56,70 @@ static int bloom_check_add(struct bloom * bloom,
if (c & mask) {
hits++;
} else {
if (add) {
if (add)
bloom->bf[byte] = c | mask;
}
}
}

/* element already in (or collision) */
if (hits == bloom->hashes) {
if (hits == bloom->hashes)
return 1;
}

return 0;
}

/* See http://en.wikipedia.org/wiki/Bloom_filter#Optimal_number_of_hash_functions */
int bloom_init(struct bloom * bloom, int entries, double error)
/*
* See
* http://en.wikipedia.org/wiki/Bloom_filter#Optimal_number_of_hash_functions
*/
int bloom_init(struct bloom *bloom, int entries, double error)
{
bloom->ready = 0;

if (entries < 1 || error == 0) {
if (entries < 1 || error == 0)
return 1;
}

bloom->entries = entries;
bloom->error = error;

double num = log(bloom->error);
double denom = 0.480453013918201; /* ln(2)^2 */

bloom->bpe = -(num / denom);

double dentries = (double)entries;

bloom->bits = (int)(dentries * bloom->bpe);

if (bloom->bits % 8) {
if (bloom->bits % 8)
bloom->bytes = (bloom->bits / 8) + 1;
} else {
else
bloom->bytes = bloom->bits / 8;
}

bloom->hashes = (int)ceil(0.693147180559945 * bloom->bpe); /* ln(2) */

bloom->bf = (unsigned char *)calloc(bloom->bytes, sizeof(unsigned char));
if (bloom->bf == NULL) {
bloom->bf = calloc(bloom->bytes, sizeof(unsigned char));
if (bloom->bf == NULL)
return 1;
}

bloom->ready = 1;
return 0;
}


int bloom_check(struct bloom * bloom, void * buffer, int len)
int bloom_check(struct bloom *bloom, void *buffer, int len)
{
return bloom_check_add(bloom, buffer, len, 0);
}


int bloom_add(struct bloom * bloom, void * buffer, int len)
int bloom_add(struct bloom *bloom, void *buffer, int len)
{
return bloom_check_add(bloom, buffer, len, 1);
}


void bloom_print(struct bloom * bloom)
void bloom_print(struct bloom *bloom)
{
printf("bloom at %p\n", (void *)bloom);
printf(" ->entries = %d\n", bloom->entries);
Expand All @@ -131,10 +131,10 @@ void bloom_print(struct bloom * bloom)
}


void bloom_free(struct bloom * bloom)
void bloom_free(struct bloom *bloom)
{
if (bloom->ready) {
if (bloom->ready)
free(bloom->bf);
}

bloom->ready = 0;
}
44 changes: 23 additions & 21 deletions bloom.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,25 @@
* every struct must be to bloom_init().
*
*/
struct bloom
{
// These fields are part of the public interface of this structure.
// Client code may read these values if desired. Client code MUST NOT
// modify any of these.
int entries;
double error;
int bits;
int bytes;
int hashes;
struct bloom {
/*
* These fields are part of the public interface of this structure.
* Client code may read these values if desired. Client code MUST NOT
* modify any of these.
*/
int entries;
double error;
int bits;
int bytes;
int hashes;

// Fields below are private to the implementation. These may go away or
// change incompatibly at any moment. Client code MUST NOT access or rely
// on these.
double bpe;
unsigned char * bf;
int ready;
/* Fields below are private to the implementation. These may go away or
* change incompatibly at any moment. Client code MUST NOT access or
* rely on these.
*/
double bpe;
unsigned char *bf;
int ready;
};


Expand Down Expand Up @@ -60,7 +62,7 @@ struct bloom
* 1 - on failure
*
*/
int bloom_init(struct bloom * bloom, int entries, double error);
int bloom_init(struct bloom *bloom, int entries, double error);


/** ***************************************************************************
Expand All @@ -80,7 +82,7 @@ int bloom_init(struct bloom * bloom, int entries, double error);
* -1 - bloom not initialized
*
*/
int bloom_check(struct bloom * bloom, void * buffer, int len);
int bloom_check(struct bloom *bloom, void *buffer, int len);


/** ***************************************************************************
Expand All @@ -101,14 +103,14 @@ int bloom_check(struct bloom * bloom, void * buffer, int len);
* -1 - bloom not initialized
*
*/
int bloom_add(struct bloom * bloom, void * buffer, int len);
int bloom_add(struct bloom *bloom, void *buffer, int len);


/** ***************************************************************************
* Print (to stdout) info about this bloom filter. Debugging aid.
*
*/
void bloom_print(struct bloom * bloom);
void bloom_print(struct bloom *bloom);


/** ***************************************************************************
Expand All @@ -124,6 +126,6 @@ void bloom_print(struct bloom * bloom);
* Return: none
*
*/
void bloom_free(struct bloom * bloom);
void bloom_free(struct bloom *bloom);

#endif
12 changes: 6 additions & 6 deletions d_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ int digest_insert(struct rb_root *root, struct d_tree *token)
struct d_tree *tmp;
int cmp;

while (*p) {
parent = *p;
while (*p) {
parent = *p;

tmp = rb_entry(parent, struct d_tree, t_node);
tmp = rb_entry(parent, struct d_tree, t_node);

cmp = memcmp(token->digest, tmp->digest, digest_len);
if (cmp < 0)
Expand All @@ -62,7 +62,7 @@ int digest_insert(struct rb_root *root, struct d_tree *token)
}

struct d_tree *digest_find(struct rb_root *root,
unsigned char* digest)
unsigned char *digest)
{
if (!root)
return NULL;
Expand All @@ -84,7 +84,7 @@ struct d_tree *digest_find(struct rb_root *root,
return NULL;
}

int digest_count(struct rb_root *root)
uint64_t digest_count(struct rb_root *root)
{
struct rb_node *n = rb_first(root);
int count;
Expand All @@ -93,7 +93,7 @@ int digest_count(struct rb_root *root)
count++;
n = rb_next(n);
}
return(count);
return count;
}

void digest_free(struct rb_root *root)
Expand Down
9 changes: 5 additions & 4 deletions d_tree.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#ifndef __DTREE__
#define __DTREE__

#include <stdint.h>
#include "rbtree.h"
#include "list.h"

struct d_tree {
unsigned char *digest;
struct rb_node t_node;
unsigned char *digest;
struct rb_node t_node;
};

struct d_tree *digest_new(unsigned char *digest);
int digest_insert(struct rb_root *root, struct d_tree *token);
struct d_tree *digest_find(struct rb_root *root,
unsigned char* digest);
unsigned char *digest);

int digest_count(struct rb_root *root);
uint64_t digest_count(struct rb_root *root);

#endif /* __DTREE__ */
7 changes: 4 additions & 3 deletions duperemove.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,11 @@ int main(int argc, char **argv)
printf("First run completed\n");
}

/* We will now reread the serialized file, and create a new shiny tree
with only 'almost-dups' hashes
*/
/* We will now reread the serialized file, and create a new
* shiny tree with only 'almost-dups' hashes
*/
struct hash_tree dups_tree;

init_hash_tree(&dups_tree);
read_hash_tree(serialize_fname, &dups_tree, &blocksize,
NULL, 0, &digest_tree);
Expand Down
Loading

0 comments on commit 9e4b2dc

Please sign in to comment.