From c17647e7b68d60db1b15f22bab9cd7979bec1cee Mon Sep 17 00:00:00 2001 From: Alexey Odinokov Date: Tue, 13 Aug 2024 23:53:51 -0500 Subject: [PATCH] removing alignment related functions for now from api --- examples/c_ffi_call/value_ffi.c | 50 ++++++++++++++++------------ examples/c_ffi_call/value_ffi_test.c | 5 --- include/metac/reflect/entry.h | 13 -------- src/entry.c | 26 --------------- 4 files changed, 29 insertions(+), 65 deletions(-) diff --git a/examples/c_ffi_call/value_ffi.c b/examples/c_ffi_call/value_ffi.c index 08f8875..ef8b67e 100644 --- a/examples/c_ffi_call/value_ffi.c +++ b/examples/c_ffi_call/value_ffi.c @@ -8,6 +8,32 @@ #include /*calloc, free*/ #include +/** + * \page ffi_call FFI call example + * + * some thoughts on ffi. + * + * it seems that there are some different ways to set the size of structures. + * the [documentation](https://github.com/libffi/libffi/blob/master/doc/libffi.texi#L509) suggests + * setting both struct size and alignment to zero and to set elements sizes. + * + * this approach worked well until we started working with aligned struct members and aligned + * structures. we're using members which are standard types. and it seems like because of that + * ffi couldn't calculate the proper size of structs/arrays. + * + * we ended up with setting struct/array size. we still set elements, but + * most likely for aligned cases their offset won't be correct. + * right now all tests with aligned structures of data pass ok, even without + * setting the correct offset. + * + * Anyway, [here is](https://github.com/libffi/libffi/blob/8e3ef965c2d0015ed129a06d0f11f30c2120a413/testsuite/libffi.call/va_struct1.c) + * and example of how to check the offset after alignment is set. + * For now we see couple of TODO in the code below, but since everything works ok the code is kept as-is. + * may be in future we can make it ideal. + * +*/ + + // convert function parameters types to ffi_type (metac->ffi) static int _val_to_ffi_type(metac_entry_t * p_entry, ffi_type ** pp_rtype) { @@ -112,7 +138,7 @@ static int _val_to_ffi_type(metac_entry_t * p_entry, ffi_type ** pp_rtype) { free(p_tmp); return -(ENOMEM); } - p_tmp->size = e_sz; + p_tmp->size = e_sz; // it appeared that this is enough to make struct work. TODO: alginment of memebers p_tmp->alignment = 0; p_tmp->type = FFI_TYPE_STRUCT; @@ -159,25 +185,6 @@ static int _val_to_ffi_type(metac_entry_t * p_entry, ffi_type ** pp_rtype) { return -(EFAULT); } - // metac_size_t sz = 0; - // if (metac_entry_byte_size(p_memb_entry, &sz) == 0 && sz != 0) { - // if (sz > p_tmp->elements[memb_id]->size) { - // p_tmp->elements[memb_id]->size = p_tmp->elements[memb_id]->alignment = sz; - // } - // } - - // metac_size_t alignment = 0; - // if (metac_entry_member_alignment(p_memb_entry, &alignment) == 0) { - // p_tmp->elements[memb_id]->alignment = alignment; - // printf(" memb_id %d set member-based alignment %d\n", (int)memb_id, (int) alignment); - // } - // else { - // if (metac_entry_alignment(p_memb_entry, &alignment) == 0) { - // p_tmp->elements[memb_id]->alignment = alignment; - // printf(" memb_id %d set final entry-based alignment %d\n", (int)memb_id, (int) alignment); - // } - // } - last_offset = current_offset; ++memb_id; } @@ -209,7 +216,7 @@ static int _val_to_ffi_type(metac_entry_t * p_entry, ffi_type ** pp_rtype) { free(p_tmp); return -(ENOMEM); } - p_tmp->size = e_sz; + p_tmp->size = e_sz; // it appeared that this is enough to make struct work. TODO: alginment of memebers p_tmp->alignment = 0; p_tmp->type = FFI_TYPE_STRUCT; @@ -407,5 +414,6 @@ int metac_value_call(metac_value_t * p_param_storage_val, void (*fn)(void), meta return -(ENOTSUP); } + _cleanup_ffi_type(rtype); return 0; } \ No newline at end of file diff --git a/examples/c_ffi_call/value_ffi_test.c b/examples/c_ffi_call/value_ffi_test.c index 1252c79..4732f13 100644 --- a/examples/c_ffi_call/value_ffi_test.c +++ b/examples/c_ffi_call/value_ffi_test.c @@ -412,13 +412,8 @@ METAC_START_TEST(test_ffi_aligned_struct_type) { char * expected = NULL; char * expected_called = NULL; - // printf("sz %d vs sz %d\n", (int)sizeof(test_aligned_struct_t), (int)sizeof(test_struct_t)); - // printf("off 0 %p vs off 0 %p\n", &((test_aligned_struct_t*)NULL)->arr[0], &((test_struct_t*)NULL)->arr[0]); - // printf("off 1 %p vs off 1 %p\n", &((test_aligned_struct_t*)NULL)->arr[1], &((test_struct_t*)NULL)->arr[1]); - fail_unless(sizeof(test_aligned_struct_t) != sizeof(test_struct_t), "size must be different"); - test_aligned_struct_t arg_00 = {.a = 1, .arr = {0,}}; test_aligned_struct_t * arg_01 = &arg_00; test_aligned_struct_t ** arg_02 = &arg_01; diff --git a/include/metac/reflect/entry.h b/include/metac/reflect/entry.h index 47b2eec..ae5110a 100644 --- a/include/metac/reflect/entry.h +++ b/include/metac/reflect/entry.h @@ -30,14 +30,6 @@ metac_name_t metac_entry_name(metac_entry_t * p_entry); */ int metac_entry_byte_size(metac_entry_t *p_entry, metac_size_t *p_sz); -/** - * @brief Check if final entry has alignment (typcially structs). to get alignment of member use metac_entry_member_alignment. - * @param p_entry - entry - * @param p_alignment - address of the memory where the calculated value is stored, can be NULL. - * @returns negative - errno in case of error. 0 - if alignment is written. - */ -int metac_entry_alignment(metac_entry_t *p_entry, metac_size_t *p_alignment); - /** * @brief Number of parents of the entry * Entries typically stored in hierarchy. top level is compile unit (CU). @@ -243,11 +235,6 @@ int metac_entry_member_raw_location_info(metac_entry_t *p_entry, struct metac_me */ int metac_entry_member_bitfield_offsets(metac_entry_t *p_memb_entry, metac_offset_t *p_byte_offset, metac_offset_t *p_bit_offset, metac_offset_t *p_bit_size); -/** - * @brief returns 0 if member has alignment and if p_alignment isn't NULL, stores the value there -*/ -int metac_entry_member_alignment(metac_entry_t *p_entry, metac_size_t *p_alignment); - /** @brief struct which is used by metac_entry_by_member_ids to locate member on arbitrary level of structure/union */ typedef struct { metac_num_t i; /**< member id (e.g. useful when member doesn't have a name). is is used only if n in NULL */ diff --git a/src/entry.c b/src/entry.c index 8fb84a5..cd2bb21 100644 --- a/src/entry.c +++ b/src/entry.c @@ -455,19 +455,6 @@ int metac_entry_member_bitfield_offsets(metac_entry_t *p_memb_entry, return 0; } -int metac_entry_member_alignment(metac_entry_t *p_entry, metac_size_t *p_alignment) { - _check_(p_entry == NULL, -(EINVAL)); - _check_(metac_entry_kind(p_entry) != METAC_KND_member, -(EINVAL)); - - if (p_entry->member_info.p_alignment == NULL) { - return -(EFAULT); - } - if (p_alignment != NULL) { - *p_alignment = *p_entry->member_info.p_alignment; - } - return 0; -} - static metac_entry_t * _entry_with_array_info(metac_entry_t * p_entry) { _check_(p_entry == NULL, NULL); @@ -672,16 +659,3 @@ metac_entry_t * metac_entry_parameter_entry(metac_entry_t *p_entry) { _check_(p_entry->func_parameter_info.unspecified_parameters != 0, NULL); return p_entry->func_parameter_info.type; } - -int metac_entry_alignment(metac_entry_t *p_entry, metac_size_t *p_alignment) { - _check_(p_entry == NULL, -(EINVAL)); - _check_(metac_entry_has_members(p_entry) == 0, -(EINVAL)); - - if (p_entry->structure_type_info.p_alignment == NULL) { - return -(EFAULT); - } - if (p_alignment != NULL) { - *p_alignment = *p_entry->structure_type_info.p_alignment; - } - return 0; -} \ No newline at end of file