Delphi: FastShareMem

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

01unit FastShareMem;
02 
03// By Frederic Hannes (http://ibeblog.com)
04// Based on FastShareMem by Emil M. Santos (http://www.codexterity.com)
05 
06interface
07 
08var
09  GetAllocMemCount: function: Integer;
10  GetAllocMemSize: function: Integer;
11 
12implementation
13 
14uses Windows;
15 
16const
17  ClassName = '_com.codexterity.fastsharemem.dataclass';
18 
19type
20  TFastShareMem = record
21    MemoryManager: TMemoryManagerEx;
22    _GetAllocMemSize: function: Integer;
23    _GetAllocMemCount: function: Integer;
24  end;
25 
26function _GetAllocMemCount: Integer;
27var
28  State: TMemoryManagerState;
29  i: Integer;
30begin
31  GetMemoryManagerState(State);
32  Result := 0;
33  for i := 0 to High(State.SmallBlockTypeStates) do
34    Inc(Result, State.SmallBlockTypeStates[i].AllocatedBlockCount);
35  Inc(Result, State.AllocatedMediumBlockCount + State.AllocatedLargeBlockCount);
36end;
37 
38function _GetAllocMemSize: Integer;
39var
40  State: TMemoryManagerState;
41  i: Integer;
42begin
43  GetMemoryManagerState(State);
44  Result := 0;
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);
50end;
51 
52var
53  wc: TWndClassA;
54  IsHost: Boolean;
55  ShareMem: TFastShareMem;
56 
57initialization
58  if not GetClassInfoA(HInstance, ClassName, wc) then
59  begin
60    GetMemoryManager(ShareMem.MemoryManager);
61    ShareMem._GetAllocMemCount := @_GetAllocMemCount;
62    ShareMem._GetAllocMemSize := @_GetAllocMemSize;
63    GetAllocMemCount := @_GetAllocMemCount;
64    GetAllocMemSize := @_GetAllocMemSize;
65 
66    FillChar(wc, SizeOf(wc), 0);
67    wc.lpszClassName := ClassName;
68    wc.style := CS_GLOBALCLASS;
69    wc.hInstance := HInstance;
70    wc.lpfnWndProc := @ShareMem;
71 
72    if RegisterClassA(wc) = 0 then
73    begin
74      MessageBox(0, 'Shared Memory Allocator setup failed: Cannot register class.',
75        'FastShareMem', 0);
76      Halt;
77    end;
78 
79    IsHost := True;
80  end else begin
81    SetMemoryManager(TFastShareMem(wc.lpfnWndProc^).MemoryManager);
82    GetAllocMemCount := TFastShareMem(wc.lpfnWndProc^)._GetAllocMemCount;
83    GetAllocMemSize := TFastShareMem(wc.lpfnWndProc^)._GetAllocMemSize;
84    IsHost := False;
85  end;
86finalization
87  if IsHost then
88    UnregisterClassA(ClassName, HInstance);
89end.

2 thoughts on “Delphi: FastShareMem

  1. we need to declare again the unit in both? Exe and DLL?

    • Freddy say:

      Yes, it has to be declared both in the dll and exe, make sure it’s the first unit declared in the project file.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.