File Coverage

lib/SPVM/Builder/src/spvm_allocator.c
Criterion Covered Total %
statement 54 58 93.1
branch 12 20 60.0
condition n/a
subroutine n/a
pod n/a
total 66 78 84.6


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              
9             #include "spvm_allocator.h"
10              
11 10038           SPVM_ALLOCATOR* SPVM_ALLOCATOR_new() {
12            
13 10038           SPVM_ALLOCATOR* allocator = SPVM_ALLOCATOR_alloc_memory_block_unmanaged(sizeof(SPVM_ALLOCATOR));
14            
15 10038 50         assert(allocator->memory_blocks_count_tmp == 0);
16 10038 50         assert(allocator->memory_blocks_count_permanent == 0);
17            
18 10038           allocator->permanent_memory_blocks_capacity = 1024;
19 10038           allocator->permanent_memory_blocks = SPVM_ALLOCATOR_alloc_memory_block_unmanaged(sizeof(void*) * allocator->permanent_memory_blocks_capacity);
20            
21 10038           return allocator;
22             }
23              
24 0           int32_t SPVM_ALLOCATOR_get_memory_blocks_count(SPVM_ALLOCATOR* allocator) {
25 0           int32_t memory_blocks_count = allocator->memory_blocks_count_tmp + allocator->memory_blocks_count_permanent;
26            
27 0           return memory_blocks_count;
28             }
29              
30 122683101           void* SPVM_ALLOCATOR_alloc_memory_block_unmanaged(size_t size) {
31            
32 122683101 50         if (size < 1) {
33 0           return NULL;
34             }
35            
36             if ((size_t)size > SIZE_MAX) {
37             return NULL;
38             }
39            
40 122683101           void* block = calloc(1, (size_t)size);
41            
42 122683101           return block;
43             }
44              
45 111284618           void SPVM_ALLOCATOR_free_memory_block_unmanaged(void* block) {
46 111284618           free(block);
47 111284618           }
48              
49 87188888           void* SPVM_ALLOCATOR_alloc_memory_block_tmp(SPVM_ALLOCATOR* allocator, size_t size) {
50            
51 87188888           void* block = SPVM_ALLOCATOR_alloc_memory_block_unmanaged(size);
52            
53 87188888 50         assert(allocator);
54 87188888           allocator->memory_blocks_count_tmp++;
55            
56 87188888           return block;
57             }
58              
59 87139159           void SPVM_ALLOCATOR_free_memory_block_tmp(SPVM_ALLOCATOR* allocator, void* block) {
60            
61 87139159           SPVM_ALLOCATOR_free_memory_block_unmanaged(block);
62            
63 87139159           allocator->memory_blocks_count_tmp--;
64 87139159           }
65              
66 33446964           void* SPVM_ALLOCATOR_alloc_memory_block_permanent(SPVM_ALLOCATOR* allocator, size_t size) {
67             (void)allocator;
68            
69 33446964           void* parmanent_block = SPVM_ALLOCATOR_alloc_memory_block_unmanaged(size);
70 33446964           allocator->memory_blocks_count_permanent++;
71            
72 33446964           int32_t length = allocator->permanent_memory_blocks_length;
73 33446964           int32_t capacity = allocator->permanent_memory_blocks_capacity;
74            
75 33446964 100         if (length >= capacity) {
76 6875           int32_t new_capacity = capacity * 2;
77             void** new_permanent_memory_blocks;
78 6875           new_permanent_memory_blocks = SPVM_ALLOCATOR_alloc_memory_block_unmanaged(sizeof(void*) * new_capacity);
79 6875           memcpy(new_permanent_memory_blocks, allocator->permanent_memory_blocks, sizeof(void*) * capacity);
80 6875           SPVM_ALLOCATOR_free_memory_block_unmanaged(allocator->permanent_memory_blocks);
81            
82 6875           allocator->permanent_memory_blocks = new_permanent_memory_blocks;
83 6875           allocator->permanent_memory_blocks_capacity = new_capacity;
84             }
85            
86 33446964           int32_t permanent_memory_blocks_index = allocator->permanent_memory_blocks_length;
87 33446964           allocator->permanent_memory_blocks[permanent_memory_blocks_index] = parmanent_block;
88 33446964           allocator->permanent_memory_blocks_length++;
89            
90 33446964           return parmanent_block;
91             }
92              
93 22106140           void SPVM_ALLOCATOR_free_memory_block_permanent(SPVM_ALLOCATOR* allocator, void* block) {
94            
95 22106140           SPVM_ALLOCATOR_free_memory_block_unmanaged(block);
96            
97 22106140           allocator->memory_blocks_count_permanent--;
98            
99 22106140           }
100              
101 7880           void SPVM_ALLOCATOR_free(SPVM_ALLOCATOR* allocator) {
102            
103             int32_t i;
104 22114020 100         for (i = 0; i < allocator->permanent_memory_blocks_length; i++) {
105 22106140           void* permanent_memory_block = allocator->permanent_memory_blocks[i];
106 22106140 50         assert(permanent_memory_block);
107 22106140 50         if (permanent_memory_block) {
108 22106140           SPVM_ALLOCATOR_free_memory_block_permanent(allocator, permanent_memory_block);
109             }
110             }
111 7880           SPVM_ALLOCATOR_free_memory_block_unmanaged(allocator->permanent_memory_blocks);
112            
113 7880 50         assert(allocator->memory_blocks_count_tmp == 0);
114            
115 7880 50         assert(allocator->memory_blocks_count_permanent == 0);
116            
117 7880           SPVM_ALLOCATOR_free_memory_block_unmanaged(allocator);
118 7880           }