zephyr/kernel/mempool.c
Andy Ross 7832738ae9 kernel/timeout: Make timeout arguments an opaque type
Add a k_timeout_t type, and use it everywhere that kernel API
functions were accepting a millisecond timeout argument.  Instead of
forcing milliseconds everywhere (which are often not integrally
representable as system ticks), do the conversion to ticks at the
point where the timeout is created.  This avoids an extra unit
conversion in some application code, and allows us to express the
timeout in units other than milliseconds to achieve greater precision.

The existing K_MSEC() et. al. macros now return initializers for a
k_timeout_t.

The K_NO_WAIT and K_FOREVER constants have now become k_timeout_t
values, which means they cannot be operated on as integers.
Applications which have their own APIs that need to inspect these
vs. user-provided timeouts can now use a K_TIMEOUT_EQ() predicate to
test for equality.

Timer drivers, which receive an integer tick count in ther
z_clock_set_timeout() functions, now use the integer-valued
K_TICKS_FOREVER constant instead of K_FOREVER.

For the initial release, to preserve source compatibility, a
CONFIG_LEGACY_TIMEOUT_API kconfig is provided.  When true, the
k_timeout_t will remain a compatible 32 bit value that will work with
any legacy Zephyr application.

Some subsystems present timeout (or timeout-like) values to their own
users as APIs that would re-use the kernel's own constants and
conventions.  These will require some minor design work to adapt to
the new scheme (in most cases just using k_timeout_t directly in their
own API), and they have not been changed in this patch, instead
selecting CONFIG_LEGACY_TIMEOUT_API via kconfig.  These subsystems
include: CAN Bus, the Microbit display driver, I2S, LoRa modem
drivers, the UART Async API, Video hardware drivers, the console
subsystem, and the network buffer abstraction.

k_sleep() now takes a k_timeout_t argument, with a k_msleep() variant
provided that works identically to the original API.

Most of the changes here are just type/configuration management and
documentation, but there are logic changes in mempool, where a loop
that used a timeout numerically has been reworked using a new
z_timeout_end_calc() predicate.  Also in queue.c, a (when POLL was
enabled) a similar loop was needlessly used to try to retry the
k_poll() call after a spurious failure.  But k_poll() does not fail
spuriously, so the loop was removed.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-03-31 19:40:47 -04:00

214 lines
4.4 KiB
C

