4.2.20. GNATCOLL.Mmap.System (unix)

package GNATCOLL.Mmap.System is

   type System_File is record
      Fd     : GNAT.OS_Lib.File_Descriptor;

      Mapped : Boolean;
      --  Whether mapping is requested by the user and available on the system

      Write  : Boolean;
      --  Whether this file can be written to

      Length : File_Size;
      --  Length of the file. Used to know what can be mapped in the file
   end record;

   type System_Mapping is record
      Address : Standard.System.Address;
      Length  : File_Size;
   end record;

   Invalid_System_File    : constant System_File :=
     (GNAT.OS_Lib.Invalid_FD, False, False, 0);
   Invalid_System_Mapping : constant System_Mapping :=
     (Standard.System.Null_Address, 0);

   function Open_Read
     (Filename              : String;
      Use_Mmap_If_Available : Boolean := True) return System_File;
   --  Open a file for reading and return the corresponding System_File. Raise
   --  a Ada.IO_Exceptions.Name_Error if unsuccessful.

   function Open_Write
     (Filename              : String;
      Use_Mmap_If_Available : Boolean := True) return System_File;
   --  Likewise for writing to a file

   procedure Close (File : in out System_File);
   --  Close a system file

   function Read_From_Disk
     (File           : System_File;
      Offset, Length : File_Size) return GNAT.Strings.String_Access;
   --  Read a fragment of a file. It is up to the caller to free the result
   --  when done with it.
   --  Doesn't use mmap.

   procedure Write_To_Disk
     (File           : System_File;
      Offset, Length : File_Size;
      Buffer         : GNAT.Strings.String_Access);
   --  Write some content to a fragment of a file

   procedure Create_Mapping
     (File           : System_File;
      Offset, Length : in out File_Size;
      Mutable        : Boolean;
      Mapping        : out System_Mapping;
      Advice         : Use_Advice := Use_Normal);
   --  Create a memory mapping for the given File, for the area starting at
   --  Offset and containing Length bytes. Store it to Mapping.
   --  Note that Offset and Length may be modified according to the system
   --  needs (for boundaries, for instance). The caller must cope with actually
   --  wider mapped areas.

   procedure Dispose_Mapping
     (Mapping : in out System_Mapping);
   --  Unmap a previously-created mapping

   function Get_Page_Size return File_Size;
   --  Return the number of bytes in a system page.

end GNATCOLL.Mmap.System;