Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4019958
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:10:08+00:00 2026-05-20T10:10:08+00:00

Good afternoon, It is a well known fact that when dealing with large files

  • 0

Good afternoon, It is a well known fact that when dealing with large files
that cannot be mapped to one view in Win32, create code that carefully maps
and unmaps file regions as they are needed. The pastebin url is:

I created and tested a cMemoryMappedFile class that deals with large files
that cannot be mapped to one view in Win32. I tested the class and found
that while it functions OK, it takes a long time(i.e 3 seconds) for
random access. This is because the class has to unmap and map a file
region for every random access. I was wondering if it was possible to
cache the mapped regions returned from MapViewFile to speed up random access.

Yesterday, I noticed that UnMapViewOfFile invalidates a previously
mapped region returned from MapViewOfFile. Does anyone have ideas
about how to speed up random access through caching or other methods?

Currently the viewport is 128KB. I believe that if I enlarge the
viewport it will reduce the number of calls to UnMapViewOfFile
and MapViewOfFile. However, I was wondering if could use other
methods. Please look at the method,
char* cMemoryMappedFile::GetPointer(int , bool) to see how the
viewport is shifted with the file mapping. Thank you.

The pastebin url for the class is
> .
I am adding the source code here in case no one can access the url.

// cMemoryMappedFile.Cpp
#include "cException.h"
#include "cMemoryMappedFile.h"

#define BUFFER_SIZE 10

#define MEM_BLOCK_SIZE 65536 * 2

/**
\class cMemoryMappedFile
\brief Encapsulation of the Windows Memory Management API.

The cMemoryMapped class makes some memory mapping operations easier.
*/

/**
\brief Constructor for cMemoryMappedFile object.

\param FileSize    Size of file.
\param OpenMode    File open mode 
\param AccessModes File access mode 
\param ShareMode   File sharing mode 
\param Flags       File attributes and flags 
\param ShareMode   File sharing mode 
\param Flags       File attributes and flags
\param Security    Security Attributes 
\param Template    Extended attributes tp apply to a newly created file
*/
cMemoryMappedFile::cMemoryMappedFile(long FileSize_, OpenModes OpenMode_,AccessModes AccessMode_,
    ShareModes ShareMode_,long Flags_,void *Security_,FILEHANDLE Template_) {
    FileSize = FileSize_;
char buffer[BUFFER_SIZE]; 
DWORD dwRetVal = 0;
UINT uRetVal   = 0; 
     DWORD dwPtr    = 0;
BOOL isSetEndOfFile = FALSE;
     LARGE_INTEGER Distance_;
DWORD ErrorCode = 0;

char lpTempPathBuffer[MAX_PATH]; 

     PreviousNCopy = 0;
PreviousN     = 0;

//  Gets the temp path env string (no guarantee it's a valid path).
dwRetVal = GetTempPath(MAX_PATH,          // length of the buffer
                       lpTempPathBuffer); // buffer for path 
if (dwRetVal > MAX_PATH || (dwRetVal == 0))
{
   throw cException(ERR_MEMORYMAPPING,"");  
     }

     //  Generates a temporary file name. 
     uRetVal = GetTempFileName(lpTempPathBuffer, // directory for tmp files
                          TEXT("DEMO"),     // temp file name prefix 
                          0,                // create unique name 
                          TempFileName);  // buffer for name 
     if (uRetVal == 0)
     {

      throw cException(ERR_MEMORYMAPPING,lpTempPathBuffer);  
     }
     //  Creates the new file
     hFile = CreateFile((LPTSTR) TempFileName, // file name 
                       AccessMode_,        // open for write 
                       0,                    // do not share 
                       (SECURITY_ATTRIBUTES *) Security_,  // default security 
                       OpenMode_, // CREATE_ALWAYS,       
                       Flags_,// normal file 
                       Template_);                // no template 
     if (hFile == INVALID_HANDLE_VALUE) 
     { 
       throw cException(ERR_MEMORYMAPPING,TempFileName);   
     } 
     Distance_.LowPart = (ULONG)FileSize_;
Distance_.HighPart = 0; // (ULONG)(FileSize_ >> 32);
dwPtr = ::SetFilePointer(hFile,Distance_.LowPart,
    &(Distance_.HighPart), FileBegin);

if (dwPtr == INVALID_SET_FILE_POINTER){
   throw cException(ERR_MEMORYMAPPING,TempFileName);
}
isSetEndOfFile = SetEndOfFile(hFile);
if (!isSetEndOfFile){
   ErrorCode = GetLastError();
   throw cException(ERR_MEMORYMAPPING,TempFileName);
}
hMapping=::CreateFileMapping(hFile,(SECURITY_ATTRIBUTES *)Security_,PAGE_READWRITE,0,0,0);
if (hMapping==NULL)
    throw cException(ERR_MEMORYMAPPING,TempFileName);   

MapPtr = 0;
adjustedptr = 0;
prevadjustedptr = adjustedptr;

    FilePath=new char[strlen(TempFileName)+1];
    strcpy(FilePath,TempFileName);
}



char * cMemoryMappedFile::GetPointer(int n, bool Caching){
unsigned int baseoff; 
if( n < MEM_BLOCK_SIZE / 2)
{
  baseoff = 0;
}
else
{
  baseoff = ((n + MEM_BLOCK_SIZE / 4) & 
    (~(MEM_BLOCK_SIZE / 2 - 1))) - MEM_BLOCK_SIZE / 2;

}
// the correct memory mapped view is already mapped in
     if (adjustedptr != 0 && mappedoffset == baseoff && Caching)
    return adjustedptr;
else if (Caching)
{
  /*    
   retrieve adjustedptr from cache
      */
}
// get a new memory mapped viewport
else{
    if (MapPtr){
                 UnmapViewOfFile(MapPtr);
       PreviousNCopy = PreviousN;
       prevadjustedptr = adjustedptr;
    }
    PreviousN = n;
              mappedlength = min(FileSize - baseoff, MEM_BLOCK_SIZE); 

              // MapViewOfFile should be aligned to 64K boundary

    MapPtr = (char*)::MapViewOfFile( hMapping, 
                       FILE_MAP_WRITE | FILE_MAP_READ, 0, 
        baseoff, mappedlength);
              mappedoffset =    baseoff;
    adjustedptr = MapPtr - mappedoffset; 
    printf("Value: %u n: %u\n",adjustedptr[n],n);

 /*
    cache PreviousNCopy,PreviousN, prevadjustedptr[PreviousNCopy]
 */

}
 return adjustedptr; 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:10:09+00:00Added an answer on May 20, 2026 at 10:10 am

    You could have a “free list” style cache — when the user of your class asks to unmap a region you don’t really, you just add it to the list. When they ask to map a new region then you reuse an existing mapping if possible, otherwise you create a new mapping, deleting the least-recently-used one from the cache if you’ve got too many mappings open, or where the mapped size of the cached mappings is too large.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good afternoon, WinUSB is working well on the development computer that I am using
Good afternoon, This should be an easy one. I've done the cookie-cutter default ASP.NET
Dear Friends good afternoon. My problem may be this is very basic one i.e.
Good afternoon, I have a web query in Excel 2002 going against a web
Good afternoon, with all the buzz around the iPhone / AppStore etc, I felt
Good morning, afternoon, evening or night (depending on your timezone). This is just a
Good morning, I am the developer of a medium sized PDA application that will
Good afternoon, I am using the code below to map a network drive in
Good afternoon, I am having a little trouble with .net's internal (System.Drawing) based MetaFile
Good Afternoon, A customer has provided me with a spreadsheet file his team uses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.