| Nico Huber | f03ef4f | 2017-03-04 13:57:09 +0100 | [diff] [blame] | 1 | -- |
| 2 | -- Copyright (C) 2017 Nico Huber <nico.h@gmx.de> |
| 3 | -- |
| 4 | -- This program is free software; you can redistribute it and/or modify |
| 5 | -- it under the terms of the GNU General Public License as published by |
| 6 | -- the Free Software Foundation; either version 2 of the License, or |
| 7 | -- (at your option) any later version. |
| 8 | -- |
| 9 | -- This program is distributed in the hope that it will be useful, |
| 10 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | -- GNU General Public License for more details. |
| 13 | -- |
| 14 | |
| 15 | with Interfaces.C; |
| 16 | with Interfaces.C.Strings; |
| 17 | |
| 18 | with HW.Debug; |
| 19 | |
| 20 | use Interfaces.C; |
| 21 | use Interfaces.C.Strings; |
| 22 | |
| 23 | package body HW.File is |
| 24 | |
| 25 | READ : constant := 16#01#; |
| 26 | WRITE : constant := 16#02#; |
| 27 | |
| 28 | function c_map |
| Nico Huber | a43b1ee | 2017-07-09 15:40:25 +0200 | [diff] [blame] | 29 | (addr : out Word64; |
| 30 | path : chars_ptr; |
| Nico Huber | f03ef4f | 2017-03-04 13:57:09 +0100 | [diff] [blame] | 31 | len : Word32; |
| Nico Huber | 967dd0d | 2017-07-09 15:51:08 +0200 | [diff] [blame] | 32 | off : Word32; |
| Nico Huber | f03ef4f | 2017-03-04 13:57:09 +0100 | [diff] [blame] | 33 | mode : Word32; |
| 34 | copy : int) |
| 35 | return int; |
| 36 | pragma Import (C, c_map, "hw_file_map"); |
| 37 | |
| 38 | procedure Map |
| Nico Huber | a43b1ee | 2017-07-09 15:40:25 +0200 | [diff] [blame] | 39 | (Addr : out Word64; |
| 40 | Path : in String; |
| Nico Huber | 967dd0d | 2017-07-09 15:51:08 +0200 | [diff] [blame] | 41 | Len : in Natural := 0; |
| 42 | Offset : in Natural := 0; |
| Nico Huber | f03ef4f | 2017-03-04 13:57:09 +0100 | [diff] [blame] | 43 | Readable : in Boolean := False; |
| 44 | Writable : in Boolean := False; |
| 45 | Map_Copy : in Boolean := False; |
| 46 | Success : out Boolean) |
| 47 | is |
| 48 | use type HW.Word32; |
| 49 | |
| 50 | cpath : chars_ptr := New_String (Path); |
| 51 | ret : constant int := c_map |
| Nico Huber | a43b1ee | 2017-07-09 15:40:25 +0200 | [diff] [blame] | 52 | (addr => Addr, |
| 53 | path => cpath, |
| Nico Huber | f03ef4f | 2017-03-04 13:57:09 +0100 | [diff] [blame] | 54 | len => Word32 (Len), |
| Nico Huber | 967dd0d | 2017-07-09 15:51:08 +0200 | [diff] [blame] | 55 | off => Word32 (Offset), |
| Nico Huber | f03ef4f | 2017-03-04 13:57:09 +0100 | [diff] [blame] | 56 | mode => (if Readable then READ else 0) or |
| 57 | (if Writable then WRITE else 0), |
| 58 | copy => (if Map_Copy then 1 else 0)); |
| 59 | begin |
| Nico Huber | 32f1489 | 2017-06-18 02:35:37 +0200 | [diff] [blame] | 60 | pragma Warnings(GNAT, Off, """cpath"" modified*, but* referenced", |
| Nico Huber | f03ef4f | 2017-03-04 13:57:09 +0100 | [diff] [blame] | 61 | Reason => "Free() demands to set it to null_ptr"); |
| 62 | Free (cpath); |
| Nico Huber | 32f1489 | 2017-06-18 02:35:37 +0200 | [diff] [blame] | 63 | pragma Warnings(GNAT, On, """cpath"" modified*, but* referenced"); |
| Nico Huber | f03ef4f | 2017-03-04 13:57:09 +0100 | [diff] [blame] | 64 | Success := ret = 0; |
| 65 | |
| 66 | pragma Debug (not Success, Debug.Put ("Mapping failed: ")); |
| 67 | pragma Debug (not Success, Debug.Put_Int32 (Int32 (ret))); |
| 68 | pragma Debug (not Success, Debug.New_Line); |
| 69 | end Map; |
| 70 | |
| 71 | end HW.File; |