[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ruby/dl question

Ara.T.Howard

2/11/2006 3:30:00 AM

8 Answers

Joel VanderWerf

2/11/2006 6:34:00 PM

0

ara.t.howard@noaa.gov wrote:
>
> if one has:
>
>
> struct flock {
> ...
> short l_type; /* Type of lock: F_RDLCK,
> F_WRLCK, F_UNLCK */
> short l_whence; /* How to interpret l_start:
> SEEK_SET, SEEK_CUR, SEEK_END */
> off_t l_start; /* Starting offset for lock */
> off_t l_len; /* Number of bytes to lock */
> pid_t l_pid; /* PID of process blocking our lock
> (F_GETLK only) */
> ...
> };
>
> can this structure be desribed using ruby/dl? i'm worried that the struct
> method assumes the fields are in order which, in the case of the flock
> structure, they may not be.

I don't understand. If the flock struct fields are not in a predictable
order, how do you access them in C?

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


Ara.T.Howard

2/11/2006 8:07:00 PM

0

Joel VanderWerf

2/11/2006 8:59:00 PM

0

ara.t.howard@noaa.gov wrote:
> On Sun, 12 Feb 2006, Joel VanderWerf wrote:
...
>> I don't understand. If the flock struct fields are not in a predictable
>> order, how do you access them in C?
>
> that's what to compiler does - it's why one must say
>
> sizeof(mystruct)
>
> vs.
>
> sizeof(int) + sizeof(float)

Ah I see. It isn't that the data structure itself is unpredictable (I
thought maybe you had to read one of the fields to know how the others
are laid out). It's rather that you don't know what the compiler is
doing with it.

Since there are a variety of alignment and packing options in any
compiler, I don't see how DL could guess what the correct ones are.
Maybe it assumes longword alignment?

On further digging... in dl.h the extra space taken to align a short,
for example, is computed by...

typedef struct { char c; short x; } s_short;
#define ALIGN_SHORT (sizeof(s_short) - sizeof(short))
#define SHORT_ALIGN ALIGN_SHORT

...and this is used by the following macro, which advances the offset
var to the next aligned position...

#define DLALIGN(ptr,offset,align) { while( (((unsigned long)((char *)ptr + offset)) % align) != 0 ) offset++;}

...to calculate the struct size in dlsizeof() in dl.c:

case 'H':
DLALIGN(0,size,SHORT_ALIGN);
case 'h':
size += sizeof(short) * n;
break;

So I guess if you use 'H', you get aligned shorts (according to whatever
compiler flags ruby and its extensions were compiled with), and if you
use 'h' you get packed shorts.


Don't get your shorts out of alignment! ;)

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


vanekl

2/11/2006 9:45:00 PM

0

Ara.T.Howard

2/11/2006 11:51:00 PM

0

Eric Hodel

2/12/2006 7:18:00 AM

0

On Feb 10, 2006, at 7:30 PM, ara.t.howard@noaa.gov wrote:

> if one has:
>
> struct flock {
> ...
> short l_type; /* Type of lock: F_RDLCK,
> F_WRLCK, F_UNLCK */
> short l_whence; /* How to interpret l_start:
> SEEK_SET, SEEK_CUR, SEEK_END */
> off_t l_start; /* Starting offset for lock */
> off_t l_len; /* Number of bytes to lock */
> pid_t l_pid; /* PID of process blocking our lock
> (F_GETLK only) */
> ...
> };
>
> can this structure be desribed using ruby/dl? i'm worried that the
> struct
> method assumes the fields are in order which, in the case of the flock
> structure, they may not be.

You have a bigger problem, off_t is usually 8 bytes and the built-in
dl can only handle 4 byte types. (Or, if it can handle 8 byte types,
I couldn't figure out how.)

--
Eric Hodel - drbrain@segment7.net - http://se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...




vanekl

2/12/2006 1:47:00 PM

0

Ara.T.Howard

2/12/2006 3:20:00 PM

0