Implement specialized arrays for fixed-size integers
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 12 Oct 2022 12:59:50 +0000 (13:59 +0100)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 12 Oct 2022 12:59:50 +0000 (13:59 +0100)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
include/side/trace.h
src/test.c
src/tracer.c

index b8399790102420e414d23e1c4dda636fe706c193..5fb703e42df8cfc6f567b376e060d3f191501e48 100644 (file)
@@ -27,14 +27,25 @@ enum side_type {
        SIDE_TYPE_S16,
        SIDE_TYPE_S32,
        SIDE_TYPE_S64,
+
        SIDE_TYPE_STRING,
        SIDE_TYPE_DYNAMIC,
        SIDE_TYPE_STRUCT,
        SIDE_TYPE_ARRAY,
        SIDE_TYPE_VLA,
        SIDE_TYPE_VLA_VISITOR,
+
+       SIDE_TYPE_ARRAY_U8,
+       SIDE_TYPE_ARRAY_U16,
+       SIDE_TYPE_ARRAY_U32,
+       SIDE_TYPE_ARRAY_U64,
+       SIDE_TYPE_ARRAY_S8,
+       SIDE_TYPE_ARRAY_S16,
+       SIDE_TYPE_ARRAY_S32,
+       SIDE_TYPE_ARRAY_S64,
+
        //TODO:
-       //specialized array and vla for fixed-size integers (optimization)
+       //specialized vla for fixed-size integers (optimization)
        //variants (discriminated unions)
 };
 
@@ -108,11 +119,14 @@ struct side_arg_vec {
                int16_t side_s16;
                int32_t side_s32;
                int64_t side_s64;
+
                const char *string;
                const struct side_arg_vec_description *side_struct;
                const struct side_arg_vec_description *side_array;
                const struct side_arg_vec_description *side_vla;
                void *side_vla_visitor_ctx;
+
+               void *side_array_fixint;
        } u;
 };
 
@@ -209,6 +223,15 @@ struct side_arg_vec_description {
 #define side_arg_vla(_side_type)       { .type = SIDE_TYPE_VLA, .u = { .side_vla = (_side_type) } }
 #define side_arg_vla_visitor(_ctx)     { .type = SIDE_TYPE_VLA_VISITOR, .u = { .side_vla_visitor_ctx = (_ctx) } }
 
+#define side_arg_array_u8(ptr) { .type = SIDE_TYPE_ARRAY_U8, .u = { .side_array_fixint = (ptr) } }
+#define side_arg_array_u16(ptr)        { .type = SIDE_TYPE_ARRAY_U16, .u = { .side_array_fixint = (ptr) } }
+#define side_arg_array_u32(ptr)        { .type = SIDE_TYPE_ARRAY_U32, .u = { .side_array_fixint = (ptr) } }
+#define side_arg_array_u64(ptr)        { .type = SIDE_TYPE_ARRAY_U64, .u = { .side_array_fixint = (ptr) } }
+#define side_arg_array_s8(ptr) { .type = SIDE_TYPE_ARRAY_S8, .u = { .side_array_fixint = (ptr) } }
+#define side_arg_array_s16(ptr)        { .type = SIDE_TYPE_ARRAY_S16, .u = { .side_array_fixint  = (ptr) } }
+#define side_arg_array_s32(ptr)        { .type = SIDE_TYPE_ARRAY_S32, .u = { .side_array_fixint = (ptr) } }
+#define side_arg_array_s64(ptr)        { .type = SIDE_TYPE_ARRAY_S64, .u = { .side_array_fixint = (ptr) } }
+
 #define side_arg_define_vec(_identifier, _sav) \
        const struct side_arg_vec _identifier##_vec[] = { _sav }; \
        const struct side_arg_vec_description _identifier = { \
index 130b93696596d3d2d3fd28ebb1bdad942ed06904..b92fd17b1435cce388c9ecca79cdd60c18abd705 100644 (file)
@@ -142,6 +142,25 @@ void test_vla_visitor(void)
        }
 }
 
