Seite 1 von 1

Structure-Alignment und Autocomplete. Wie besser?

Verfasst: 19.07.2012 22:14
von Regenduft
Hallo liebe Leute,

ich bin auf die Schnappsidee gekommen, mir per Macros ein "AlignedStructure/EndAlignedStructure" zu bauen, so dass die Struktur immer am Ende mit Blindbytes aufgefüllt wird, bis das ganze eben "aligned" ist (prozessorabhängiges oder manuelles Alignment).

Sinn und Zweck: Komplexe Strukturen können einfach "hirnlos" erweitert werden und das Alignment stimmt immer.
Problem: Ich habe natürlich nicht daran gedacht, dass ich so den Autovervollständiger aushebel, was bei komplexen Strukturen natürlich ungeschickt ist...

Hat jemand vielleicht eine Ansatzidee für eine bessere Lösung oder einen Trick auf Lager betreffs Autovervollständigung?

Code: Alles auswählen

Macro __AlignedStructureDQ
  "
EndMacro

Macro AlignedStructure(_StructureName_, _AlignmentValue_=SizeOf(Integer))
  
  CompilerIf Defined(_StructureName_, #PB_Structure)
    CompilerError "'Structure/AlignedStructure' already declared: "+__AlignedStructureDQ#_StructureName_#__AlignedStructureDQ
  CompilerEndIf
  
  CompilerIf _AlignmentValue_ <= 0
    CompilerError "'AlignmentValue' has to be greater than zero: "+__AlignedStructureDQ#_AlignmentValue_#__AlignedStructureDQ
  CompilerEndIf
  
  #__StructureAlignment_#_StructureName_ = _AlignmentValue_
  
  Structure __AlignedStructureDummy_#_StructureName_
  
EndMacro

Macro EndAlignedStructure(_StructureName_)
  
  EndStructure
  
  Structure _StructureName_ Extends __AlignedStructureDummy_#_StructureName_
    CompilerIf SizeOf(__AlignedStructureDummy_#_StructureName_) % #__StructureAlignment_#_StructureName_
      zzzzStructureAlignment.b[ #__StructureAlignment_#_StructureName_ - SizeOf(__AlignedStructureDummy_#_StructureName_) % #__StructureAlignment_#_StructureName_ ]
    CompilerEndIf
  EndStructure
  
EndMacro

Re: Structure-Alignment und Autocomplete. Wie besser?

Verfasst: 23.07.2012 00:02
von Regenduft
Ich habe zwar noch immer keine wirklich zufriedenstellende Lösung gefunden (und werde es wohl auch nicht), aber eine halbwegige:

Alignment-Macro:

Code: Alles auswählen

Macro __AlignStructureDQ
  "
EndMacro

Macro AlignStructure(_SizeOfStructure_, _AlignmentValue_=SizeOf(Integer))
  
  CompilerIf _AlignmentValue_ <= 0
    EndStructure:CompilerError "'AlignmentValue' has to be greater than zero: "+__AlignStructureDQ#_AlignmentValue_#__AlignStructureDQ
  CompilerEndIf
  
  CompilerIf _SizeOfStructure_ < 0
    EndStructure:CompilerError "'SizeOfStructure' has to be the real structures size (which cannot be smaller than zero): "+__AlignStructureDQ#_SizeOfStructure_#__AlignStructureDQ
  CompilerEndIf
  
  CompilerIf _SizeOfStructure_ % _AlignmentValue_
    ___StructureAlignment.b[ _AlignmentValue_ - _SizeOfStructure_ % _AlignmentValue_ ]
  CompilerEndIf
  
EndMacro
Beispiel 1 (Nutzung):

Code: Alles auswählen

Structure s0 : b.b[0] : AlignStructure(0) : EndStructure
Structure s1 : b.b[1] : AlignStructure(1) : EndStructure
Structure s4 : b.b[4] : AlignStructure(4) : EndStructure
Structure s5 : b.b[5] : AlignStructure(5) : EndStructure
Structure s8 : b.b[8] : AlignStructure(8) : EndStructure
Structure s9 : b.b[9] : AlignStructure(9) : EndStructure
Structure s16: b.b[16]: AlignStructure(16): EndStructure
Structure s17: b.b[17]: AlignStructure(17): EndStructure

Debug "=== x86 und x64 liefern natürlich unterschiedliche Ergebnisse ==="
Debug ""
Debug "0 Byte -> " + Str(SizeOf(s0)) + " Byte"
Debug "1 Byte -> " + Str(SizeOf(s1)) + " Byte"
Debug "..."
Debug "4 Byte -> " + Str(SizeOf(s4)) + " Byte"
Debug "5 Byte -> " + Str(SizeOf(s5)) + " Byte"
Debug "..."
Debug "8 Byte -> " + Str(SizeOf(s8)) + " Byte"
Debug "9 Byte -> " + Str(SizeOf(s9)) + " Byte"
Debug "..."
Debug "16 Byte -> " + Str(SizeOf(s16)) + " Byte"
Debug "17 Byte -> " + Str(SizeOf(s17)) + " Byte"
Debug "..."
Beispiel 2 (Fehlermeldungen):

Code: Alles auswählen

Structure WrongSizeOfStructure
  AlignStructure(-123)
EndStructure

Structure WrongAlignmentValue
  AlignStructure(0, -123)
EndStructure