5 * Created by Alyssa Milburn on Sat Jul 24 2004.
6 * Copyright (c) 2004 Alyssa Milburn. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
20 #include "mmapifstream.h"
25 #include <sys/types.h>
28 #include <iostream> // debug needs only
30 mmapifstream::mmapifstream(std::string filename
) {
35 void mmapifstream::mmapopen(std::string filename
) {
36 open(filename
.c_str(), std::ios::binary
);
37 if (!is_open()) return;
40 // todo: store the handle somewhere?
41 HANDLE hFile
= CreateFileA(filename
.c_str(), GENERIC_READ
, FILE_SHARE_READ
,
42 NULL
, OPEN_EXISTING
, 0, NULL
);
43 HANDLE hMap
= CreateFileMapping(hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
44 void *mapr
= MapViewOfFile(hMap
, FILE_MAP_READ
, 0, 0, 0);
46 FILE *f
= fopen(filename
.c_str(), "r");
55 // now do the mmap (work out filesize, then mmap)
57 seekg(0, std::ios::end
);
59 assert((int)filesize
!= -1);
60 seekg(0, std::ios::beg
);
62 void *mapr
= mmap(0, filesize
, PROT_READ
, MAP_PRIVATE
, fno
, 0);
63 fclose(f
); // we don't need it, now!
66 assert(mapr
!= (void *)-1);
71 mmapifstream::~mmapifstream() {
76 munmap(map
, filesize
);