Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-08

Converting C's bit field struct

12 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

leledumbo

Since no one answers on message board, I'll post it here.

C struct:

typedef unsigned int u32int;
typedef struct page
{
   u32int present    : 1;   // Page present in memory
   u32int rw         : 1;   // Read-only if clear, readwrite if set
   u32int user       : 1;   // Supervisor level only if clear
   u32int accessed   : 1;   // Has the page been accessed since last
refresh?
   u32int dirty      : 1;   // Has the page been written to since last
refresh?
   u32int unused     : 7;   // Amalgamation of unused and reserved bits
   u32int frame      : 20;  // Frame address (shifted right 12 bits)
} page_t;

Pascal record please...

View this message in context: http://www.nabble.com/Converting-C%27s-bit-field-struct-tp18959002p18959002.html Sent from the Free Pascal - General mailing list .

Reply by email

Vincent Snijders
to leledumbo
leledumbo wrote
Found on the net that Pascal doesn't provide it :-(( What if FPC creates it as an extension =)?

FPC supports bitpacked records. I cannot find it in the docs right now.

Vincent

Reply by email

Vinzent Höfler
to leledumbo: Since no one answers on message board, I'll post it here. C struct: typedef unsigned int…
leledumbo wrote 18 lines
Since no one answers on message board, I'll post it here. C struct: typedef unsigned int u32int; typedef struct page { u32int present : 1; // Page present in memory u32int rw : 1; // Read-only if clear, readwrite if set u32int user : 1; // Supervisor level only if clear u32int accessed : 1; // Has the page been accessed since last refresh? u32int dirty : 1; // Has the page been written to since last refresh? u32int unused : 7; // Amalgamation of unused and reserved bits u32int frame : 20; // Frame address (shifted right 12 bits) } page_t;

Hmm. That's nasty. ;)

Pascal record please...

Should be something like that:

type
    Unsigned_7  = 0 .. (1 shl 7)  - 1;
    Unsigned_20 = 0 .. (1 shl 20) - 1;
type
    page_t = bitpacked record
       present  : boolean;
       rw       : boolean;
       user     : boolean;
       accessed : boolean;
       dirty    : boolean;
       unused   : Unsigned_7;
       frame    : Unsigned_20;
    end;

I guess, portability is of no concern with such a structure. ;)

Vinzent.

Reply by email

leledumbo
to Vinzent Höfler
Vincent Snijders wrote
FPC supports bitpacked records. I cannot find it in the docs right now.

Oh... didn't know if such a thing exists.

Vinzent Höfler wrote 16 lines
type Unsigned_7 = 0 .. (1 shl 7) - 1; Unsigned_20 = 0 .. (1 shl 20) - 1; type page_t = bitpacked record present : boolean; rw : boolean; user : boolean; accessed : boolean; dirty : boolean; unused : Unsigned_7; frame : Unsigned_20; end;

Weird... never seen anything like this before :confused:, but I'll try. Thanks.

View this message in context: http://www.nabble.com/Converting-C%27s-bit-field-struct-tp18959002p18961028.html Sent from the Free Pascal - General mailing list .

Reply by email

tom_at_work
to Vincent Snijders

Hello,

Vincent Snijders wrote 13 lines
Michael Van Canneyt schreef: > On Wed, 13 Aug 2008, leledumbo wrote: >> Found on the net that Pascal doesn't provide it :-(( >> What if FPC creates it as an extension =)? > FPC has it, but only in MacPas mode. > I copied the example program from: http://qc.codegear.com/wc/ qcmain.aspx?d=56480 Added {$mode delphi} or {$mode objfpc} and the program compiled without errors.

Bitpacked records have been implemented and/or greatly improved for all platforms some time ago by Jonas; there were some problems with the "old" implementation iirc. Bitpacked records are also supported in other modes than macpas mode, however I do not know whether it has been intentional to enable them in Delphi mode.

Regards,
Thomas

Reply by email

