Ruby  2.7.1p83(2020-03-31revisiona0c7c23c9cec0d0ffcba012279cd652d28ad5bf3)
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 
8 enum {
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 
14 struct coroutine_context
15 {
16  void **stack_pointer;
17 };
18 
20 
21 static inline void coroutine_initialize_main(struct coroutine_context * context) {
22  context->stack_pointer = NULL;
23 }
24 
25 static 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 
44 struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
45 
46 static inline void coroutine_destroy(struct coroutine_context * context)
47 {
48  context->stack_pointer = NULL;
49 }
coroutine_context::stack
void * stack
Definition: Context.h:29
assert
#define assert(x)
Definition: dlmalloc.c:1176
COROUTINE_REGISTERS
@ COROUTINE_REGISTERS
Definition: Context.h:15
assert.h
uintptr_t
unsigned int uintptr_t
Definition: win32.h:106
NULL
#define NULL
Definition: _sdbm.c:101
COROUTINE
#define COROUTINE
Definition: Context.h:6
coroutine_start
COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self)
Definition: Context.h:22
coroutine_context::from
struct coroutine_context * from
Definition: Context.h:37
size
int size
Definition: encoding.c:58
coroutine_context
Definition: Context.h:17
coroutine_transfer
struct coroutine_context * coroutine_transfer(struct coroutine_context *current, struct coroutine_context *target)
Definition: Context.c:115
memset
void * memset(void *, int, size_t)
top
unsigned int top
Definition: nkf.c:4323
coroutine_context::stack_pointer
void ** stack_pointer
Definition: Context.h:19