Planeshift

memory_mapped_file.h

Go to the documentation of this file.
00001 // Copyright (c) 2011, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030 // memory_mapped_file.h: Define the google_breakpad::MemoryMappedFile
00031 // class, which maps a file into memory for read-only access.
00032 
00033 #ifndef COMMON_LINUX_MEMORY_MAPPED_FILE_H_
00034 #define COMMON_LINUX_MEMORY_MAPPED_FILE_H_
00035 
00036 #include "common/basictypes.h"
00037 #include "common/memory_range.h"
00038 
00039 namespace google_breakpad {
00040 
00041 // A utility class for mapping a file into memory for read-only access of
00042 // the file content. Its implementation avoids calling into libc functions
00043 // by directly making system calls for open, close, mmap, and munmap.
00044 class MemoryMappedFile {
00045  public:
00046   MemoryMappedFile();
00047 
00048   // Constructor that calls Map() to map a file at |path| into memory.
00049   // If Map() fails, the object behaves as if it is default constructed.
00050   explicit MemoryMappedFile(const char* path);
00051 
00052   ~MemoryMappedFile();
00053 
00054   // Maps a file at |path| into memory, which can then be accessed via
00055   // content() as a MemoryRange object or via data(), and returns true on
00056   // success. Mapping an empty file will succeed but with data() and size()
00057   // returning NULL and 0, respectively. An existing mapping is unmapped
00058   // before a new mapping is created.
00059   bool Map(const char* path);
00060 
00061   // Unmaps the memory for the mapped file. It's a no-op if no file is
00062   // mapped.
00063   void Unmap();
00064 
00065   // Returns a MemoryRange object that covers the memory for the mapped
00066   // file. The MemoryRange object is empty if no file is mapped.
00067   const MemoryRange& content() const { return content_; }
00068 
00069   // Returns a pointer to the beginning of the memory for the mapped file.
00070   // or NULL if no file is mapped or the mapped file is empty.
00071   const void* data() const { return content_.data(); }
00072 
00073   // Returns the size in bytes of the mapped file, or zero if no file
00074   // is mapped.
00075   size_t size() const { return content_.length(); }
00076 
00077  private:
00078   // Mapped file content as a MemoryRange object.
00079   MemoryRange content_;
00080 
00081   DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile);
00082 };
00083 
00084 }  // namespace google_breakpad
00085 
00086 #endif  // COMMON_LINUX_MEMORY_MAPPED_FILE_H_