Ruby: coroutine/arm32/Context.h Source File

Ruby 3.4.4p34 (2025-05-14 revision a38531fd3f617bf734ef7d6c595325f69985ea1d)

Context.h
1#ifndef COROUTINE_ARM32_CONTEXT_H
2#define COROUTINE_ARM32_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#define COROUTINE_LIMITED_ADDRESS_SPACE
20
21enum {COROUTINE_REGISTERS = 8};
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);
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 = (void*)(uintptr_t)start;
48
49 context->stack_pointer -= COROUTINE_REGISTERS;
50 memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
51}
52
53struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
54
55static inline void coroutine_destroy(struct coroutine_context * context)
56{
57}
58
59#endif /* COROUTINE_ARM32_CONTEXT_H */
assert.h
coroutine_context
Definition Context.h:36