Differenze tra le versioni di "Mmap ()"

Da Gambas-it.org - Wikipedia.
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     <FONT color=gray>' ''Return value of `mmap' in case of an error''</font>
+
  Private Const MAP_FAILED As Integer = -1
  Private Const PROT_READ As Byte = 1          <FONT color=gray>' ''Data can be read''</font>  
+
Private Const PROT_READ As Byte = 1
  Private Const MAP_PRIVATE As Byte = 2        <FONT color=gray>' ''Changes are private''</font>
+
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   
 
   
 
   
Riga 29: Riga 76:
 
   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  
+
   percorsoFile = "<FONT color=gray>''/percorso/del/file''</font>"  
 
        
 
        
   initium = 8    <FONT color=gray>' ''numero d'indice 'esemplificativo' di partenza dei byte da leggere''</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>
   lung = 5        <FONT color=gray>' ''quantità 'esemplificativa' dei byte da leggere''</font>
 
 
        
 
        
 
   fl = Open percorsoFile For Read   
 
   fl = Open percorsoFile For Read   
   If IsNull(fl) Then Error.Raise("Impossibile aprire il file !") 
+
   lungh_file = lof(fl)
 
        
 
        
 
   pa_offset = initium And (Not (sysconf(_SC_PAGE_SIZE) - 1))   
 
   pa_offset = initium And (Not (sysconf(_SC_PAGE_SIZE) - 1))   
 
        
 
        
   If initium >= lungh_file Then Error.Raise("Il valore dell variabile 'initium' ha oltrepassato la fine del file !")   
+
   If initium >= lungh_file Then Error.Raise("Il valore della variabile 'initium' ha oltrepassato la fine del file !")   
 
        
 
        
 
   If lung > 0 Then   
 
   If lung > 0 Then   
Riga 56: Riga 98:
 
   Endif
 
   Endif
 
        
 
        
   addr = <FONT color=#B22222>mmap</font>(0, lung + initium - pa_offset, PROT_READ, MAP_PRIVATE, fl.Handle, pa_offset)
+
   map = <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()' !")
+
   If Int@(map) = MAP_FAILED Then Error.Raise("Impossibile mappare il file !")
     
 
<FONT color=gray>' ''Dereferenziamo il 'Puntatore' "addr" mediante i "Memory Stream":''</font>
 
  st = Memory addr For Read 
 
 
        
 
        
 +
<FONT color=gray>' ''Dereferenziamo il Puntatore "map" per leggere i dati del file:''</font>
 
   For i = 0 To lung - 1   
 
   For i = 0 To lung - 1   
     Seek #st, (initium - pa_offset) + i
+
     Print i + initium, Hex(Byte@(map + (initium - pa_offset + i)), 2)
    Read #st,
 
    Print b
 
 
   Next
 
   Next
 +
 
 +
  i = munmap(map, lung + initium - pa_offset)
 +
  If i = MAP_FAILED Then Error.Raise("Impossibile demappare il file !")
 
   
 
   
  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

Versione delle 03:02, 31 gen 2017

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)
' Restituisce il valore del parametro di sistema "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