Ruby: coroutine/x86/Context.h Source File

Ruby 3.4.7p58 (2025-10-08 revision 7a5688e2a27668e48f8d6ff4af5b2208b98a2f5e)

Context.h
1#ifndef COROUTINE_X86_CONTEXT_H
2#define COROUTINE_X86_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 3/11/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, fastcall)) void
19#define COROUTINE_LIMITED_ADDRESS_SPACE
20
21enum {COROUTINE_REGISTERS = 4};
22
23struct coroutine_context
24{
25 void **stack_pointer;
26 void *argument;
27};
28
29typedef COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self) __attribute__((fastcall));
30
31static inline void coroutine_initialize_main(struct coroutine_context * context) {
32 context->stack_pointer = NULL;
33}
34
35static inline void coroutine_initialize(
36 struct coroutine_context *context,
37 coroutine_start start,
38 void *stack,
39 size_t size
40) {
41 assert(start && stack && size >= 1024);
42
43 // Stack grows down. Force 16-byte alignment.
44 char * top = (char*)stack + size;
45 context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
46
47 *--context->stack_pointer = NULL;
48 *--context->stack_pointer = (void*)(uintptr_t)start;
49
50 context->stack_pointer -= COROUTINE_REGISTERS;
51 memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
52}
53
54struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target) __attribute__((fastcall));
55
56static inline void coroutine_destroy(struct coroutine_context * context)
57{
58 context->stack_pointer = NULL;
59}
60
61#endif /* COROUTINE_X86_CONTEXT_H */
assert.h
coroutine_context
Definition Context.h:36
__attribute__
Definition vm_insnhelper.c:2285