Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows version of map file #23

Open
ekcury opened this issue Feb 3, 2019 · 3 comments
Open

Windows version of map file #23

ekcury opened this issue Feb 3, 2019 · 3 comments

Comments

@ekcury
Copy link

ekcury commented Feb 3, 2019

Hi!!!
I was using the lib, on a windows platform
I made a Map File version for windows, if you want to use it, feel free to use it.
The only problem that you need to allocate space for the file and if this file is very large, read it in chunks
The code has been tested

 char *g_pData=NULL;//Global data  free It later

static const char* mmap_fileWIN(size_t* len, const char* filename) 
{
	HANDLE hfile =
		CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
	if(hfile == INVALID_HANDLE_VALUE)
	{
		printf("Erro:mmap_file   CreateFileA=INVALID_HANDLE_VALUE of %s \n",filename);
		printf("Could not open %s file, error %d\n", filename, GetLastError());
		return NULL;
	}

	DWORD dwsize = GetFileSize( hfile, NULL);
	if (dwsize == 0xFFFFFFFF) 
	{
		printf("Error:map_file Getfilesize 0xff\n");
		return NULL;
	}
	g_pData = (char*)malloc(dwsize);
        if(g_pData ==NULL)
                return NULL;

	HANDLE hFileMapping = CreateFileMapping(hfile, NULL, PAGE_READONLY, 0, 0, NULL);
	if(hFileMapping == INVALID_HANDLE_VALUE)
	{
		printf("Erro:mmap_file   CreateFileMapping=INVALID_HANDLE_of %s \n",filename);
		return NULL;
	}
	int iPos = 0;
	const unsigned int BlockSize = 128 * 1024;
	while(iPos < dwsize)//Read chunk of data
	{
		int iLeft = dwsize - iPos;
		int iBytesToRead = iLeft > BlockSize ? BlockSize: iLeft;

		void *rawBuffer = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, iPos, iBytesToRead);
		if(rawBuffer!=NULL)
		{
			memcpy(&g_pData[iPos], rawBuffer, iBytesToRead);
			UnmapViewOfFile(rawBuffer);
		}
		else
		{
			UnmapViewOfFile(rawBuffer);
			break;
		}
		iPos += iBytesToRead;
	}
	*len = dwsize;
	if(iPos!=0)
		return &g_pData[0];
	return NULL;
}
@syoyo
Copy link
Owner

syoyo commented Feb 3, 2019

Awesome!

Could you please send PR of this Map File version?

@ekcury
Copy link
Author

ekcury commented Feb 3, 2019

Ok I'll try to adapt to the code, test to see if it has memory leaks etc.

@ekcury
Copy link
Author

ekcury commented Feb 5, 2019

I send PR in my copy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants