diff --git a/kernel/Kconfig b/kernel/Kconfig index 62713a5c9bb..361fb67f17e 100644 --- a/kernel/Kconfig +++ b/kernel/Kconfig @@ -677,6 +677,13 @@ config POLL concurrently, which can be either directly triggered or triggered by the availability of some kernel objects (semaphores and FIFOs). +config MEM_SLAB_POINTER_VALIDATE + bool "Validate the memory slab pointer when allocating or freeing" + default ASSERT + help + This enables additional runtime checks to validate the memory slab + pointer during when allocating or freeing a memory slab. + config MEM_SLAB_TRACE_MAX_UTILIZATION bool "Getting maximum slab utilization" help diff --git a/kernel/mem_slab.c b/kernel/mem_slab.c index 45ba08e27cb..7abb61af504 100644 --- a/kernel/mem_slab.c +++ b/kernel/mem_slab.c @@ -206,6 +206,10 @@ out: static bool slab_ptr_is_good(struct k_mem_slab *slab, const void *ptr) { + if (!IS_ENABLED(CONFIG_MEM_SLAB_POINTER_VALIDATE)) { + return true; + } + const char *p = ptr; ptrdiff_t offset = p - slab->buffer;