aboutsummaryrefslogtreecommitdiff
path: root/src/solvers/tables.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/solvers/tables.h')
-rw-r--r--src/solvers/tables.h72
1 files changed, 50 insertions, 22 deletions
diff --git a/src/solvers/tables.h b/src/solvers/tables.h
index 9da90df..d73fa31 100644
--- a/src/solvers/tables.h
+++ b/src/solvers/tables.h
@@ -1,13 +1,33 @@
1/* Type definitions and macros are in a separate file for easier testing */ 1/* Type definitions and macros are in a separate file for easier testing */
2#include "tables_types_macros.h" 2#include "tables_types_macros.h"
3 3
4STATIC uint64_t read_unaligned_u64(const void *);
5STATIC void write_unaligned_u64(void *, uint64_t);
4STATIC bool readtableinfo(const void *, tableinfo_t *); 6STATIC bool readtableinfo(const void *, tableinfo_t *);
5STATIC bool readtableinfo_n(const void *, uint8_t, tableinfo_t *); 7STATIC bool readtableinfo_n(const void *, uint8_t, tableinfo_t *);
6STATIC bool writetableinfo(const tableinfo_t *, void *); 8STATIC bool writetableinfo(const tableinfo_t *, void *);
7 9
10STATIC uint64_t
11read_unaligned_u64(const void *buf)
12{
13 uint64_t ret;
14
15 memcpy(&ret, buf, sizeof(uint64_t));
16
17 return ret;
18}
19
20STATIC void
21write_unaligned_u64(void *buf, uint64_t x)
22{
23 memcpy(buf, &x, sizeof(uint64_t));
24}
25
8STATIC bool 26STATIC bool
9readtableinfo(const void *buf, tableinfo_t *info) 27readtableinfo(const void *buf, tableinfo_t *info)
10{ 28{
29 size_t i;
30
11 if (buf == NULL) { 31 if (buf == NULL) {
12 LOG("Error reading table: buffer is NULL\n"); 32 LOG("Error reading table: buffer is NULL\n");
13 return false; 33 return false;
@@ -18,16 +38,17 @@ readtableinfo(const void *buf, tableinfo_t *info)
18 return false; 38 return false;
19 } 39 }
20 40
21 memcpy(info->distribution, OFFSET(buf, INFO_OFFSET_DISTRIBUTION), 41 for (i = 0; i < INFO_DISTRIBUTION_LEN; i++)
22 INFO_DISTRIBUTION_LEN * sizeof(uint64_t)); 42 info->distribution[i] = read_unaligned_u64(OFFSET(buf,
43 INFO_OFFSET_DISTRIBUTION + i * sizeof(uint64_t)));
23 44
24 info->type = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_TYPE); 45 info->type = read_unaligned_u64(OFFSET(buf, INFO_OFFSET_TYPE));
25 info->infosize = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_INFOSIZE); 46 info->infosize = read_unaligned_u64(OFFSET(buf, INFO_OFFSET_INFOSIZE));
26 info->fullsize = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_FULLSIZE); 47 info->fullsize = read_unaligned_u64(OFFSET(buf, INFO_OFFSET_FULLSIZE));
27 info->hash = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_HASH); 48 info->hash = read_unaligned_u64(OFFSET(buf, INFO_OFFSET_HASH));
28 info->entries = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_ENTRIES); 49 info->entries = read_unaligned_u64(OFFSET(buf, INFO_OFFSET_ENTRIES));
29 info->classes = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_CLASSES); 50 info->classes = read_unaligned_u64(OFFSET(buf, INFO_OFFSET_CLASSES));
30 info->next = *(const uint64_t* )OFFSET(buf, INFO_OFFSET_NEXT); 51 info->next = read_unaligned_u64(OFFSET(buf, INFO_OFFSET_NEXT));
31 52
32 memcpy(info->solver, OFFSET(buf, INFO_OFFSET_SOLVER), 53 memcpy(info->solver, OFFSET(buf, INFO_OFFSET_SOLVER),
33 INFO_SOLVER_STRLEN); 54 INFO_SOLVER_STRLEN);
@@ -53,7 +74,9 @@ readtableinfo_n(const void *buf, uint8_t n, tableinfo_t *info)
53STATIC bool 74STATIC bool
54writetableinfo(const tableinfo_t *info, void *buf) 75writetableinfo(const tableinfo_t *info, void *buf)
55{ 76{
56 int i; 77 size_t i;
78 bool end;
79 uint8_t *c;
57 80
58 if (buf == NULL) { 81 if (buf == NULL) {
59 LOG("Error writing table: buffer is NULL\n"); 82 LOG("Error writing table: buffer is NULL\n");
@@ -65,24 +88,29 @@ writetableinfo(const tableinfo_t *info, void *buf)
65 return false; 88 return false;
66 } 89 }
67 90
68 memcpy(OFFSET(buf, INFO_OFFSET_DISTRIBUTION), info->distribution, 91 for (i = 0; i < INFO_DISTRIBUTION_LEN; i++)
69 INFO_DISTRIBUTION_LEN * sizeof(uint64_t)); 92 write_unaligned_u64(OFFSET(buf, INFO_OFFSET_DISTRIBUTION +
93 i * sizeof(uint64_t)), info->distribution[i]);
70 94
71 *(uint64_t *)OFFSET(buf, INFO_OFFSET_TYPE) = info->type; 95 write_unaligned_u64(OFFSET(buf, INFO_OFFSET_TYPE), info->type);
72 *(uint64_t *)OFFSET(buf, INFO_OFFSET_INFOSIZE) = info->infosize; 96 write_unaligned_u64(OFFSET(buf, INFO_OFFSET_INFOSIZE), info->infosize);
73 *(uint64_t *)OFFSET(buf, INFO_OFFSET_FULLSIZE) = info->fullsize; 97 write_unaligned_u64(OFFSET(buf, INFO_OFFSET_FULLSIZE), info->fullsize);
74 *(uint64_t *)OFFSET(buf, INFO_OFFSET_HASH) = info->hash; 98 write_unaligned_u64(OFFSET(buf, INFO_OFFSET_HASH), info->hash);
75 *(uint64_t *)OFFSET(buf, INFO_OFFSET_ENTRIES) = info->entries; 99 write_unaligned_u64(OFFSET(buf, INFO_OFFSET_ENTRIES), info->entries);
76 *(uint64_t *)OFFSET(buf, INFO_OFFSET_CLASSES) = info->classes; 100 write_unaligned_u64(OFFSET(buf, INFO_OFFSET_CLASSES), info->classes);
77 *(uint64_t *)OFFSET(buf, INFO_OFFSET_NEXT) = info->next; 101 write_unaligned_u64(OFFSET(buf, INFO_OFFSET_NEXT), info->next);
78 102
79 memcpy(OFFSET(buf, INFO_OFFSET_SOLVER), info->solver, 103 memcpy(OFFSET(buf, INFO_OFFSET_SOLVER), info->solver,
80 INFO_SOLVER_STRLEN); 104 INFO_SOLVER_STRLEN);
81 105
82 /* Zeroing all chars after the end of the string, for consistency */ 106 /* Zeroing all chars after the end of the string, for consistency */
83 for (i = 1; i < INFO_SOLVER_STRLEN; i++) 107 end = false;
84 if (*OFFSET(buf, i) == 0) 108 for (i = 0; i < INFO_SOLVER_STRLEN; i++) {
85 *OFFSET(buf, i) = 0; 109 c = OFFSET(buf, INFO_OFFSET_SOLVER + i);
110 end = end || *c == 0;
111 if (end)
112 *c = 0;
113 }
86 114
87 *OFFSET(buf, INFO_OFFSET_H48H) = info->h48h; 115 *OFFSET(buf, INFO_OFFSET_H48H) = info->h48h;
88 *OFFSET(buf, INFO_OFFSET_BITS) = info->bits; 116 *OFFSET(buf, INFO_OFFSET_BITS) = info->bits;

Generated with cgit - Back to sebastiano.tronto.net