Arena storage pool: Difference between revisions

Content added Content deleted
m (→‎{{header|Raku}}: Fix Perl 6 -> Raku)
(Added Delphi example)
Line 383: Line 383:


} // Here my_pool goes out of scope, deallocating the memory for p1, p2 and p3</lang>
} // Here my_pool goes out of scope, deallocating the memory for p1, p2 and p3</lang>
=={{header|Delphi}}==
{{libheader| Winapi.Windows}}
{{libheader| System.SysUtils}}
{{libheader| system.generics.collections}}
<lang Delphi>
program Arena_storage_pool;

{$APPTYPE CONSOLE}

uses
Winapi.Windows,
System.SysUtils,
system.generics.collections;

type
TPool = class
private
FStorage: TList<Pointer>;
public
constructor Create;
destructor Destroy; override;
function Allocate(aSize: Integer): Pointer;
function Release(P: Pointer): Integer;
end;

{ TPool }

function TPool.Allocate(aSize: Integer): Pointer;
begin
Result := GetMemory(aSize);
if Assigned(Result) then
FStorage.Add(Result);
end;

constructor TPool.Create;
begin
FStorage := TList<Pointer>.Create;
end;

destructor TPool.Destroy;
var
p: Pointer;
begin
while FStorage.Count > 0 do
begin
p := FStorage[0];
Release(p);
end;
FStorage.Free;
inherited;
end;

function TPool.Release(P: Pointer): Integer;
var
index: Integer;
begin
index := FStorage.IndexOf(P);
if index > -1 then
FStorage.Delete(index);
FreeMemory(P)
end;

var
Manager: TPool;
int1, int2: PInteger;
str: PChar;

begin
Manager := TPool.Create;
int1 := Manager.Allocate(sizeof(Integer));
int1^ := 5;

int2 := Manager.Allocate(sizeof(Integer));
int2^ := 3;

writeln('Allocate at addres ', cardinal(int1).ToHexString, ' with value of ', int1^);
writeln('Allocate at addres ', cardinal(int2).ToHexString, ' with value of ', int2^);

Manager.Free;
readln;
end.</lang>
{{out}}
<pre>Allocate at addres 026788D0 with value of 5
Allocate at addres 026788E0 with value of 3</pre>


=={{header|Erlang}}==
=={{header|Erlang}}==