aboutsummaryrefslogtreecommitdiffstats
path: root/include/llvm/System/MappedFile.h
blob: 6092de6ffb8e5e3c9edb8c5a6a15bcc654ac35f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//===- llvm/System/MappedFile.h - MappedFile OS Concept ---------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by Reid Spencer and is distributed under the
// University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file declares the llvm::sys::MappedFile class.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SYSTEM_MAPPEDFILE_H
#define LLVM_SYSTEM_MAPPEDFILE_H

#include "llvm/System/Path.h"

namespace llvm {
namespace sys {

  /// Forward declare a class used for holding platform specific information
  /// that needs to be
  struct MappedFileInfo;

  /// This class provides an abstraction for a memory mapped file in the
  /// operating system's filesystem. It provides platform independent operations
  /// for mapping a file into memory for both read and write access. This class
  /// does not provide facilities for finding the file or operating on paths to
  /// files. The sys::Path class is used for that.
  /// @since 1.4
  /// @brief An abstraction for memory mapped files.
  class MappedFile {
  /// @name Types
  /// @{
  public:
    enum MappingOptions {
      READ_ACCESS = 0x0001,     ///< Map the file for reading
      WRITE_ACCESS = 0x0002,    ///< Map the file for write access
      EXEC_ACCESS = 0x0004,     ///< Map the file for execution access
      SHARED_MAPPING = 0x0008,  ///< Map the file shared with other processes
    };
  /// @}
  /// @name Constructors
  /// @{
  public:
    /// Construct a MappedFile to the \p path in the operating system's file
    /// system with the mapping \p options provided.
    /// @throws std::string if an error occurs
    MappedFile(const Path& path, int options = READ_ACCESS)
      : path_(path), options_(options), base_(0), info_(0) { initialize(); }

    /// Destruct a MappedFile and release all memory associated with it.
    /// @throws std::string if an error occurs
    ~MappedFile() { if (info_) terminate(); }

  /// @}
  /// @name Accessors
  /// @{
  public:
    /// This function determines if the file is currently mapped or not.
    /// @returns true iff the file is mapped into memory, false otherwise
    /// @brief Determine if a MappedFile is currently mapped
    /// @throws nothing
    bool isMapped() const { return base_ != 0; }

    /// This function returns a void* pointer to the base address of the file
    /// mapping. This is the memory address of the first byte in the file.
    /// Note that although a non-const pointer is returned, the memory might
    /// not actually be writable, depending on the MappingOptions used when
    /// the MappedFile was opened.
    /// @returns The base pointer to the memory mapped file.
    /// @brief Obtain the base pointer to the memory mapped file.
    /// @throws nothing
    void* base() const { return base_; }

    /// This function returns a char* pointer to the base address of the file
    /// mapping. This is the memory address of the first byte in the file.
    /// Note that although a non-const pointer is returned, the memory might
    /// not actually be writable, depending on the MappingOptions used when
    /// the MappedFile was opened.
    /// @returns The base pointer to the memory mapped file as a char pointer.
    /// @brief Obtain the base pointer to the memory mapped file.
    /// @throws nothing
    char* charBase() const { return reinterpret_cast<char*>(base_); }

    /// This function returns a reference to the sys::Path object kept by the
    /// MappedFile object. This contains the path to the file that is or
    /// will be mapped.
    /// @returns sys::Path containing the path name.
    /// @brief Returns the mapped file's path as a sys::Path
    /// @throws nothing
    const sys::Path& path() const { return path_; }

    /// This function returns the number of bytes in the file.
    /// @throws std::string if an error occurs
    size_t size() const;

  /// @}
  /// @name Mutators
  /// @{
  public:
    /// The mapped file is removed from memory. If the file was mapped for
    /// write access, the memory contents will be automatically synchronized
    /// with the file's disk contents.
    /// @brief Remove the file mapping from memory.
    void unmap();

    /// The mapped file is put into memory.
    /// @returns The base memory address of the mapped file.
    /// @brief Map the file into memory.
    void* map();

    /// This method causes the size of the file, and consequently the size
    /// of the mapping to be set. This is logically the same as unmap(),
    /// adjust size of the file, map(). Consequently, when calling this
    /// function, the caller should not rely on previous results of the
    /// map(), base(), or baseChar() members as they may point to invalid
    /// areas of memory after this call.
    /// @throws std::string if an error occurs
    /// @brief Set the size of the file and memory mapping.
    void size(size_t new_size);

    void close() { terminate(); }

  /// @}
  /// @name Implementation
  /// @{
  private:
    void initialize(); ///< Initialize platform-specific portion
    void terminate();  ///< Terminate platform-specific portion

  /// @}
  /// @name Data
  /// @{
  private:
    sys::Path path_;       ///< Path to the file.
    int options_;          ///< Options used to create the mapping
    void* base_;           ///< Pointer to the base memory address
    mutable MappedFileInfo* info_; ///< Platform specific info for the mapping

  /// @}
  /// @name Disabled
  /// @{
  private:
    ///< Disallow assignment
    MappedFile& operator = ( const MappedFile & that );
    ///< Disallow copying
    MappedFile(const MappedFile& that);
  /// @}
  };
}
}


#endif