summaryrefslogtreecommitdiffstats
path: root/mozglue/linker/Mappable.h
blob: 3e25fc88c99bdc66123aecbf7a65fa248c520dde (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef Mappable_h
#define Mappable_h

#include <optional>
#include "mozilla/RefCounted.h"
#include "Utils.h"

/**
 * Helper class to mmap plain files.
 */
class Mappable : public mozilla::RefCounted<Mappable> {
 public:
  MOZ_DECLARE_REFCOUNTED_TYPENAME(Mappable)
  ~Mappable() {}

  MemoryRange mmap(const void* addr, size_t length, int prot, int flags,
                   off_t offset);

 private:
  void munmap(void* addr, size_t length) { ::munmap(addr, length); }
  /* Limit use of Mappable::munmap to classes that keep track of the address
   * and size of the mapping. This allows to ignore ::munmap return value. */
  friend class Mappable1stPagePtr;
  friend class LibHandle;

 public:
  /**
   * Indicate to a Mappable instance that no further mmap is going to happen.
   */
  void finalize();

  /**
   * Returns the maximum length that can be mapped from this Mappable for
   * offset = 0.
   */
  size_t GetLength() const;

  /**
   * Create a Mappable instance for the given file path.
   */
  static Mappable* Create(const char* path);

 protected:
  explicit Mappable(int fd) : fd(fd) {}

 private:
  /* File descriptor */
  std::optional<AutoCloseFD> fd;
};

#endif /* Mappable_h */