# Ruby/dl question

**URL:** <https://www.ruby-forum.com/t/ruby-dl-question/65703>\
**Category:** Ruby\
**Created:** [February 11, 2006, 4:30am UTC](https://www.ruby-forum.com/t/ruby-dl-question/65703 "2006-02-11T04:30:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 11, 2006, 4:30am UTC](https://www.ruby-forum.com/t/ruby-dl-question/65703/1 "2006-02-11T04:30:05Z")

</div>

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.

regards.

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 11, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/ruby-dl-question/65703/2 "2006-02-11T19:34:05Z")

</div>

removed\_email\_address@domain.invalid wrote:

> ```
> 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?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 11, 2006, 9:07pm UTC](https://www.ruby-forum.com/t/ruby-dl-question/65703/3 "2006-02-11T21:07:05Z")

</div>

On Sun, 12 Feb 2006, Joel VanderWerf wrote:

> > ```
> > SEEK_SET, SEEK_CUR, SEEK_END */
> > 
> > ```
> 
> 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)

assuming

struct mystruct { int i; float f; };

i recently learned of the offsetof macro which tells you this info.

regards.

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 11, 2006, 9:58pm UTC](https://www.ruby-forum.com/t/ruby-dl-question/65703/4 "2006-02-11T21:58:43Z")

</div>

removed\_email\_address@domain.invalid 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! 😉

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 11, 2006, 10:45pm UTC](https://www.ruby-forum.com/t/ruby-dl-question/65703/5 "2006-02-11T22:45:42Z")

</div>

Joel VanderWerf wrote:

> > 
> 
> doing with it.  
> #define SHORT\_ALIGN ALIGN\_SHORT  
> case ‘H’:  
> Don’t get your shorts out of alignment! 😉  
> And, for what it’s worth, both gcc 3.4.4 and VC++ 13.10 pack the two  
> shorts together  
> into the same word on x86.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 12, 2006, 12:50am UTC](https://www.ruby-forum.com/t/ruby-dl-question/65703/6 "2006-02-12T00:50:48Z")

</div>

On Sun, 12 Feb 2006, Joel VanderWerf wrote:

> Don’t get your shorts out of alignment! 😉

lol!

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 12, 2006, 8:18am UTC](https://www.ruby-forum.com/t/ruby-dl-question/65703/7 "2006-02-12T08:18:33Z")

</div>

On Feb 10, 2006, at 7:30 PM, removed\_email\_address@domain.invalid wrote:

> ```
> 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 H. - removed\_email\_address@domain.invalid - [http://segment7.net](http://segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 12, 2006, 4:20pm UTC](https://www.ruby-forum.com/t/ruby-dl-question/65703/8 "2006-02-12T16:20:38Z")

</div>

On Sun, 12 Feb 2006, Eric H. wrote:

> > ```
> > off_t l_start; /* Starting offset for lock */
> > 
> > ```
> 
> 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.)

i can pack my own struct easily enough… but knowing the layout is  
impossible.

right now i’m considering a ‘configure’ setup to my install that  
requires the  
compiler to determine the layout of the flock struct and generate ruby  
code to  
describe it. that way, if your arch is standard you can use the dl  
binding  
with requiring a compiler and you only need one to determine struct  
layout if  
it’s not.

might be too much work though… an extension is what i’ve already done  
anyhow (posixlock) but i’d just like to hook into the existing IO::fcntl  
call  
with the ability to apply posixlocking.

regards.

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 12, 2006, 2:47pm UTC](https://www.ruby-forum.com/t/ruby-dl-question/65703/9 "2006-02-12T14:47:26Z")

</div>

Eric H. wrote:

> > ```
> > off_t l_start; /* Starting offset for lock */
> > 
> > ```
> 
> 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.)

good pickup.  
I would expect the higher-order word to be 0 in 99% of cases.  
You could treat an 8-byte off\_t field as two separate unsigned longs and  
extract the two longs separately and then add them together  
if the higher-order word is indeed anything but 0.
