Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)
Context.h
Go to the documentation of this file.
1#pragma once
2
3#include <assert.h>
4#include <stddef.h>
5#include <stdint.h>
6#include <string.h>
7
8#define COROUTINE __attribute__((noreturn)) void
9
10enum {
12 19 /* 18 general purpose registers (r14-r31) and 1 return address */
13 + 4 /* space for fiber_entry() to store the link register */
14};
15
17{
18 void **stack_pointer;
19};
20
22
23static inline void coroutine_initialize_main(struct coroutine_context * context) {
24 context->stack_pointer = NULL;
25}
26
27static inline void coroutine_initialize(
28 struct coroutine_context *context,
29 coroutine_start start,
30 void *stack,
31 size_t size
32) {
33 assert(start && stack && size >= 1024);
34
35 // Stack grows down. Force 16-byte alignment.
36 char * top = (char*)stack + size;
37 context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
38
40 memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
41
42 /* Skip a global prologue that sets the TOC register */
43 context->stack_pointer[18] = ((char*)start) + 8;
44}
45
46struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
47
48static inline void coroutine_destroy(struct coroutine_context * context)
49{
50 context->stack_pointer = NULL;
51}
COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self)
Definition: Context.h:24
#define COROUTINE
Definition: Context.h:15
struct coroutine_context * coroutine_transfer(struct coroutine_context *current, struct coroutine_context *target)
Definition: Context.c:136
@ COROUTINE_REGISTERS
Definition: Context.h:17
#define assert(x)
Definition: dlmalloc.c:1176
voidpf void uLong size
Definition: ioapi.h:138
unsigned int top
Definition: nkf.c:4323
#define NULL
Definition: regenc.h:69
struct coroutine_context * from
Definition: Context.h:41
void ** stack_pointer
Definition: Context.h:21
void * stack
Definition: Context.h:33
unsigned int uintptr_t
Definition: win32.h:106