summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Hughes <tomhughes@chromium.org>2022-12-05 11:15:09 -0800
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-03-22 00:07:51 +0000
commitc87b76286871566a3040ec05261e9c1a331d611e (patch)
tree0930e28b03281eb083f717a2bcf97115d5457bb6
parentfb0c1eb9e7578e77b194cbced5347abf38f8f859 (diff)
downloadchrome-ec-c87b76286871566a3040ec05261e9c1a331d611e.tar.gz
common: Add a shared mem implementation using malloc/free
BRANCH=none BUG=b:234181908 TEST=none Signed-off-by: Tom Hughes <tomhughes@chromium.org> Change-Id: Ie27af4272982609521e77a004d7a0a30257212ad Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4081572 Reviewed-by: Andrea Grandi <agrandi@google.com>
-rw-r--r--common/build.mk4
-rw-r--r--common/shared_mem_libc.c62
2 files changed, 65 insertions, 1 deletions
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 <stdlib.h>
+
+#include <malloc.h>
+
+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 */