File Coverage

lib/SPVM/Builder/src/spvm_list.c
Criterion Covered Total %
statement 80 95 84.2
branch 33 52 63.4
condition n/a
subroutine n/a
pod n/a
total 113 147 76.8


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              
8             #include "spvm_list.h"
9             #include "spvm_allocator.h"
10              
11 8023428           SPVM_LIST* SPVM_LIST_new(SPVM_ALLOCATOR* allocator, int32_t capacity, int32_t memory_block_type) {
12            
13 8023428 50         assert(capacity >= 0);
14            
15             SPVM_LIST* list;
16 8023428 100         if (memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
17 5954749           list = SPVM_ALLOCATOR_alloc_memory_block_tmp(allocator, sizeof(SPVM_LIST));
18             }
19 2068679 50         else if (memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_PERMANENT) {
20 2068679           list = SPVM_ALLOCATOR_alloc_memory_block_permanent(allocator, sizeof(SPVM_LIST));
21             }
22             else {
23 0           assert(0);
24             }
25            
26 8023428           list->length = 0;
27            
28 8023428 100         if (capacity == 0) {
29 7925289           list->capacity = 1;
30             }
31             else {
32 98139           list->capacity = capacity;
33             }
34            
35             void** values;
36 8023428 100         if (memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
37 5954749           values = SPVM_ALLOCATOR_alloc_memory_block_tmp(allocator, list->capacity * sizeof(void*));
38             }
39 2068679 50         else if (memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_PERMANENT) {
40 2068679           values = SPVM_ALLOCATOR_alloc_memory_block_permanent(allocator, list->capacity * sizeof(void*));
41             }
42             else {
43 0           assert(0);
44             }
45              
46 8023428           list->values = values;
47            
48 8023428           list->allocator = allocator;
49            
50 8023428           list->memory_block_type = memory_block_type;
51            
52 8023428           return list;
53             }
54              
55 1384766           SPVM_LIST* SPVM_LIST_new_list_permanent(SPVM_ALLOCATOR* allocator, int32_t capacity) {
56             (void)allocator;
57              
58 1384766           int32_t memory_block_type = SPVM_ALLOCATOR_C_ALLOC_TYPE_PERMANENT;
59 1384766           SPVM_LIST* list = SPVM_LIST_new(allocator, capacity, memory_block_type);
60            
61 1384766           return list;
62             }
63              
64 77177120           void SPVM_LIST_maybe_extend(SPVM_LIST* list) {
65            
66 77177120 50         assert(list);
67            
68 77177120           SPVM_ALLOCATOR* allocator = list->allocator;
69            
70 77177120           int32_t length = list->length;
71 77177120           int32_t capacity = list->capacity;
72            
73 77177120 100         if (length >= capacity) {
74 4059945           int32_t new_capacity = capacity * 2;
75            
76             void** new_values;
77 4059945 100         if (list->memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
78 2217661           new_values = SPVM_ALLOCATOR_alloc_memory_block_tmp(allocator, new_capacity * sizeof(void*));
79             }
80 1842284 50         else if (list->memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_PERMANENT) {
81 1842284           new_values = SPVM_ALLOCATOR_alloc_memory_block_permanent(allocator, new_capacity * sizeof(void*));
82             }
83             else {
84 0           assert(0);
85             }
86 4059945           memcpy(new_values, list->values, capacity * sizeof(void*));
87 4059945 100         if (list->memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
88 2217661           SPVM_ALLOCATOR_free_memory_block_tmp(allocator, list->values);
89             }
90 1842284 50         else if (list->memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_PERMANENT) {
91             // Nothing
92             }
93             else {
94 0           assert(0);
95             }
96              
97 4059945           list->values = new_values;
98            
99 4059945           list->capacity = new_capacity;
100             }
101 77177120           }
102              
103 5954749           void SPVM_LIST_free(SPVM_LIST* list) {
104              
105 5954749           SPVM_ALLOCATOR* allocator = list->allocator;
106              
107 5954749 50         if (list->memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
108 5954749           SPVM_ALLOCATOR_free_memory_block_tmp(allocator, list->values);
109 5954749           SPVM_ALLOCATOR_free_memory_block_tmp(allocator, list);
110             }
111 0 0         else if (list->memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_PERMANENT) {
112             // Nothing
113             }
114             else {
115 0           assert(0);
116             }
117 5954749           }
118              
119 77177120           void SPVM_LIST_push(SPVM_LIST* list, void* value) {
120            
121 77177120           SPVM_LIST_maybe_extend(list);
122            
123 77177120           int32_t length = list->length;
124            
125 77177120           *(void**)&list->values[length] = value;
126 77177120           list->length++;
127 77177120           }
128              
129 0           void SPVM_LIST_unshift(SPVM_LIST* list, void* value) {
130            
131 0           SPVM_LIST_maybe_extend(list);
132            
133 0           int32_t length = list->length;
134            
135 0           memmove(list->values + 1, list->values, length);
136            
137 0           *(void**)&list->values[0] = value;
138 0           list->length++;
139 0           }
140              
141 175973025           void* SPVM_LIST_get(SPVM_LIST* list, int32_t index) {
142 175973025 50         assert(list);
143 175973025 50         assert(index >= 0);
144 175973025 50         assert(index < list->length);
145            
146            
147 175973025           return *(void**)&list->values[index];
148             }
149              
150 10456           void SPVM_LIST_set(SPVM_LIST* list, int32_t index, void* value) {
151            
152 10456 50         assert(list);
153 10456 50         assert(index >= 0);
154 10456 50         assert(index < list->length);
155            
156 10456           *(void**)&list->values[index] = value;
157 10456           }
158              
159 8829180           void* SPVM_LIST_pop(SPVM_LIST* list) {
160            
161 8829180 50         assert(list->length >= 0);
162            
163 8829180 50         if (list->length == 0) {
164 0           return NULL;
165             }
166             else {
167 8829180           list->length--;
168 8829180           return *(void**)&list->values[list->length];
169             }
170             }
171              
172 1155           void SPVM_LIST_clear(SPVM_LIST* list) {
173            
174 32980 100         while (list->length > 0) {
175 31825           list->length--;
176             }
177 1155           }
178              
179 128991           void* SPVM_LIST_shift(SPVM_LIST* list) {
180            
181 128991 50         assert(list->length >= 0);
182            
183 128991 50         if (list->length == 0) {
184 0           return NULL;
185             }
186             else {
187 128991           void* return_value = list->values[0];
188 1739752 100         for (int32_t i = 0; i < list->length - 1; i++) {
189 1610761           list->values[i] = list->values[i + 1];
190             }
191              
192 128991           list->length--;
193 128991           return return_value;
194             }
195             }