--------------------------------------------------------------------------------
--                                                                            --
-- Copyright (C) 2004, RISC OS Ada Library (RASCAL) developers.               --
--                                                                            --
-- This library is free software; you can redistribute it and/or              --
-- modify it under the terms of the GNU Lesser General Public                 --
-- License as published by the Free Software Foundation; either               --
-- version 2.1 of the License, or (at your option) any later version.         --
--                                                                            --
-- This library is distributed in the hope that it will be useful,            --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of             --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU           --
-- Lesser General Public License for more details.                            --
--                                                                            --
-- You should have received a copy of the GNU Lesser General Public           --
-- License along with this library; if not, write to the Free Software        --
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA    --
--                                                                            --
--------------------------------------------------------------------------------

-- $Author$
-- $Date$
-- $Revision$

with RASCAL.Utility;   use RASCAL.Utility;
with RASCAL.Memory;    use RASCAL.Memory;
with RASCAL.OS;        use RASCAL.OS;

with System.Unsigned_Types;      use System.Unsigned_Types;

with Interfaces.C;     use Interfaces.C;
with Kernel;           use Kernel;
with System;           use System;



package body RASCAL.HeapSort is

   OS_HeapSort      : constant Interfaces.C.unsigned :=16#4f#;

   --
   --
   --
   procedure Sort (First_Element : in Address;
                   HowMany       : in Positive;
                   Element_Size  : in Positive) is

      Register : aliased Kernel.SWI_regs;
      Error    : Kernel.OSError_access;
      Buffer   : array (0..HowMany) of Integer;
      Adr_Flags: System.Unsigned_Types.Unsigned
               := Int_To_Unsigned(Adr_to_Integer(Buffer'address))+3*2**30;
   begin
      Register.R(0) := int(HowMany);
      Register.R(1) := int(Unsigned_to_Int(Adr_Flags));
      Register.R(2) := 5;
      Register.R(3) := 0;
      Register.R(4) := Adr_To_Int(First_Element);
      Register.R(5) := int(Element_Size);
      Register.R(6) := 0;
      Error := Kernel.SWI (OS_HeapSort, Register'Access, Register'Access);

      if Error /= null then
         pragma Debug(Report("HeapSort.Sort: " & To_Ada(Error.ErrMess)));
         OS.Raise_Error(Error);
      end if;
   end Sort;

   --

end RASCAL.HeapSort;
