xref: /aosp_15_r20/external/google-breakpad/src/common/linux/memory_mapped_file.h (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1 // Copyright 2011 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //     * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //     * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 // memory_mapped_file.h: Define the google_breakpad::MemoryMappedFile
30 // class, which maps a file into memory for read-only access.
31 
32 #ifndef COMMON_LINUX_MEMORY_MAPPED_FILE_H_
33 #define COMMON_LINUX_MEMORY_MAPPED_FILE_H_
34 
35 #include <stddef.h>
36 
37 #include "common/memory_range.h"
38 
39 namespace google_breakpad {
40 
41 // 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 // by directly making system calls for open, close, mmap, and munmap.
44 class MemoryMappedFile {
45  public:
46   MemoryMappedFile();
47 
48   // 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   MemoryMappedFile(const char* path, size_t offset);
51 
52   MemoryMappedFile(const MemoryMappedFile&) = delete;
53   void operator=(const MemoryMappedFile&) = delete;
54 
55   ~MemoryMappedFile();
56 
57   // Maps a file at |path| into memory, which can then be accessed via
58   // content() as a MemoryRange object or via data(), and returns true on
59   // success. Mapping an empty file will succeed but with data() and size()
60   // returning NULL and 0, respectively. An existing mapping is unmapped
61   // before a new mapping is created.
62   bool Map(const char* path, size_t offset);
63 
64   // Unmaps the memory for the mapped file. It's a no-op if no file is
65   // mapped.
66   void Unmap();
67 
68   // Returns a MemoryRange object that covers the memory for the mapped
69   // file. The MemoryRange object is empty if no file is mapped.
content()70   const MemoryRange& content() const { return content_; }
71 
72   // Returns a pointer to the beginning of the memory for the mapped file.
73   // or NULL if no file is mapped or the mapped file is empty.
data()74   const void* data() const { return content_.data(); }
75 
76   // Returns the size in bytes of the mapped file, or zero if no file
77   // is mapped.
size()78   size_t size() const { return content_.length(); }
79 
80  private:
81   // Mapped file content as a MemoryRange object.
82   MemoryRange content_;
83 };
84 
85 }  // namespace google_breakpad
86 
87 #endif  // COMMON_LINUX_MEMORY_MAPPED_FILE_H_
88