FastShareMem.pas 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. unit FastShareMem;
  2. (*
  3. * Shared Memory Allocator for Delphi DLL's
  4. * Version: 2.10
  5. *
  6. * Features:
  7. * No runtime dll required.
  8. * No performance degradation.
  9. * Faster than ShareMem/Borlndmm.dll.
  10. *
  11. * Usage:
  12. * Windows:
  13. * Must be the FIRST unit listed in the project file's USES section
  14. * for both dll and exe projects. If you install a memory manager for
  15. * leak detection, it should be listed immediately AFTER this unit.
  16. * Linux:
  17. * Not needed. May be commented out using conditional directives:
  18. * uses {$IFDEF WIN32} FastShareMem, {$ENDIF}
  19. *
  20. * Author: Emil M. Santos
  21. * You may use and modify this software as you wish, but this section
  22. * must be kept intact. Please see Readme.txt for copyright and disclaimer.
  23. *
  24. * Send bugs/comments to fastsharemem@codexterity.com
  25. * On the web: http://www.codexterity.com
  26. * To be notified of new versions by email, subscribe to the site alerter facility.
  27. Revision History:
  28. 2003 Dec 03: Version 2.10. Added GetAllocMemCount and GetAllocMemSize functions.
  29. From a contribution by Andrey Nikolayevich Aban'shin (andrey@ecobank.san.ru).
  30. 2003 Dec 03: Version 2.00 released. Complete rewrite; now uses a window class
  31. to exchange data between modules. Safer, and *much* simpler.
  32. The code is also much shorter.
  33. 2003 Aug 27: Removed reference to SysUtils. This was causing subtle bugs.
  34. Update by Alex Blach (entwicklung@zmi.de)
  35. 2003 May 7: Fixed "Combining signed and unsigned types" warning. Replaced
  36. integers with longword where appropriate.
  37. Thanks to Nagy Krisztián (chris@manage.co.hu)
  38. 2002 Oct 9: Separated MEM_DECOMMIT and MEM_RELEASE calls. Thanks to Maurice Fletcher and Alexandr Kozin.
  39. 2002 Sep 9: Thanks to Ai Ming (aiming@ynxx.com) for these changes:
  40. Modified to work with Windows NT/2000/XP.
  41. Added reference-counting mechanism.
  42. 2002 Aug 14: Rewrote address-computation code to better match windows 98
  43. allocation. VirtualAlloc may round down requested address *twice*.
  44. Replaced ASSERTs with (lower-level) Win32 MessageBox calls.
  45. (Thanks to Darryl Strickland (DStrickland@carolina.rr.com))
  46. *)
  47. (*
  48. Note to contributors:
  49. If you're going to edit this code, keep in mind the following things:
  50. * We shouldn't dynamically allocate Delphi 'objects' here, like strings,
  51. obejcts etc. All memory should come from the Windows API, or be statically
  52. allocated.
  53. * We shouln't raise exceptions here, since an exception is a Delphi object,
  54. and thus consumes heap memory.
  55. * For the above reasons, we cannot use most VCL facilities here.
  56. *)
  57. interface
  58. var
  59. GetAllocMemCount: function: integer;
  60. GetAllocMemSize : function: integer;
  61. implementation
  62. uses Windows;
  63. const ClassName = '_com.codexterity.fastsharemem.dataclass';
  64. type
  65. TFastSharememPack = record
  66. MemMgr: TMemoryManager;
  67. _GetAllocMemSize :function :integer;
  68. _GetAllocMemCount :function :integer;
  69. end;
  70. function _GetAllocMemCount: Integer;
  71. begin
  72. Result := System.AllocMemCount;
  73. end;
  74. function _GetAllocMemSize: Integer;
  75. begin
  76. Result := System.AllocMemSize;
  77. end;
  78. var
  79. MemPack: TFastSharememPack;
  80. OldMemMgr: TMemoryManager;
  81. wc: TWndClass;
  82. isHost: boolean;
  83. initialization
  84. if (not GetClassInfo(HInstance, ClassName, wc)) then
  85. begin
  86. GetMemoryManager(MemPack.MemMgr);
  87. MemPack._GetAllocMemCount := @_GetAllocMemCount;
  88. MemPack._GetAllocMemSize := @_GetAllocMemSize;
  89. GetAllocMemCount := @_GetAllocMemCount;
  90. GetAllocMemSize := @_GetAllocMemSize;
  91. FillChar(wc, sizeof(wc), 0);
  92. wc.lpszClassName := ClassName;
  93. wc.style := CS_GLOBALCLASS;
  94. wc.hInstance := hInstance;
  95. wc.lpfnWndProc := @MemPack;
  96. if RegisterClass(wc)=0 then
  97. begin
  98. MessageBox( 0, 'Shared Memory Allocator setup failed: Cannot register class.', 'FastShareMem', 0 );
  99. Halt;
  100. end;
  101. isHost := true;
  102. end else
  103. begin
  104. GetMemoryManager(OldMemMgr); // optional
  105. SetMemoryManager(TFastSharememPack(wc.lpfnWndProc^).MemMgr);
  106. GetAllocMemCount := TFastSharememPack(wc.lpfnWndProc^)._GetAllocMemCount;
  107. GetAllocMemSize := TFastSharememPack(wc.lpfnWndProc^)._GetAllocMemSize;
  108. isHost := false;
  109. end;
  110. finalization
  111. if isHost then UnregisterClass(ClassName, HInstance)
  112. else SetMemoryManager(OldMemMgr); // optional
  113. end.