summaryrefslogtreecommitdiffstats
path: root/lib/generic
diff options
context:
space:
mode:
authorVladimír Čunát <vladimir.cunat@nic.cz>2019-02-19 19:41:19 +0100
committerVladimír Čunát <vladimir.cunat@nic.cz>2019-03-05 17:01:41 +0100
commit0b3cecb50af18695def7b58c06f32cbed8d14c19 (patch)
treec7d8826d1449c803713096d681f5f9bbaae5d63c /lib/generic
parentcontrib/murmurhash3: fix bad type punning (diff)
downloadknot-resolver-0b3cecb50af18695def7b58c06f32cbed8d14c19.tar.xz
knot-resolver-0b3cecb50af18695def7b58c06f32cbed8d14c19.zip
lib/generic/lru: fix alignment of struct lru
Diffstat (limited to 'lib/generic')
-rw-r--r--lib/generic/lru.c10
-rw-r--r--lib/generic/lru.h1
2 files changed, 11 insertions, 0 deletions
diff --git a/lib/generic/lru.c b/lib/generic/lru.c
index 12bba4ed..98d9033e 100644
--- a/lib/generic/lru.c
+++ b/lib/generic/lru.c
@@ -16,6 +16,7 @@
#include "lib/generic/lru.h"
#include "contrib/murmurhash3/murmurhash3.h"
+#include "contrib/ucw/mempool.h"
typedef struct lru_group lru_group_t;
@@ -108,6 +109,15 @@ KR_EXPORT struct lru * lru_create_impl(uint max_slots, knot_mm_t *mm_array, knot
group_count = 1 << log_groups;
assert(max_slots <= group_count * LRU_ASSOC && group_count * LRU_ASSOC < 2 * max_slots);
+ /* Get a sufficiently aligning mm_array if NULL is passed. */
+ if (!mm_array) {
+ static knot_mm_t mm_array_default = { 0 };
+ if (!mm_array_default.ctx)
+ mm_ctx_init_aligned(&mm_array_default, __alignof(struct lru));
+ mm_array = &mm_array_default;
+ }
+ assert(mm_array->alloc != mm_malloc && mm_array->alloc != (knot_mm_alloc_t)mp_alloc);
+
size_t size = offsetof(struct lru, groups[group_count]);
struct lru *lru = mm_alloc(mm_array, size);
if (unlikely(lru == NULL))
diff --git a/lib/generic/lru.h b/lib/generic/lru.h
index b5c9bcd6..1e195c6d 100644
--- a/lib/generic/lru.h
+++ b/lib/generic/lru.h
@@ -88,6 +88,7 @@
* @param ptable pointer to a pointer to the LRU
* @param max_slots number of slots
* @param mm_ctx_array memory context to use for the huge array, NULL for default
+ * If you pass your own, it needs to produce CACHE_ALIGNED allocations (ubsan).
* @param mm_ctx memory context to use for individual key-value pairs, NULL for default
*
* @note The pointers to memory contexts need to remain valid