Page 1 of 1
Alignment of structure
Posted: Sat Jun 16, 2012 1:20 pm
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?
Re: Alignment of structure
Posted: Sat Jun 16, 2012 1:38 pm
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
Re: Alignment of structure
Posted: Sat Jun 16, 2012 1:46 pm
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
Re: Alignment of structure
Posted: Sat Jun 16, 2012 1:50 pm
by ts-soft
The PB Compiler doesn't support this.
Re: Alignment of structure
Posted: Sat Sep 15, 2012 2:03 pm
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
Re: Alignment of structure
Posted: Sat Sep 15, 2012 3:27 pm
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.
Re: Alignment of structure
Posted: Sat Sep 15, 2012 4:47 pm
by jassing
Josh wrote:Like we have ProcedureC, it would be nice to have an additional StructureC.
+1