classes.pp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. {$IF FPC_FULLVERSION>=30301}
  14. {$modeswitch FUNCTIONREFERENCES}
  15. {$define FPC_HAS_REFERENCE_PROCEDURE}
  16. {$endif}
  17. { determine the type of the resource/form file }
  18. {$define Win16Res}
  19. {$IFNDEF FPC_DOTTEDUNITS}
  20. unit Classes;
  21. {$ENDIF FPC_DOTTEDUNITS}
  22. interface
  23. {$IFDEF FPC_DOTTEDUNITS}
  24. uses
  25. System.RtlConsts,
  26. System.SysUtils,
  27. System.Types,
  28. System.SortBase,
  29. {$ifdef FPC_TESTGENERICS}
  30. System.FGL,
  31. {$endif}
  32. System.TypInfo,
  33. WinApi.Windows;
  34. {$ELSE FPC_DOTTEDUNITS}
  35. uses
  36. rtlconsts,
  37. sysutils,
  38. types,
  39. sortbase,
  40. {$ifdef FPC_TESTGENERICS}
  41. fgl,
  42. {$endif}
  43. typinfo,
  44. windows;
  45. {$ENDIF FPC_DOTTEDUNITS}
  46. type
  47. TWndMethod = procedure(var msg : TMessage) of object;
  48. function MakeObjectInstance(Method: TWndMethod): Pointer;
  49. procedure FreeObjectInstance(ObjectInstance: Pointer);
  50. function AllocateHWnd(Method: TWndMethod): HWND;
  51. procedure DeallocateHWnd(Wnd: HWND);
  52. {$i classesh.inc}
  53. implementation
  54. {$IFDEF FPC_DOTTEDUNITS}
  55. uses
  56. System.SysConst;
  57. {$ELSE FPC_DOTTEDUNITS}
  58. uses
  59. sysconst;
  60. {$ENDIF FPC_DOTTEDUNITS}
  61. { OS - independent class implementations are in /inc directory. }
  62. {$i classes.inc}
  63. type
  64. PMethodWrapperTrampoline = ^TMethodWrapperTrampoline;
  65. PWrapperBlock = ^TWrapperBlock;
  66. TMethodWrapperTrampoline = packed record
  67. Call : byte;
  68. CallOffset : PtrInt;
  69. Jmp : byte;
  70. JmpOffset : PtrInt;
  71. case Integer of
  72. 0: (Next: PMethodWrapperTrampoline; Block : PWrapperBlock);
  73. 1: (Method: TWndMethod);
  74. end;
  75. TWrapperBlock = packed record
  76. Next : PWrapperBlock;
  77. UsageCount : Longint;
  78. Trampolines : array[0..0] of TMethodWrapperTrampoline;
  79. end;
  80. var
  81. WrapperBlockList : PWrapperBlock;
  82. TrampolineFreeList : PMethodWrapperTrampoline;
  83. CritObjectInstance : TCriticalSection;
  84. function TrampolineWndProc(Window: HWND; Message, WParam: WPARAM;LParam: LPARAM): HRESULT; stdcall; assembler;
  85. asm
  86. // build up tmessage structure
  87. pushl $0
  88. movl (%eax),%ecx
  89. pushl LPARAM
  90. pushl WPARAM
  91. pushl Message
  92. // msg
  93. leal (%esp),%edx
  94. // load self
  95. movl 4(%eax),%eax
  96. // call method
  97. call %ecx
  98. addl $12,%esp
  99. // load result
  100. popl %eax
  101. end;
  102. function get_method_offset : Pointer;assembler;nostackframe;
  103. asm
  104. movl (%esp),%eax
  105. addl $5,%eax
  106. end;
  107. const
  108. SizeOfPage = 4096;
  109. function MakeObjectInstance(Method: TWndMethod): Pointer;
  110. var
  111. NewBlock : PWrapperBlock;
  112. Trampoline : PMethodWrapperTrampoline;
  113. begin
  114. EnterCriticalSection(CritObjectInstance);
  115. try
  116. if not(assigned(TrampolineFreeList)) then
  117. begin
  118. NewBlock:=VirtualAlloc(nil,SizeOfPage,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
  119. NewBlock^.UsageCount:=0;
  120. NewBlock^.Next:=WrapperBlockList;
  121. WrapperBlockList:=NewBlock;
  122. Trampoline:=@NewBlock^.Trampolines;
  123. while pointer(Trampoline)+sizeof(Trampoline)<pointer(NewBlock)+SizeOfPage do
  124. begin
  125. Trampoline^.Next:=TrampolineFreeList;
  126. Trampoline^.Block:=NewBlock;
  127. TrampolineFreeList:=Trampoline;
  128. inc(Trampoline);
  129. end;
  130. end;
  131. Trampoline:=TrampolineFreeList;
  132. TrampolineFreeList:=TrampolineFreeList^.Next;
  133. // inc(Trampoline^.Block^.UsageCount);
  134. Trampoline^.Call:=$e8;
  135. Trampoline^.CallOffset:=pointer(@get_method_offset)-pointer(@Trampoline^.Call)-5;
  136. Trampoline^.Jmp:=$e9;
  137. Trampoline^.JmpOffset:=pointer(@TrampolineWndProc)-pointer(@Trampoline^.Jmp)-5;
  138. Trampoline^.Method:=Method;
  139. Result:=Trampoline;
  140. finally
  141. LeaveCriticalSection(CritObjectInstance);
  142. end;
  143. end;
  144. procedure FreeObjectInstance(ObjectInstance: Pointer);
  145. begin
  146. EnterCriticalSection(CritObjectInstance);
  147. try
  148. // block gets overwritten by method dec(PMethodWrapperTrampoline(ObjectInstance)^.Block^.UsageCount);
  149. PMethodWrapperTrampoline(ObjectInstance)^.Next:=TrampolineFreeList;
  150. TrampolineFreeList:=PMethodWrapperTrampoline(ObjectInstance);
  151. finally
  152. LeaveCriticalSection(CritObjectInstance);
  153. end;
  154. end;
  155. procedure DeleteInstBlockList;
  156. var
  157. hp : PWrapperBlock;
  158. begin
  159. EnterCriticalSection(CritObjectInstance);
  160. try
  161. while assigned(WrapperBlockList) do
  162. begin
  163. hp:=WrapperBlockList^.Next;
  164. if VirtualFree(WrapperBlockList,4096,MEM_DECOMMIT) then
  165. VirtualFree(WrapperBlockList,0,MEM_RELEASE);
  166. WrapperBlockList:=hp;
  167. end;
  168. finally
  169. LeaveCriticalSection(CritObjectInstance);
  170. end;
  171. end;
  172. function AllocateHWnd(Method: TWndMethod): HWND;
  173. begin
  174. { dummy }
  175. runerror(217);
  176. Result:=0;
  177. end;
  178. procedure DeallocateHWnd(Wnd: HWND);
  179. begin
  180. { dummy }
  181. runerror(217);
  182. end;
  183. initialization
  184. WrapperBlockList:=nil;
  185. TrampolineFreeList:=nil;
  186. InitCriticalSection(CritObjectInstance);
  187. CommonInit;
  188. finalization
  189. CommonCleanup;
  190. DeleteInstBlockList;
  191. DoneCriticalSection(CritObjectInstance);
  192. end.