Skip to content

Commit

Permalink
code style update
Browse files Browse the repository at this point in the history
-all struct init functions follow a consistent style
-renamed struct init functions to new, because they allocate and initialize the struct
-conditional inclusion of assert on binary tree
  • Loading branch information
plerros committed Apr 15, 2021
1 parent c273bce commit 4b9fabd
Show file tree
Hide file tree
Showing 23 changed files with 146 additions and 87 deletions.
13 changes: 9 additions & 4 deletions helsing/src/binary_tree/bthandle.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@

#ifdef PROCESS_RESULTS
#include <stdlib.h>
#include <assert.h>
#include "llhandle.h"
#include "btnode.h"
#include "bthandle.h"
#endif

#if defined (PROCESS_RESULTS) && SANITY_CHECK
#include <assert.h>
#endif

#ifdef PROCESS_RESULTS
void bthandle_init(struct bthandle **ptr)
void bthandle_new(struct bthandle **ptr)
{
struct bthandle *new = NULL;
new = malloc(sizeof(struct bthandle));
if (ptr == NULL)
return;

struct bthandle *new = malloc(sizeof(struct bthandle));
if (new == NULL)
abort();

Expand Down
4 changes: 2 additions & 2 deletions helsing/src/binary_tree/bthandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct bthandle
struct btnode *node;
vamp_t size;
};
void bthandle_init(struct bthandle **ptr);
void bthandle_new(struct bthandle **ptr);
void bthandle_free(struct bthandle *handle);
void bthandle_add(struct bthandle *handle, vamp_t key);
void bthandle_reset(struct bthandle *handle);
Expand All @@ -32,7 +32,7 @@ void bthandle_cleanup(
struct bthandle
{
};
static inline void bthandle_init(__attribute__((unused)) struct bthandle **ptr)
static inline void bthandle_new(__attribute__((unused)) struct bthandle **ptr)
{
}
static inline void bthandle_free(
Expand Down
12 changes: 9 additions & 3 deletions helsing/src/binary_tree/btnode.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@

#ifdef PROCESS_RESULTS
#include <stdlib.h>
#include <assert.h>
#include "llhandle.h"
#include "btnode.h"
#endif

#if defined (PROCESS_RESULTS) && SANITY_CHECK
#include <assert.h>
#endif

#ifdef PROCESS_RESULTS

void btnode_init(struct btnode **ptr, vamp_t key)
void btnode_new(struct btnode **ptr, vamp_t key)
{
if (ptr == NULL)
return;

struct btnode *new = malloc(sizeof(struct btnode));
if (new == NULL)
abort();
Expand Down Expand Up @@ -155,7 +161,7 @@ struct btnode *btnode_add(
if (node == NULL) {
*count += 1;
struct btnode *tmpptr;
btnode_init(&tmpptr, key);
btnode_new(&tmpptr, key);
return (tmpptr);
}
if (key == node->key) {
Expand Down
4 changes: 2 additions & 2 deletions helsing/src/binary_tree/btnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct btnode
length_t height; // Should probably be less than 32
uint8_t fang_pairs;
};
void btnode_init(struct btnode **ptr, vamp_t key);
void btnode_new(struct btnode **ptr, vamp_t key);
void btnode_free(struct btnode *node);
struct btnode *btnode_add(
struct btnode *node,
Expand All @@ -37,7 +37,7 @@ struct btnode *btnode_cleanup(
struct btnode
{
};
static inline void btnode_init(
static inline void btnode_new(
__attribute__((unused)) struct btnode **ptr,
__attribute__((unused)) vamp_t key)
{
Expand Down
14 changes: 7 additions & 7 deletions helsing/src/hash/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@
#ifdef CHECKSUM_RESULTS
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <openssl/evp.h>
#include "hash.h"
#endif

#ifdef CHECKSUM_RESULTS

void hash_init(struct hash **ptr)
void hash_new(struct hash **ptr)
{
if (ptr == NULL)
return;

struct hash *new = malloc(sizeof(struct hash));
if (new == NULL)
abort();

OpenSSL_add_all_digests();

new->md = EVP_get_digestbyname(DIGEST_NAME);
if (!new->md) {
printf("Unknown message digest %s\n", DIGEST_NAME);
exit(1);
}
assert(new->md != NULL);

new->md_size = EVP_MD_size(new->md);
new->md_value = malloc(sizeof(unsigned char) * new->md_size);
Expand All @@ -34,8 +35,7 @@ void hash_init(struct hash **ptr)
new->md_value[i] = 0;

new->mdctx = EVP_MD_CTX_create();

*ptr = new;
*ptr = new;
}

void hash_free(struct hash *ptr)
Expand Down
4 changes: 2 additions & 2 deletions helsing/src/hash/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ struct hash
unsigned char *md_value;
int md_size;
};
void hash_init(struct hash **ptr);
void hash_new(struct hash **ptr);
void hash_free(struct hash *ptr);
void hash_print(struct hash *ptr);
#else /* CHECKSUM_RESULTS */
struct hash
{
};
static inline void hash_init(__attribute__((unused)) struct hash **ptr)
static inline void hash_new(__attribute__((unused)) struct hash **ptr)
{
}
static inline void hash_free(__attribute__((unused)) struct hash *ptr)
Expand Down
7 changes: 5 additions & 2 deletions helsing/src/linked_list/llhandle.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
#endif

#ifdef PROCESS_RESULTS
void llhandle_init(struct llhandle **ptr)
void llhandle_new(struct llhandle **ptr)
{
if (ptr == NULL)
return;

struct llhandle *new = malloc(sizeof(struct llhandle));
if (new == NULL)
abort();
Expand All @@ -35,7 +38,7 @@ void llhandle_add(struct llhandle *ptr, vamp_t value)
if (ptr == NULL)
return;

llnode_init(&(ptr->first), value, ptr->first);
llnode_add(&(ptr->first), value, ptr->first);
ptr->size += 1;
}

Expand Down
4 changes: 2 additions & 2 deletions helsing/src/linked_list/llhandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ struct llhandle
struct llnode *first;
vamp_t size;
};
void llhandle_init(struct llhandle **ptr);
void llhandle_new(struct llhandle **ptr);
void llhandle_free(struct llhandle *ptr);
void llhandle_add(struct llhandle *ptr, vamp_t value);
void llhandle_reset(struct llhandle *ptr);
#else /* PROCESS_RESULTS */
struct llhandle
{
};
static inline void llhandle_init(__attribute__((unused)) struct llhandle **ptr)
static inline void llhandle_new(__attribute__((unused)) struct llhandle **ptr)
{
struct llhandle *new = malloc(sizeof(struct llhandle));
if (new == NULL)
Expand Down
40 changes: 26 additions & 14 deletions helsing/src/linked_list/llnode.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,18 @@

#ifdef STORE_RESULTS

void llnode_init(struct llnode **ptr, vamp_t value, struct llnode *next)
void llnode_new(struct llnode **ptr, vamp_t value, struct llnode *next)
{
struct llnode *new;
if (next != NULL && next->current < LINK_SIZE) {
new = next;
new->value[new->current] = value;
new->current += 1;
} else {
new = malloc(sizeof(struct llnode));
if (new == NULL)
abort();
if (ptr == NULL)
return;

new->value[0] = value;
new->current = 1;
new->next = next;
}
struct llnode *new = malloc(sizeof(struct llnode));
if (new == NULL)
abort();

new->value[0] = value;
new->current = 1;
new->next = next;
*ptr = new;
}

Expand All @@ -48,6 +44,22 @@ void llnode_free(struct llnode *node)
}
}

void llnode_add(struct llnode **ptr, vamp_t value, struct llnode *next)
{
if (ptr == NULL)
return;

struct llnode *new;
if (next != NULL && next->current < LINK_SIZE) {
new = next;
new->value[new->current] = value;
new->current += 1;
} else {
llnode_new(&new, value, next);
}
*ptr = new;
}

#endif /* STORE_RESULTS */

#if defined(STORE_RESULTS) && defined(CHECKSUM_RESULTS)
Expand Down
11 changes: 9 additions & 2 deletions helsing/src/linked_list/llnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ struct llnode
uint16_t current;
struct llnode *next;
};
void llnode_init(struct llnode **ptr, vamp_t value, struct llnode *next);
void llnode_new(struct llnode **ptr, vamp_t value, struct llnode *next);
void llnode_free(struct llnode *list);
void llnode_add(struct llnode **ptr, vamp_t value, struct llnode *next);
#else /* STORE_RESULTS */
struct llnode
{
};
static inline void llnode_init(
static inline void llnode_new(
__attribute__((unused)) struct llnode **ptr,
__attribute__((unused)) vamp_t value,
__attribute__((unused)) struct llnode *next)
Expand All @@ -32,6 +33,12 @@ static inline void llnode_free(
__attribute__((unused)) struct llnode *list)
{
}
static inline void llnode_add(
__attribute__((unused)) struct llnode **ptr,
__attribute__((unused)) vamp_t value,
__attribute__((unused)) struct llnode *next)
{
}
#endif /* STORE_RESULTS */

#if defined(STORE_RESULTS) && defined(CHECKSUM_RESULTS)
Expand Down
6 changes: 4 additions & 2 deletions helsing/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ static vamp_t get_lmax(vamp_t lmin, vamp_t max)
int main(int argc, char *argv[])
{
vamp_t min, max;
struct taskboard *progress = taskboard_init();
struct taskboard *progress;
taskboard_new(&(progress));

#if !USE_CHECKPOINT
if (argc != 3) {
Expand Down Expand Up @@ -124,7 +125,8 @@ int main(int argc, char *argv[])
vamp_t lmax = get_lmax(lmin, max);

pthread_t threads[THREADS];
struct targs_handle *thhandle = targs_handle_init(max, progress);
struct targs_handle *thhandle;
targs_handle_new(&(thhandle), max, progress);

for (; lmax <= max;) {
fprintf(stderr, "Checking range: [%llu, %llu]\n", lmin, lmax);
Expand Down
7 changes: 5 additions & 2 deletions helsing/src/task/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
#include <assert.h>
#endif

struct task *task_init(vamp_t lmin, vamp_t lmax)
void task_new(struct task **ptr, vamp_t lmin, vamp_t lmax)
{
if (ptr == NULL)
return;

struct task *new = malloc(sizeof(struct task));
if (new == NULL)
abort();
Expand All @@ -24,7 +27,7 @@ struct task *task_init(vamp_t lmin, vamp_t lmax)
new->lmax = lmax;
new->result = NULL;
new->count = 0;
return new;
*ptr = new;
}

void task_free(struct task *ptr)
Expand Down
2 changes: 1 addition & 1 deletion helsing/src/task/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct task
vamp_t count;
};

struct task *task_init(vamp_t lmin, vamp_t lmax);
void task_new(struct task **ptr, vamp_t lmin, vamp_t lmax);
void task_free(struct task *ptr);
void task_copy_vargs(struct task *ptr, struct vargs *vamp_args);

Expand Down
16 changes: 11 additions & 5 deletions helsing/src/task/taskboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
#include "checkpoint.h"
#include "hash.h"

struct taskboard *taskboard_init()
void taskboard_new(struct taskboard **ptr)
{
if (ptr == NULL)
return;

struct taskboard *new = malloc(sizeof(struct taskboard));
if (new == NULL)
abort();
Expand All @@ -28,9 +31,8 @@ struct taskboard *taskboard_init()
new->fmax = 0;
new->done = 0;
new->common_count = 0;
hash_init(&(new->checksum));

return new;
hash_new(&(new->checksum));
*ptr = new;
}

void taskboard_free(struct taskboard *ptr)
Expand Down Expand Up @@ -92,14 +94,18 @@ void taskboard_set(struct taskboard *ptr, vamp_t lmin, vamp_t lmax)
if (ptr->tasks == NULL)
abort();

for (vamp_t i = 0; i < ptr->size; i++)
ptr->tasks[i] = NULL;

vamp_t x = 0;
vamp_t iterator = interval_size;
for (vamp_t i = lmin; i <= lmax; i += iterator + 1) {
if (lmax - i < interval_size)
iterator = lmax - i;

ptr->tasks[x++] = task_init(i, i + iterator);
task_new(&(ptr->tasks[x]), i, i + iterator);

x++;
if (i == lmax)
break;
if (i + iterator == vamp_max)
Expand Down
2 changes: 1 addition & 1 deletion helsing/src/task/taskboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct taskboard
struct hash *checksum;
};

struct taskboard *taskboard_init();
void taskboard_new(struct taskboard **ptr);
void taskboard_free(struct taskboard *ptr);
void taskboard_set(struct taskboard *ptr, vamp_t lmin, vamp_t lmax);
void taskboard_reset(struct taskboard *ptr);
Expand Down
Loading

0 comments on commit 4b9fabd

Please sign in to comment.