summaryrefslogtreecommitdiff
path: root/include/shared_mem.h
diff options
context:
space:
mode:
authorVadim Bendebury <vbendeb@chromium.org>2016-12-14 21:31:25 -0800
committerchrome-bot <chrome-bot@chromium.org>2017-01-05 21:13:09 -0800
commit32b064108a94fa163b5e9f9aef2b85615c68ee1d (patch)
treee5cd6fe1bf84f555d5aa32f7ab970eb9001f827e /include/shared_mem.h
parent02d9c311ec93b3dc87f8c600b9f3653e3b13009a (diff)
downloadchrome-ec-32b064108a94fa163b5e9f9aef2b85615c68ee1d.tar.gz
common: introduce malloc/free implementation
The new code allows to replace the existing one buffer at a time shared memory facility with a malloc/free implementation. A new configuration option is being provided (CONFIG_MALLOC). The names of functions allocating and freeing memory are not being changed to allow to switch between the two implementations seamlessly. A double linked list of buffers is used to keep track of free and allocated memory. During initialization the entire free memory block is considered a single free buffer. No allocations/frees are allowed from within interrupts. The control structures are protected by a semaphore, so allocate and free invocation could be blocking. A test is added which randomly allocates and frees memory, continuing until all branches in the allocate and free functions are taken. BUG=chrome-os-partner: TEST=make buildall -j succeeds, which includes testing the new malloc/free implementation. Change-Id: I5e71c0190c6c247ec73bb459f66a6d7a06e3d248 Signed-off-by: Vadim Bendebury <vbendeb@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/420466 Reviewed-by: Randall Spangler <rspangler@chromium.org>
Diffstat (limited to 'include/shared_mem.h')
-rw-r--r--include/shared_mem.h41
1 files changed, 35 insertions, 6 deletions
diff --git a/include/shared_mem.h b/include/shared_mem.h
index f7b8bcbccb..b99cdb1341 100644
--- a/include/shared_mem.h
+++ b/include/shared_mem.h
@@ -18,11 +18,7 @@
#define __CROS_EC_SHARED_MEM_H
#include "common.h"
-
-/**
- * Initializes the module.
- */
-int shared_mem_init(void);
+#include <stddef.h>
/**
* Returns the maximum amount of shared memory which can be acquired, in
@@ -30,9 +26,14 @@ int shared_mem_init(void);
*/
int shared_mem_size(void);
-/**
+/*
* Acquires a shared memory area of the requested size in bytes.
*
+ * Doing a sysjump between images will corrupt and/or erase shared memory as
+ * jump tags are added and .bss is reinitialized. Due to the way jump tags are
+ * added to the end of RAM, shared memory must not be allocated, accessed, or
+ * freed after the start of a sysjump (for example, in HOOK_SYSJUMP).
+ *
* @param size Number of bytes requested
* @param dest_ptr If successful, set on return to the start of the
* granted memory buffer.
@@ -47,4 +48,32 @@ int shared_mem_acquire(int size, char **dest_ptr);
*/
void shared_mem_release(void *ptr);
+/*
+ * This structure is allocated at the base of the free memory chunk and every
+ * allocated buffer.
+ */
+struct shm_buffer {
+ struct shm_buffer *next_buffer;
+ struct shm_buffer *prev_buffer;
+ size_t buffer_size;
+};
+
+#ifdef TEST_BUILD
+
+/*
+ * When in test mode, all possible paths in the allocation/free functions set
+ * unique bits in an integer bitmap.
+ *
+ * The test function generates random allocation and free requests and
+ * monitors the bitmap until all bits have been set, which indicates that all
+ * possible paths have been executed.
+ */
+
+#define MAX_MASK_BIT 24
+#define ALL_PATHS_MASK ((1 << (MAX_MASK_BIT + 1)) - 1)
+void set_map_bit(uint32_t mask);
+extern struct shm_buffer *free_buf_chain;
+extern struct shm_buffer *allocced_buf_chain;
+#endif
+
#endif /* __CROS_EC_SHARED_MEM_H */