Branch data Line data Source code
1 : : #include <cstdlib>
2 : : #include <cstdint>
3 : : #include <cstring>
4 : : #include "table.h"
5 : :
6 : 571 : table::table() {
7 : 571 : memset(data, 0, sizeof(data));
8 : 571 : utf8_mode = true;
9 : 571 : }
10 : :
11 : 915 : void table::clear() {
12 [ + + ]: 235155 : for(int i=0; i<256; i++) {
13 [ + + ]: 234240 : if(data[i] != nullptr) {
14 [ + + ]: 8481 : for(int j=0; j<256; j++) {
15 [ + + ]: 8448 : if(data[i][j] != nullptr) free(data[i][j]);
16 : : }
17 : 33 : free(data[i]);
18 : : }
19 : : }
20 : 915 : memset(data, 0, sizeof(data));
21 : 915 : }
22 : :
23 : 344 : void table::copy_from(const table& from) {
24 : 344 : memcpy(data, from.data, sizeof(data));
25 : 344 : utf8_mode = from.utf8_mode;
26 : : // copy over all allocated pages
27 [ + + ]: 88408 : for(int i=0; i<256; i++) {
28 [ + + ]: 88064 : if(data[i] != nullptr) {
29 : 6 : table_page** newp = (table_page**)calloc(256,sizeof(void*));
30 : 6 : memcpy(newp, data[i], 256*sizeof(void*));
31 : 6 : data[i] = newp;
32 [ + + ]: 1542 : for(int j=0; j<256; j++) {
33 [ + + ]: 1536 : if(data[i][j] != nullptr) {
34 : 6 : table_page* newp = (table_page*)calloc(1,sizeof(table_page));
35 : 6 : memcpy(newp, data[i][j], sizeof(table_page));
36 : 6 : data[i][j] = newp;
37 : : }
38 : : }
39 : : }
40 : : }
41 : 344 : }
42 : :
43 : 344 : table& table::operator=(const table& from) {
44 : 344 : clear();
45 : 344 : copy_from(from);
46 : 344 : return *this;
47 : : }
48 : :
49 : 0 : table::table(const table& from) {
50 : 0 : copy_from(from);
51 : 0 : }
52 : :
53 : 571 : table::~table() {
54 : 571 : clear();
55 : 571 : }
56 : :
57 : 198 : void table::set_val(int off, uint32_t val) {
58 [ + + ]: 198 : if(data[off >> 16] == nullptr) {
59 : 27 : data[off >> 16] = (table_page**)calloc(256,sizeof(void*));
60 : : }
61 : 198 : table_page** thisbank = data[off >> 16];
62 [ + + ]: 198 : if(thisbank[(off >> 8) & 255] == nullptr) {
63 : 39 : thisbank[(off >> 8) & 255] = (table_page*)calloc(1,sizeof(table_page));
64 : : }
65 : 198 : table_page* thispage = thisbank[(off >> 8) & 255];
66 : 198 : int idx = (off & 255) / 32;
67 : : int bit = off % 32;
68 : 198 : thispage->defined[idx] |= 1<<bit;
69 : 198 : thispage->chars[off & 255] = val;
70 : 198 : }
71 : :
72 : 551 : int64_t table::get_val(int off) {
73 [ + - ]: 551 : int64_t def = utf8_mode ? off : -1;
74 : 551 : table_page** thisbank = data[off >> 16];
75 [ + + ]: 551 : if(thisbank == nullptr) return def;
76 : 402 : table_page* thispage = thisbank[(off >> 8) & 255];
77 [ + - ]: 402 : if(thispage == nullptr) return def;
78 : 402 : int idx = (off & 255) / 32;
79 : : int bit = off % 32;
80 [ + - ]: 402 : if(((thispage->defined[idx] >> bit) & 1) == 0) return def;
81 : 402 : return thispage->chars[off & 255];
82 : : }
|