Back to index

python3.2  3.2.2
cls_align_pointer.c
Go to the documentation of this file.
00001 /* Area:      ffi_call, closure_call
00002    Purpose:   Check structure alignment of pointer.
00003    Limitations:      none.
00004    PR:        none.
00005    Originator:       <hos@tamanegi.org> 20031203  */
00006 
00007 /* { dg-do run } */
00008 #include "ffitest.h"
00009 
00010 typedef struct cls_struct_align {
00011   unsigned char a;
00012   void *b;
00013   unsigned char c;
00014 } cls_struct_align;
00015 
00016 cls_struct_align cls_struct_align_fn(struct cls_struct_align a1,
00017                          struct cls_struct_align a2)
00018 {
00019   struct cls_struct_align result;
00020 
00021   result.a = a1.a + a2.a;
00022   result.b = (void *)((uintptr_t)a1.b + (uintptr_t)a2.b);
00023   result.c = a1.c + a2.c;
00024 
00025   printf("%d %" PRIuPTR " %d %d %" PRIuPTR " %d: %d %" PRIuPTR " %d\n", 
00026          a1.a, (uintptr_t)a1.b, a1.c,
00027         a2.a, (uintptr_t)a2.b, a2.c,
00028          result.a, (uintptr_t)result.b,
00029         result.c);
00030 
00031   return result;
00032 }
00033 
00034 static void
00035 cls_struct_align_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
00036                   void* userdata __UNUSED__)
00037 {
00038 
00039   struct cls_struct_align a1, a2;
00040 
00041   a1 = *(struct cls_struct_align*)(args[0]);
00042   a2 = *(struct cls_struct_align*)(args[1]);
00043 
00044   *(cls_struct_align*)resp = cls_struct_align_fn(a1, a2);
00045 }
00046 
00047 int main (void)
00048 {
00049   ffi_cif cif;
00050   void *code;
00051   ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
00052   void* args_dbl[5];
00053   ffi_type* cls_struct_fields[4];
00054   ffi_type cls_struct_type;
00055   ffi_type* dbl_arg_types[5];
00056 
00057   cls_struct_type.size = 0;
00058   cls_struct_type.alignment = 0;
00059   cls_struct_type.type = FFI_TYPE_STRUCT;
00060   cls_struct_type.elements = cls_struct_fields;
00061 
00062   struct cls_struct_align g_dbl = { 12, (void *)4951, 127 };
00063   struct cls_struct_align f_dbl = { 1, (void *)9320, 13 };
00064   struct cls_struct_align res_dbl;
00065 
00066   cls_struct_fields[0] = &ffi_type_uchar;
00067   cls_struct_fields[1] = &ffi_type_pointer;
00068   cls_struct_fields[2] = &ffi_type_uchar;
00069   cls_struct_fields[3] = NULL;
00070 
00071   dbl_arg_types[0] = &cls_struct_type;
00072   dbl_arg_types[1] = &cls_struct_type;
00073   dbl_arg_types[2] = NULL;
00074 
00075   CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
00076                    dbl_arg_types) == FFI_OK);
00077 
00078   args_dbl[0] = &g_dbl;
00079   args_dbl[1] = &f_dbl;
00080   args_dbl[2] = NULL;
00081 
00082   ffi_call(&cif, FFI_FN(cls_struct_align_fn), &res_dbl, args_dbl);
00083   /* { dg-output "12 4951 127 1 9320 13: 13 14271 140" } */
00084   printf("res: %d %" PRIuPTR " %d\n", res_dbl.a, (uintptr_t)res_dbl.b, res_dbl.c);
00085   /* { dg-output "\nres: 13 14271 140" } */
00086 
00087   CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_align_gn, NULL, code) == FFI_OK);
00088 
00089   res_dbl = ((cls_struct_align(*)(cls_struct_align, cls_struct_align))(code))(g_dbl, f_dbl);
00090   /* { dg-output "\n12 4951 127 1 9320 13: 13 14271 140" } */
00091   printf("res: %d %" PRIuPTR " %d\n", res_dbl.a, (uintptr_t)res_dbl.b, res_dbl.c);
00092   /* { dg-output "\nres: 13 14271 140" } */
00093 
00094   exit(0);
00095 }