Ruby  3.4.0dev (2024-12-06 revision 892c46283a5ea4179500d951c9d4866c0051f27b)
Context.h
1 #ifndef COROUTINE_AMD64_CONTEXT_H
2 #define COROUTINE_AMD64_CONTEXT_H 1
3 
4 /*
5  * This file is part of the "Coroutine" project and released under the MIT License.
6  *
7  * Created by Samuel Williams on 10/5/2018.
8  * Copyright, 2018, by Samuel Williams.
9 */
10 
11 #pragma once
12 
13 #include <assert.h>
14 #include <stddef.h>
15 #include <stdint.h>
16 #include <string.h>
17 
18 #define COROUTINE __attribute__((noreturn)) void
19 
20 enum {COROUTINE_REGISTERS = 6};
21 
22 #if defined(__SANITIZE_ADDRESS__)
23  #define COROUTINE_SANITIZE_ADDRESS
24 #elif defined(__has_feature)
25  #if __has_feature(address_sanitizer)
26  #define COROUTINE_SANITIZE_ADDRESS
27  #endif
28 #endif
29 
30 #if defined(COROUTINE_SANITIZE_ADDRESS)
31 #include <sanitizer/common_interface_defs.h>
32 #include <sanitizer/asan_interface.h>
33 #endif
34 
36 {
37  void **stack_pointer;
38  void *argument;
39 
40 #if defined(COROUTINE_SANITIZE_ADDRESS)
41  void *fake_stack;
42  void *stack_base;
43  size_t stack_size;
44 #endif
45 };
46 
47 typedef COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self);
48 
49 static inline void coroutine_initialize_main(struct coroutine_context * context) {
50  context->stack_pointer = NULL;
51 }
52 
53 static inline void coroutine_initialize(
54  struct coroutine_context *context,
55  coroutine_start start,
56  void *stack,
57  size_t size
58 ) {
59  assert(start && stack && size >= 1024);
60 
61 #if defined(COROUTINE_SANITIZE_ADDRESS)
62  context->fake_stack = NULL;
63  context->stack_base = stack;
64  context->stack_size = size;
65 #endif
66 
67  // Stack grows down. Force 16-byte alignment.
68  char * top = (char*)stack + size;
69  context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
70 
71  *--context->stack_pointer = NULL;
72  *--context->stack_pointer = (void*)(uintptr_t)start;
73 
74  context->stack_pointer -= COROUTINE_REGISTERS;
75  memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
76 }
77 
78 struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
79 
80 static inline void coroutine_destroy(struct coroutine_context * context)
81 {
82  context->stack_pointer = NULL;
83 }
84 
85 #endif /* COROUTINE_AMD64_CONTEXT_H */