File Coverage

lib/SPVM/Builder/src/spvm_string.c
Criterion Covered Total %
statement 11 31 35.4
branch 2 4 50.0
condition n/a
subroutine n/a
pod n/a
total 13 35 37.1


line stmt bran cond sub pod time code
1             // Copyright (c) 2023 Yuki Kimoto
2             // MIT License
3              
4             #include "spvm_string.h"
5             #include "spvm_allocator.h"
6             #include "spvm_compiler.h"
7             #include "spvm_list.h"
8             #include "spvm_hash.h"
9              
10 9632403           SPVM_STRING* SPVM_STRING_new(SPVM_COMPILER* compiler, const char* value, int32_t length) {
11            
12 9632403           SPVM_STRING* found_constant_string = SPVM_HASH_get(compiler->constant_string_symtable, value, length);
13 9632403 100         if (found_constant_string) {
14 9061878           return found_constant_string;
15             }
16             else {
17 570525           SPVM_STRING* constant_string = SPVM_ALLOCATOR_alloc_memory_block_permanent(compiler->global_allocator, sizeof(SPVM_STRING));
18 570525           constant_string->value = SPVM_ALLOCATOR_alloc_memory_block_permanent(compiler->global_allocator, length + 1);
19 570525           memcpy((char*)constant_string->value, value, length);
20 570525           constant_string->length = length;
21            
22 570525           SPVM_LIST_push(compiler->constant_strings, constant_string);
23 570525           SPVM_HASH_set(compiler->constant_string_symtable, constant_string->value, length, constant_string);
24            
25 570525           return constant_string;
26             }
27             }
28              
29 0           SPVM_STRING* SPVM_STRING_new_global(SPVM_COMPILER* compiler, const char* value, int32_t length) {
30            
31 0           SPVM_STRING* found_global_string = SPVM_HASH_get(compiler->global_string_symtable, value, length);
32 0 0         if (found_global_string) {
33 0           return found_global_string;
34             }
35             else {
36 0           SPVM_STRING* global_string = SPVM_ALLOCATOR_alloc_memory_block_permanent(compiler->global_allocator, sizeof(SPVM_STRING));
37 0           global_string->value = SPVM_ALLOCATOR_alloc_memory_block_permanent(compiler->global_allocator, length + 1);
38 0           memcpy((char*)global_string->value, value, length);
39 0           global_string->length = length;
40            
41 0           SPVM_LIST_push(compiler->global_strings, global_string);
42 0           SPVM_HASH_set(compiler->global_string_symtable, global_string->value, length, global_string);
43            
44 0           return global_string;
45             }
46             }
47              
48 0           SPVM_STRING* SPVM_STRING_new_global_tmp(SPVM_COMPILER* compiler, const char* value, int32_t length) {
49            
50 0           SPVM_STRING* global_string = SPVM_ALLOCATOR_alloc_memory_block_tmp(compiler->global_allocator, sizeof(SPVM_STRING));
51 0           global_string->value = SPVM_ALLOCATOR_alloc_memory_block_permanent(compiler->global_allocator, length + 1);
52 0           memcpy((char*)global_string->value, value, length);
53 0           global_string->length = length;
54            
55 0           return global_string;
56             }
57              
58 0           void SPVM_STRING_free_global_tmp(SPVM_COMPILER* compiler, SPVM_STRING* string) {
59            
60 0           SPVM_ALLOCATOR_free_memory_block_tmp(compiler->global_allocator, string);
61 0           }