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 10189           SPVM_ALLOCATOR* SPVM_ALLOCATOR_new() {
12            
13 10189           SPVM_ALLOCATOR* allocator = SPVM_ALLOCATOR_alloc_memory_block_unmanaged(sizeof(SPVM_ALLOCATOR));
14            
15 10189 50         assert(allocator->memory_blocks_count_tmp == 0);
16 10189 50         assert(allocator->memory_blocks_count_permanent == 0);
17            
18 10189           allocator->permanent_memory_blocks_capacity = 1024;
19 10189           allocator->permanent_memory_blocks = SPVM_ALLOCATOR_alloc_memory_block_unmanaged(sizeof(void*) * allocator->permanent_memory_blocks_capacity);
20            
21 10189           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 124336889           void* SPVM_ALLOCATOR_alloc_memory_block_unmanaged(size_t size) {
31            
32 124336889 50         if (size < 1) {
33 0           return NULL;
34             }
35            
36             if ((size_t)size > SIZE_MAX) {
37             return NULL;
38             }
39            
40 124336889           void* block = calloc(1, (size_t)size);
41            
42 124336889           return block;
43             }
44              
45 113039453           void SPVM_ALLOCATOR_free_memory_block_unmanaged(void* block) {
46 113039453           free(block);
47 113039453           }
48              
49 88386850           void* SPVM_ALLOCATOR_alloc_memory_block_tmp(SPVM_ALLOCATOR* allocator, size_t size) {
50            
51 88386850           void* block = SPVM_ALLOCATOR_alloc_memory_block_unmanaged(size);
52            
53 88386850 50         assert(allocator);
54 88386850           allocator->memory_blocks_count_tmp++;
55            
56 88386850           return block;
57             }
58              
59 88336891           void SPVM_ALLOCATOR_free_memory_block_tmp(SPVM_ALLOCATOR* allocator, void* block) {
60            
61 88336891           SPVM_ALLOCATOR_free_memory_block_unmanaged(block);
62            
63 88336891           allocator->memory_blocks_count_tmp--;
64 88336891           }
65              
66 33914389           void* SPVM_ALLOCATOR_alloc_memory_block_permanent(SPVM_ALLOCATOR* allocator, size_t size) {
67             (void)allocator;
68            
69 33914389           void* parmanent_block = SPVM_ALLOCATOR_alloc_memory_block_unmanaged(size);
70 33914389           allocator->memory_blocks_count_permanent++;
71            
72 33914389           int32_t length = allocator->permanent_memory_blocks_length;
73 33914389           int32_t capacity = allocator->permanent_memory_blocks_capacity;
74            
75 33914389 100         if (length >= capacity) {
76 6967           int32_t new_capacity = capacity * 2;
77             void** new_permanent_memory_blocks;
78 6967           new_permanent_memory_blocks = SPVM_ALLOCATOR_alloc_memory_block_unmanaged(sizeof(void*) * new_capacity);
79 6967           memcpy(new_permanent_memory_blocks, allocator->permanent_memory_blocks, sizeof(void*) * capacity);
80 6967           SPVM_ALLOCATOR_free_memory_block_unmanaged(allocator->permanent_memory_blocks);
81            
82 6967           allocator->permanent_memory_blocks = new_permanent_memory_blocks;
83 6967           allocator->permanent_memory_blocks_capacity = new_capacity;
84             }
85            
86 33914389           int32_t permanent_memory_blocks_index = allocator->permanent_memory_blocks_length;
87 33914389           allocator->permanent_memory_blocks[permanent_memory_blocks_index] = parmanent_block;
88 33914389           allocator->permanent_memory_blocks_length++;
89            
90 33914389           return parmanent_block;
91             }
92              
93 22675018           void SPVM_ALLOCATOR_free_memory_block_permanent(SPVM_ALLOCATOR* allocator, void* block) {
94            
95 22675018           SPVM_ALLOCATOR_free_memory_block_unmanaged(block);
96            
97 22675018           allocator->memory_blocks_count_permanent--;
98            
99 22675018           }
100              
101 7973           void SPVM_ALLOCATOR_free(SPVM_ALLOCATOR* allocator) {
102            
103             int32_t i;
104 22682991 100         for (i = 0; i < allocator->permanent_memory_blocks_length; i++) {
105 22675018           void* permanent_memory_block = allocator->permanent_memory_blocks[i];
106 22675018 50         assert(permanent_memory_block);
107 22675018 50         if (permanent_memory_block) {
108 22675018           SPVM_ALLOCATOR_free_memory_block_permanent(allocator, permanent_memory_block);
109             }
110             }
111 7973           SPVM_ALLOCATOR_free_memory_block_unmanaged(allocator->permanent_memory_blocks);
112            
113 7973 50         assert(allocator->memory_blocks_count_tmp == 0);
114            
115 7973 50         assert(allocator->memory_blocks_count_permanent == 0);
116            
117 7973           SPVM_ALLOCATOR_free_memory_block_unmanaged(allocator);
118 7973           }