unit dstack_oldstyle;

interface

type
PStack = ^TStack;
TStack = object
private
  FBuffer: array of Pointer;
  FTop: LongInt;
public
  procedure Init; inline;
  procedure Done; inline;
  procedure Push(P: Pointer);
  function Pop: Pointer;
  function Top: Pointer; inline;
  property Length: LongInt read FTop;
end;

implementation

procedure TStack.Init;
begin
  SetLength(FBuffer, 8);
  FTop := 0;
end;

procedure TStack.Done;
begin
  SetLength(FBuffer, 0);
end;

procedure TStack.Push(P: Pointer);
begin
  Inc(FTop);
  if FTop > High(FBuffer) then
    SetLength(FBuffer, System.Length(FBuffer) * 2);
  FBuffer[FTop] := P;
end;

function TStack.Pop: Pointer;
begin
  Result := Top;
  Dec(FTop);
end;

function TStack.Top: Pointer;
begin
  Result := FBuffer[FTop];
end;

end.
