Ruby
2.7.1p83(2020-03-31revisiona0c7c23c9cec0d0ffcba012279cd652d28ad5bf3)
coroutine
x86
Context.h
Go to the documentation of this file.
1
/*
2
* This file is part of the "Coroutine" project and released under the MIT License.
3
*
4
* Created by Samuel Williams on 3/11/2018.
5
* Copyright, 2018, by Samuel Williams. All rights reserved.
6
*/
7
8
#pragma once
9
10
#include <
assert.h
>
11
#include <string.h>
12
13
#define COROUTINE __attribute__((noreturn, fastcall)) void
14
#define COROUTINE_LIMITED_ADDRESS_SPACE
15
16
enum
{
COROUTINE_REGISTERS
= 4};
17
18
struct
coroutine_context
19
{
20
void
**
stack_pointer
;
21
};
22
23
typedef
COROUTINE
(*
coroutine_start
)(
struct
coroutine_context
*
from
,
struct
coroutine_context
*
self
)
__attribute__
((fastcall));
24
25
static
inline
void
coroutine_initialize_main(
struct
coroutine_context
* context) {
26
context->
stack_pointer
=
NULL
;
27
}
28
29
static
inline
void
coroutine_initialize(
30
struct
coroutine_context
*context,
31
coroutine_start
start,
32
void
*
stack
,
33
size_t
size
34
) {
35
assert
(start &&
stack
&&
size
>= 1024);
36
37
// Stack grows down. Force 16-byte alignment.
38
char
*
top
= (
char
*)
stack
+
size
;
39
context->
stack_pointer
= (
void
**)((
uintptr_t
)
top
& ~0xF);
40
41
*--context->
stack_pointer
=
NULL
;
42
*--context->
stack_pointer
= (
void
*)start;
43
44
context->
stack_pointer
-=
COROUTINE_REGISTERS
;
45
memset
(context->
stack_pointer
, 0,
sizeof
(
void
*) *
COROUTINE_REGISTERS
);
46
}
47
48
struct
coroutine_context
*
coroutine_transfer
(
struct
coroutine_context
* current,
struct
coroutine_context
* target)
__attribute__
((fastcall));
49
50
static
inline
void
coroutine_destroy(
struct
coroutine_context
* context)
51
{
52
context->
stack_pointer
=
NULL
;
53
}
__attribute__
unsigned int UINT8 __attribute__((__mode__(__QI__)))
Definition:
ffi_common.h:110
COROUTINE
#define COROUTINE
Definition:
Context.h:13
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_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
Generated by
1.8.17