+#ifdef HAVE_VALGRIND_VALGRIND_H
+ unsigned int valgrind_stack_id =
+ VALGRIND_STACK_REGISTER(stack, (char *)stack + smx_context_stack_size);
+ memcpy((char *)stack + smx_context_usable_stack_size, &valgrind_stack_id,
+ sizeof valgrind_stack_id);
+#endif
+
+ return stack;
+}
+
+void SIMIX_context_stack_delete(void *stack)
+{
+ if (!stack)
+ return;
+
+#ifdef HAVE_VALGRIND_VALGRIND_H
+ unsigned int valgrind_stack_id;
+ memcpy(&valgrind_stack_id, (char *)stack + smx_context_usable_stack_size,
+ sizeof valgrind_stack_id);
+ VALGRIND_STACK_DEREGISTER(valgrind_stack_id);
+#endif
+
+ if (smx_context_guard_size > 0 && !MC_is_active()) {
+ stack = (char *)stack - smx_context_guard_size;
+ if (mprotect(stack, smx_context_guard_size,
+ PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
+ XBT_WARN("Failed to remove page protection: %s", strerror(errno));
+ /* try to pursue anyway */
+ }
+#ifdef HAVE_MC
+ /* Retrieve the saved pointer. See SIMIX_context_stack_new above. */
+ stack = *((void **)stack - 1);