File: GenericFile_map.cpp

package info (click to toggle)
snap-aligner 2.0.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,652 kB
  • sloc: cpp: 41,051; ansic: 5,239; python: 227; makefile: 85; sh: 28
file content (78 lines) | stat: -rw-r--r-- 1,500 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*++

Module Name:

    GenericFile_map.cpp

Abstract:

    Generic IO class for SNAP that can map an input file.

Authors:

    Bill Bolosky, September, 2014

Environment:

User mode service.

Revision History:


--*/

#include "stdafx.h"
#include "GenericFile_map.h"
#include "Error.h"
#include "exit.h"

GenericFile_map *GenericFile_map::open(const char *filename)
{
	size_t fileSize = QueryFileSize(filename);
	if (0 == fileSize) {
		return new GenericFile_map(NULL, NULL, 0);
	}
	void *contents;
	MemoryMappedFile *mappedFile = OpenMemoryMappedFile(filename, 0, fileSize, &contents);

	return new GenericFile_map(mappedFile, contents, fileSize);
}

GenericFile_map::GenericFile_map(MemoryMappedFile *i_mappedFile, void *i_contents, size_t i_fileSize) : mappedFile(i_mappedFile), contents((const char *)i_contents), fileSize(i_fileSize), GenericFile_Blob(i_contents, i_fileSize)
{
}

	void
GenericFile_map::close()
{
	if (NULL != mappedFile) {
		CloseMemoryMappedFile(mappedFile);
		mappedFile = NULL;

		GenericFile_Blob::close();
	}
}

GenericFile_map::~GenericFile_map()
{
	close();
}

	_int64
GenericFile_map::prefetch()
{
	if (NULL == mappedFile) {
		return 0;
	}

	AdviseMemoryMappedFilePrefetch(mappedFile);
	int pageSize = getpagesize();

	_int64 total = 0;

	for (size_t offset = 0; offset < fileSize / sizeof (_int64); offset += 4 ) {
		total += ((_int64 *)contents)[offset];
	}

	return total;		// We're returning this just to keep the compiler from optimizing away the whole thing.
}