asar coverage - build #168


src/asar/
File: src/asar/table.cpp
Date: 2024-01-27 19:36:05
Lines:
55/58
94.8%
Functions:
7/8
87.5%
Branches:
27/28
96.4%

Line Branch Exec Source
1 #include <cstdlib>
2 #include <cstdint>
3 #include <cstring>
4 #include "table.h"
5
6 2715 table::table() {
7 2715 memset(data, 0, sizeof(data));
8 2715 utf8_mode = true;
9 2715 }
10
11 4702 void table::clear() {
12
2/2
✓ Branch 0 taken 1203712 times.
✓ Branch 1 taken 4702 times.
1208414 for(int i=0; i<256; i++) {
13
2/2
✓ Branch 0 taken 190 times.
✓ Branch 1 taken 1203522 times.
1203712 if(data[i] != nullptr) {
14
2/2
✓ Branch 0 taken 48640 times.
✓ Branch 1 taken 190 times.
48830 for(int j=0; j<256; j++) {
15
2/2
✓ Branch 0 taken 258 times.
✓ Branch 1 taken 48382 times.
48640 if(data[i][j] != nullptr) free(data[i][j]);
16 }
17 190 free(data[i]);
18 }
19 }
20 4702 memset(data, 0, sizeof(data));
21 4702 }
22
23 2235 void table::copy_from(const table& from) {
24 2235 memcpy(data, from.data, sizeof(data));
25 2235 utf8_mode = from.utf8_mode;
26 // copy over all allocated pages
27
2/2
✓ Branch 0 taken 572160 times.
✓ Branch 1 taken 2235 times.
574395 for(int i=0; i<256; i++) {
28
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 572124 times.
572160 if(data[i] != nullptr) {
29 36 table_page** newp = (table_page**)calloc(256,sizeof(void*));
30 36 memcpy(newp, data[i], 256*sizeof(void*));
31 36 data[i] = newp;
32
2/2
✓ Branch 0 taken 9216 times.
✓ Branch 1 taken 36 times.
9252 for(int j=0; j<256; j++) {
33
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 9180 times.
9216 if(data[i][j] != nullptr) {
34 36 table_page* newp = (table_page*)calloc(1,sizeof(table_page));
35 36 memcpy(newp, data[i][j], sizeof(table_page));
36 36 data[i][j] = newp;
37 }
38 }
39 }
40 }
41 2235 }
42
43 2235 table& table::operator=(const table& from) {
44 2235 clear();
45 2235 copy_from(from);
46 2235 return *this;
47 }
48
49 table::table(const table& from) {
50 copy_from(from);
51 }
52
53 2467 table::~table() {
54 2467 clear();
55 2467 }
56
57 1188 void table::set_val(int off, uint32_t val) {
58
2/2
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 1026 times.
1188 if(data[off >> 16] == nullptr) {
59 162 data[off >> 16] = (table_page**)calloc(256,sizeof(void*));
60 }
61 1188 table_page** thisbank = data[off >> 16];
62
2/2
✓ Branch 0 taken 234 times.
✓ Branch 1 taken 954 times.
1188 if(thisbank[(off >> 8) & 255] == nullptr) {
63 234 thisbank[(off >> 8) & 255] = (table_page*)calloc(1,sizeof(table_page));
64 }
65 1188 table_page* thispage = thisbank[(off >> 8) & 255];
66 1188 int idx = (off & 255) / 32;
67 594 int bit = off % 32;
68 1188 thispage->defined[idx] |= 1<<bit;
69 1188 thispage->chars[off & 255] = val;
70 1188 }
71
72 3378 int64_t table::get_val(int off) {
73
1/2
✓ Branch 0 taken 3378 times.
✗ Branch 1 not taken.
3378 int64_t def = utf8_mode ? off : -1;
74 3378 table_page** thisbank = data[off >> 16];
75
2/2
✓ Branch 0 taken 1698 times.
✓ Branch 1 taken 1680 times.
3378 if(thisbank == nullptr) return def;
76 2412 table_page* thispage = thisbank[(off >> 8) & 255];
77
2/2
✓ Branch 0 taken 1206 times.
✓ Branch 1 taken 1206 times.
2412 if(thispage == nullptr) return def;
78 2412 int idx = (off & 255) / 32;
79 1206 int bit = off % 32;
80
2/2
✓ Branch 0 taken 1206 times.
✓ Branch 1 taken 1206 times.
2412 if(((thispage->defined[idx] >> bit) & 1) == 0) return def;
81 2412 return thispage->chars[off & 255];
82 }
83