+static int64_t array_fixint[] = { -444, 555, 123, 2897432587 };
+
+static side_define_event(my_provider_event_array_fixint, "myprovider", "myarrayfixint", SIDE_LOGLEVEL_DEBUG,
+       side_field_list(
+               side_field_array("arrfixint", side_elem(SIDE_TYPE_S64), SIDE_ARRAY_SIZE(array_fixint)),
+               side_field(SIDE_TYPE_S64, "v"),
+       )
+);
+
+static
+void test_array_fixint(void)
+{
+       my_provider_event_array_fixint.enabled = 1;
+       side_event_cond(&my_provider_event_array_fixint) {
+               side_event_call(&my_provider_event_array_fixint,
+                       side_arg_list(side_arg_array_s64(array_fixint), side_arg_s64(42)));
+       }
+}
+
 int main()
 {
        test_fields();
@@ -149,5 +168,6 @@ int main()
        test_array();
        test_vla();
        test_vla_visitor();
+       test_array_fixint();
        return 0;
 }
index 0ef424504fbe3fe1a8d7cac8669412231eb86005..eea7df6da2cf2ff03cf03917ed767e156fdc01aa 100644 (file)
@@ -18,15 +18,33 @@ static
 void tracer_print_vla(const struct side_type_description *type_desc, const struct side_arg_vec_description *sav_desc);
 static
 void tracer_print_vla_visitor(const struct side_type_description *type_desc, void *ctx);
