Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / May 2004

Tip: Looking for answers? Try searching our database.

Sequential class layout and packing

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike - 21 May 2004 15:11 GMT
Hell

I've been playing with some IL code, and have encountered two interesting issues

The first is probably a usage misunderstanding on my part. The ".pack <n>" directive seems to have no effect on my object layout

The second is that the CLR appears to ignore me if I mark the class hosting the entrypoint as "sequential"

I've attached the IL I've been playing with. (Sorry if it's a bit long, IL can get like that ;-) In essence it defines three classes, all marked "sequential" in layout, with their fields to be laid out in reverse order. The IL then simply dumps the addresses of the field of the various objects

The interesting thing is that for the "FirstStruct" and "FirstClass" objects my layout has been respected, but for the "HostClass" it has not. None of them have my honoured packing request honoured

Any explanations would be very much appreciated

Thank

Mik

After assembling and running my output is

<execution_output

HostClass::ShowLayout (this = 77863876
HostClass::one   : 7786389
HostClass::two   : 7786389
HostClass::three : 7786388
HostClass::four  : 77863880 (reference = 77863920
HostClass::five  : 7786388
FirstStruct::ShowLayout (this = 77863892
FirstStruct::one   : 7786390
FirstStruct::two   : 7786390
FirstStruct::three : 7786390
FirstStruct::four  : 7786390
FirstStruct::five  : 7786389
FirstClass::ShowLayout (this = 77863920
FirstClass::one   : 7786396
FirstClass::two   : 7786395
FirstClass::three : 7786393
FirstClass::four  : 7786392

</execution_output><il_code

.assembly extern mscorli

 .publickeytoken = (B7 7A 5C 56 19 34 E0 89
 .ver 1:0:5000:

.assembly FieldTes

 .hash algorithm 0x0000800
 .ver 0:0:0:

.module FieldTest.ex
.corflags 0x0001000

.class public sequential ansi sealed beforefieldinit FirstStruct extends [mscorlib]System.ValueTyp

 .pack 1

 .field [4]  public int32 on
 .field [3]  public int32 tw
 .field [2]  public int16 thre
 .field [1]  public int8  fou
 .field [0]  public int64 fiv

 .method public hidebysig instance void ShowLayout() cil manage
 
   ldstr      "FirstStruct::ShowLayout (this =
   call       void [mscorlib]System.Console::Write(string
   ldarg.
   conv.
   call       void [mscorlib]System.Console::Write(int32
   ldstr      ")
   call       void [mscorlib]System.Console::WriteLine(string

   ldstr      " FirstStruct::one   :
   call       void [mscorlib]System.Console::Write(string
   ldarg.
   ldflda     int32 FirstStruct::on
   conv.
   call       void [mscorlib]System.Console::WriteLine(int32

   ldstr      " FirstStruct::two   :
   call       void [mscorlib]System.Console::Write(string
   ldarg.
   ldflda     int32 FirstStruct::tw
   conv.
   call       void [mscorlib]System.Console::WriteLine(int32

   ldstr      " FirstStruct::three :
   call       void [mscorlib]System.Console::Write(string
   ldarg.
   ldflda     int16 FirstStruct::thre
   conv.
   call       void [mscorlib]System.Console::WriteLine(int32

   ldstr      " FirstStruct::four  :
   call       void [mscorlib]System.Console::Write(string
   ldarg.
   ldflda     int8 FirstStruct::fou
   conv.
   call       void [mscorlib]System.Console::WriteLine(int32

   ldstr      " FirstStruct::five  :
   call       void [mscorlib]System.Console::Write(string
   ldarg.
   ldflda     int64 FirstStruct::fiv
   conv.
   call       void [mscorlib]System.Console::WriteLine(int32

   re
 

.class public sequential ansi beforefieldinit FirstClass extends [mscorlib]System.Objec

 .pack 3

 .field [3]  public int16                 on
 .field [2]  public int32                 tw
 .field [1]  public valuetype FirstStruct thre
 .field [0]  public int64                 fou

 .method public hidebysig specialname rtspecialname instance void  .ctor() cil manage
 
   .maxstack  

   ldarg.
   call       instance void [mscorlib]System.Object::.ctor(
   re
 

 .method public hidebysig instance void ShowLayout() cil manage
 
   ldstr      "FirstClass::ShowLayout (this =
   call       void [mscorlib]System.Console::Write(string)
   ldarg.0
   conv.i
   call       void [mscorlib]System.Console::Write(int32)
   ldstr      ")"
   call       void [mscorlib]System.Console::WriteLine(string)

   ldstr      " FirstClass::one   : "
   call       void [mscorlib]System.Console::Write(string)
   ldarg.0
   ldflda     int16 FirstClass::one
   conv.i
   call       void [mscorlib]System.Console::WriteLine(int32)

   ldstr      " FirstClass::two   : "
   call       void [mscorlib]System.Console::Write(string)
   ldarg.0
   ldflda     int32 FirstClass::two
   conv.i
   call       void [mscorlib]System.Console::WriteLine(int32)

   ldstr      " FirstClass::three : "
   call       void [mscorlib]System.Console::Write(string)
   ldarg.0
   ldflda     valuetype FirstStruct FirstClass::three
   conv.i
   call       void [mscorlib]System.Console::WriteLine(int32)

   ldstr      " FirstClass::four  : "
   call       void [mscorlib]System.Console::Write(string)
   ldarg.0
   ldflda     int64 FirstClass::four
   conv.i
   call       void [mscorlib]System.Console::WriteLine(int32)

   ret
 }
}

.class public sequential ansi beforefieldinit HostClass extends [mscorlib]System.Object
{

 .pack 64

 .field [4] public bool                  one
 .field [3] public valuetype FirstStruct two
 .field [2] public char                  three
 .field [1] public class FirstClass      four
 .field [0] public int32                 five

 .method public hidebysig specialname rtspecialname instance void  .ctor() cil managed
 {
   .maxstack  2

   ldarg.0
   call       instance void [mscorlib]System.Object::.ctor()
   ldarg.0
   newobj     instance void FirstClass::.ctor()
   stfld      class FirstClass HostClass::four
   ret
 }

 .method public hidebysig instance void ShowLayout() cil managed
 {
   .maxstack  2

   ldstr      "HostClass::ShowLayout (this = "
   call       void [mscorlib]System.Console::Write(string)
   ldarg.0
   conv.i
   call       void [mscorlib]System.Console::Write(int32)
   ldstr      ")"
   call       void [mscorlib]System.Console::WriteLine(string)

   ldstr      " HostClass::one   : "
   call       void [mscorlib]System.Console::Write(string)
   ldarg.0
   ldflda     bool HostClass::one
   conv.i
   call       void [mscorlib]System.Console::WriteLine(int32)

   ldstr      " HostClass::two   : "
   call       void [mscorlib]System.Console::Write(string)
   ldarg.0
   ldflda     valuetype FirstStruct HostClass::two
   conv.i
   call       void [mscorlib]System.Console::WriteLine(int32)

   ldstr      " HostClass::three : "
   call       void [mscorlib]System.Console::Write(string)
   ldarg.0
   ldflda     char HostClass::three
   conv.i
   call       void [mscorlib]System.Console::WriteLine(int32)

   ldstr      " HostClass::four  : "
   call       void [mscorlib]System.Console::Write(string)
   ldarg.0
   ldflda     class FirstClass HostClass::four
   conv.i
   call       void [mscorlib]System.Console::Write(int32)
   ldstr      " (reference = "
   call       void [mscorlib]System.Console::Write(string)
   ldarg.0
   ldfld      class FirstClass HostClass::four
   conv.i
   call       void [mscorlib]System.Console::Write(int32)
   ldstr      ")"
   call       void [mscorlib]System.Console::WriteLine(string)

   ldstr      " HostClass::five  : "
   call       void [mscorlib]System.Console::Write(string)
   ldarg.0
   ldflda     int32 HostClass::five
   conv.i
   call       void [mscorlib]System.Console::WriteLine(int32)

   ldarg.0
   ldflda     valuetype FirstStruct HostClass::two
   call       instance void FirstStruct::ShowLayout()

   ldarg.0
   ldfld      class FirstClass HostClass::four
   call       instance void FirstClass::ShowLayout()

   ret
 }

 .method public hidebysig static void Main(string[] args) cil managed
 {
   .entrypoint
Mattias Sj?gren - 21 May 2004 20:27 GMT
Mike,

>The first is probably a usage misunderstanding on my part. The ".pack <n>" directive seems to have no effect on my object layout.

You can't use .pack to add more padding between fields if that's what
you're trying to accomplish, only to reduce it (make the type more
compact).

The offset of a field is basicly determined by

offset = end_of_previous_field + min(pack, size_of_field)

and since no field is larger than 16 or 32 bytes repectively in
FirstStruct or FirstClass, your .pack directives do no difference. Try
setting the packing if FirstStruct to 1 to see some actual result.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Diana Milirud [MSFT] - 24 May 2004 21:42 GMT
Mike,

>The interesting thing is that for the "FirstStruct" and "FirstClass" objects my layout has been respected, but for the "HostClass" it has not. None of them have >
my honoured packing request honoured

A type is laid out sequentially only if it has no reference fields. In your case, "HostClass" has a reference field and the runtime falls back to autolayout.

Thanks,
Dian

Signature

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note:  For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.