• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef _MMAP_INCLUDED_
16 #define _MMAP_INCLUDED_
17 
18 //
19 // Encapsulate memory mapped files
20 //
21 
22 class TMMap {
23 public:
TMMap(const char * fileName)24 	TMMap(const char* fileName) :
25 		fSize(-1), // -1 is the error value returned by GetFileSize()
26 		fp(nullptr),
27 		fBuff(0)   // 0 is the error value returned by MapViewOfFile()
28 	{
29 		if ((fp = fopen(fileName, "r")) == NULL)
30 			return;
31 		char c = getc(fp);
32 		fSize = 0;
33 		while (c != EOF) {
34 			fSize++;
35 			c = getc(fp);
36 		}
37 		if (c == EOF)
38 			fSize++;
39 		rewind(fp);
40 		fBuff = (char*)malloc(sizeof(char) * fSize);
41 		int count = 0;
42 		c = getc(fp);
43 		while (c != EOF) {
44 			fBuff[count++] = c;
45 			c = getc(fp);
46 		}
47 		fBuff[count++] = c;
48 	}
49 
getData()50 	char* getData() { return fBuff; }
getSize()51 	int   getSize() { return fSize; }
52 
~TMMap()53 	~TMMap() {
54 		if (fp != NULL)
55 			fclose(fp);
56 	}
57 
58 private:
59 	int             fSize;      // size of file to map in
60 	FILE *fp;
61 	char*           fBuff;      // the actual data;
62 };
63 
64 #endif // _MMAP_INCLUDED_
65