Implement fixed-integer vla
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 12 Oct 2022 13:25:59 +0000 (14:25 +0100)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 12 Oct 2022 13:25:59 +0000 (14:25 +0100)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
include/side/trace.h
src/Makefile
src/test.c
src/tracer.c

index 5fb703e42df8cfc6f567b376e060d3f191501e48..0a0764d96e6568064e8dca844a0934831e044fa2 100644 (file)
@@ -44,8 +44,16 @@ enum side_type {
        SIDE_TYPE_ARRAY_S32,
        SIDE_TYPE_ARRAY_S64,
 
+       SIDE_TYPE_VLA_U8,
+       SIDE_TYPE_VLA_U16,
+       SIDE_TYPE_VLA_U32,
+       SIDE_TYPE_VLA_U64,
+       SIDE_TYPE_VLA_S8,
+       SIDE_TYPE_VLA_S16,
+       SIDE_TYPE_VLA_S32,
+       SIDE_TYPE_VLA_S64,
+
        //TODO:
-       //specialized vla for fixed-size integers (optimization)
        //variants (discriminated unions)
 };
 
@@ -127,6 +135,10 @@ struct side_arg_vec {
                void *side_vla_visitor_ctx;
 
                void *side_array_fixint;
+               struct {
+                       void *p;
+                       uint32_t length;
+               } side_vla_fixint;
        } u;
 };
 
@@ -223,14 +235,23 @@ 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_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_vla_u8(_ptr, _length) { .type = SIDE_TYPE_VLA_U8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } }
+#define side_arg_vla_u16(_ptr, _length)        { .type = SIDE_TYPE_VLA_U16, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
+#define side_arg_vla_u32(_ptr, _length)        { .type = SIDE_TYPE_VLA_U32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
+#define side_arg_vla_u64(_ptr, _length)        { .type = SIDE_TYPE_VLA_U64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
+#define side_arg_vla_s8(_ptr, _length) { .type = SIDE_TYPE_VLA_S8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
+#define side_arg_vla_s16(_ptr, _length)        { .type = SIDE_TYPE_VLA_S16, .u = { .side_vla_fixint  = { .p = (_ptr), .length = (_length) } } }
+#define side_arg_vla_s32(_ptr, _length)        { .type = SIDE_TYPE_VLA_S32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
+#define side_arg_vla_s64(_ptr, _length)        { .type = SIDE_TYPE_VLA_S64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
 
 #define side_arg_define_vec(_identifier, _sav) \
        const struct side_arg_vec _identifier##_vec[] = { _sav }; \
index 24c93d26561d96e3e503349058dae184ec9c9c5a..5056c136a04a45b3f0e9074c7942a5da1b8de547 100644 (file)
@@ -12,4 +12,4 @@ test: tracer.o test.o
 .PHONY: clean
 
 clean:
-       rm -f test
+       rm -f test *.o
index b92fd17b1435cce388c9ecca79cdd60c18abd705..f4f95a705dbf636e0fc7c8ac1864bc34bf079ad2 100644 (file)
@@ -155,10 +155,25 @@ 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)));
-       }
+       side_event(&my_provider_event_array_fixint,
+               side_arg_list(side_arg_array_s64(array_fixint), side_arg_s64(42)));
+}
+
+static int64_t vla_fixint[] = { -444, 555, 123, 2897432587 };
+
+static side_define_event(my_provider_event_vla_fixint, "myprovider", "myvlafixint", SIDE_LOGLEVEL_DEBUG,
+       side_field_list(
+               side_field_vla("vlafixint", side_elem(SIDE_TYPE_S64)),
+               side_field(SIDE_TYPE_S64, "v"),
+       )
+);
+
+static
+void test_vla_fixint(void)
+{
+       my_provider_event_vla_fixint.enabled = 1;
+       side_event(&my_provider_event_vla_fixint,
+               side_arg_list(side_arg_vla_s64(vla_fixint, SIDE_ARRAY_SIZE(vla_fixint)), side_arg_s64(42)));
 }
 
 int main()
@@ -169,5 +184,6 @@ int main()
        test_vla();
        test_vla_visitor();
        test_array_fixint();
+       test_vla_fixint();
        return 0;
 }
