// deprecated, see collection ddefaultallocator
{$MODE OBJFPC}
unit dallocator;

interface

type
PAllocator = ^TAllocator;
TAllocator = object
public
  constructor Init;
  destructor Done; virtual;
  function Alloc(Size: LongInt): Pointer; virtual;
  procedure Dealloc(P: Pointer); virtual;
  function Realloc(P: Pointer; NewSize: LongInt): Pointer; virtual;
end;

TFpcAllocator = object(TAllocator)
public
  function Alloc(Size: LongInt): Pointer; virtual;
  procedure Dealloc(P: Pointer); virtual;
  function Realloc(P: Pointer; NewSize: LongInt): Pointer; virtual;
end;

function GetDefaultAllocator: PAllocator;
function SetDefaultAllocator(Allocator: PAllocator): PAllocator;

implementation

constructor TAllocator.Init;
begin
end;

destructor TAllocator.Done;
begin
end;

function TAllocator.Alloc(Size: LongInt): Pointer;
begin
  Result := nil;
end;

procedure TAllocator.Dealloc(P: Pointer);
begin
end;

function TAllocator.Realloc(P: Pointer; NewSize: LongInt): Pointer;
begin
  Result := nil;
end;

function TFpcAllocator.Alloc(Size: LongInt): Pointer;
begin
  Result := GetMem(Size);
end;

procedure TFpcAllocator.Dealloc(P: Pointer);
begin
  FreeMem(P);
end;

function TFpcAllocator.Realloc(P: Pointer; NewSize: LongInt): Pointer;
begin
  Result := ReallocMem(P, NewSize);
end;

var
  DefaultAllocator: PAllocator;
  FpcAllocator: TFpcAllocator;

function GetDefaultAllocator: PAllocator;
begin
  Result := DefaultAllocator;
end;

function SetDefaultAllocator(Allocator: PAllocator): PAllocator;
begin
  Result := DefaultAllocator;
  DefaultAllocator := Allocator;
end;

initialization
  FpcAllocator.Init;
  DefaultAllocator := @FpcAllocator;
finalization
  FpcAllocator.Done;
end.