+static
+void tracer_print_array_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item);
 
 static
 void tracer_print_type(const struct side_type_description *type_desc, const struct side_arg_vec *item)
 {
-       if (type_desc->type != SIDE_TYPE_DYNAMIC && type_desc->type != item->type) {
-               printf("ERROR: type mismatch between description and arguments\n");
-               abort();
+       switch (item->type) {
+       case SIDE_TYPE_ARRAY_U8:
+       case SIDE_TYPE_ARRAY_U16:
+       case SIDE_TYPE_ARRAY_U32:
+       case SIDE_TYPE_ARRAY_U64:
+       case SIDE_TYPE_ARRAY_S8:
+       case SIDE_TYPE_ARRAY_S16:
+       case SIDE_TYPE_ARRAY_S32:
+       case SIDE_TYPE_ARRAY_S64:
+               if (type_desc->type != SIDE_TYPE_ARRAY) {
+                       printf("ERROR: type mismatch between description and arguments\n");
+                       abort();
+               }
+               break;
+       default:
+               if (type_desc->type != SIDE_TYPE_DYNAMIC && type_desc->type != item->type) {
+                       printf("ERROR: type mismatch between description and arguments\n");
+                       abort();
+               }
+               break;
        }
-
        switch (item->type) {
        case SIDE_TYPE_U8:
                printf("%" PRIu8, item->u.side_u8);
@@ -67,6 +85,16 @@ void tracer_print_type(const struct side_type_description *type_desc, const stru
        case SIDE_TYPE_VLA_VISITOR:
                tracer_print_vla_visitor(type_desc, item->u.side_vla_visitor_ctx);
                break;
+       case SIDE_TYPE_ARRAY_U8:
+       case SIDE_TYPE_ARRAY_U16:
+       case SIDE_TYPE_ARRAY_U32:
+       case SIDE_TYPE_ARRAY_U64:
+       case SIDE_TYPE_ARRAY_S8:
+       case SIDE_TYPE_ARRAY_S16:
+       case SIDE_TYPE_ARRAY_S32:
+       case SIDE_TYPE_ARRAY_S64:
+               tracer_print_array_fixint(type_desc, item);
+               break;
        default:
                printf("<UNKNOWN TYPE>");
                abort();
@@ -174,6 +202,127 @@ void tracer_print_vla_visitor(const struct side_type_description *type_desc, voi
        }
 }
 
+void tracer_print_array_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item)
+{
+       const struct side_type_description *elem_type = type_desc->u.side_array.elem_type;
+       uint32_t side_sav_len = type_desc->u.side_array.length;
+       void *p = item->u.side_array_fixint;
+       enum side_type side_type;
+       int i;
+
+       if (elem_type->type != SIDE_TYPE_DYNAMIC) {
+               switch (item->type) {
+               case SIDE_TYPE_ARRAY_U8:
+                       if (elem_type->type != SIDE_TYPE_U8)
+                               goto type_error;
+                       break;
+               case SIDE_TYPE_ARRAY_U16:
+                       if (elem_type->type != SIDE_TYPE_U16)
+                               goto type_error;
+                       break;
+               case SIDE_TYPE_ARRAY_U32:
+                       if (elem_type->type != SIDE_TYPE_U32)
+                               goto type_error;
+                       break;
+               case SIDE_TYPE_ARRAY_U64:
+                       if (elem_type->type != SIDE_TYPE_U64)
+                               goto type_error;
+                       break;
+               case SIDE_TYPE_ARRAY_S8:
+                       if (elem_type->type != SIDE_TYPE_S8)
+                               goto type_error;
+                       break;
+               case SIDE_TYPE_ARRAY_S16:
+                       if (elem_type->type != SIDE_TYPE_S16)
+                               goto type_error;
+                       break;
+               case SIDE_TYPE_ARRAY_S32:
+                       if (elem_type->type != SIDE_TYPE_S32)
+                               goto type_error;
+                       break;
+               case SIDE_TYPE_ARRAY_S64:
+                       if (elem_type->type != SIDE_TYPE_S64)
+                               goto type_error;
+                       break;
+               }
+               side_type = elem_type->type;
+       } else {
+               switch (item->type) {
+               case SIDE_TYPE_ARRAY_U8:
+                       side_type = SIDE_TYPE_U8;
+                       break;
+               case SIDE_TYPE_ARRAY_U16:
+                       side_type = SIDE_TYPE_U16;
+                       break;
+               case SIDE_TYPE_ARRAY_U32:
+                       side_type = SIDE_TYPE_U32;
+                       break;
+               case SIDE_TYPE_ARRAY_U64:
+                       side_type = SIDE_TYPE_U64;
+                       break;
+               case SIDE_TYPE_ARRAY_S8:
+                       side_type = SIDE_TYPE_S8;
+                       break;
+               case SIDE_TYPE_ARRAY_S16:
+                       side_type = SIDE_TYPE_S16;
+                       break;
+               case SIDE_TYPE_ARRAY_S32:
+                       side_type = SIDE_TYPE_S32;
+                       break;
+               case SIDE_TYPE_ARRAY_S64:
+                       side_type = SIDE_TYPE_S64;
+                       break;
+               }
+       }
+
+       printf("[ ");
+       for (i = 0; i < side_sav_len; i++) {
+               struct side_arg_vec sav_elem = {
+                       .type = side_type,
+               };
+
+               switch (side_type) {
+               case SIDE_TYPE_U8:
+                       sav_elem.u.side_u8 = ((const uint8_t *) p)[i];
+                       break;
+               case SIDE_TYPE_S8:
+                       sav_elem.u.side_s8 = ((const int8_t *) p)[i];
+                       break;
+               case SIDE_TYPE_U16:
+                       sav_elem.u.side_u16 = ((const uint16_t *) p)[i];
+                       break;
+               case SIDE_TYPE_S16:
+                       sav_elem.u.side_s16 = ((const int16_t *) p)[i];
+                       break;
+               case SIDE_TYPE_U32:
+                       sav_elem.u.side_u32 = ((const uint32_t *) p)[i];
+                       break;
+               case SIDE_TYPE_S32:
+                       sav_elem.u.side_s32 = ((const int32_t *) p)[i];
+                       break;
+               case SIDE_TYPE_U64:
+                       sav_elem.u.side_u64 = ((const uint64_t *) p)[i];
+                       break;
+               case SIDE_TYPE_S64:
+                       sav_elem.u.side_s64 = ((const int64_t *) p)[i];
+                       break;
+
+               default:
+                       printf("ERROR: Unexpected type\n");
+                       abort();
+               }
+
+               printf("%s", i ? ", " : "");
+               tracer_print_type(elem_type, &sav_elem);
+       }
+       printf(" ]");
+       return;
+
+type_error:
+       printf("ERROR: type mismatch\n");
+       abort();
+}
+
 void tracer_call(const struct side_event_description *desc, const struct side_arg_vec_description *sav_desc)
 {
        const struct side_arg_vec *sav = sav_desc->sav;
This page took 0.026697 seconds and 4 git commands to generate.