From c87b76286871566a3040ec05261e9c1a331d611e Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Mon, 5 Dec 2022 11:15:09 -0800 Subject: common: Add a shared mem implementation using malloc/free BRANCH=none BUG=b:234181908 TEST=none Signed-off-by: Tom Hughes Change-Id: Ie27af4272982609521e77a004d7a0a30257212ad Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4081572 Reviewed-by: Andrea Grandi --- common/build.mk | 4 +++- common/shared_mem_libc.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 common/shared_mem_libc.c diff --git a/common/build.mk b/common/build.mk index 2da6a89b38..482e6b4853 100644 --- a/common/build.mk +++ b/common/build.mk @@ -224,7 +224,9 @@ ifneq ($(HAVE_PRIVATE_AUDIO_CODEC_WOV_LIBS),y) common-$(CONFIG_AUDIO_CODEC_WOV)+=hotword_dsp_api.o endif -ifneq ($(CONFIG_COMMON_RUNTIME),) +ifeq ($(USE_BUILTIN_STDLIB), 0) +common-y+=shared_mem_libc.o +else ifneq ($(CONFIG_COMMON_RUNTIME),) ifeq ($(CONFIG_DFU_BOOTMANAGER_MAIN),ro) # Ordinary RO is replaced with DFU bootloader stub, CONFIG_MALLOC should only affect RW. ifeq ($(CONFIG_MALLOC),y) diff --git a/common/shared_mem_libc.c b/common/shared_mem_libc.c new file mode 100644 index 0000000000..5013562c26 --- /dev/null +++ b/common/shared_mem_libc.c @@ -0,0 +1,62 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * @brief Shared mem implementation that uses malloc/free from libc. + */ + +#include "common.h" +#include "console.h" +#include "shared_mem.h" +#include "task.h" + +#include + +#include + +int shared_mem_size(void) +{ + struct mallinfo info = mallinfo(); + + return info.fordblks + info.fsmblks; +} + +int shared_mem_acquire(int size, char **dest_ptr) +{ + *dest_ptr = NULL; + + if (in_interrupt_context()) + return EC_ERROR_INVAL; + + *dest_ptr = malloc(size); + if (!*dest_ptr) + return EC_ERROR_MEMORY_ALLOCATION; + + return EC_SUCCESS; +} + +void shared_mem_release(void *ptr) +{ + if (in_interrupt_context()) + return; + + free(ptr); +} + +#ifdef CONFIG_CMD_SHMEM +static int command_shmem(int argc, const char **argv) +{ + struct mallinfo info = mallinfo(); + + ccprintf("Total: %d\n", + info.uordblks + info.fordblks + info.fsmblks); + ccprintf("Allocated: %d\n", info.uordblks); + ccprintf("Free: %d\n", info.fordblks + info.fsmblks); + + return EC_SUCCESS; +} +DECLARE_SAFE_CONSOLE_COMMAND(shmem, command_shmem, NULL, + "Print shared memory stats"); +#endif /* CONFIG_CMD_SHMEM */ -- cgit v1.2.1