Recent

Author Topic: use Application Memorymanager inside library  (Read 2100 times)

ϻαϻɾΣɀО

  • Jr. Member
  • **
  • Posts: 54
  • MaMrEzO
use Application Memorymanager inside library
« on: July 24, 2014, 01:28:54 am »
Hi
In some reason such as heavy transmision between Shared library and application I do somthings that described bellow:
After loading the library by the application, Application call a method inside the library and send it the application memory manager record!
And inside the library, any allocation or freeing memory use the application memory manager instead itself memory manager!
I tested it under Linux-x64 and heaptrc  report no any memory leak!

1. Unit dappmem that interface of application memeory manager inside the library.
Code: [Select]
unit dappmem;

{$mode objfpc}{$H+}

interface

function damGetMem(size:ptruint):pointer;
function damFreemem(p:pointer):ptruint;
function damAllocMem(Size:ptruint):pointer;
function damReAllocMem(var p:pointer;Size:ptruint):pointer;

procedure setAppMemMan(amm: PMemoryManager); cdecl;
procedure unsetAppMemMan; cdecl;

implementation

var
  appMemMan : PMemoryManager = nil;

function appmem(raiseE: Boolean = True): Boolean;
begin
  Result := appMemMan <> nil;
  //if not Result then
  //  raise Exception.;
end;

function damGetMem(size: ptruint): pointer;
begin
  if appmem then
    Result := appMemMan^.Getmem(size);
end;

function damFreemem(p: pointer): ptruint;
begin
  if appmem then
    Result := appMemMan^.Freemem(p);
end;

function damAllocMem(Size: ptruint): pointer;
begin
  if appmem then
    Result := appMemMan^.AllocMem(Size);
end;

function damReAllocMem(var p: pointer; Size: ptruint): pointer;
begin
  if appmem then
    Result := appMemMan^.ReAllocMem(p, Size);
end;

procedure setAppMemMan(amm: PMemoryManager); cdecl;
begin
  appMemMan := amm;
end;

procedure unsetAppMemMan; cdecl;
begin
  if appmem(False) then
    appMemMan := nil;
end;

end. 

2. An example of usage inside library:
Code: [Select]
function Hello(Data: PDSomeData): PDSomeData; cdecl;
var
  ln: UInt64;
  nName : PChar;
begin
  Result := Data;
  With TDSomeData(Result^) do
  begin
    ln := strlen(Name) + Length(' With this :|') + 1;
    damReAllocMem(Name, ln);//This line use the application memory manager instead of library memory manager!
    strcat(Name, PChar(' With this :|'));
    Bound += 12;
    Age -= 12;
  end;
end;

3. And finally application example code:
Code: [Select]
  GetMemoryManager(mMan);
  setAppMemMan(@mMan);//Make library able to use application memory manager!
  Res:= GetMem(SizeOf(TDSomeData));
  With Res^ do
  begin
    Name:= GetMem(Length('Ali zare') + 1);
    strcopy(Name, pchar('Ali zare'));
    Bound:= 12;
    Age:=32;
  end;
  Res := Hello(Res);
  with Res^ do
  WriteLn(Name, Age:5, Bound:6);
  Freemem(Res^.Name);
  Freemem(Res);
  unsetAppMemMan;//Just unset memory manager inside library!

So, If there is no any limition or reason to prevent me to do this, I will spend time to implement my plugins base of this.
And onething! This method gives the ability to allocate and freeing memory with single memory manager!
And Libraries made by this way never used by any other language like C.
« Last Edit: July 24, 2014, 01:32:44 am by ϻαϻɾΣɀО »
Debio-Sql is a new brand of GUI Database tool for the Firebird RDBMS.
http://debio-sql.ariaian.com/

 

TinyPortal © 2005-2018