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 7916916           SPVM_LIST* SPVM_LIST_new(SPVM_ALLOCATOR* allocator, int32_t capacity, int32_t memory_block_type) {
12            
13 7916916 50         assert(capacity >= 0);
14            
15             SPVM_LIST* list;
16 7916916 100         if (memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
17 5875809           list = SPVM_ALLOCATOR_alloc_memory_block_tmp(allocator, sizeof(SPVM_LIST));
18             }
19 2041107 50         else if (memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_PERMANENT) {
20 2041107           list = SPVM_ALLOCATOR_alloc_memory_block_permanent(allocator, sizeof(SPVM_LIST));
21             }
22             else {
23 0           assert(0);
24             }
25            
26 7916916           list->length = 0;
27            
28 7916916 100         if (capacity == 0) {
29 7820155           list->capacity = 1;
30             }
31             else {
32 96761           list->capacity = capacity;
33             }
34            
35             void** values;
36 7916916 100         if (memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
37 5875809           values = SPVM_ALLOCATOR_alloc_memory_block_tmp(allocator, list->capacity * sizeof(void*));
38             }
39 2041107 50         else if (memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_PERMANENT) {
40 2041107           values = SPVM_ALLOCATOR_alloc_memory_block_permanent(allocator, list->capacity * sizeof(void*));
41             }
42             else {
43 0           assert(0);
44             }
45              
46 7916916           list->values = values;
47            
48 7916916           list->allocator = allocator;
49            
50 7916916           list->memory_block_type = memory_block_type;
51            
52 7916916           return list;
53             }
54              
55 1365984           SPVM_LIST* SPVM_LIST_new_list_permanent(SPVM_ALLOCATOR* allocator, int32_t capacity) {
56             (void)allocator;
57              
58 1365984           int32_t memory_block_type = SPVM_ALLOCATOR_C_ALLOC_TYPE_PERMANENT;
59 1365984           SPVM_LIST* list = SPVM_LIST_new(allocator, capacity, memory_block_type);
60            
61 1365984           return list;
62             }
63              
64 76124364           void SPVM_LIST_maybe_extend(SPVM_LIST* list) {
65            
66 76124364 50         assert(list);
67            
68 76124364           SPVM_ALLOCATOR* allocator = list->allocator;
69            
70 76124364           int32_t length = list->length;
71 76124364           int32_t capacity = list->capacity;
72            
73 76124364 100         if (length >= capacity) {
74 4005387           int32_t new_capacity = capacity * 2;
75            
76             void** new_values;
77 4005387 100         if (list->memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
78 2187813           new_values = SPVM_ALLOCATOR_alloc_memory_block_tmp(allocator, new_capacity * sizeof(void*));
79             }
80 1817574 50         else if (list->memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_PERMANENT) {
81 1817574           new_values = SPVM_ALLOCATOR_alloc_memory_block_permanent(allocator, new_capacity * sizeof(void*));
82             }
83             else {
84 0           assert(0);
85             }
86 4005387           memcpy(new_values, list->values, capacity * sizeof(void*));
87 4005387 100         if (list->memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
88 2187813           SPVM_ALLOCATOR_free_memory_block_tmp(allocator, list->values);
89             }
90 1817574 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 4005387           list->values = new_values;
98            
99 4005387           list->capacity = new_capacity;
100             }
101 76124364           }
102              
103 5875809           void SPVM_LIST_free(SPVM_LIST* list) {
104              
105 5875809           SPVM_ALLOCATOR* allocator = list->allocator;
106              
107 5875809 50         if (list->memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
108 5875809           SPVM_ALLOCATOR_free_memory_block_tmp(allocator, list->values);
109 5875809           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 5875809           }
118              
119 76124364           void SPVM_LIST_push(SPVM_LIST* list, void* value) {
120            
121 76124364           SPVM_LIST_maybe_extend(list);
122            
123 76124364           int32_t length = list->length;
124            
125 76124364           *(void**)&list->values[length] = value;
126 76124364           list->length++;
127 76124364           }
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 173605705           void* SPVM_LIST_get(SPVM_LIST* list, int32_t index) {
142 173605705 50         assert(list);
143 173605705 50         assert(index >= 0);
144 173605705 50         assert(index < list->length);
145            
146            
147 173605705           return *(void**)&list->values[index];
148             }
149              
150 10296           void SPVM_LIST_set(SPVM_LIST* list, int32_t index, void* value) {
151            
152 10296 50         assert(list);
153 10296 50         assert(index >= 0);
154 10296 50         assert(index < list->length);
155            
156 10296           *(void**)&list->values[index] = value;
157 10296           }
158              
159 8707538           void* SPVM_LIST_pop(SPVM_LIST* list) {
160            
161 8707538 50         assert(list->length >= 0);
162            
163 8707538 50         if (list->length == 0) {
164 0           return NULL;
165             }
166             else {
167 8707538           list->length--;
168 8707538           return *(void**)&list->values[list->length];
169             }
170             }
171              
172 1145           void SPVM_LIST_clear(SPVM_LIST* list) {
173            
174 32430 100         while (list->length > 0) {
175 31285           list->length--;
176             }
177 1145           }
178              
179 126447           void* SPVM_LIST_shift(SPVM_LIST* list) {
180            
181 126447 50         assert(list->length >= 0);
182            
183 126447 50         if (list->length == 0) {
184 0           return NULL;
185             }
186             else {
187 126447           void* return_value = list->values[0];
188 1706266 100         for (int32_t i = 0; i < list->length - 1; i++) {
189 1579819           list->values[i] = list->values[i + 1];
190             }
191              
192 126447           list->length--;
193 126447           return return_value;
194             }
195             }