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