Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage object improvements #436

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 17 additions & 13 deletions include/utils/hashmap.h
Expand Up @@ -9,22 +9,25 @@ enum hashmap_flags
HASHMAP_AUTO_DEC = 0x2
};

typedef struct hashmap_pair_t hashmap_pair;
typedef struct hashmap_node_t hashmap_node;
typedef struct hashmap_t hashmap;
typedef void (*hashmap_free_cb)(void *);

struct hashmap_pair_t {
typedef struct hashmap_node hashmap_node;
typedef struct hashmap_pair hashmap_pair;
typedef struct hashmap hashmap;

struct hashmap_pair {
unsigned int keylen, vallen;
void *key, *val;
};

struct hashmap_node_t {
struct hashmap_node {
hashmap_pair pair;
hashmap_node *next;
};

struct hashmap_t {
struct hashmap {
hashmap_node **buckets;
hashmap_free_cb free_cb;
unsigned int buckets_x;
unsigned int buckets_x_min;
unsigned int buckets_x_max;
Expand All @@ -35,21 +38,22 @@ struct hashmap_t {
};

void hashmap_create(hashmap *hashmap, int n_size); // actual size will be 2^n_size
void hashmap_create_cb(hashmap *hashmap, int n_size, hashmap_free_cb free_cb);
void hashmap_free(hashmap *hashmap);
void hashmap_set_opts(hashmap *hm, unsigned int flags, float min_pressure, float max_pressure, int buckets_min,
void hashmap_set_opts(hashmap *hashmap, unsigned int flags, float min_pressure, float max_pressure, int buckets_min,
int buckets_max);
int hashmap_resize(hashmap *hm, int n_size);
float hashmap_get_pressure(hashmap *hm);
void hashmap_autoresize(hashmap *hm);
int hashmap_resize(hashmap *hashmap, int n_size);
float hashmap_get_pressure(hashmap *hashmap);
void hashmap_autoresize(hashmap *hashmap);
unsigned int hashmap_size(const hashmap *hashmap);
unsigned int hashmap_reserved(const hashmap *hashmap);
void *hashmap_put(hashmap *hm, const void *key, unsigned int keylen, const void *val, unsigned int vallen);
void *hashmap_put(hashmap *hashmap, const void *key, unsigned int keylen, const void *val, unsigned int vallen);
void hashmap_sput(hashmap *hashmap, const char *key, void *value, unsigned int value_len);
void hashmap_iput(hashmap *hashmap, unsigned int key, void *value, unsigned int value_len);
int hashmap_get(hashmap *hm, const void *key, unsigned int keylen, void **val, unsigned int *vallen);
int hashmap_get(hashmap *hashmap, const void *key, unsigned int keylen, void **val, unsigned int *vallen);
int hashmap_sget(hashmap *hashmap, const char *key, void **value, unsigned int *value_len);
int hashmap_iget(hashmap *hashmap, unsigned int key, void **value, unsigned int *value_len);
int hashmap_del(hashmap *hm, const void *key, unsigned int keylen);
int hashmap_del(hashmap *hashmap, const void *key, unsigned int keylen);
void hashmap_sdel(hashmap *hashmap, const char *key);
void hashmap_idel(hashmap *hashmap, unsigned int key);
void hashmap_iter_begin(const hashmap *hashmap, iterator *iter);
Expand Down
7 changes: 5 additions & 2 deletions include/utils/vector.h
Expand Up @@ -3,17 +3,20 @@

#include "iterator.h"

typedef void (*vector_free_cb)(void *);
typedef int (*vector_compare_func)(const void *, const void *);

typedef struct vector_t {
char *data;
unsigned int block_size;
unsigned int blocks;
unsigned int reserved;
unsigned int inc_factor;
vector_free_cb free_cb;
} vector;

typedef int (*vector_compare_func)(const void *, const void *);

void vector_create(vector *vector, unsigned int block_size);
void vector_create_cb(vector *vector, unsigned int block_size, vector_free_cb free_cb);
void vector_free(vector *vector);
void vector_clear(vector *vector);
void *vector_get(const vector *vector, unsigned int key);
Expand Down
13 changes: 13 additions & 0 deletions src/utils/hashmap.c
Expand Up @@ -60,6 +60,19 @@ void hashmap_create(hashmap *hm, int n_size) {
hm->flags = 0;
hm->buckets = omf_calloc(hashmap_size(hm), sizeof(hashmap_node *));
hm->reserved = 0;
hm->free_cb = NULL;
}

/**
* @brief Same as hashmap_create, except that this one accepts item free function callback.
*
* @param hm Allocated hashmap pointer
* @param n_size Size of the hashmap. Final size will be pow(2, n_size)
* @param free_cb Item free() function callback
*/
void hashmap_create_cb(hashmap *hm, int n_size, hashmap_free_cb free_cb) {
hashmap_create(hm, n_size);
hm->free_cb = free_cb;
}

/** \brief Set hashmap options
Expand Down
21 changes: 20 additions & 1 deletion src/utils/vector.c
Expand Up @@ -18,15 +18,31 @@ void vector_init(vector *vec) {

void vector_create(vector *vec, unsigned int block_size) {
vec->block_size = block_size;
vec->free_cb = NULL;
vector_init(vec);
}

void vector_create_cb(vector *vec, unsigned int block_size, vector_free_cb free_cb) {
vec->block_size = block_size;
vec->free_cb = free_cb;
vector_init(vec);
}

void vector_clear(vector *vec) {
void *dst;
if(vec->free_cb) {
for(unsigned int i = 0; i < vec->blocks; i++) {
dst = vec->data + i * vec->block_size;
if(vec->free_cb) {
vec->free_cb(dst);
}
}
}
vec->blocks = 0;
}

void vector_free(vector *vec) {
vec->blocks = 0;
vector_clear(vec);
vec->reserved = 0;
vec->block_size = 0;
omf_free(vec->data);
Expand Down Expand Up @@ -90,6 +106,9 @@ int vector_delete(vector *vec, iterator *iter) {
void *dst = vec->data + real * vec->block_size;
void *src = vec->data + (real + 1) * vec->block_size;
unsigned int size = (vec->blocks - 1 - real) * vec->block_size;
if(vec->free_cb) {
vec->free_cb(dst);
}
memmove(dst, src, size);

// If we are iterating forwards, moving an entry will hop iterator forwards by two.
Expand Down