summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md3
-rw-r--r--src/deque.c2
-rw-r--r--src/log.h1
-rw-r--r--src/map.c13
-rw-r--r--src/string.c17
-rw-r--r--src/threadpool.c3
6 files changed, 28 insertions, 11 deletions
diff --git a/README.md b/README.md
index 831e3ac..1db9e64 100644
--- a/README.md
+++ b/README.md
@@ -21,3 +21,6 @@
- [ ] thread_pool
- [ ] vec2sw
- [ ] hashmap
+- [x] Stupid bug
+ - In smart arena, when I create a scratch arena and allocate 10 bytes for a char pointer and change them all to non '\0' then end the arena, then create another scratch arena on the same global scratch, allocate 8 bytes and change them all to non '\0', this can not be used as a null terminated string because the byte after 8 (9 in the original buffer) is not a '\0'.
+ - Verdict: Leave it up to the user to allocate strlen+1 bytes.
diff --git a/src/deque.c b/src/deque.c
index 1046575..c66b25e 100644
--- a/src/deque.c
+++ b/src/deque.c
@@ -65,7 +65,7 @@ ts_deque_t ts_deque_create(smrt_arena_t *arena, ez_deque_t *q) {
sem_init(s, 0, 0);
#ifndef NLOG_TRACE
- log_trace("Created thread-safe Deque; queue %p", q);
+ log_trace("Created thread-safe Deque; queue %p", (void*)q);
#endif /* ifndef NLOG_TRACE */
return (ts_deque_t){
diff --git a/src/log.h b/src/log.h
index 287ee28..1c63275 100644
--- a/src/log.h
+++ b/src/log.h
@@ -35,4 +35,5 @@ void log_set_fp(FILE *log_file);
const char *log_get_level();
+__attribute__((format(printf, 4, 5)))
void log_log(LogLevelE lvl, const char *src_file, u64 src_line, const char *fmt, ...);
diff --git a/src/map.c b/src/map.c
index 788095f..0d26eb6 100644
--- a/src/map.c
+++ b/src/map.c
@@ -1,10 +1,10 @@
#include "map.h"
#include "common.h"
#include "smrt_arena.h"
+#include "log.h"
#include "../include/a5hash.h"
-#include <immintrin.h>
#include <stdint.h>
#include <string.h>
@@ -34,6 +34,11 @@ map_t map_create(smrt_arena_t *arena, u64 capacity) {
map->values = SMRTA_ALLOC_ARRAY(arena, void*, capacity);
map->table_size = capacity;
+ #ifndef NLOG_TRACE
+ log_trace("Created hashmap; Total capacity %lu",
+ capacity);
+ #endif /* ifndef NLOG_TRACE */
+
return map;
}
@@ -73,6 +78,8 @@ i32 map_insert(map_t const map, u8 const *key, u64 keylen, void *value) {
}
}
+ log_warn("Attempted to insert value with key \"%.*s...\" into hashmap that is full",
+ (i32)MIN(5, keylen), key);
return -1;
}
@@ -107,7 +114,7 @@ void *map_lookup(map_t const map, u8 const *key, u64 keylen) {
u64 tsize = map->table_size;
for (u64 i = start; i != start - 1; i = (i+1)%tsize) {
u8 h_lookupcmp = map->hashlookup[i];
- if (h_lookupcmp == ENTRY_EMPTY) return NULL;
+ if (h_lookupcmp == ENTRY_EMPTY) break;
if (h_lookupcmp == ENTRY_TOMBSTONE) continue;
map_key_entry_t k = map->keys[i];
@@ -115,7 +122,7 @@ void *map_lookup(map_t const map, u8 const *key, u64 keylen) {
h == map->hashes[i] &&
k.keylen == keylen &&
(memcmp(key, k.key, keylen) == 0))
- { return map->values[i]; }
+ { return map->values[i]; }
}
return NULL;
diff --git a/src/string.c b/src/string.c
index 795eb95..63c886a 100644
--- a/src/string.c
+++ b/src/string.c
@@ -224,11 +224,13 @@ i32 sv_find_substr(strng_view_t const *sv, char const *_needle) {
char const *restrict haystack = sv->string+sv->start;
char const *restrict needle = _needle;
- __m256i first_vec = _mm256_set1_epi8(*needle);
-
u64 end_idx = hlen - nlen + 1;
u64 i = 0;
+ if (hlen < 32) goto lt_32;
+
+ __m256i first_vec = _mm256_set1_epi8(*needle);
+
for (; i + 32 <= end_idx; i += 32) {
__m256i hay_vec = _mm256_loadu_si256((const __m256i*)(haystack+i));
__m256i cmp = _mm256_cmpeq_epi8(hay_vec, first_vec);
@@ -236,12 +238,13 @@ i32 sv_find_substr(strng_view_t const *sv, char const *_needle) {
while (mask != 0) {
i32 offset = __builtin_ctz(mask);
- if (strncmp(haystack+i+offset, needle, nlen) == 0) return i+offset;
+ if (memcmp(haystack+i+offset, needle, nlen) == 0) return i+offset;
mask &= (mask - 1);
}
}
- for (; i < end_idx; i++) { if (strncmp(haystack + i, needle, nlen) == 0) return i; }
+lt_32:
+ for (; i < end_idx; i++) { if (memcmp(haystack + i, needle, nlen) == 0) return i; }
return -1;
}
@@ -254,9 +257,10 @@ i32 sv_find_char(strng_view_t const *sv, char n) {
if (hlen == 0) return -1;
if (hlen == 1 && *haystack == n) return 0;
+ u64 i = 0;
+ if (hlen < 32) goto lt_32;
__m256i needle_vec = _mm256_set1_epi8(n);
- u64 i = 0;
for (; i + 32 <= hlen; i += 32) {
__m256i hay_vec = _mm256_loadu_si256((const __m256i*)(haystack+i));
@@ -269,6 +273,7 @@ i32 sv_find_char(strng_view_t const *sv, char n) {
}
}
+lt_32:
for (; i < hlen; i++) { if (haystack[i] == n) return i; }
return -1;
@@ -320,7 +325,7 @@ b32 sv_eq_case_insensitive(strng_view_t const *sv1, strng_view_t const *sv2) {
v1 = _mm256_or_si256(v1, U1);
v2 = _mm256_or_si256(v2, U2);
- __m256i cmp = _mm256_cmpeq_epi64(v1, v2);
+ __m256i cmp = _mm256_cmpeq_epi8(v1, v2);
u32 mask = _mm256_movemask_epi8(cmp);
if (mask != 0xFFFFFFFF) return false;
diff --git a/src/threadpool.c b/src/threadpool.c
index 152ca66..162fcf2 100644
--- a/src/threadpool.c
+++ b/src/threadpool.c
@@ -1,3 +1,4 @@
+#include <stdint.h>
#if defined(__linux__)
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
@@ -196,7 +197,7 @@ i32 tp_push_job(thread_pool_t *tp, tp_job_proc job, void *args) {
};
#ifndef NLOG_TRACE
- log_trace("Pushing job %p with arg %p onto threadpool.", job, args);
+ log_trace("Pushing job 0x%lx with arg %p onto threadpool.", (uintptr_t)job, args);
#endif /* ifndef NLOG_TRACE */
return ts_deque_enqueue(tp->jobs, &j);