SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MemoryMappedFile.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2009 Soeren Sonnenburg
8  * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #ifndef __MEMORYMAPPEDFILE_H__
12 #define __MEMORYMAPPEDFILE_H__
13 
14 #include <shogun/io/SGIO.h>
15 #include <shogun/base/SGObject.h>
16 
17 #include <stdio.h>
18 #include <string.h>
19 #include <sys/mman.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 
25 namespace shogun
26 {
31 template <class T> class CMemoryMappedFile : public CSGObject
32 {
33  public:
36  {
37  SG_UNSTABLE("CMemoryMappedFile::CMemoryMappedFile()",
38  "\n");
39 
40  fd = 0;
41  length = 0;
42  address = NULL;
43  rw = 'r';
45  }
46 
60  CMemoryMappedFile(const char* fname, char flag='r', int64_t fsize=0)
61  : CSGObject()
62  {
64  rw=flag;
65 
66  int open_flags;
67  int mmap_prot;
68  int mmap_flags;
69 
70  if (rw=='w')
71  {
72  open_flags=O_RDWR | O_CREAT;
73  mmap_prot=PROT_READ|PROT_WRITE;
74  mmap_flags=MAP_SHARED;
75  }
76  else if (rw=='r')
77  {
78  open_flags=O_RDONLY;
79  mmap_prot=PROT_READ;
80  mmap_flags=MAP_PRIVATE;
81  }
82  else
83  SG_ERROR("Unknown flags\n");
84 
85  fd = open(fname, open_flags, S_IRWXU | S_IRWXG | S_IRWXO);
86  if (fd == -1)
87  SG_ERROR("Error opening file\n");
88 
89  if (rw=='w' && fsize)
90  {
91  uint8_t byte=0;
92  if (lseek(fd, fsize, SEEK_SET) != fsize || write(fd, &byte, 1) != 1)
93  SG_ERROR("Error creating file of size %ld bytes\n", fsize);
94  }
95 
96  struct stat sb;
97  if (fstat(fd, &sb) == -1)
98  SG_ERROR("Error determining file size\n");
99 
100  length = sb.st_size;
101  address = mmap(NULL, length, mmap_prot, mmap_flags, fd, 0);
102  if (address == MAP_FAILED)
103  SG_ERROR("Error mapping file");
104  }
105 
108  {
109  munmap(address, length);
110  if (rw=='w' && last_written_byte && ftruncate(fd, last_written_byte) == -1)
111 
112  {
113  close(fd);
114  SG_ERROR("Error Truncating file to %ld bytes\n", last_written_byte);
115  }
116  close(fd);
117  }
118 
128  inline T* get_map()
129  {
130  return (T*) address;
131  }
132 
137  uint64_t get_length()
138  {
139  return length/sizeof(T);
140  }
141 
146  uint64_t get_size()
147  {
148  return length;
149  }
150 
162  char* get_line(uint64_t& len, uint64_t& offs)
163  {
164  char* s = (char*) address;
165  for (uint64_t i=offs; i<length; i++)
166  {
167  if (s[i] == '\n')
168  {
169  char* line=&s[offs];
170  len=i-offs;
171  offs=i+1;
172  return line;
173  }
174  }
175 
176  len=0;
177  offs=length;
178  return NULL;
179  }
180 
191  void write_line(const char* line, uint64_t len, uint64_t& offs)
192  {
193  char* s = ((char*) address) + offs;
194  if (len+1+offs > length)
195  SG_ERROR("Writing beyond size of file\n");
196 
197  for (uint64_t i=0; i<len; i++)
198  s[i] = line[i];
199 
200  s[len]='\n';
201  offs+=length+1;
202  last_written_byte=offs-1;
203  }
204 
216  inline void set_truncate_size(uint64_t sz=0)
217  {
219  }
220 
225  int32_t get_num_lines()
226  {
227  char* s = (char*) address;
228  int32_t linecount=0;
229  for (uint64_t i=0; i<length; i++)
230  {
231  if (s[i] == '\n')
232  linecount++;
233  }
234 
235  return linecount;
236  }
237 
245  inline T operator[](uint64_t index) const
246  {
247  return ((T*)address)[index];
248  }
249 
257  inline T operator[](int32_t index) const
258  {
259  return ((T*)address)[index];
260  }
261 
263  inline virtual const char* get_name() const { return "MemoryMappedFile"; }
264 
265  protected:
267  int fd;
269  uint64_t length;
271  void* address;
273  char rw;
274 
277 };
278 }
279 #endif

SHOGUN Machine Learning Toolbox - Documentation