From e1e0e9662d807b0323125ba65b72ba4fc5324f3a Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Date: Tue, 10 Jan 2023 14:50:49 +0100 Subject: [PATCH] Pack the value structure to be hashable --- src/emu/mux.c | 7 ++----- src/emu/value.h | 6 ++++-- test/unit/CMakeLists.txt | 1 + test/unit/value.c | 23 +++++++++++++++++++++++ 4 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 test/unit/value.c diff --git a/src/emu/mux.c b/src/emu/mux.c index c6208ef..15c990c 100644 --- a/src/emu/mux.c +++ b/src/emu/mux.c @@ -157,11 +157,8 @@ struct mux_input * mux_find_input(struct mux *mux, struct value value) { struct mux_input *input = NULL; - /* Only int64 due to garbage */ - if (value.type != VALUE_INT64) - die("bad value type\n"); - HASH_FIND(hh, mux->input, &value.i, sizeof(value.i), input); + HASH_FIND(hh, mux->input, &value, sizeof(value), input); return input; } @@ -185,7 +182,7 @@ mux_add_input(struct mux *mux, struct value key, struct chan *chan) input->key = key; input->chan = chan; - HASH_ADD_KEYPTR(hh, mux->input, &input->key.i, sizeof(input->key.i), input); + HASH_ADD_KEYPTR(hh, mux->input, &input->key, sizeof(input->key), input); if (bay_add_cb(mux->bay, chan, cb_input, mux) != 0) { err("mux_add_input: bay_add_cb failed\n"); diff --git a/src/emu/value.h b/src/emu/value.h index d620968..05b9e6a 100644 --- a/src/emu/value.h +++ b/src/emu/value.h @@ -12,7 +12,9 @@ enum value_type { VALUE_DOUBLE }; -struct value { +/* Packed allows the struct to be hashable, as we don't have any + * unitialized data */ +struct __attribute__((packed)) value { enum value_type type; union { int64_t i; @@ -44,7 +46,7 @@ value_int64(int64_t i) static inline struct value value_null(void) { - struct value v = { .type = VALUE_NULL }; + struct value v = { .type = VALUE_NULL, .i = 0 }; return v; } diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 3bedb95..75aa0a7 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -11,3 +11,4 @@ unit_test(chan.c) unit_test(bay.c) unit_test(mux.c) +unit_test(value.c) diff --git a/test/unit/value.c b/test/unit/value.c new file mode 100644 index 0000000..b2e650a --- /dev/null +++ b/test/unit/value.c @@ -0,0 +1,23 @@ +#include "emu/value.h" +#include "common.h" + +int +main(void) +{ + struct value a, b; + + memset(&a, 66, sizeof(struct value)); + memset(&b, 0, sizeof(struct value)); + + a = value_null(); + + /* Ensure we can use the whole size of the value struct to + * compare two values, so we don't have problems with + * unitialized holes due to alignment */ + if (memcmp(&a, &b, sizeof(struct value)) != 0) + die("values are not the same\n"); + + err("OK\n"); + + return 0; +}