sysmarshalh.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. type
  2. TMarshaller = record
  3. private type
  4. { Descendants of TDeferBase are stored in-place inside TDeferQueueNode.Mem.
  5. First node is built into TState and uses StaticStore. }
  6. PDeferQueueNode = ^TDeferQueueNode;
  7. TDeferQueueNode = record
  8. Used, Alloc: Int32;
  9. Next: PDeferQueueNode;
  10. case Cardinal of
  11. 0: (Mem: array[0 .. 0] of Byte);
  12. 1: (StaticStore: array[0 .. 15] of Pointer); { Also aligns variable part on SizeOf(Pointer). }
  13. end;
  14. TState = class(TInterfacedObject, IInterface)
  15. DeferHead: TDeferQueueNode;
  16. DeferTail: PDeferQueueNode;
  17. constructor Create;
  18. destructor Destroy; override;
  19. procedure Flush;
  20. procedure FlushQueue;
  21. procedure ClearQueue;
  22. procedure NotePointerChanged(OldPtr, NewPtr: TPtrWrapper);
  23. end;
  24. { Deferred operation (performed by Done). }
  25. PDeferBase = ^TDeferBase;
  26. TDeferBase = object
  27. constructor Init;
  28. destructor Done; virtual; abstract;
  29. procedure NotePointerChanged(OldPtr, NewPtr: TPtrWrapper); virtual;
  30. end;
  31. PDeferFreeMem = ^TDeferFreeMem;
  32. TDeferFreeMem = object(TDeferBase)
  33. P: TPtrWrapper;
  34. destructor Done; virtual;
  35. procedure NotePointerChanged(OldPtr, NewPtr: TPtrWrapper); virtual;
  36. end;
  37. TUnfixProc = procedure(Ptr: TPtrWrapper);
  38. { Not required if there is a way to take an address of a generic procedure specialization... }
  39. generic TAddressableUnfixArraySpecialization<T> = record
  40. class procedure UnfixArray(ArrPtr: TPtrWrapper); static;
  41. end;
  42. PDeferUnfix = ^TDeferUnfix;
  43. TDeferUnfix = object(TDeferBase)
  44. Unfix: TUnfixProc;
  45. P: TPtrWrapper;
  46. destructor Done; virtual;
  47. end;
  48. PDeferMoveToSBAndFree = ^TDeferMoveToSBAndFree;
  49. TDeferMoveToSBAndFree = object(TDeferBase)
  50. Src: TPtrWrapper;
  51. SB: TUnicodeStringBuilder;
  52. MaxLen: SizeInt;
  53. destructor Done; virtual;
  54. end;
  55. function PushDefer(InstanceSize: SizeInt): PDeferBase;
  56. var
  57. FState: TState;
  58. FStateLife: IInterface;
  59. public
  60. procedure Flush;
  61. function AllocMem(Size: SizeInt): TPtrWrapper;
  62. function ReallocMem(OldPtr: TPtrWrapper; NewSize: NativeInt): TPtrWrapper;
  63. function AllocStringAsAnsi(const Str: UnicodeString): TPtrWrapper; inline;
  64. function AllocStringAsAnsi(const Str: UnicodeString; CodePage: Word): TPtrWrapper; inline;
  65. function AllocStringAsUnicode(const Str: UnicodeString): TPtrWrapper;
  66. function AllocStringAsUtf8(const Str: UnicodeString): TPtrWrapper; inline;
  67. function AsAnsi(const S: UnicodeString): TPtrWrapper; inline;
  68. function AsAnsi(S: PUnicodeChar): TPtrWrapper; inline;
  69. function AsAnsi(const S: UnicodeString; CodePage: Word): TPtrWrapper; inline;
  70. function AsAnsi(S: PUnicodeChar; CodePage: Word): TPtrWrapper; inline;
  71. function AsUtf8(const S: UnicodeString): TPtrWrapper; inline;
  72. function AsUtf8(S: PUnicodeChar): TPtrWrapper; inline;
  73. private
  74. function AllocStringAsAnsi(S: PUnicodeChar; Len: SizeInt; CodePage: Word): TPtrWrapper;
  75. public
  76. { No clue what's it, let it be a synonym of FixArray for now... }
  77. function AsRaw(const B: TBytes): TPtrWrapper; inline;
  78. generic function FixArray<T>(const Arr: specialize TArray<T>): TPtrWrapper;
  79. function FixString(var Str: UnicodeString): TPtrWrapper;
  80. function UnsafeFixString(const Str: UnicodeString): TPtrWrapper;
  81. function InString(SB: TUnicodeStringBuilder; MaxLen: SizeInt): TPtrWrapper;
  82. function OutString(const S: UnicodeString): TPtrWrapper; inline;
  83. function InOutString(SB: TUnicodeStringBuilder; MaxLen: SizeInt): TPtrWrapper;
  84. end;