summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomasz Michalec <tm@semihalf.com>2022-03-16 15:17:03 +0100
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-03-25 12:49:03 +0000
commit457a5569a2877af539865b44dd07c4b93f7bd99f (patch)
tree9100f5f82667aea8407b7c4fdbe8695cffef07e1
parentda3901660fad87875f4931983c8ee94cd40bbb5d (diff)
downloadchrome-ec-457a5569a2877af539865b44dd07c4b93f7bd99f.tar.gz
zephyr: Add test memory heap
Add memory heap that is exclusive for test utilities and is separate from system memory (allocated using k_malloc() and k_free()). Memory can be allocated using test_malloc() and test_free() functions. This way it is completely separate from system wide memory pool. BUG=b:224930006 BRANCH=none TEST=zmake configure --test test-drivers Signed-off-by: Tomasz Michalec <tm@semihalf.com> Change-Id: I009a7e4c96ab989e9b1ca0719e247c8770b26fc3 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3536367 Tested-by: Tomasz Michalec <tmichalec@google.com> Reviewed-by: Al Semjonovs <asemjonovs@google.com> Commit-Queue: Tomasz Michalec <tmichalec@google.com>
-rw-r--r--zephyr/emul/tcpc/emul_tcpci_partner_common.c26
-rw-r--r--zephyr/test/drivers/include/utils.h16
-rw-r--r--zephyr/test/drivers/prj.conf1
-rw-r--r--zephyr/test/drivers/src/utils.c20
4 files changed, 50 insertions, 13 deletions
diff --git a/zephyr/emul/tcpc/emul_tcpci_partner_common.c b/zephyr/emul/tcpc/emul_tcpci_partner_common.c
index 87e4f43726..c55d2a5330 100644
--- a/zephyr/emul/tcpc/emul_tcpci_partner_common.c
+++ b/zephyr/emul/tcpc/emul_tcpci_partner_common.c
@@ -8,11 +8,13 @@ LOG_MODULE_REGISTER(tcpci_partner, CONFIG_TCPCI_EMUL_LOG_LEVEL);
#include <sys/byteorder.h>
#include <zephyr.h>
+#include <ztest.h>
#include "common.h"
#include "emul/tcpc/emul_tcpci_partner_common.h"
#include "emul/tcpc/emul_tcpci.h"
#include "usb_pd.h"
+#include "utils.h"
/** Length of PDO, RDO and BIST request object in SOP message in bytes */
#define TCPCI_MSG_DO_LEN 4
@@ -25,14 +27,14 @@ struct tcpci_partner_msg *tcpci_partner_alloc_msg(int data_objects)
struct tcpci_partner_msg *new_msg;
size_t size = TCPCI_MSG_HEADER_LEN + TCPCI_MSG_DO_LEN * data_objects;
- new_msg = k_malloc(sizeof(struct tcpci_partner_msg));
+ new_msg = test_malloc(sizeof(struct tcpci_partner_msg));
if (new_msg == NULL) {
return NULL;
}
- new_msg->msg.buf = k_malloc(size);
+ new_msg->msg.buf = test_malloc(size);
if (new_msg->msg.buf == NULL) {
- k_free(new_msg);
+ test_free(new_msg);
return NULL;
}
@@ -65,16 +67,16 @@ static void tcpci_partner_log_msg(struct tcpci_partner_data *data,
return;
}
- log_msg = k_malloc(sizeof(struct tcpci_partner_log_msg));
+ log_msg = test_malloc(sizeof(struct tcpci_partner_log_msg));
if (log_msg == NULL) {
return;
}
/* We log length of actual buffer without SOP byte */
cnt = msg->cnt;
- log_msg->buf = k_malloc(cnt);
+ log_msg->buf = test_malloc(cnt);
if (log_msg->buf == NULL) {
- k_free(log_msg);
+ test_free(log_msg);
return;
}
@@ -88,8 +90,8 @@ static void tcpci_partner_log_msg(struct tcpci_partner_data *data,
ret = k_mutex_lock(&data->msg_log_mutex, K_FOREVER);
if (ret) {
- k_free(log_msg->buf);
- k_free(log_msg);
+ test_free(log_msg->buf);
+ test_free(log_msg);
return;
}
@@ -101,8 +103,8 @@ static void tcpci_partner_log_msg(struct tcpci_partner_data *data,
/** Check description in emul_common_tcpci_partner.h */
void tcpci_partner_free_msg(struct tcpci_partner_msg *msg)
{
- k_free(msg->msg.buf);
- k_free(msg);
+ test_free(msg->msg.buf);
+ test_free(msg);
}
/** Check description in emul_common_tcpci_partner.h */
@@ -747,8 +749,8 @@ void tcpci_partner_common_clear_logged_msgs(struct tcpci_partner_data *data)
while (!sys_slist_is_empty(&data->msg_log)) {
msg = CONTAINER_OF(sys_slist_get_not_empty(&data->msg_log),
struct tcpci_partner_log_msg, node);
- k_free(msg->buf);
- k_free(msg);
+ test_free(msg->buf);
+ test_free(msg);
}
k_mutex_unlock(&data->msg_log_mutex);
diff --git a/zephyr/test/drivers/include/utils.h b/zephyr/test/drivers/include/utils.h
index fa66911f99..6d5d0c33bf 100644
--- a/zephyr/test/drivers/include/utils.h
+++ b/zephyr/test/drivers/include/utils.h
@@ -290,4 +290,20 @@ void connect_source_to_port(struct tcpci_src_emul *src, int pdo_index,
void disconnect_source_from_port(const struct emul *tcpci_emul,
const struct emul *charger_emul);
+/**
+ * @brief Allocate memory for a test pourpose
+ *
+ * @param bytes Number of bytes to allocate
+ *
+ * @return Pointer to valid memory or NULL
+ */
+void *test_malloc(size_t bytes);
+
+/**
+ * @brief Free memory allocated by @ref test_malloc
+ *
+ * @param mem Pointer to the memory
+ */
+void test_free(void *mem);
+
#endif /* ZEPHYR_TEST_DRIVERS_INCLUDE_UTILS_H_ */
diff --git a/zephyr/test/drivers/prj.conf b/zephyr/test/drivers/prj.conf
index 2de49068ba..eabc297968 100644
--- a/zephyr/test/drivers/prj.conf
+++ b/zephyr/test/drivers/prj.conf
@@ -58,7 +58,6 @@ CONFIG_EMUL_BC12_DETECT_PI3USB9201=y
CONFIG_EMUL_PPC_SYV682X=y
CONFIG_ADC=y
CONFIG_ADC_EMUL=y
-CONFIG_HEAP_MEM_POOL_SIZE=2048
CONFIG_EMUL_BMA255=y
CONFIG_EMUL_BMI=y
CONFIG_EMUL_TCS3400=y
diff --git a/zephyr/test/drivers/src/utils.c b/zephyr/test/drivers/src/utils.c
index 02d66b0afd..3021a98c90 100644
--- a/zephyr/test/drivers/src/utils.c
+++ b/zephyr/test/drivers/src/utils.c
@@ -129,3 +129,23 @@ void host_cmd_typec_discovery(int port, enum typec_partner_type partner_type,
zassume_ok(host_command_process(&args),
"Failed to get Type-C state for port %d", port);
}
+
+K_HEAP_DEFINE(test_heap, 2048);
+
+void *test_malloc(size_t bytes)
+{
+ void *mem;
+
+ mem = k_heap_alloc(&test_heap, bytes, K_NO_WAIT);
+
+ if (mem == NULL) {
+ printk("Failed to alloc %d bytes\n", bytes);
+ }
+
+ return mem;
+}
+
+void test_free(void *mem)
+{
+ k_heap_free(&test_heap, mem);
+}