hughperman / pure-lang

Automatically exported from code.google.com/p/pure-lang
0 stars 0 forks source link

Add offsetof function for ffi structs #43

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Add offsetof function for structs defined using the ffi interface giving the 
offset of an individual struct member.

Example:

using ffi;
> let my_t = struct_t (sint_t, double_t, sint_t);
> let x = struct my_t (1,2.0,3);
> offsetof (x!0);
0
> offsetof (x!1);
4
> offsetof (x!2);
12
> 

The patch for the proposed change is enclosed.

Original issue reported on code.google.com by jiri.spitz@gmail.com on 21 Jul 2010 at 11:06

Attachments:

GoogleCodeExporter commented 8 years ago
What would you like to use this for?

Original comment by aggraef@gmail.com on 22 Jul 2010 at 8:46

GoogleCodeExporter commented 8 years ago
Several graph and network API routines from the pure-glpk package work with 
with memory blocks used for storing selected data items. The routines require 
input of the  block's total size and of offsets of individual items to be 
stored in the block. The items can be of various data types.

The easiest way to implement this is using structs. The required sizeof 
function is already in the place but ofsetoff is missing. Calculating the 
offsets manually is neither safe nor convenient. 

Original comment by jiri.spitz@gmail.com on 22 Jul 2010 at 9:04

GoogleCodeExporter commented 8 years ago
Maybe it would be more appropriate to define the offsetof function on the 
struct type rather than on the struct itself. 

Original comment by jiri.spitz@gmail.com on 22 Jul 2010 at 9:16

GoogleCodeExporter commented 8 years ago
Ok, I see why this is useful now. :) Please fell free to commit it when you're 
ready and also update the docs accordingly.

Concerning the argument: Why not just make offsetof take either a struct or a 
struct type as argument?

Original comment by aggraef@gmail.com on 22 Jul 2010 at 12:51

GoogleCodeExporter commented 8 years ago
Fixed in rev. 3521. The function accepts both struct types and struct 
instances. Since the function 'sizeof' is complementary to 'offsetof' I changed 
it to accept both argument types as well.

I changed the comments in ffi.pure, the README is currently too brief.

Original comment by jiri.spitz@gmail.com on 22 Jul 2010 at 11:19

GoogleCodeExporter commented 8 years ago
Great, many thanks! A forward declaration was still needed to make pure -w 
happy, I added that in r3522.

Yeah, the README definitely needs some love, but I guess that people will find 
their way reading the comments in ffi.pure.

Original comment by aggraef@gmail.com on 22 Jul 2010 at 11:45