@@ -913,41 +913,81 @@ pub fn page_size() -> uint {
913
913
}
914
914
}
915
915
916
+ /// A memory mapped file or chunk of memory. This is a very system-specific interface to the OS's
917
+ /// memory mapping facilities (`mmap` on POSIX, `VirtualAlloc`/`CreateFileMapping` on win32). It
918
+ /// makes no attempt at abstracting platform differences, besides in error values returned. Consider
919
+ /// yourself warned.
920
+ ///
921
+ /// The memory map is released (unmapped) when the destructor is run, so don't let it leave scope by
922
+ /// accident if you want it to stick around.
916
923
pub struct MemoryMap {
924
+ /// Pointer to the memory created or modified by this map.
917
925
data : * mut u8 ,
926
+ /// Number of bytes this map applies to
918
927
len : size_t ,
928
+ /// Type of mapping
919
929
kind : MemoryMapKind
920
930
}
921
931
932
+ /// Type of memory map
922
933
pub enum MemoryMapKind {
934
+ /// Memory-mapped file. On Windows, the inner pointer is a handle to the mapping, and
935
+ /// corresponds to `CreateFileMapping`. Elsewhere, it is null.
923
936
MapFile ( * c_void ) ,
937
+ /// Virtual memory map. Usually used to change the permissions of a given chunk of memory.
938
+ /// Corresponds to `VirtualAlloc` on Windows.
924
939
MapVirtual
925
940
}
926
941
942
+ /// Options the memory map is created with
927
943
pub enum MapOption {
944
+ /// The memory should be readable
928
945
MapReadable ,
946
+ /// The memory should be writable
929
947
MapWritable ,
948
+ /// The memory should be executable
930
949
MapExecutable ,
950
+ /// Create a map for a specific address range. Corresponds to `MAP_FIXED` on POSIX.
931
951
MapAddr ( * c_void ) ,
952
+ /// Create a memory mapping for a file with a given fd.
932
953
MapFd ( c_int ) ,
954
+ /// When using `MapFd`, the start of the map is `uint` bytes from the start of the file.
933
955
MapOffset ( uint )
934
956
}
935
957
958
+ /// Possible errors when creating a map.
936
959
pub enum MapError {
937
- // Linux-specific errors
960
+ /// ## The following are POSIX-specific
961
+ ///
962
+ /// fd was not open for reading or, if using `MapWritable`, was not open for writing.
938
963
ErrFdNotAvail ,
964
+ /// fd was not valid
939
965
ErrInvalidFd ,
966
+ /// Either the address given by `MapAddr` or offset given by `MapOffset` was not a multiple of
967
+ /// `MemoryMap::granularity` (unaligned to page size).
940
968
ErrUnaligned ,
969
+ /// With `MapFd`, the fd does not support mapping.
941
970
ErrNoMapSupport ,
971
+ /// If using `MapAddr`, the address + `min_len` was outside of the process's address space. If
972
+ /// using `MapFd`, the target of the fd didn't have enough resources to fulfill the request.
942
973
ErrNoMem ,
974
+ /// Unrecognized error. The inner value is the unrecognized errno.
943
975
ErrUnknown ( libc:: c_int ) ,
944
-
945
- // Windows-specific errors
976
+ /// ## The following are win32-specific
977
+ ///
978
+ /// Unsupported combination of protection flags (`MapReadable`/`MapWritable`/`MapExecutable`).
946
979
ErrUnsupProt ,
980
+ /// When using `MapFd`, `MapOffset` was given (Windows does not support this at all)
947
981
ErrUnsupOffset ,
982
+ /// When using `MapFd`, there was already a mapping to the file.
948
983
ErrAlreadyExists ,
984
+ /// Unrecognized error from `VirtualAlloc`. The inner value is the return value of GetLastError.
949
985
ErrVirtualAlloc ( uint ) ,
986
+ /// Unrecognized error from `CreateFileMapping`. The inner value is the return value of
987
+ /// `GetLastError`.
950
988
ErrCreateFileMappingW ( uint ) ,
989
+ /// Unrecognized error from `MapViewOfFile`. The inner value is the return value of
990
+ /// `GetLastError`.
951
991
ErrMapViewOfFile ( uint )
952
992
}
953
993
@@ -973,6 +1013,7 @@ impl to_str::ToStr for MapError {
973
1013
974
1014
#[ cfg( unix) ]
975
1015
impl MemoryMap {
1016
+ /// Create a new mapping with the given `options`, at least `min_len` bytes long.
976
1017
pub fn new( min_len : uint, options : & [ MapOption ] ) -> Result < MemoryMap , MapError > {
977
1018
#[ fixed_stack_segment] ; #[ inline( never) ] ;
978
1019
@@ -1028,13 +1069,15 @@ impl MemoryMap {
1028
1069
}
1029
1070
}
1030
1071
1072
+ /// Granularity that the offset or address must be for `MapOffset` and `MapAddr` respectively.
1031
1073
pub fn granularity( ) -> uint {
1032
1074
page_size( )
1033
1075
}
1034
1076
}
1035
1077
1036
1078
#[ cfg( unix) ]
1037
1079
impl Drop for MemoryMap {
1080
+ /// Unmap the mapping. Fails the task if `munmap` fails.
1038
1081
fn drop( & mut self) {
1039
1082
#[ fixed_stack_segment] ; #[ inline( never) ] ;
1040
1083
@@ -1053,6 +1096,7 @@ impl Drop for MemoryMap {
1053
1096
1054
1097
#[ cfg ( windows) ]
1055
1098
impl MemoryMap {
1099
+ /// Create a new mapping with the given `options`, at least `min_len` bytes long.
1056
1100
pub fn new( min_len: uint, options: & [ MapOption ] ) -> Result <MemoryMap , MapError > {
1057
1101
#[ fixed_stack_segment] ; #[ inline( never) ] ;
1058
1102
@@ -1161,6 +1205,8 @@ impl MemoryMap {
1161
1205
1162
1206
#[ cfg( windows) ]
1163
1207
impl Drop for MemoryMap {
1208
+ /// Unmap the mapping. Fails the task if any of `VirtualFree`, `UnmapViewOfFile`, or
1209
+ /// `CloseHandle` fail.
1164
1210
fn drop ( & mut self ) {
1165
1211
#[ fixed_stack_segment] ; #[ inline( never) ] ;
1166
1212
0 commit comments