classes.pp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 1998 by Michael Van Canneyt and Florian Klaempfl
  4. Classes unit for win32
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$mode objfpc}
  12. {$H+}
  13. {$modeswitch advancedrecords}
  14. {$IF FPC_FULLVERSION>=30301}
  15. {$modeswitch FUNCTIONREFERENCES}
  16. {$define FPC_HAS_REFERENCE_PROCEDURE}
  17. {$ifndef CPULLVM}
  18. {$if DEFINED(CPUARM) or DEFINED(CPUAARCH64)}
  19. {$define FPC_USE_INTRINSICS}
  20. {$endif}
  21. {$if defined(CPUPOWERPC) or defined(CPUPOWERPC64)}
  22. {$define FPC_USE_INTRINSICS}
  23. {$endif}
  24. {$if defined(CPURISCV32) or defined(CPURISCV64)}
  25. {$define FPC_USE_INTRINSICS}
  26. {$endif}
  27. {$endif}
  28. {$endif}
  29. { determine the type of the resource/form file }
  30. {$define Win16Res}
  31. {$IFNDEF FPC_DOTTEDUNITS}
  32. unit Classes;
  33. {$ENDIF FPC_DOTTEDUNITS}
  34. interface
  35. {$IFDEF FPC_DOTTEDUNITS}
  36. uses
  37. System.RtlConsts,
  38. System.SysUtils,
  39. System.Types,
  40. System.SortBase,
  41. {$ifdef FPC_TESTGENERICS}
  42. System.FGL,
  43. {$endif}
  44. System.TypInfo,
  45. {$ifdef FPC_USE_INTRINSICS}
  46. System.Intrinsics,
  47. {$endif}
  48. WinApi.Windows;
  49. {$ELSE FPC_DOTTEDUNITS}
  50. uses
  51. rtlconsts,
  52. sysutils,
  53. types,
  54. sortbase,
  55. {$ifdef FPC_TESTGENERICS}
  56. fgl,
  57. {$endif}
  58. typinfo,
  59. {$ifdef FPC_USE_INTRINSICS}
  60. intrinsics,
  61. {$endif}
  62. windows;
  63. {$ENDIF FPC_DOTTEDUNITS}
  64. { Also set FPC_USE_INTRINSICS for i386 and x86_64,
  65. but only after _USES clause as there
  66. is not intinsics unit for those CPUs }
  67. {$IF FPC_FULLVERSION>=30301}
  68. {$ifndef CPULLVM}
  69. {$if defined(CPUI386) or defined(CPUX86_64)}
  70. {$define FPC_USE_INTRINSICS}
  71. {$endif}
  72. {$endif}
  73. {$endif}
  74. type
  75. TWndMethod = procedure(var msg : TMessage) of object;
  76. function MakeObjectInstance(Method: TWndMethod): Pointer;
  77. procedure FreeObjectInstance(ObjectInstance: Pointer);
  78. function AllocateHWnd(Method: TWndMethod): HWND;
  79. procedure DeallocateHWnd(Wnd: HWND);
  80. {$i classesh.inc}
  81. implementation
  82. {$IFDEF FPC_DOTTEDUNITS}
  83. uses
  84. System.SysConst;
  85. {$ELSE FPC_DOTTEDUNITS}
  86. uses
  87. sysconst;
  88. {$ENDIF FPC_DOTTEDUNITS}
  89. { OS - independent class implementations are in /inc directory. }
  90. {$i classes.inc}
  91. type
  92. PMethodWrapperTrampoline = ^TMethodWrapperTrampoline;
  93. PWrapperBlock = ^TWrapperBlock;
  94. TMethodWrapperTrampoline = packed record
  95. Call : byte;
  96. CallOffset : PtrInt;
  97. Jmp : byte;
  98. JmpOffset : PtrInt;
  99. case Integer of
  100. 0: (Next: PMethodWrapperTrampoline; Block : PWrapperBlock);
  101. 1: (Method: TWndMethod);
  102. end;
  103. TWrapperBlock = packed record
  104. Next : PWrapperBlock;
  105. UsageCount : Longint;
  106. Trampolines : array[0..0] of TMethodWrapperTrampoline;
  107. end;
  108. var
  109. WrapperBlockList : PWrapperBlock;
  110. TrampolineFreeList : PMethodWrapperTrampoline;
  111. CritObjectInstance : TCriticalSection;
  112. function TrampolineWndProc(Window: HWND; Message, WParam: WPARAM;LParam: LPARAM): HRESULT; stdcall; assembler;
  113. asm
  114. // build up tmessage structure
  115. pushl $0
  116. movl (%eax),%ecx
  117. pushl LPARAM
  118. pushl WPARAM
  119. pushl Message
  120. // msg
  121. leal (%esp),%edx
  122. // load self
  123. movl 4(%eax),%eax
  124. // call method
  125. call %ecx
  126. addl $12,%esp
  127. // load result
  128. popl %eax
  129. end;
  130. function get_method_offset : Pointer;assembler;nostackframe;
  131. asm
  132. movl (%esp),%eax
  133. addl $5,%eax
  134. end;
  135. const
  136. SizeOfPage = 4096;
  137. function MakeObjectInstance(Method: TWndMethod): Pointer;
  138. var
  139. NewBlock : PWrapperBlock;
  140. Trampoline : PMethodWrapperTrampoline;
  141. begin
  142. EnterCriticalSection(CritObjectInstance);
  143. try
  144. if not(assigned(TrampolineFreeList)) then
  145. begin
  146. NewBlock:=VirtualAlloc(nil,SizeOfPage,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
  147. NewBlock^.UsageCount:=0;
  148. NewBlock^.Next:=WrapperBlockList;
  149. WrapperBlockList:=NewBlock;
  150. Trampoline:=@NewBlock^.Trampolines;
  151. while pointer(Trampoline)+sizeof(Trampoline)<pointer(NewBlock)+SizeOfPage do
  152. begin
  153. Trampoline^.Next:=TrampolineFreeList;
  154. Trampoline^.Block:=NewBlock;
  155. TrampolineFreeList:=Trampoline;
  156. inc(Trampoline);
  157. end;
  158. end;
  159. Trampoline:=TrampolineFreeList;
  160. TrampolineFreeList:=TrampolineFreeList^.Next;
  161. // inc(Trampoline^.Block^.UsageCount);
  162. Trampoline^.Call:=$e8;
  163. Trampoline^.CallOffset:=pointer(@get_method_offset)-pointer(@Trampoline^.Call)-5;
  164. Trampoline^.Jmp:=$e9;
  165. Trampoline^.JmpOffset:=pointer(@TrampolineWndProc)-pointer(@Trampoline^.Jmp)-5;
  166. Trampoline^.Method:=Method;
  167. Result:=Trampoline;
  168. finally
  169. LeaveCriticalSection(CritObjectInstance);
  170. end;
  171. end;
  172. procedure FreeObjectInstance(ObjectInstance: Pointer);
  173. begin
  174. EnterCriticalSection(CritObjectInstance);
  175. try
  176. // block gets overwritten by method dec(PMethodWrapperTrampoline(ObjectInstance)^.Block^.UsageCount);
  177. PMethodWrapperTrampoline(ObjectInstance)^.Next:=TrampolineFreeList;
  178. TrampolineFreeList:=PMethodWrapperTrampoline(ObjectInstance);
  179. finally
  180. LeaveCriticalSection(CritObjectInstance);
  181. end;
  182. end;
  183. procedure DeleteInstBlockList;
  184. var
  185. hp : PWrapperBlock;
  186. begin
  187. EnterCriticalSection(CritObjectInstance);
  188. try
  189. while assigned(WrapperBlockList) do
  190. begin
  191. hp:=WrapperBlockList^.Next;
  192. if VirtualFree(WrapperBlockList,4096,MEM_DECOMMIT) then
  193. VirtualFree(WrapperBlockList,0,MEM_RELEASE);
  194. WrapperBlockList:=hp;
  195. end;
  196. finally
  197. LeaveCriticalSection(CritObjectInstance);
  198. end;
  199. end;
  200. function AllocateHWnd(Method: TWndMethod): HWND;
  201. begin
  202. { dummy }
  203. runerror(217);
  204. Result:=0;
  205. end;
  206. procedure DeallocateHWnd(Wnd: HWND);
  207. begin
  208. { dummy }
  209. runerror(217);
  210. end;
  211. initialization
  212. WrapperBlockList:=nil;
  213. TrampolineFreeList:=nil;
  214. InitCriticalSection(CritObjectInstance);
  215. CommonInit;
  216. finalization
  217. CommonCleanup;
  218. DeleteInstBlockList;
  219. DoneCriticalSection(CritObjectInstance);
  220. end.