Pack the value structure to be hashable

This commit is contained in:
Rodrigo Arias 2023-01-10 14:50:49 +01:00 committed by Rodrigo Arias Mallo
parent 1cea193ea3
commit e1e0e9662d
4 changed files with 30 additions and 7 deletions

View File

@ -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");

View File

@ -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;
}

View File

@ -11,3 +11,4 @@
unit_test(chan.c)
unit_test(bay.c)
unit_test(mux.c)
unit_test(value.c)

23
test/unit/value.c Normal file
View File

@ -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;
}