File Coverage

lib/SPVM/Builder/src/spvm_runtime.c
Criterion Covered Total %
statement 50 50 100.0
branch 11 20 55.0
condition n/a
subroutine n/a
pod n/a
total 61 70 87.1


line stmt bran cond sub pod time code
1             // Copyright (c) 2023 Yuki Kimoto
2             // MIT License
3              
4             #include
5             #include
6             #include
7             #include
8             #include
9             #include
10              
11             #include "spvm_allocator.h"
12             #include "spvm_runtime.h"
13             #include "spvm_hash.h"
14             #include "spvm_runtime_string.h"
15             #include "spvm_runtime_basic_type.h"
16             #include "spvm_runtime_field.h"
17             #include "spvm_runtime_class_var.h"
18             #include "spvm_runtime_method.h"
19             #include "spvm_runtime_arg.h"
20             #include "spvm_opcode.h"
21             #include "spvm_mutex.h"
22              
23 1501           SPVM_RUNTIME* SPVM_RUNTIME_new() {
24 1501           SPVM_RUNTIME* runtime = SPVM_ALLOCATOR_alloc_memory_block_unmanaged(sizeof(SPVM_RUNTIME));
25            
26             // Allocator
27 1501           SPVM_ALLOCATOR* allocator = SPVM_ALLOCATOR_new();
28 1501           runtime->allocator = allocator;
29            
30 1501           runtime->basic_type_symtable = SPVM_HASH_new_hash_permanent(runtime->allocator, 0);
31            
32 1501           runtime->assignability_symtable = SPVM_HASH_new_hash_permanent(runtime->allocator, 0);
33            
34 1501           int32_t mutex_assignability_symtable_size = SPVM_MUTEX_size();
35 1501           void* mutex_assignability_symtable = SPVM_ALLOCATOR_alloc_memory_block_permanent(runtime->allocator, mutex_assignability_symtable_size);
36 1501           SPVM_MUTEX_init(mutex_assignability_symtable);
37 1501           runtime->mutex_assignability_symtable = mutex_assignability_symtable;
38            
39 1501           int32_t mutex_update_object_size = SPVM_MUTEX_size();
40 1501           void* mutex_update_object = SPVM_ALLOCATOR_alloc_memory_block_permanent(runtime->allocator, mutex_update_object_size);
41 1501           SPVM_MUTEX_init(mutex_update_object);
42 1501           runtime->mutex_update_object = mutex_update_object;
43            
44 1501           SPVM_RUNTIME_init_stdio(runtime);
45            
46 1501           return runtime;
47             }
48              
49 1501           void SPVM_RUNTIME_init_stdio(SPVM_RUNTIME* runtime) {
50            
51             // stdin
52             {
53 1501           int32_t stdin_fileno = fileno(stdin);
54            
55 1501 50         assert(stdin_fileno >= 0);
56            
57 1501           int32_t stdin_fileno_dup = dup(stdin_fileno);
58            
59 1501 50         assert(stdin_fileno_dup > 2);
60            
61 1501           FILE* spvm_stdin = fdopen(stdin_fileno_dup, "r");
62            
63 1501 50         assert(spvm_stdin);
64            
65             #if defined(_WIN32)
66            
67             setmode(fileno(spvm_stdin), _O_BINARY);
68            
69             #endif
70            
71 1501           runtime->spvm_stdin = spvm_stdin;
72             }
73            
74             // stdout
75             {
76 1501           int32_t stdout_fileno = fileno(stdout);
77            
78 1501 50         assert(stdout_fileno >= 0);
79            
80 1501           int32_t stdout_fileno_dup = dup(stdout_fileno);
81            
82 1501 50         assert(stdout_fileno_dup > 2);
83            
84 1501           FILE* spvm_stdout = fdopen(stdout_fileno_dup, "w");
85            
86 1501 50         assert(spvm_stdout);
87            
88             #if defined(_WIN32)
89            
90             setmode(fileno(spvm_stdout), _O_BINARY);
91            
92             #endif
93            
94 1501           runtime->spvm_stdout = spvm_stdout;
95            
96             }
97            
98             // stderr
99             {
100 1501           int32_t stderr_fileno = fileno(stderr);
101            
102 1501 50         assert(stderr_fileno >= 0);
103            
104 1501           int32_t stderr_fileno_dup = dup(stderr_fileno);
105            
106 1501 50         assert(stderr_fileno_dup > 2);
107            
108 1501           FILE* spvm_stderr = fdopen(stderr_fileno_dup, "w");
109            
110 1501 50         assert(spvm_stderr);
111            
112             #if defined(_WIN32)
113            
114             setmode(fileno(spvm_stderr), _O_BINARY);
115            
116             #endif
117            
118 1501           setvbuf(spvm_stderr, NULL, _IONBF, 0);
119            
120 1501           runtime->spvm_stderr = spvm_stderr;
121             }
122            
123 1501           }
124              
125 1155           void SPVM_RUNTIME_free(SPVM_RUNTIME* runtime) {
126            
127 1155 100         if (runtime->basic_types) {
128 607           SPVM_ALLOCATOR_free_memory_block_tmp(runtime->allocator, runtime->basic_types);
129             }
130            
131 1155           SPVM_MUTEX_destroy(runtime->mutex_assignability_symtable);
132            
133 1155           fclose(runtime->spvm_stdin);
134            
135 1155           fclose(runtime->spvm_stdout);
136            
137 1155           fclose(runtime->spvm_stderr);
138            
139             // Free allocator
140 1155           SPVM_ALLOCATOR_free(runtime->allocator);
141 1155           runtime->allocator = NULL;
142 1155           }