/*
* Copyright (c) 2017 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <kernel.h>
#include <ksched.h>
#include <wait_q.h>
#include <init.h>
#include <string.h>
#include <sys/__assert.h>
#include <sys/math_extras.h>
#include <stdbool.h>
static struct k_spinlock lock;
static struct k_mem_pool *get_pool(int id)
{
extern struct k_mem_pool _k_mem_pool_list_start[];
return &_k_mem_pool_list_start[id];
}
static int pool_id(struct k_mem_pool *pool)
{
extern struct k_mem_pool _k_mem_pool_list_start[];
return pool - &_k_mem_pool_list_start[0];
}
static void k_mem_pool_init(struct k_mem_pool *p)
{
z_waitq_init(&p->wait_q);
z_sys_mem_pool_base_init(&p->base);
}
int init_static_pools(struct device *unused)
{
ARG_UNUSED(unused);
Z_STRUCT_SECTION_FOREACH(k_mem_pool, p) {
k_mem_pool_init(p);
}
return 0;
}
SYS_INIT(init_static_pools, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
int k_mem_pool_alloc(struct k_mem_pool *p, struct k_mem_block *block,
size_t size, k_timeout_t timeout)
{
int ret;
u64_t end = 0;
__ASSERT(!(arch_is_in_isr() && !K_TIMEOUT_EQ(timeout, K_NO_WAIT)), "");
end = z_timeout_end_calc(timeout);
while (true) {
u32_t level_num, block_num;
ret = z_sys_mem_pool_block_alloc(&p->base, size,
&level_num, &block_num,
&block->data);
block->id.pool = pool_id(p);
block->id.level = level_num;
block->id.block = block_num;
if (ret == 0 || K_TIMEOUT_EQ(timeout, K_NO_WAIT) ||
ret != -ENOMEM) {
return ret;
}
z_pend_curr_unlocked(&p->wait_q, timeout);
if (!K_TIMEOUT_EQ(timeout, K_FOREVER)) {
s64_t remaining = end - z_tick_get();
if (remaining <= 0) {
break;
}
timeout = Z_TIMEOUT_TICKS(remaining);
}
}
return -EAGAIN;
}
void k_mem_pool_free_id(struct k_mem_block_id *id)
{
int need_sched = 0;
struct k_mem_pool *p = get_pool(id->pool);
z_sys_mem_pool_block_free(&p->base, id->level, id->block);
/* Wake up anyone blocked on this pool and let them repeat
* their allocation attempts
*
* (Note that this spinlock only exists because z_unpend_all()
* is unsynchronized. Maybe we want to put the lock into the
* wait_q instead and make the API safe?)
*/
k_spinlock_key_t key = k_spin_lock(&lock);
need_sched = z_unpend_all(&p->wait_q);
if (need_sched != 0) {
z_reschedule(&lock, key);
} else {
k_spin_unlock(&lock, key);
}
}
void k_mem_pool_free(struct k_mem_block *block)
{
k_mem_pool_free_id(&block->id);
}
void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size)
{
struct k_mem_block block;
/*
* get a block large enough to hold an initial (hidden) block
* descriptor, as well as the space the caller requested
*/
if (size_add_overflow(size, WB_UP(sizeof(struct k_mem_block_id)),
&size)) {
return NULL;
}
if (k_mem_pool_alloc(pool, &block, size, K_NO_WAIT) != 0) {
return NULL;
}
/* save the block descriptor info at the start of the actual block */
(void)memcpy(block.data, &block.id, sizeof(struct k_mem_block_id));
/* return address of the user area part of the block to the caller */
return (char *)block.data + WB_UP(sizeof(struct k_mem_block_id));
}
void k_free(void *ptr)
{
if (ptr != NULL) {
/* point to hidden block descriptor at start of block */
ptr = (char *)ptr - WB_UP(sizeof(struct k_mem_block_id));
/* return block to the heap memory pool */
k_mem_pool_free_id(ptr);
}
}
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
/*
* Heap is defined using HEAP_MEM_POOL_SIZE configuration option.
*
* This module defines the heap memory pool and the _HEAP_MEM_POOL symbol
* that has the address of the associated memory pool struct.
*/
K_MEM_POOL_DEFINE(_heap_mem_pool, CONFIG_HEAP_MEM_POOL_MIN_SIZE,
CONFIG_HEAP_MEM_POOL_SIZE, 1, 4);
#define _HEAP_MEM_POOL (&_heap_mem_pool)
void *k_malloc(size_t size)
{
return k_mem_pool_malloc(_HEAP_MEM_POOL, size);
}
void *k_calloc(size_t nmemb, size_t size)
{
void *ret;
size_t bounds;
if (size_mul_overflow(nmemb, size, &bounds)) {
return NULL;
}
ret = k_malloc(bounds);
if (ret != NULL) {
(void)memset(ret, 0, bounds);
}
return ret;
}
void k_thread_system_pool_assign(struct k_thread *thread)
{
thread->resource_pool = _HEAP_MEM_POOL;
}
#else
#define _HEAP_MEM_POOL NULL
#endif
void *z_thread_malloc(size_t size)
{
void *ret;
struct k_mem_pool *pool;
if (k_is_in_isr()) {
pool = _HEAP_MEM_POOL;
} else {
pool = _current->resource_pool;
}
if (pool) {
ret = k_mem_pool_malloc(pool, size);
} else {
ret = NULL;
}
return ret;
}