1 // 2 // This file is part of the aMule Project. 3 // 4 // Copyright (c) 2009-2011 aMule Team ( admin@amule.org / http://www.amule.org ) 5 // Copyright (c) 2009-2011 Frediano Ziglio (freddy77@gamilc.com) 6 // 7 // This program is free software; you can redistribute it and/or modify 8 // it under the terms of the GNU General Public License as published by 9 // the Free Software Foundation; either version 2 of the License, or 10 // (at your option) any later version. 11 // 12 // This program is distributed in the hope that it will be useful, 13 // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 // GNU General Public License for more details. 16 // 17 // You should have received a copy of the GNU General Public License 18 // along with this program; if not, write to the Free Software 19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 20 // 21 22 #ifndef FILEAREA_H 23 #define FILEAREA_H 24 25 #include "Types.h" // Needed for byte 26 27 class CFileAreaSigHandler; 28 class CFileAutoClose; 29 30 /** 31 * This class is used to optimize file read/write using mapped memory 32 * if supported. 33 */ 34 class CFileArea 35 { 36 friend class CFileAreaSigHandler; 37 public: 38 /** 39 * Creates a uninitialized file area. 40 */ 41 CFileArea(); 42 43 44 /** 45 * Destructor, closes the file if opened. 46 */ 47 virtual ~CFileArea(); 48 49 /** 50 * Closes the file. 51 */ 52 bool Close(); 53 54 /** 55 * Init area with a given piece of file. 56 * 57 * @param file file to read. 58 * @param offset seek address in file. 59 * @param count bytes to read. 60 * 61 * Initialize buffer. Buffer will contain data from current file 62 * position for count length. Buffer will be a memory mapped area 63 * or a allocated buffer depending on systems. 64 */ 65 void ReadAt(CFileAutoClose& file, uint64 offset, size_t count); 66 Init()67 /** 68 * Start a new write 69 */ 70 void StartWriteAt(CFileAutoClose& file, uint64 offset, size_t count); CFileAreaSigHandler()71 72 /** 73 * Flushes data not yet written. 74 */ 75 bool FlushAt(CFileAutoClose& file, uint64 offset, size_t count); 76 77 /** 78 * Get buffer that contains data readed or to write. 79 * @return allocated buffer or NULL if not initialized 80 */ 81 byte *GetBuffer() const { return m_buffer; }; 82 CFileAreaSigHandler()83 /** 84 * Report error pending 85 */ 86 void CheckError(); 87 88 private: 89 //! A CFileArea is neither copyable nor assignable. 90 //@{ 91 CFileArea(const CFileArea&); 92 CFileArea& operator=(const CFileArea&); 93 //@} 94 95 /** 96 * Pointer to buffer used for read/write operations. 97 * If mapped points inside m_mmap_buffer area otherwise 98 * point to an allocated buffer to be freed. 99 */ 100 byte *m_buffer; 101 /** 102 * Pointer to memory mapped area or NULL if not mapped. 103 */ 104 byte *m_mmap_buffer; Handler(int sig,siginfo_t * info,void * ctx)105 /** 106 * Length of the mapped region, currently used only for munmap. 107 */ 108 size_t m_length; 109 /** 110 * Global chain 111 */ 112 CFileArea* m_next; 113 /** 114 * File handle to release 115 */ 116 CFileAutoClose * m_file; 117 /** 118 * true if error detected 119 */ 120 bool m_error; 121 }; 122 123 #endif // FILEAREA_H 124 // File_checked_for_headers 125