asar coverage - build #241


src/asar/
File: src/asar/table.cpp
Date: 2025-02-21 03:13:54
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 2795 table::table() {
7 2795 memset(data, 0, sizeof(data));
8 2795 utf8_mode = true;
9 2795 }
10
11 4844 void table::clear() {
12
2/2
✓ Branch 0 taken 1240064 times.
✓ Branch 1 taken 4844 times.
1244908 for(int i=0; i<256; i++) {
13
2/2
✓ Branch 0 taken 190 times.
✓ Branch 1 taken 1239874 times.
1240064 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 4844 memset(data, 0, sizeof(data));
21 4844 }
22
23 2301 void table::copy_from(const table& from) {
24 2301 memcpy(data, from.data, sizeof(data));
25 2301 utf8_mode = from.utf8_mode;
26 // copy over all allocated pages
27
2/2
✓ Branch 0 taken 589056 times.
✓ Branch 1 taken 2301 times.
591357 for(int i=0; i<256; i++) {
28
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 589020 times.
589056 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 2301 }
42
43 2301 table& table::operator=(const table& from) {
44 2301 clear();
45 2301 copy_from(from);
46 2301 return *this;
47 }
48
49 table::table(const table& from) {
50 copy_from(from);
51 }
52
53 2543 table::~table() {
54 2543 clear();
55 2543 }
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