FastShareMem is a very lightweight memory manager for Delphi that was designed to replace the default memory manager that required the inclusion of a dll with applications. However, due to the addition of unicode support to Delphi 2009/2010, the include is no longer compatible with dlls compiled with older versions of Delphi using it. So I’ve written a copy that that will work in Delphi 2009/2010 and still is compatible with the older versions. Due take in mind this version of the include isn’t written for older versions of Delphi. For an older version use the original you can find here: http://www.codexterity.com/fastsharemem.htm
09 | GetAllocMemCount: function : Integer ; |
10 | GetAllocMemSize: function : Integer ; |
17 | ClassName = '_com.codexterity.fastsharemem.dataclass' ; |
20 | TFastShareMem = record |
21 | MemoryManager: TMemoryManagerEx; |
22 | _GetAllocMemSize: function : Integer ; |
23 | _GetAllocMemCount: function : Integer ; |
26 | function _GetAllocMemCount: Integer ; |
28 | State: TMemoryManagerState; |
31 | GetMemoryManagerState(State); |
33 | for i := 0 to High(State . SmallBlockTypeStates) do |
34 | Inc(Result, State . SmallBlockTypeStates[i].AllocatedBlockCount); |
35 | Inc(Result, State . AllocatedMediumBlockCount + State . AllocatedLargeBlockCount); |
38 | function _GetAllocMemSize: Integer ; |
40 | State: TMemoryManagerState; |
43 | GetMemoryManagerState(State); |
45 | for i := 0 to High(State . SmallBlockTypeStates) do |
46 | Inc(Result, State . SmallBlockTypeStates[i].AllocatedBlockCount * |
47 | State . SmallBlockTypeStates[i].UseableBlockSize); |
48 | Inc(Result, State . TotalAllocatedMediumBlockSize + |
49 | State . TotalAllocatedLargeBlockSize); |
55 | ShareMem: TFastShareMem; |
58 | if not GetClassInfoA(HInstance, ClassName, wc) then |
60 | GetMemoryManager(ShareMem . MemoryManager); |
61 | ShareMem . _GetAllocMemCount := @_GetAllocMemCount; |
62 | ShareMem . _GetAllocMemSize := @_GetAllocMemSize; |
63 | GetAllocMemCount := @_GetAllocMemCount; |
64 | GetAllocMemSize := @_GetAllocMemSize; |
66 | FillChar(wc, SizeOf(wc), 0 ); |
67 | wc . lpszClassName := ClassName; |
68 | wc . style := CS_GLOBALCLASS; |
69 | wc . hInstance := HInstance; |
70 | wc . lpfnWndProc := @ShareMem; |
72 | if RegisterClassA(wc) = 0 then |
74 | MessageBox( 0 , 'Shared Memory Allocator setup failed: Cannot register class.' , |
81 | SetMemoryManager(TFastShareMem(wc . lpfnWndProc^).MemoryManager); |
82 | GetAllocMemCount := TFastShareMem(wc . lpfnWndProc^)._GetAllocMemCount; |
83 | GetAllocMemSize := TFastShareMem(wc . lpfnWndProc^)._GetAllocMemSize; |
88 | UnregisterClassA(ClassName, HInstance); |
we need to declare again the unit in both? Exe and DLL?
Yes, it has to be declared both in the dll and exe, make sure it’s the first unit declared in the project file.