aboutsummaryrefslogtreecommitdiff
path: root/src/libsok/sok-cache.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libsok/sok-cache.c')
-rw-r--r--src/libsok/sok-cache.c53
1 files changed, 31 insertions, 22 deletions
diff --git a/src/libsok/sok-cache.c b/src/libsok/sok-cache.c
index 748614e..261d6b9 100644
--- a/src/libsok/sok-cache.c
+++ b/src/libsok/sok-cache.c
@@ -1,23 +1,16 @@
#include <stdbool.h> // bool
#include <stdint.h> // uint64_t
#include <stdlib.h> // bsearch(), qsort()
+#include <string.h> // memcpy
+#include <stdio.h> // debug
#include "sok.h"
#define UNUSED(a) ((void) (a))
#define GROW_SIZE (4096 / sizeof(uint64_t))
-static uint64_t
-hash_ctx(
- const sok_ctx_t * const ctx
-) {
- UNUSED(ctx);
- // TODO
- return 0;
-}
-
static int
u64_cmp(
- const void * const ap,
+ const void * const ap,
const void * const bp
) {
const uint64_t a = *((uint64_t*) ap),
@@ -37,7 +30,7 @@ sok_cache_grow(
}
// reallocate memory
- uint64_t * const vals = realloc(cache->vals, new_capacity);
+ uint64_t * const vals = realloc(cache->vals, new_capacity * sizeof(uint64_t));
if (!vals) {
return false;
}
@@ -55,6 +48,16 @@ sok_cache_has_hash(
const sok_cache_t * const cache,
const uint64_t hash
) {
+#if 0
+ for (size_t i = 0; i < cache->num_vals; i++) {
+ if (cache->vals[i] == hash) {
+ return true;
+ }
+ }
+
+ // return failure
+ return false;
+#else
return !!bsearch(
&hash,
cache->vals,
@@ -62,6 +65,7 @@ sok_cache_has_hash(
sizeof(uint64_t),
u64_cmp
);
+#endif /* 0 */
}
bool
@@ -69,7 +73,7 @@ sok_cache_has(
const sok_cache_t * const cache,
const sok_ctx_t * const ctx
) {
- return sok_cache_has_hash(cache, hash_ctx(ctx));
+ return sok_cache_has_hash(cache, sok_ctx_hash(ctx));
}
bool
@@ -77,13 +81,15 @@ sok_cache_add(
sok_cache_t * const cache,
const sok_ctx_t * const ctx
) {
- // hash context
- const uint64_t hash = hash_ctx(ctx);
-
- // check for duplicates
- if (sok_cache_has_hash(cache, hash)) {
- return true;
- }
+/*
+ * // hash context
+ * const uint64_t hash = sok_ctx_hash(ctx);
+ *
+ * // check for duplicates
+ * if (sok_cache_has_hash(cache, hash)) {
+ * return false;
+ * }
+ */
// check capacity
if (cache->num_vals >= cache->capacity) {
@@ -94,8 +100,11 @@ sok_cache_add(
}
}
- // append hash, sort values
- cache->vals[cache->num_vals++] = hash_ctx(ctx);
+ // append hash
+ cache->vals[cache->num_vals] = sok_ctx_hash(ctx);
+ cache->num_vals++;
+
+ // sort values
qsort(cache->vals, cache->num_vals, sizeof(uint64_t), u64_cmp);
// return success
@@ -108,7 +117,7 @@ sok_cache_init(
const size_t capacity
) {
// alloc memory, check for error
- uint64_t * const vals = malloc(sizeof(uint64_t) * capacity);
+ uint64_t * const vals = malloc(capacity * sizeof(uint64_t));
if (!vals) {
// return failure
return false;