index eea7df6da2cf2ff03cf03917ed767e156fdc01aa..2637f4aa8ff0dd9412fd67cb56db1f740bd1611a 100644 (file)
@@ -20,6 +20,8 @@ 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_vla_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)
@@ -38,6 +40,20 @@ void tracer_print_type(const struct side_type_description *type_desc, const stru
                        abort();
                }
                break;
+       case SIDE_TYPE_VLA_U8:
+       case SIDE_TYPE_VLA_U16:
+       case SIDE_TYPE_VLA_U32:
+       case SIDE_TYPE_VLA_U64:
+       case SIDE_TYPE_VLA_S8:
+       case SIDE_TYPE_VLA_S16:
+       case SIDE_TYPE_VLA_S32:
+       case SIDE_TYPE_VLA_S64:
+               if (type_desc->type != SIDE_TYPE_VLA) {
+                       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");
@@ -95,6 +111,16 @@ void tracer_print_type(const struct side_type_description *type_desc, const stru
        case SIDE_TYPE_ARRAY_S64:
                tracer_print_array_fixint(type_desc, item);
                break;
+       case SIDE_TYPE_VLA_U8:
+       case SIDE_TYPE_VLA_U16:
+       case SIDE_TYPE_VLA_U32:
+       case SIDE_TYPE_VLA_U64:
+       case SIDE_TYPE_VLA_S8:
+       case SIDE_TYPE_VLA_S16:
+       case SIDE_TYPE_VLA_S32:
+       case SIDE_TYPE_VLA_S64:
+               tracer_print_vla_fixint(type_desc, item);
+               break;
        default:
                printf("<UNKNOWN TYPE>");
                abort();
@@ -323,6 +349,131 @@ type_error:
        abort();
 }
 
+void tracer_print_vla_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_vla.elem_type;
+       uint32_t side_sav_len = item->u.side_vla_fixint.length;
+       void *p = item->u.side_vla_fixint.p;
+       enum side_type side_type;
+       int i;
+
+       if (elem_type->type != SIDE_TYPE_DYNAMIC) {
+               switch (item->type) {
+               case SIDE_TYPE_VLA_U8:
+                       if (elem_type->type != SIDE_TYPE_U8)
+                               goto type_error;
+                       break;
+               case SIDE_TYPE_VLA_U16:
+                       if (elem_type->type != SIDE_TYPE_U16)
+                               goto type_error;
+                       break;
+               case SIDE_TYPE_VLA_U32:
+                       if (elem_type->type != SIDE_TYPE_U32)
+                               goto type_error;
+                       break;
+               case SIDE_TYPE_VLA_U64:
+                       if (elem_type->type != SIDE_TYPE_U64)
+                               goto type_error;
+                       break;
+               case SIDE_TYPE_VLA_S8:
+                       if (elem_type->type != SIDE_TYPE_S8)
+                               goto type_error;
+                       break;
+               case SIDE_TYPE_VLA_S16:
+                       if (elem_type->type != SIDE_TYPE_S16)
+                               goto type_error;
+                       break;
+               case SIDE_TYPE_VLA_S32:
+                       if (elem_type->type != SIDE_TYPE_S32)
+                               goto type_error;
+                       break;
+               case SIDE_TYPE_VLA_S64:
+                       if (elem_type->type != SIDE_TYPE_S64)
+                               goto type_error;
+                       break;
+               default:
+                       goto type_error;
+               }
+               side_type = elem_type->type;
+       } else {
+               switch (item->type) {
+               case SIDE_TYPE_VLA_U8:
+                       side_type = SIDE_TYPE_U8;
+                       break;
+               case SIDE_TYPE_VLA_U16:
+                       side_type = SIDE_TYPE_U16;
+                       break;
+               case SIDE_TYPE_VLA_U32:
+                       side_type = SIDE_TYPE_U32;
+                       break;
+               case SIDE_TYPE_VLA_U64:
+                       side_type = SIDE_TYPE_U64;
+                       break;
+               case SIDE_TYPE_VLA_S8:
+                       side_type = SIDE_TYPE_S8;
+                       break;
+               case SIDE_TYPE_VLA_S16:
+                       side_type = SIDE_TYPE_S16;
+                       break;
+               case SIDE_TYPE_VLA_S32:
+                       side_type = SIDE_TYPE_S32;
+                       break;
+               case SIDE_TYPE_VLA_S64:
+                       side_type = SIDE_TYPE_S64;
+                       break;
+               default:
+                       goto type_error;
+               }
+       }
+
+       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.029947 seconds and 4 git commands to generate.