Alignment of structure

Just starting out? Need help? Post your questions and find answers here.
User_Russian
Addict
Addict
Posts: 1520
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Alignment of structure

Post by User_Russian »

How to align the structure to the extent of 8?
For example there is a structure:

Code: Select all

Structure UNICODE_STRING 
  Length.w 
  MaximumLength.w 
  Buffer.i 
EndStructure 

Structure DRIVER_OBJECT 
  Type.w 
  Size.w   
  DeviceObject.i 
  Flags.l   
  DriverStart.i 
  DriverSize.l   
  DriverSection.i 
  DriverExtension.i 
  DriverName.UNICODE_STRING 
  HardwareDatabase.i 
  FastIoDispatch.i 
  DriverInit.i 
  DriverStartIo.i 
  DriverUnload.i 
  MajorFunction.i[#IRP_MJ_MAXIMUM_FUNCTION+1] 
EndStructure
On Windows x86, no problems, but they appear on the x64, where the structure should be aligned to the degree 8.
The structure can be aligned:

Code: Select all

Structure UNICODE_STRING 
  Length.w 
  MaximumLength.w 
  
  align.l 
  
  Buffer.i 
EndStructure 

Structure DRIVER_OBJECT 
  Type.w 
  Size.w 
  
  align.l 
  
  DeviceObject.i 
  Flags.l 
  
  align_1.l 
  
  DriverStart.i 
  DriverSize.l 
  
  align_2.l 
  
  DriverSection.i 
  DriverExtension.i 
  DriverName.UNICODE_STRING 
  HardwareDatabase.i 
  FastIoDispatch.i 
  DriverInit.i 
  DriverStartIo.i 
  DriverUnload.i 
  MajorFunction.i[#IRP_MJ_MAXIMUM_FUNCTION+1] 
EndStructure
But the structures of many hundreds, and it is better to instruct the compiler to align how to do this?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Alignment of structure

Post by ts-soft »

Code: Select all

Structure UNICODE_STRING
  Length.w
  MaximumLength.w
  CompilerIf #PB_Processor_x64
  PB_Alignment1.b[4]
  CompilerEndIf
  Buffer.i
EndStructure

Structure DRIVER_OBJECT
  Type.w
  Size.w
  CompilerIf #PB_Processor_x64
  PB_Alignment1.b[4]
  CompilerEndIf
  DeviceObject.i
  Flags.l
  CompilerIf #PB_Processor_x64
  PB_Alignment2.b[4]
  CompilerEndIf
  DriverStart.i
  DriverSize.l
  CompilerIf #PB_Processor_x64
  PB_Alignment3.b[4]
  CompilerEndIf
  DriverSection.i
  DriverExtension.i
  DriverName.UNICODE_STRING
  HardwareDatabase.i
  FastIoDispatch.i
  DriverInit.i
  DriverStartIo.i
  DriverUnload.i
  MajorFunction.i[#IRP_MJ_MAXIMUM_FUNCTION+1]
EndStructure 
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User_Russian
Addict
Addict
Posts: 1520
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Alignment of structure

Post by User_Russian »

Structures are too many to be manually aligned.
I spend at it for several months and certainly made ​​many bugs. Therefore, the alignment needed to get the compiler.

We are talking about the WDK. http://en.wikipedia.org/wiki/Windows_Driver_Kit
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Alignment of structure

Post by ts-soft »

The PB Compiler doesn't support this.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Alignment of structure

Post by Tenaja »

There are times when you may need a structure to be NOT padded (i.e. not aligned, as is standard), and others where you NEED it padded. So we can't ask Fred to change the default operation. However, you can do this manually with a macro and StructureUnion.

I just thew this together quickly, so I'll leave the tuning of this to the coder who requires the alignment padding. This code appears to work with w7-32:

Code: Select all

; Macro to pad a variable in a structure to a minimum length
Macro PaddedVar(name, type, bytes)
	StructureUnion
		_#name#bytes.s{bytes}
		name#.type
	EndStructureUnion
EndMacro

Structure Test1
	PaddedVar(a, w, 8)
	PaddedVar(b, w, 8)
	c.w
	d.w
EndStructure
testvar.Test1

testvar\a = 5
testvar\b = 88
testvar\c = 6
testvar\d = 77

Debug @testvar\a
Debug @testvar\b
Debug @testvar\c
Debug @testvar\d




; Test regular non-padded structure:
Debug "-----"

Structure Test2
	a.w
	b.l
	c.w
	d.w
EndStructure
testvar2.Test2

testvar2\a = 5
testvar2\b = 88
testvar2\c = 6
testvar2\d = 77

Debug @testvar2\a
Debug @testvar2\b
Debug @testvar2\c
Debug @testvar2\d
Of course, if you knew ahead of time that all of your structs would have the same alignment, you could eliminate one macro argument and replace it with a constant...

Code: Select all

; This variation uses a fixed padding value, 
; so there are only two macro arguments.


; Macro to pad a variable in a structure to a minimum length
Macro PaddedVar(name, type)
	
	StructureUnion
		_#name#bytes.s{#PaddedVarSize}
		name#.type
	EndStructureUnion
EndMacro

; Size of padded variables
#VarSizeb = 1
#VarSizea = 1
CompilerIf #PB_Compiler_Unicode  = 1
	#VarSizec = 2
CompilerElse
	#VarSizec = 1
CompilerEndIf
#VarSizew = 2
#VarSizeu = 2
#VarSizel = 4
CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
	#VarSizei = 4
CompilerElse
	#VarSizei = 8
CompilerEndIf
#VarSizef = 4
#VarSizeq = 8
#VarSized = 8

; Demo the macro...
#PaddedVarSize = #VarSizeq

Structure Test1
	PaddedVar(a, w)
	PaddedVar(b, w)
	c.w
	d.w
EndStructure


testvar.Test1

testvar\a = 5
testvar\b = 88
testvar\c = 6
testvar\d = 77

Debug @testvar\a
Debug @testvar\b
Debug @testvar\c
Debug @testvar\d
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Alignment of structure

Post by Josh »

Tenaja wrote:There are times when you may need a structure to be NOT padded (i.e. not aligned, as is standard), and others where you NEED it padded. So we can't ask Fred to change the default operation. However, you can do this manually with a macro and StructureUnion.
Like we have ProcedureC, it would be nice to have an additional StructureC. I don't think, you can solve this with macros. The problem is too complex. If i have to calculate, i don't need a macro.
sorry for my bad english
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Alignment of structure

Post by jassing »

Josh wrote:Like we have ProcedureC, it would be nice to have an additional StructureC.
+1
Post Reply