summaryrefslogtreecommitdiff
path: root/libcr
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2024-10-05 21:50:49 -0600
committerLuke T. Shumaker <lukeshu@lukeshu.com>2024-10-05 21:50:49 -0600
commit7ca16c45a2ecfbf415fffd384eb5efd32180c5da (patch)
treeeed9c8a9f56e45cfb14d7b4a574b432a01ae2491 /libcr
parentcb108cfecb78c01ae8a2957ca5a32b58f9ddc4aa (diff)
fixes, valgrind
Diffstat (limited to 'libcr')
-rw-r--r--libcr/coroutine.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/libcr/coroutine.c b/libcr/coroutine.c
index df9cad3..176590a 100644
--- a/libcr/coroutine.c
+++ b/libcr/coroutine.c
@@ -32,6 +32,13 @@
#ifndef CONFIG_COROUTINE_DEBUG
# error config.h must define CONFIG_COROUTINE_DEBUG
#endif
+#ifndef CONFIG_COROUTINE_VALGRIND
+# error config.h must define CONFIG_COROUTINE_VALGRIND
+#endif
+
+#if CONFIG_COROUTINE_VALGRIND
+# include <valgrind/valgrind.h>
+#endif
/* Implementation *************************************************************/
@@ -271,6 +278,9 @@ struct coroutine {
#endif
size_t stack_size;
void *stack;
+#if CONFIG_COROUTINE_VALGRIND
+ unsigned stack_id;
+#endif
};
/* constants ******************************************************************/
@@ -433,6 +443,11 @@ cid_t coroutine_add_with_stack_size(size_t stack_size, cr_fn_t fn, void *args) {
((uint8_t *)coroutine_table[child-1].stack)[i] =
stack_pattern[i%sizeof(stack_pattern)];
#endif
+#if CONFIG_COROUTINE_VALGRIND
+ coroutine_table[child-1].stack_id = VALGRIND_STACK_REGISTER(
+ coroutine_table[child-1].stack + STACK_GUARD_SIZE,
+ coroutine_table[child-1].stack + stack_size - STACK_GUARD_SIZE);
+#endif
coroutine_running = child;
coroutine_table[child-1].state = CR_INITIALIZING;
@@ -488,6 +503,9 @@ void coroutine_main(void) {
/* This is where we jump to from cr_exit(), and from
* nowhere else. */
assert_cid_state(coroutine_running, state == CR_NONE);
+#if CONFIG_COROUTINE_VALGRIND
+ VALGRIND_STACK_DEREGISTER(coroutine_table[coroutine_running-1].stack_id);
+#endif
free(coroutine_table[coroutine_running-1].stack);
coroutine_table[coroutine_running-1] = (struct coroutine){0};
}