Skip to content

Commit

Permalink
removing alignment related functions for now from api
Browse files Browse the repository at this point in the history
  • Loading branch information
aodinokov committed Aug 15, 2024
1 parent 4f48aa3 commit c17647e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 65 deletions.
50 changes: 29 additions & 21 deletions examples/c_ffi_call/value_ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@
#include <stdlib.h> /*calloc, free*/
#include <string.h>

/**
* \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) {

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
5 changes: 0 additions & 5 deletions examples/c_ffi_call/value_ffi_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 0 additions & 13 deletions include/metac/reflect/entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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 */
Expand Down
26 changes: 0 additions & 26 deletions src/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}

0 comments on commit c17647e

Please sign in to comment.