Mmap (): differenze tra le versioni
Vai alla navigazione
Vai alla ricerca
(Creata pagina con "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 indirizz...") |
Nessun oggetto della modifica |
||
| Riga 9: | Riga 9: | ||
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: | 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: | ||
Library "libc:6" | |||
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 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_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> | 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)'' | ||
Versione delle 10:07, 25 dic 2016
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"
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:
Library "libc:6"
Private Const _SC_PAGE_SIZE As Byte = 30
Private Const PROT_READ As Byte = 1 ' Data can be read
Private Const MAP_PRIVATE As Byte = 2 ' Changes are private
Private Const MAP_FAILED As Integer = -1 ' Return value of `mmap' in case of an error
' 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 addr As Pointer
Dim lungh_file, lung, i As Integer
Dim initium, pa_offset As Long
Dim st As Stream
Dim b As Byte
percorsoFile = "/percorso/del/file/da/leggere"
lungh_file = Stat(percorsoFile).Size
initium = 8 ' numero d'indice 'esemplificativo' di partenza dei byte da leggere
lung = 5 ' quantità 'esemplificativa' dei byte da leggere
fl = Open percorsoFile For Read
If IsNull(fl) Then Error.Raise("Impossibile aprire il file !")
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 lung > 0 Then
If initium + lung > lungh_file Then lung = lungh_file - initium
Else
lung = lungh_file - initium
Endif
addr = mmap(0, lung + initium - pa_offset, PROT_READ, MAP_PRIVATE, fl.Handle, pa_offset)
If addr = MAP_FAILED Then Error.Raise("Errore alla funzione 'mmap()' !")
' Dereferenziamo il 'Puntatore' "addr" mediante i "Memory Stream":
st = Memory addr For Read
For i = 0 To lung - 1
Seek #st, (initium - pa_offset) + i
Read #st, b
Print b
Next
st.Close
fl.Close
End