Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ACGL
acgl
Commits
8c672f65
Commit
8c672f65
authored
May 14, 2014
by
Martin Schultz
Browse files
* implemented posix compliant version of memorymappedFile
parent
02a18624
Changes
4
Hide whitespace changes
Inline
Side-by-side
include/ACGL/Utils/MemoryMappedFile.hh
View file @
8c672f65
...
...
@@ -54,8 +54,8 @@ namespace ACGL{
////////////////////////////////////////////////////////////////
enum
shareMode
{
MAP_PRIVATE
,
MAP_SHARED
MAP
PING
_PRIVATE
,
MAP
PING
_SHARED
};
////////////////////////////////////////////////////////////////
...
...
@@ -99,7 +99,7 @@ namespace ACGL{
********************************************************************/
MemoryMappedFile
(
const
char
*
_fileName
,
accessMode
_accessMode
=
accessMode
::
READ_ONLY
,
shareMode
_shareMode
=
shareMode
::
MAP_PRIVATE
,
shareMode
_shareMode
=
shareMode
::
MAP
PING
_PRIVATE
,
size_t
_length
=
0
,
off_t
_offset
=
0
);
...
...
@@ -125,4 +125,4 @@ namespace ACGL{
};
}
}
\ No newline at end of file
}
include/ACGL/Utils/MemoryMappedFilePosixImpl.hh
View file @
8c672f65
...
...
@@ -24,5 +24,12 @@
off_t
length
();
off_t
length
()
const
;
~
MemoryMappedFilePosixImpl
();
private:
int
mFileHandle
;
off_t
mPageOffset
;
off_t
mLength
;
char
*
mpData
;
int
mErrorCode
;
};
#endif
\ No newline at end of file
#endif
src/ACGL/Utils/MemoryMappedFilePosixImpl.cc
View file @
8c672f65
...
...
@@ -6,49 +6,106 @@
#pragma once
#ifdef __unix
#include
"ACGL/Utils/MemoryMappedFilePosixImpl.hh"
#include
<sys/types.h>
#include
<sys/mman.h>
#include
<sys/stat.h>
#include
<fcntl.h>
#include
<unistd.h>
MemoryMappedFilePosixImpl
::
MemoryMappedFilePosixImpl
(
const
char
*
_fileName
,
ACGL
::
Utils
::
MemoryMappedFile
::
accessMode
_accessMode
,
ACGL
::
Utils
::
MemoryMappedFile
::
shareMode
_shareMode
,
size_t
_length
,
off_t
_offset
)
off_t
_offset
)
:
mFileHandle
(
NULL
),
mPageOffset
(
0
),
mpData
(
nullptr
),
mErrorCode
(
0
)
{
int
pageAccess
=
0
;
int
pageShare
=
0
;
if
(
_shareMode
==
ACGL
::
Utils
::
MemoryMappedFile
::
MAPPING_SHARED
)
pageShare
=
MAP_SHARED
;
else
pageShare
=
MAP_PRIVATE
;
if
(
_accessMode
==
ACGL
::
Utils
::
MemoryMappedFile
::
READ_ONLY
)
{
mFileHandle
=
open
(
_fileName
,
O_RDONLY
);
pageAccess
=
PROT_READ
;
}
else
{
mFileHandle
=
open
(
_fileName
,
O_RDWR
);
pageAccess
=
PROT_READ
|
PROT_WRITE
;
}
if
(
mFileHandle
==
NULL
)
{
mErrorCode
=
2
;
ACGL
::
Utils
::
error
()
<<
"Could not map the File: "
<<
_fileName
<<
" to Ram. File was not found"
<<
std
::
endl
;
return
;
}
if
(
_length
<=
0
)
{
struct
stat
fileStat
;
stat
(
_fileName
,
&
fileStat
);
mLength
=
fileStat
.
st_size
;
}
if
(
_offset
>
0
)
{
mPageOffset
=
_offset
%
sysconf
(
_SC_PAGE_SIZE
);
mLength
-=
_offset
;
}
mpData
=
reinterpret_cast
<
char
*>
(
mmap
(
nullptr
,
mLength
,
pageAccess
,
pageShare
,
mFileHandle
,
_offset
-
mPageOffset
));
if
(
!
mpData
)
{
ACGL
::
Utils
::
error
()
<<
"could not map the File: "
<<
_fileName
<<
" to Ram. Error when calling mmap (are you shure offset and length are correct?)"
<<
std
::
endl
;
}
}
char
*
MemoryMappedFilePosixImpl
::
data
()
{
return
0
;
return
mpData
;
}
const
char
*
MemoryMappedFilePosixImpl
::
data
()
const
{
return
0
;
return
mpData
;
}
int
MemoryMappedFilePosixImpl
::
errorCode
()
{
return
0
;
return
mErrorCode
;
}
int
MemoryMappedFilePosixImpl
::
errorCode
()
const
{
return
0
;
return
mErrorCode
;
}
off_t
MemoryMappedFilePosixImpl
::
pageOffset
()
{
return
0
;
return
mPageOffset
;
}
off_t
MemoryMappedFilePosixImpl
::
pageOffset
()
const
{
return
0
;
return
mPageOffset
;
}
off_t
MemoryMappedFilePosixImpl
::
length
()
{
return
0
;
return
mLength
;
}
off_t
MemoryMappedFilePosixImpl
::
length
()
const
{
return
0
;
return
mLength
;
}
MemoryMappedFilePosixImpl
::~
MemoryMappedFilePosixImpl
()
{
if
(
mpData
)
munmap
(
mpData
,
mLength
);
if
(
mFileHandle
)
close
(
mFileHandle
);
}
#endif
src/ACGL/Utils/MemoryMappedFileWinImpl.cc
View file @
8c672f65
...
...
@@ -35,7 +35,7 @@ MemoryMappedFileWinImpl::MemoryMappedFileWinImpl(
//
/////////////////////////////////////////////////////
if
(
_shareMode
==
ACGL
::
Utils
::
MemoryMappedFile
::
MAP_SHARED
)
if
(
_shareMode
==
ACGL
::
Utils
::
MemoryMappedFile
::
MAP
PING
_SHARED
)
{
fileShare
=
FILE_SHARE_READ
|
FILE_SHARE_WRITE
;
...
...
@@ -82,7 +82,7 @@ MemoryMappedFileWinImpl::MemoryMappedFileWinImpl(
SYSTEM_INFO
info
;
GetSystemInfo
(
&
info
);
mPageOffset
=
_offset
%
info
.
dwAllocationGranularity
;
mapSize
-=
(
_offset
-
mPageOffset
);
mapSize
-=
(
_offset
);
}
if
(
_length
>
0
)
{
...
...
@@ -98,7 +98,7 @@ MemoryMappedFileWinImpl::MemoryMappedFileWinImpl(
}
//create the file mapping view
mpData
=
reinterpret_cast
<
PCHAR
>
(
MapViewOfFile
(
mFileMappingHandle
,
mapViewAccess
,
offsetHigh
,
offsetLow
-
mPageOffset
,
mapSize
));
(
MapViewOfFile
(
mFileMappingHandle
,
mapViewAccess
,
offsetHigh
,
offsetLow
,
mapSize
));
if
(
!
mpData
)
{
mErrorCode
=
GetLastError
();
...
...
@@ -148,4 +148,4 @@ MemoryMappedFileWinImpl::~MemoryMappedFileWinImpl()
CloseHandle
(
mFileHandle
);
}
#endif
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment