Skip to content
Snippets Groups Projects
Verified Commit b13ce190 authored by Vladimír Čunát's avatar Vladimír Čunát
Browse files

contrib/mempattern: hide mm_malloc()

Experience proved that it can be confused with mm_alloc()
and it may not be trivial to find the mistake.
parent ff11f326
1 merge request!1115refactor memory allocation patterns a little
......@@ -24,7 +24,7 @@ static void mm_nofree(void *p)
/* nop */
}
void *mm_malloc(void *ctx, size_t n)
static void *mm_malloc(void *ctx, size_t n)
{
(void)ctx;
return malloc(n);
......
......@@ -55,9 +55,6 @@ void mm_ctx_mempool(knot_mm_t *mm, size_t chunk_size);
/* API in addition to Knot's mempattern. */
/*! \brief Simple malloc wrapper. */
void *mm_malloc(void *ctx, size_t n);
/*! \brief Readability: avoid const-casts in code. */
static inline void free_const(const void *what)
{
......@@ -71,12 +68,14 @@ void *mm_malloc_aligned(void *ctx, size_t n);
static inline void mm_ctx_init_aligned(knot_mm_t *mm, size_t alignment)
{
assert(__builtin_popcount(alignment) == 1);
mm_ctx_init(mm);
mm->ctx = (uint8_t *)NULL + alignment; /*< roundabout to satisfy linters */
/* posix_memalign() doesn't allow alignment < sizeof(void*),
* and there's no point in using it for small values anyway,
* as plain malloc() guarantees at least max_align_t. */
mm->alloc = alignment > sizeof(max_align_t) ? mm_malloc_aligned : mm_malloc;
mm->free = free;
if (alignment > sizeof(max_align_t)) {
mm->alloc = mm_malloc_aligned;
}
}
/*! \brief New memory pool context, allocated on itself. */
......
......@@ -119,7 +119,7 @@ KR_EXPORT struct lru * lru_create_impl(uint max_slots, uint val_alignment,
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);
assert(mm_array->alloc && 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);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment