Differenze tra le versioni di "Mmap ()"

Da Gambas-it.org - Wikipedia.
 
(5 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
 
La funzione della libreria di C
 
La funzione della libreria di C
  void *mmap (void *__addr, size_t __len, int __prot, int __flags, int __fd, __off_t __offset)
+
  void *'''mmap''' (void *__addr, size_t __len, int __prot, int __flags, int __fd, __off_t __offset)
 
crea una nuova mappatura nello spazio di indirizzi virtuali del processo chiamante. L'indirizzo di partenza per la nuova mappatura è specificato in ''addr''. L'argomento ''length'' specifica la lunghezza della mappatura.
 
crea una nuova mappatura nello spazio di indirizzi virtuali del processo chiamante. L'indirizzo di partenza per la nuova mappatura è specificato in ''addr''. L'argomento ''length'' specifica la lunghezza della mappatura.
  
Riga 8: Riga 8:
  
  
L'esempio che segue scrive in console una parte di un file. L'intervallo di byte da mostrare in console viene specificato tramite la variabile ''initium'' ed il valore della variabile ''lung''. Il programma crea una mappatura della memoria delle pagine richieste del file e successivamente scrive in console i byte prescelti:
+
Nel seguente esempio viene mappato un file per la lettura di tutti i suoi dati:
 
  Library "libc:6"
 
  Library "libc:6"
 
   
 
   
 +
Private Const MAP_FAILED As Integer = -1
 +
Private Const PROT_READ As Byte = 1
 +
Private Const MAP_PRIVATE As Byte = 2
 +
 +
<FONT color=gray>' ''void *mmap (void *__addr, size_t __len, int __prot, int __flags, int __fd, __off_t __offset)''
 +
' ''Map addresses starting near ADDR and extending for LEN bytes.''</font>
 +
Private Extern <FONT color=#B22222>mmap</font>(__addr As Pointer, __len As Long, __prot As Integer, __flags As Integer, __fd As Integer, __offset As Long) As Pointer
 +
 +
<FONT color=gray>' ''void *unmmap (void *__addr, size_t __len)''
 +
' ''Deallocate any mapping for the region starting at ADDR and extending LEN bytes.''</font>
 +
Private Extern unmmap(__addr As Pointer, __len As Long) As Integer
 +
 +
 +
'''Public Sub Main()
 +
 
 +
  Dim percorsoFile As String
 +
  Dim fl As File
 +
  Dim lun As Long
 +
  Dim map As Pointer
 +
  Dim i As Integer
 +
 +
  percorsoFile = "<FONT color=gray>''/percorso/del/file''</font>"
 +
  Print "File da leggere:    "; percorsoFile
 +
 
 +
  fl = Open percorsoFile For Read
 +
 
 +
  lun = Lof(fl)
 +
  Print "Lunghezza del file: "; lun; " byte"
 +
 
 +
  map = <FONT color=#B22222>mmap</font>(0, lun, PROT_READ, MAP_PRIVATE, fl.Handle, 0)
 +
  If Int@(map) == MAP_FAILED Then Error.Raise("Impossibile mappare il file !")
 +
 
 +
  For i = 0 To lun - 1
 +
    Print i, Hex(Byte@(map + i), 2)
 +
  Next
 +
 
 +
  i = munmap(map, lun)
 +
  If i == MAP_FAILED Then Error.Raise("Impossibile demappare il file !")
 +
 
 +
  fl.Close
 +
 
 +
'''End'''
 +
 +
In quest'altro esempio volendo leggere soltanto una parte del file, viene mappata solo quella parte di file. L'intervallo di byte da mostrare in console viene specificato tramite la variabile ''initium'' ed il valore della variabile ''lung''. Il programma crea una mappatura della memoria delle pagine richieste del file e successivamente scrive in console i byte prescelti:
 +
Library "libc:6"
 +
 +
Private Const MAP_FAILED As Integer = -1
 +
Private Const PROT_READ As Byte = 1
 +
Private Const MAP_PRIVATE As Byte = 2
 
  Private Const _SC_PAGE_SIZE As Byte = 30   
 
  Private Const _SC_PAGE_SIZE As Byte = 30   
  Private Const PROT_READ As Byte = 1          <FONT color=gray>' ''Data can be read''</font>
+
   
Private Const MAP_PRIVATE As Byte = 2        <FONT color=gray>' ''Changes are private''</font>
 
Private Const MAP_FAILED As Integer = -1    <FONT color=gray>' ''Return value of `mmap' in case of an error''</font>
 
 
 
 
  <FONT color=gray>' ''long sysconf(int name)''
 
  <FONT color=gray>' ''long sysconf(int name)''
  ' ''Restituisce il valore del parametro di sistema "name".''</font>
+
  ' ''Get the value of the system variable NAME.''</font>
 
  Private Extern sysconf(name As Integer) As Long   
 
  Private Extern sysconf(name As Integer) As Long   
 
        
 
        
Riga 29: Riga 75:
 
   Dim percorsoFile As String
 
   Dim percorsoFile As String
 
   Dim fl as File
 
   Dim fl as File
   Dim addr As Pointer   
+
   Dim map As Pointer   
 
   Dim lungh_file, lung, i As Integer   
 
   Dim lungh_file, lung, i As Integer   
 
   Dim initium, pa_offset As Long   
 
   Dim initium, pa_offset As Long   
  Dim st As Stream 
 
  Dim b As Byte 
 
     
 
  percorsoFile = "<FONT color=gray>''/percorso/del/file/da/leggere''</font>" 
 
     
 
  lungh_file = Stat(percorsoFile).Size 
 
     
 
  initium = 8    <FONT color=gray>' ''numero d'indice 'esemplificativo' di partenza dei byte da leggere''</font>
 
 
   
 
   
  lung = 5        <FONT color=gray>' ''quantità 'esemplificativa' dei byte da leggere''</font>
+
  percorsoFile = "<FONT color=gray>''/percorso/del/file''</font>" 
 +
   
 +
  initium = 8    <FONT color=gray>' ''Numero d'indice di partenza dei byte da leggere''</font>
 +
  lung = 5        <FONT color=gray>' ''Quantità dei byte da leggere''</font>
 +
   
 +
  fl = Open percorsoFile For Read 
 +
  lungh_file = lof(fl)
 +
   
 +
  pa_offset = initium And (Not (sysconf(_SC_PAGE_SIZE) - 1)) 
 +
   
 +
  If initium >= lungh_file Then Error.Raise("Il valore della variabile 'initium' ha oltrepassato la fine del file !") 
 
        
 
        
  fl = Open percorsoFile For Read 
+
  If lung > 0 Then   
  If IsNull(fl) Then Error.Raise("Impossibile aprire il file !") 
+
    If initium + lung > lungh_file Then lung = lungh_file - initium   
     
+
  Else
  pa_offset = initium And (Not (sysconf(_SC_PAGE_SIZE) - 1)) 
+
    lung = lungh_file - initium   
     
+
  Endif
  If initium >= lungh_file Then Error.Raise("Il valore dell variabile 'initium' ha oltrepassato la fine del file !") 
+
   
     
+
  map = <FONT color=#B22222>mmap</font>(0, lung + initium - pa_offset, PROT_READ, MAP_PRIVATE, fl.Handle, pa_offset)
  If lung > 0 Then   
+
  If Int@(map) == MAP_FAILED Then Error.Raise("Impossibile mappare il file !")
    If initium + lung > lungh_file Then lung = lungh_file - initium   
+
   
  Else
+
  <FONT color=gray>' ''Dereferenziamo il Puntatore "map" per leggere i dati del file:''</font>
    lung = lungh_file - initium   
+
  For i = 0 To lung - 1   
  Endif
+
    Print i + initium, Hex(Byte@(map + (initium - pa_offset + i)), 2)
     
+
  Next
  addr = <FONT color=#B22222>mmap</font>(0, lung + initium - pa_offset, PROT_READ, MAP_PRIVATE, fl.Handle, pa_offset)
+
 
  If addr = MAP_FAILED Then Error.Raise("Errore alla funzione 'mmap()' !")
+
  i = munmap(map, lung + initium - pa_offset)
     
+
  If i == MAP_FAILED Then Error.Raise("Impossibile demappare il file !")
  <FONT color=gray>' ''Dereferenziamo il 'Puntatore' "addr" mediante i "Memory Stream":''</font>
 
  st = Memory addr For Read 
 
     
 
  For i = 0 To lung - 1   
 
    Seek #st, (initium - pa_offset) + i
 
    Read #st,
 
    Print b
 
  Next
 
 
   
 
   
  st.Close
+
  fl.Close
  fl.Close
+
   
     
 
 
  '''End'''
 
  '''End'''
 +
 +
 +
 +
=Riferimenti=
 +
* http://man7.org/linux/man-pages/man2/mmap.2.html
 +
* http://www.gnu.org/software/libc/manual/html_node/Memory_002dmapped-I_002fO.html
 +
* https://www.safaribooksonline.com/library/view/linux-system-programming/0596009585/ch04s03.html

Versione attuale delle 07:54, 4 set 2022

La funzione della libreria di C

void *mmap (void *__addr, size_t __len, int __prot, int __flags, int __fd, __off_t __offset)

crea una nuova mappatura nello spazio di indirizzi virtuali del processo chiamante. L'indirizzo di partenza per la nuova mappatura è specificato in addr. L'argomento length specifica la lunghezza della mappatura.


Volendola utilizzare in Gambas, bisognerà dichiararla con Extern, nonché bisognerà dichiarare la libreria di C: libc.so.6, nella quale la funzione è contenuta:

Private Extern mmap(__addr As Pointer, __len As Long, __prot As Integer, __flags As Integer, __fd As Integer, __offset As Long) As Pointer In "libc:6"


Nel seguente esempio viene mappato un file per la lettura di tutti i suoi dati:

Library "libc:6"

Private Const MAP_FAILED As Integer = -1
Private Const PROT_READ As Byte = 1
Private Const MAP_PRIVATE As Byte = 2

' void *mmap (void *__addr, size_t __len, int __prot, int __flags, int __fd, __off_t __offset)
' Map addresses starting near ADDR and extending for LEN bytes.
Private Extern mmap(__addr As Pointer, __len As Long, __prot As Integer, __flags As Integer, __fd As Integer, __offset As Long) As Pointer

' void *unmmap (void *__addr, size_t __len)
' Deallocate any mapping for the region starting at ADDR and extending LEN bytes.
Private Extern unmmap(__addr As Pointer, __len As Long) As Integer


Public Sub Main()
 
 Dim percorsoFile As String
 Dim fl As File
 Dim lun As Long
 Dim map As Pointer
 Dim i As Integer

 percorsoFile = "/percorso/del/file"
 Print "File da leggere:    "; percorsoFile
 
 fl = Open percorsoFile For Read
 
 lun = Lof(fl)
 Print "Lunghezza del file: "; lun; " byte"
 
 map = mmap(0, lun, PROT_READ, MAP_PRIVATE, fl.Handle, 0)
 If Int@(map) == MAP_FAILED Then Error.Raise("Impossibile mappare il file !")
 
 For i = 0 To lun - 1
   Print i, Hex(Byte@(map + i), 2)
 Next
 
 i = munmap(map, lun)
 If i == MAP_FAILED Then Error.Raise("Impossibile demappare il file !")
 
 fl.Close
 
End

In quest'altro esempio volendo leggere soltanto una parte del file, viene mappata solo quella parte di file. L'intervallo di byte da mostrare in console viene specificato tramite la variabile initium ed il valore della variabile lung. Il programma crea una mappatura della memoria delle pagine richieste del file e successivamente scrive in console i byte prescelti:

Library "libc:6"

Private Const MAP_FAILED As Integer = -1
Private Const PROT_READ As Byte = 1
Private Const MAP_PRIVATE As Byte = 2
Private Const _SC_PAGE_SIZE As Byte = 30  

' long sysconf(int name)
' Get the value of the system variable NAME.
Private Extern sysconf(name As Integer) As Long  
     
' void *mmap (void *__addr, size_t __len, int __prot, int __flags, int __fd, __off_t __offset)
' Map addresses starting near ADDR and extending for LEN bytes.
Private Extern mmap(__addr As Pointer, __len As Long, __prot As Integer, __flags As Integer, __fd As Integer, __offset As Long) As Pointer
     
     
Public Sub Main()  
     
 Dim percorsoFile As String
 Dim fl as File
 Dim map As Pointer  
 Dim lungh_file, lung, i As Integer  
 Dim initium, pa_offset As Long  

 percorsoFile = "/percorso/del/file"  
    
 initium = 8     ' Numero d'indice di partenza dei byte da leggere
 lung = 5        ' Quantità dei byte da leggere
    
 fl = Open percorsoFile For Read  
 lungh_file = lof(fl)
    
 pa_offset = initium And (Not (sysconf(_SC_PAGE_SIZE) - 1))  
    
 If initium >= lungh_file Then Error.Raise("Il valore della variabile 'initium' ha oltrepassato la fine del file !")  
     
 If lung > 0 Then  
   If initium + lung > lungh_file Then lung = lungh_file - initium  
 Else
   lung = lungh_file - initium  
 Endif
    
 map = mmap(0, lung + initium - pa_offset, PROT_READ, MAP_PRIVATE, fl.Handle, pa_offset)
 If Int@(map) == MAP_FAILED Then Error.Raise("Impossibile mappare il file !")
    
' Dereferenziamo il Puntatore "map" per leggere i dati del file:
 For i = 0 To lung - 1  
   Print i + initium, Hex(Byte@(map + (initium - pa_offset + i)), 2)
 Next
 
 i = munmap(map, lung + initium - pa_offset)
 If i == MAP_FAILED Then Error.Raise("Impossibile demappare il file !")

 fl.Close
    
End


Riferimenti