Jonas Maebe
to Vinzent Höfler: Hmm. That's nasty. ;) Should be something like that: type Unsigned_7 = 0 .. (1 shl 7) -…
Vinzent Höfler wrote 29 lines
leledumbo wrote: > Since no one answers on message board, I'll post it here. > C struct: > typedef unsigned int u32int; > typedef struct page > { > u32int present : 1; // Page present in memory > u32int rw : 1; // Read-only if clear, readwrite if set > u32int user : 1; // Supervisor level only if clear > u32int accessed : 1; // Has the page been accessed since last > refresh? > u32int dirty : 1; // Has the page been written to since last > refresh? > u32int unused : 7; // Amalgamation of unused and reserved bits > u32int frame : 20; // Frame address (shifted right 12 bits) > } page_t; Hmm. That's nasty. ;)w > Pascal record please... Should be something like that: type Unsigned_7 = 0 .. (1 shl 7) - 1; Unsigned_20 = 0 .. (1 shl 20) - 1; type page_t = bitpacked record

Yes and no: the layout of bitpacked records is by no means guaranteed to be the same as that of a C record, regardless of the definition of the C record. The internal format of bitpacked records is opaque and must not be used in any case where data has to be interchanged or where a particular layout is desired.

Jonas

Reply by email

Vinzent Höfler
to Jonas Maebe

-------- Original-Nachricht --------

Show quoted text 19 lines
Datum: Wed, 13 Aug 2008 22:03:23 +0100 Von: Jonas Maebe An: FPC-Pascal users discussions Betreff: Re: [fpc-pascal] Converting C\'s bit field struct On 13 Aug 2008, at 11:59, Vinzent Höfler wrote: > Should be something like that: > > type > Unsigned_7 = 0 .. (1 shl 7) - 1; > Unsigned_20 = 0 .. (1 shl 20) - 1; > > type > page_t = bitpacked record Yes and no: the layout of bitpacked records is by no means guaranteed to be the same as that of a C record, regardless of the definition of the C record.

Well, this is obviously a hardware structure and the actual layout should be checked regardless of the definition in C (IIRC, the C-standard doesn't guarantee any particular layout on such records neither).

The internal format of bitpacked records is opaque and must not be used in any case where data has to be interchanged or where a particular layout is desired.

There was a reason why I brought up the portability issue. Honestly, what do you propose to mimic such stuff? I remember, we already had a discussion about that a couple of months ago. ;)

I don't want to bring up Ada again, but ... some years ago I implemented such nasty stuff for an embedded system. You might take a look for the implementation of system structures in that language http://www.gmx.de/mc/h9DZhQD3l4X4hfKafRIagg6e3PI6TW . Well, apart from Endianess issues, successful compilation virtually guarantees the layout.

I hope the link works, currently I don't own any web-space... Just click on that "GMX MediaCenter starten" button...

Vinzent.

GMX startet ShortView.de. Hier findest Du Leute mit Deinen Interessen! Jetzt dabei sein:

Reply by email

leledumbo
to Vinzent Höfler

Tried Vinzent solution and it works :clap:!
The layout is I think the same as in C. The difference is that FPC generates it as

resb 4

while C uses

.comm 16

I don't know what .comm for. But since everything runs fine, who cares? I just have some issues where assignment to the fields using pointer to that type (^page_t) doesn't work. For instance:

var
  P: PPage; // the type is named TPage
...
with P^ do begin
  Present:=true;
  ReadWrite:=false;
  UserMode:=true;
  Frame:=FrameStack[FrameStackTop];
end;

When I check the value of each field, it's all ZERO! But the pointer isn't nil. I guess the problem lies in my OS, but I'll do a re-check.

View this message in context: http://www.nabble.com/Converting-C%27s-bit-field-struct-tp18959002p18997771.html Sent from the Free Pascal - General mailing list .

Reply by email

Jonas Maebe
to leledumbo
leledumbo wrote
Tried Vinzent solution and it works :clap:! The layout is I think the same as in C.

It may be the same as in C in certain cases on certain platforms when compiled with a particular version of FPC. Do not depend on it always being or remaining the same, because it *will* not work at some point.

Jonas

Reply by email