本文整理汇总了C++中MapViewOfFileEx函数的典型用法代码示例。如果您正苦于以下问题:C++ MapViewOfFileEx函数的具体用法?C++ MapViewOfFileEx怎么用?C++ MapViewOfFileEx使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MapViewOfFileEx函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: verify
void* MemoryMappedFile::remapPrivateView(void *oldPrivateAddr) {
verify( Lock::isW() );
LockMongoFilesExclusive lockMongoFiles;
RemapLock lk; // Interlock with PortMessageServer::acceptedMP() to stop thread creation
clearWritableBits(oldPrivateAddr);
if( !UnmapViewOfFile(oldPrivateAddr) ) {
DWORD dosError = GetLastError();
log() << "UnMapViewOfFile for " << filename()
<< " failed with error " << errnoWithDescription( dosError )
<< " in MemoryMappedFile::remapPrivateView"
<< endl;
fassertFailed( 16168 );
}
void* newPrivateView = MapViewOfFileEx(
maphandle, // file mapping handle
FILE_MAP_READ, // access
0, 0, // file offset, high and low
0, // bytes to map, 0 == all
oldPrivateAddr ); // we want the same address we had before
if ( 0 == newPrivateView ) {
DWORD dosError = GetLastError();
log() << "MapViewOfFileEx for " << filename()
<< " failed with error " << errnoWithDescription( dosError )
<< " (file size is " << len << ")"
<< " in MemoryMappedFile::remapPrivateView"
<< endl;
}
fassert( 16148, newPrivateView == oldPrivateAddr );
return newPrivateView;
}
示例2: DL_MapViewOfFileExNuma
LPVOID DL_MapViewOfFileExNuma(HANDLE hFileMappingObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow, SIZE_T dwNumberOfBytesToMap, LPVOID lpBaseAddress, DWORD nndPreferred)
{
typedef LPVOID (WINAPI * MapViewOfFileExNuma_t)(HANDLE, DWORD, DWORD, DWORD, SIZE_T, LPVOID, DWORD);
static auto fcn = (MapViewOfFileExNuma_t)GetKernelProcAddress("MapViewOfFileExNuma");
if (not fcn) return MapViewOfFileEx(hFileMappingObject, dwDesiredAccess, dwFileOffsetHigh, dwFileOffsetLow, dwNumberOfBytesToMap, lpBaseAddress);
return fcn(hFileMappingObject, dwDesiredAccess, dwFileOffsetHigh, dwFileOffsetLow, dwNumberOfBytesToMap, lpBaseAddress, nndPreferred);
}
示例3: CreateFileMapping
/* mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); */
void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset) {
HANDLE h;
void *data;
(void) offset;
if ((flags != MAP_SHARED) || (prot != PROT_READ)) {
/* Not supported in this port */
return MAP_FAILED;
};
h = CreateFileMapping((HANDLE) FDAPI_get_osfhandle(fd), NULL, PAGE_READONLY, 0, 0, NULL);
if (!h) {
return MAP_FAILED;
}
data = MapViewOfFileEx(h, FILE_MAP_READ, 0, 0, length, start);
CloseHandle(h);
if (!data) {
return MAP_FAILED;
}
return data;
}
示例4: shmget
/*
In order to create a new message queue, or access an existing queue, the shmget() system call is used.
SYSTEM CALL: shmget();
PROTOTYPE: int shmget ( key_t key, int size, int shmflg );
RETURNS: shared memory segment identifier on success
-1 on error: errno = EINVAL (Invalid segment size specified)
EEXIST (Segment exists, cannot create)
EIDRM (Segment is marked for deletion, or was removed)
ENOENT (Segment does not exist)
EACCES (Permission denied)
ENOMEM (Not enough memory to create segment)
NOTES:
This particular call should almost seem like old news at this point.
It is strikingly similar to the corresponding get calls for message queues and semaphore sets.
The first argument to shmget() is the key value (in our case returned by a call to ftok()).
This key value is then compared to existing key values that exist within the kernel for other shared memory segments.
At that point, the open or access operation is dependent upon the contents of the shmflg argument.
IPC_CREAT
Create the segment if it doesn't already exist in the kernel.
IPC_EXCL
When used with IPC_CREAT, fail if segment already exists.
If IPC_CREAT is used alone, shmget() either returns the segment identifier for a newly created segment,
or returns the identifier for a segment which exists with the same key value.
If IPC_EXCL is used along with IPC_CREAT, then either a new segment is created, or if the segment exists, the call fails with -1.
IPC_EXCL is useless by itself, but when combined with IPC_CREAT, it can be used as a facility to guarantee that no existing segment is opened for access.
Once again, an optional octal mode may be OR'd into the mask.
Let's create a wrapper function for locating or creating a shared memory segment :
int open_segment( key_t keyval, int segsize )
{
int shmid;
if((shmid = shmget( keyval, segsize, IPC_CREAT | 0660 )) == -1)
{
return(-1);
}
return(shmid);
}
Note the use of the explicit permissions of 0660.
This small function either returns a shared memory segment identifier (int), or -1 on error.
The key value and requested segment size (in bytes) are passed as arguments.
Once a process has a valid IPC identifier for a given segment, the next step is for the process to attach or map the segment into its own addressing space.
*/
TSRM_API int shmget ( key_t key, int size, int shmflg ) { //int shmget(int key, int size, int flags) {
shm_pair *shm;
char shm_segment[26], shm_info[29];
HANDLE shm_handle, info_handle;
BOOL created = FALSE;
DWORD errnum; //for DEBUG
if (size < 0) {
return -1;
}
sprintf(shm_segment, "TSRM_SHM_SEGMENT:%d", key);
sprintf(shm_info, "TSRM_SHM_DESCRIPTOR:%d", key);
shm_handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, shm_segment);
info_handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, shm_info);
if ((!shm_handle && !info_handle)) {
if (shmflg & IPC_CREAT) {
shm_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, shm_segment);
info_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(shm->descriptor), shm_info);
created = TRUE;
}
if ((!shm_handle || !info_handle)) {
errnum = GetLastError();
return -1;
}
} else {
if (shmflg & IPC_EXCL) {
return -1;
}
}
if ((shm = shm_get(CREATE_NEW_IF_NOT_EXISTS, key, NULL)) == NULL) return -1;
shm->segment = shm_handle;
shm->info = info_handle;
shm->descriptor = MapViewOfFileEx(shm->info, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL);
if (created) {
shm->descriptor->shm_perm.key = key;
shm->descriptor->shm_segsz = size;
shm->descriptor->shm_ctime = time(NULL);
shm->descriptor->shm_cpid = getpid();
shm->descriptor->shm_perm.mode = shmflg;
shm->descriptor->shm_perm.cuid = shm->descriptor->shm_perm.cgid= 0;
shm->descriptor->shm_perm.gid = shm->descriptor->shm_perm.uid = 0;
shm->descriptor->shm_atime = shm->descriptor->shm_dtime = 0;
shm->descriptor->shm_lpid = shm->descriptor->shm_nattch = 0;
//.........这里部分代码省略.........
示例5: MapViewOfFileEx
void* MemArena::CreateViewAt(s64 offset, size_t size, void* base)
{
#ifdef _WIN32
return MapViewOfFileEx(hMemoryMapping, FILE_MAP_ALL_ACCESS, 0, (DWORD)((u64)offset), size, base);
#else
return mmap(base, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, offset);
#endif
}
示例6: MPL_shm_seg_create_attach_templ
/* A template function which creates/attaches shm seg handle
* to the shared memory. Used by user-exposed functions below
*/
static inline int MPL_shm_seg_create_attach_templ(MPL_shm_hnd_t hnd, intptr_t seg_sz,
void **shm_addr_ptr, int offset, int flag)
{
HANDLE lhnd = INVALID_HANDLE_VALUE;
int rc = MPL_SHM_SUCCESS;
ULARGE_INTEGER seg_sz_large;
seg_sz_large.QuadPart = seg_sz;
if (!MPLI_shm_ghnd_is_valid(hnd)) {
rc = MPLI_shm_ghnd_set_uniq(hnd);
if (rc) {
goto fn_exit;
}
}
if (flag & MPLI_SHM_FLAG_SHM_CREATE) {
lhnd = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
PAGE_READWRITE, seg_sz_large.HighPart, seg_sz_large.LowPart,
MPLI_shm_ghnd_get_by_ref(hnd));
if (lhnd == NULL) {
rc = MPL_SHM_EINTERN;
goto fn_exit;
}
MPLI_shm_lhnd_set(hnd, lhnd);
} else {
if (!MPLI_shm_lhnd_is_valid(hnd)) {
/* Strangely OpenFileMapping() returns NULL on error! */
lhnd = OpenFileMapping(FILE_MAP_WRITE, FALSE, MPLI_shm_ghnd_get_by_ref(hnd));
if (lhnd == NULL) {
rc = MPL_SHM_EINTERN;
goto fn_exit;
}
MPLI_shm_lhnd_set(hnd, lhnd);
}
}
if (flag & MPLI_SHM_FLAG_SHM_ATTACH) {
if (flag & MPLI_SHM_FLAG_FIXED_ADDR) {
void *start_addr = (void *) *shm_addr_ptr;
/* The start_addr must be a multiple of the system's memory allocation granularity,
* or the function fails. To determine the memory allocation granularity of the system,
* use the GetSystemInfo function. If there is not enough address space at the
* specified address, the function fails.
* If the function fails, the return value is NULL.*/
*shm_addr_ptr = MapViewOfFileEx(MPLI_shm_lhnd_get(hnd),
FILE_MAP_WRITE, 0, offset, 0, start_addr);
} else {
*shm_addr_ptr = MapViewOfFile(MPLI_shm_lhnd_get(hnd), FILE_MAP_WRITE, 0, offset, 0);
}
if (*shm_addr_ptr == NULL) {
rc = MPL_SHM_EINVAL;
}
}
fn_exit:
return rc;
}
示例7: DosGetSharedMem
APIRET os2APIENTRY DosGetSharedMem(PVOID pb,
ULONG flag)
{
if(!pb)
return 487; //error_invalid_address
if(flag&(PAG_READ|PAG_WRITE|PAG_EXECUTE|PAG_GUARD)!=flag)
return 87; //error_invalid_parameter
PoolLock pl;
SM_Pool *ppool=lockPool(&pl);
for(int i=0; i<MAXSHAREDMEMOBJECTS; i++)
if(ppool->SM_Object[i].SM_ReferenceCount && ppool->SM_Object[i].SM_BaseAddress==(DWORD)pb)
break;
if(i>=MAXSHAREDMEMOBJECTS) {
unlockPool(&pl);
return 487; //error_invalid_address
}
if(hFileMapping[i]!=NULL) {
//already mapped
unlockPool(&pl);
return 0;
}
//open file mapping
hFileMapping[i] = OpenFileMapping(flag&PAG_WRITE?FILE_MAP_WRITE:FILE_MAP_READ,
FALSE,
ppool->SM_Object[i].SM_Filemapname
);
if(hFileMapping[i]==NULL) {
unlockPool(&pl);
return 8; //error_not_enough_memory
}
//map
unreserveObjectAddresses(i);
LPVOID lpv = MapViewOfFileEx(hFileMapping[i],
flag&PAG_WRITE ? FILE_MAP_WRITE:FILE_MAP_READ,
0,0,
(DWORD)ppool->SM_Object[i].SM_Size,
(LPVOID)ppool->SM_Object[i].SM_BaseAddress
);
if((DWORD)lpv!=ppool->SM_Object[i].SM_BaseAddress) {
//nt didn't like it
reserveObjectAddresses(i);
CloseHandle(hFileMapping[i]);
hFileMapping[i]=NULL;
return 8; //error_not_enough_memory
}
unlockPool(&pl);
return 0;
}
示例8: UnmapViewOfFile
void* MemoryMappedFile::remapPrivateView(void *oldPrivateAddr) {
bool ok = UnmapViewOfFile(oldPrivateAddr);
assert(ok);
// we want the new address to be the same as the old address in case things keep pointers around (as namespaceindex does).
void *p = MapViewOfFileEx(maphandle, FILE_MAP_COPY, 0, 0,
/*dwNumberOfBytesToMap 0 means to eof*/0 /*len*/,
oldPrivateAddr);
assert(p);
assert(p == oldPrivateAddr);
return p;
}
示例9: MapViewOfFileEx
void *shmat(int shmid, LPVOID shmaddr, int shmflg)
{
struct shmid_ds *shm = &shmids[shmid];
if(shm->addr == shmaddr )
return shm->addr;
shm->addr = MapViewOfFileEx (shm->filemapping, FILE_MAP_WRITE, 0, 0, shm->size, shmaddr);
if (addr == NULL)
win32_error("MapViewOfFileEx %08X", shmaddr);
write_log ("shmat %08X -> %08X\n", shmaddr, shm->addr);
shm->attached = 1;
return shm->addr;
}
示例10: My_MapViewOfFileEx
BOOL My_MapViewOfFileEx()
{
HANDLE hFileMappingObject=NULL;
DWORD dwDesiredAccess=NULL;
DWORD dwFileOffsetHigh=NULL;
DWORD dwFileOffsetLow=NULL;
SIZE_T dwNumberOfBytesToMap=NULL;
LPVOID lpBaseAddress=NULL;
LPVOID returnVal_Real = NULL;
LPVOID returnVal_Intercepted = NULL;
DWORD error_Real = 0;
DWORD error_Intercepted = 0;
__try{
disableInterception();
returnVal_Real = MapViewOfFileEx (hFileMappingObject,dwDesiredAccess,dwFileOffsetHigh,dwFileOffsetLow,dwNumberOfBytesToMap,lpBaseAddress);
error_Real = GetLastError();
enableInterception();
returnVal_Intercepted = MapViewOfFileEx (hFileMappingObject,dwDesiredAccess,dwFileOffsetHigh,dwFileOffsetLow,dwNumberOfBytesToMap,lpBaseAddress);
error_Intercepted = GetLastError();
}__except(puts("in filter"), 1){puts("exception caught");}
return ((returnVal_Real == returnVal_Intercepted) && (error_Real == error_Intercepted));
}
示例11: read
int File::mmread()
{
#if POSIX
return read();
#elif _WIN32
HANDLE hFile;
HANDLE hFileMap;
DWORD size;
char *name;
name = this->name->toChars();
hFile = CreateFile(name, GENERIC_READ,
FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
goto Lerr;
size = GetFileSize(hFile, NULL);
//printf(" file created, size %d\n", size);
hFileMap = CreateFileMapping(hFile,NULL,PAGE_READONLY,0,size,NULL);
if (CloseHandle(hFile) != TRUE)
goto Lerr;
if (hFileMap == NULL)
goto Lerr;
//printf(" mapping created\n");
if (!ref)
mem.free(buffer);
ref = 2;
buffer = (utf8_t *)MapViewOfFileEx(hFileMap, FILE_MAP_READ,0,0,size,NULL);
if (CloseHandle(hFileMap) != TRUE)
goto Lerr;
if (buffer == NULL) // mapping view failed
goto Lerr;
len = size;
//printf(" buffer = %p\n", buffer);
return 0;
Lerr:
return GetLastError(); // failure
#else
assert(0);
#endif
}
示例12: shm_get
TSRM_API void *shmat(int key, const void *shmaddr, int flags)
{
shm_pair *shm = shm_get(key, NULL);
if (!shm->segment) {
return (void*)-1;
}
shm->descriptor->shm_atime = time(NULL);
shm->descriptor->shm_lpid = getpid();
shm->descriptor->shm_nattch++;
shm->addr = MapViewOfFileEx(shm->segment, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL);
return shm->addr;
}
示例13: shmat
/*
SYSTEM CALL: shmat();
PROTOTYPE: int shmat ( int shmid, char *shmaddr, int shmflg);
RETURNS: address at which segment was attached to the process, or
-1 on error: errno = EINVAL (Invalid IPC ID value or attach address passed)
ENOMEM (Not enough memory to attach segment)
EACCES (Permission denied)
NOTES:
If the addr argument is zero (0), the kernel tries to find an unmapped region.
This is the recommended method.
An address can be specified, but is typically only used to facilitate proprietary hardware
or to resolve conflicts with other apps.
The SHM_RND flag can be OR'd into the flag argument to force a passed address to be page aligned (rounds down to the nearest page size).
In addition, if the SHM_RDONLY flag is OR'd in with the flag argument, then the shared memory segment will be mapped in, but marked as readonly.
This call is perhaps the simplest to use.
Consider this wrapper function, which is passed a valid IPC identifier for a segment, and returns the address that the segment was attached to:
char *attach_segment( int shmid )
{
return(shmat(shmid, 0, 0));
}
Once a segment has been properly attached, and a process has a pointer to the start of that segment,
reading and writing to the segment become as easy as simply referencing or dereferencing the pointer!
Be careful not to lose the value of the original pointer! If this happens, you will have no way of accessing the base (start) of the segment.
*/
TSRM_API void *shmat(int shmid, const void *shmaddr, int shmflg) { //void *shmat(int key, const void *shmaddr, int flags) {
shm_pair *shm;
if ((shm = shm_get(GET_IF_EXISTS_BY_KEY, shmid, NULL)) == NULL) return (void*)-1;
if (!shm->segment) {
return (void*)-1;
}
shm->descriptor->shm_atime = time(NULL);
shm->descriptor->shm_lpid = getpid();
shm->descriptor->shm_nattch++;
shm->addr = MapViewOfFileEx(shm->segment, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL);
return shm->addr;
}
示例14: MapViewOfFile
/*
* @implemented
*/
LPVOID
NTAPI
MapViewOfFile(HANDLE hFileMappingObject,
DWORD dwDesiredAccess,
DWORD dwFileOffsetHigh,
DWORD dwFileOffsetLow,
SIZE_T dwNumberOfBytesToMap)
{
/* Call the extended API */
return MapViewOfFileEx(hFileMappingObject,
dwDesiredAccess,
dwFileOffsetHigh,
dwFileOffsetLow,
dwNumberOfBytesToMap,
NULL);
}
示例15: REprintf
int MmapFile::open(const char* fileName) {
int filedes = ::open(fileName, O_RDONLY);
if (filedes < 0) {
REprintf("Cannot open file");
// exit(1);
return -1;
}
this->fileSize = ::getFileSize(fileName);
if (data) {
this->close();
}
#ifndef _WIN32
this->data = mmap(0, this->fileSize, PROT_READ, MAP_SHARED, filedes, 0);
if (this->data == MAP_FAILED) {
REprintf("mmap() failed!");
// exit(1);
return -1;
}
#else
// see:
// http://courses.washington.edu/hypertxt/cwb/cl/windows-mmap.c
// https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-createfilemappinga
this->handle = CreateFileMapping(
(HANDLE)_get_osfhandle(filedes),
NULL, // the file mapping object gets a default security descriptor
PAGE_READONLY, // can also be PAGE_WRITECOPY,
0, // dwMaximumSizeHigh are 0 (zero), the maximum size of the file
// mapping object is equal to the current size of the file that hFile
// identifies
0, // dwMaximumSizeLow (see above)
NULL); // lpName = NULL, the file mapping object is created without a
// name.
if (handle == NULL) {
return -1;
}
this->data = MapViewOfFileEx(handle, FILE_MAP_READ,
0, // dwFileOffsetHigh: The high-order DWORD of
// the file offset where the view is to begin
0, // dwFileOffsetLow: see above
this->fileSize, // dwNumberOfBytesToMap
NULL); // lpBaseAddress: A pointer to the memory
// address in the calling process address
// space where mapping begins.
#endif
return 0;
}