Ruby: coroutine/arm64/Context.h Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

Context.h
1#ifndef COROUTINE_ARM64_CONTEXT_H
2#define COROUTINE_ARM64_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
20enum {COROUTINE_REGISTERS = 0xb0 / 8};
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
35struct coroutine_context
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
47typedef COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self);
48
49static inline void coroutine_initialize_main(struct coroutine_context * context) {
50 context->stack_pointer = NULL;
51}
52
53static 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 -= COROUTINE_REGISTERS;
72 memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
73
74 context->stack_pointer[0xa0 / 8] = (void*)start;
75}
76
77struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
78
79static inline void coroutine_destroy(struct coroutine_context * context)
80{
81}
82
83#endif /* COROUTINE_ARM64_CONTEXT_H */
assert.h
coroutine_context
Definition Context.h:36