Ruby: coroutine/win64/Context.h Source File

Ruby 4.0.5p0 (2026-05-20 revision 64336ffd0ee9e1f4c05891695a3d7b49cb709721)

Context.h
1#ifndef COROUTINE_WIN64_CONTEXT_H
2#define COROUTINE_WIN64_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 __declspec(noreturn) void
19#define COROUTINE_DECL void
20
21enum {
22 COROUTINE_REGISTERS = 8,
23 COROUTINE_XMM_REGISTERS = 1+10*2,
24};
25
26struct coroutine_context
27{
28 void **stack_pointer;
29 void *argument;
30};
31
32typedef void(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self);
33
34void coroutine_trampoline(void);
35
36static inline void coroutine_initialize_main(struct coroutine_context * context) {
37 context->stack_pointer = NULL;
38}
39
40static inline void coroutine_initialize(
41 struct coroutine_context *context,
42 coroutine_start start,
43 void *stack,
44 size_t size
45) {
46 assert(start && stack && size >= 1024);
47
48 // Stack grows down. Force 16-byte alignment.
49 char * top = (char*)stack + size;
50 context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
51
52 /* Win64 ABI requires space for arguments */
53 context->stack_pointer -= 4;
54
55 /* Return address */
56 *--context->stack_pointer = 0;
57 *--context->stack_pointer = (void*)(uintptr_t)start;
58 *--context->stack_pointer = (void*)coroutine_trampoline;
59
60 /* Windows Thread Information Block */
61 /* *--context->stack_pointer = 0; */ /* gs:[0x00] is not used */
62 *--context->stack_pointer = (void*)top; /* gs:[0x08] */
63 *--context->stack_pointer = (void*)stack; /* gs:[0x10] */
64
65 context->stack_pointer -= COROUTINE_REGISTERS;
66 memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
67 memset(context->stack_pointer - COROUTINE_XMM_REGISTERS, 0, sizeof(void*) * COROUTINE_XMM_REGISTERS);
68}
69
70struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
71
72static inline void coroutine_destroy(struct coroutine_context * context)
73{
74}
75
76#endif /* COROUTINE_WIN64_CONTEXT_H */
assert.h
coroutine_context
Definition Context.h:36