Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 // Copyright (c) 2011, Google Inc. | 1 // Copyright (c) 2011, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 15 matching lines...) Expand all Loading... | |
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | 29 |
30 // memory_mapped_file.h: Define the google_breakpad::MemoryMappedFile | 30 // memory_mapped_file.h: Define the google_breakpad::MemoryMappedFile |
31 // class, which maps a file into memory for read-only access. | 31 // class, which maps a file into memory for read-only access. |
32 | 32 |
33 #ifndef COMMON_LINUX_MEMORY_MAPPED_FILE_H_ | 33 #ifndef COMMON_LINUX_MEMORY_MAPPED_FILE_H_ |
34 #define COMMON_LINUX_MEMORY_MAPPED_FILE_H_ | 34 #define COMMON_LINUX_MEMORY_MAPPED_FILE_H_ |
35 | 35 |
36 #include <stddef.h> | |
36 #include "common/basictypes.h" | 37 #include "common/basictypes.h" |
37 #include "common/memory_range.h" | 38 #include "common/memory_range.h" |
38 | 39 |
39 namespace google_breakpad { | 40 namespace google_breakpad { |
40 | 41 |
41 // A utility class for mapping a file into memory for read-only access of | 42 // A utility class for mapping a file into memory for read-only access of |
42 // the file content. Its implementation avoids calling into libc functions | 43 // the file content. Its implementation avoids calling into libc functions |
43 // by directly making system calls for open, close, mmap, and munmap. | 44 // by directly making system calls for open, close, mmap, and munmap. |
44 class MemoryMappedFile { | 45 class MemoryMappedFile { |
45 public: | 46 public: |
46 MemoryMappedFile(); | 47 MemoryMappedFile(); |
47 | 48 |
48 // Constructor that calls Map() to map a file at |path| into memory. | 49 // Constructor that calls Map() to map a file at |path| into memory. |
49 // If Map() fails, the object behaves as if it is default constructed. | 50 // If Map() fails, the object behaves as if it is default constructed. |
50 explicit MemoryMappedFile(const char* path, size_t offset = 0); | 51 MemoryMappedFile(const char* path, size_t offset); |
Lei Zhang (chromium)
2014/07/16 23:28:25
no default argument: http://google-styleguide.goog
anton
2014/07/17 11:15:48
Done.
| |
51 | 52 |
52 ~MemoryMappedFile(); | 53 ~MemoryMappedFile(); |
53 | 54 |
54 // Maps a file at |path| into memory, which can then be accessed via | 55 // Maps a file at |path| into memory, which can then be accessed via |
55 // content() as a MemoryRange object or via data(), and returns true on | 56 // content() as a MemoryRange object or via data(), and returns true on |
56 // success. Mapping an empty file will succeed but with data() and size() | 57 // success. Mapping an empty file will succeed but with data() and size() |
57 // returning NULL and 0, respectively. An existing mapping is unmapped | 58 // returning NULL and 0, respectively. An existing mapping is unmapped |
58 // before a new mapping is created. | 59 // before a new mapping is created. |
59 bool Map(const char* path, size_t offset = 0); | 60 bool Map(const char* path, size_t offset); |
60 | 61 |
61 // Unmaps the memory for the mapped file. It's a no-op if no file is | 62 // Unmaps the memory for the mapped file. It's a no-op if no file is |
62 // mapped. | 63 // mapped. |
63 void Unmap(); | 64 void Unmap(); |
64 | 65 |
65 // Returns a MemoryRange object that covers the memory for the mapped | 66 // Returns a MemoryRange object that covers the memory for the mapped |
66 // file. The MemoryRange object is empty if no file is mapped. | 67 // file. The MemoryRange object is empty if no file is mapped. |
67 const MemoryRange& content() const { return content_; } | 68 const MemoryRange& content() const { return content_; } |
68 | 69 |
69 // Returns a pointer to the beginning of the memory for the mapped file. | 70 // Returns a pointer to the beginning of the memory for the mapped file. |
70 // or NULL if no file is mapped or the mapped file is empty. | 71 // or NULL if no file is mapped or the mapped file is empty. |
71 const void* data() const { return content_.data(); } | 72 const void* data() const { return content_.data(); } |
72 | 73 |
73 // Returns the size in bytes of the mapped file, or zero if no file | 74 // Returns the size in bytes of the mapped file, or zero if no file |
74 // is mapped. | 75 // is mapped. |
75 size_t size() const { return content_.length(); } | 76 size_t size() const { return content_.length(); } |
76 | 77 |
77 private: | 78 private: |
78 // Mapped file content as a MemoryRange object. | 79 // Mapped file content as a MemoryRange object. |
79 MemoryRange content_; | 80 MemoryRange content_; |
80 | 81 |
81 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile); | 82 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile); |
82 }; | 83 }; |
83 | 84 |
84 } // namespace google_breakpad | 85 } // namespace google_breakpad |
85 | 86 |
86 #endif // COMMON_LINUX_MEMORY_MAPPED_FILE_H_ | 87 #endif // COMMON_LINUX_MEMORY_MAPPED_FILE_H_ |
LEFT | RIGHT |