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