00001
00002
00003
00004
00005 #ifndef dsMmap_h
00006 #define dsMmap_h
00007
00008 #ifdef HAVE_CONFIG_H
00009 # include <config.h>
00010 #endif
00011
00012 #include <stdlib.h>
00013 #include <errno.h>
00014 #include <fcntl.h>
00015 #include <sys/types.h>
00016 #include <sys/stat.h>
00017
00018 #ifdef Win32
00019 # include <windows.h>
00020 # include <sys/locking.h>
00021 #else
00022 # include <sys/file.h>
00023 # include <sys/mman.h>
00024 #endif
00025
00026 #ifdef HAVE_UNISTD_H
00027 # include <unistd.h>
00028 #endif
00029
00030 #ifdef HAVE_IO_H
00031 # include <io.h>
00032 #endif
00033
00034
00035 #ifdef FreeBSD
00036 # define MAP_ANONYMOUS MAP_ANON
00037 #endif
00038
00039
00040 #ifndef MADV_NORMAL
00041 # define MADV_NORMAL 0
00042 #endif
00043
00044 #ifndef MAP_FAILED
00045 # define MAP_FAILED -1
00046 #endif
00047
00048 #ifndef S_IWUSR
00049 # define S_IWUSR 0200
00050 #endif
00051
00052 #include <dsSmartException.h>
00053 #include <dsutil.h>
00054
00055 DECLARE_EXCEPTION(dsMmap);
00056
00057 #ifdef Win32
00058 typedef unsigned int mode_t;
00059 #endif
00060
00061
00065 class dsMmap
00066 {
00067 protected:
00068 int _fd;
00069 char *_filename;
00070 mode_t _mode;
00071 size_t _pagesize;
00072 size_t _pages;
00073
00074 char *_ptr;
00075 size_t _size;
00076 int _advize;
00077 bool _close_fd;
00078
00079 #ifdef Win32
00080 HANDLE _mhandle;
00081 #endif
00082
00083 int getPageSize();
00084
00085 virtual void mapCreate(const int fd, const size_t size, const mode_t openmode = 0444, const int advize = MADV_NORMAL);
00086 virtual void mapDestroy(bool close_fd = false);
00087 virtual void mapResize(size_t new_size);
00088 virtual void fileResize(size_t new_size);
00089
00090
00091 public:
00092
00093 dsMmap(const char *fname, const mode_t openmode = 0444, const int advize = MADV_NORMAL);
00094 dsMmap(const int fd, const size_t size, const mode_t openmode = 0444, const int advize = MADV_NORMAL);
00095 dsMmap();
00096 virtual ~dsMmap();
00097
00098 int fd();
00099
00100 char operator[](int i){ return _ptr[i]; }
00101 void operator+=(int i){ mapResize(i); }
00102
00103 operator char *(){ return _ptr; }
00104 operator size_t(){ return _size;}
00105
00106 char *ptr() { return _ptr; }
00107 size_t size(){ return _size;}
00108
00109 friend class dsMmapStreambuf;
00110 };
00111
00112 inline int dsMmap::fd()
00113 { return _fd;
00114 }
00115
00116
00117 class dsMmapAnon: public dsMmap
00118 {
00119 public:
00120 dsMmapAnon();
00121 dsMmapAnon(const size_t size, const int advize = MADV_NORMAL);
00122
00123 ~dsMmapAnon();
00124
00125
00126 void mapCreate(const size_t size, const int advize = MADV_NORMAL);
00127 void mapResize(size_t newsize);
00128 };
00129
00130 #endif