OpenShot Library | libopenshot-audio  0.2.0
juce_MemoryMappedFile.h
1 
2 /** @weakgroup juce_core-files
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  Maps a file into virtual memory for easy reading and/or writing.
33 
34  @tags{Core}
35 */
37 {
38 public:
39  /** The read/write flags used when opening a memory mapped file. */
41  {
42  readOnly, /**< Indicates that the memory can only be read. */
43  readWrite /**< Indicates that the memory can be read and written to - changes that are
44  made will be flushed back to disk at the whim of the OS. */
45  };
46 
47  /** Opens a file and maps it to an area of virtual memory.
48 
49  The file should already exist, and should already be the size that you want to work with
50  when you call this. If the file is resized after being opened, the behaviour is undefined.
51 
52  If the file exists and the operation succeeds, the getData() and getSize() methods will
53  return the location and size of the data that can be read or written. Note that the entire
54  file is not read into memory immediately - the OS simply creates a virtual mapping, which
55  will lazily pull the data into memory when blocks are accessed.
56 
57  If the file can't be opened for some reason, the getData() method will return a null pointer.
58 
59  If exclusive is false then other apps can also open the same memory mapped file and use this
60  mapping as an effective way of communicating. If exclusive is true then the mapped file will
61  be opened exclusively - preventing other apps to access the file which may improve the
62  performance of accessing the file.
63  */
64  MemoryMappedFile (const File& file, AccessMode mode, bool exclusive = false);
65 
66  /** Opens a section of a file and maps it to an area of virtual memory.
67 
68  The file should already exist, and should already be the size that you want to work with
69  when you call this. If the file is resized after being opened, the behaviour is undefined.
70 
71  If the file exists and the operation succeeds, the getData() and getSize() methods will
72  return the location and size of the data that can be read or written. Note that the entire
73  file is not read into memory immediately - the OS simply creates a virtual mapping, which
74  will lazily pull the data into memory when blocks are accessed.
75 
76  If the file can't be opened for some reason, the getData() method will return a null pointer.
77 
78  NOTE: The start of the actual range used may be rounded-down to a multiple of the OS's page-size,
79  so do not assume that the mapped memory will begin at exactly the position you requested - always
80  use getRange() to check the actual range that is being used.
81  */
82  MemoryMappedFile (const File& file,
83  const Range<int64>& fileRange,
84  AccessMode mode,
85  bool exclusive = false);
86 
87  /** Destructor. */
89 
90  /** Returns the address at which this file has been mapped, or a null pointer if
91  the file couldn't be successfully mapped.
92  */
93  void* getData() const noexcept { return address; }
94 
95  /** Returns the number of bytes of data that are available for reading or writing.
96  This will normally be the size of the file.
97  */
98  size_t getSize() const noexcept { return (size_t) range.getLength(); }
99 
100  /** Returns the section of the file at which the mapped memory represents. */
101  Range<int64> getRange() const noexcept { return range; }
102 
103 private:
104  //==============================================================================
105  void* address = nullptr;
106  Range<int64> range;
107 
108  #if JUCE_WINDOWS
109  void* fileHandle = nullptr;
110  #else
111  int fileHandle = 0;
112  #endif
113 
114  void openInternal (const File&, AccessMode, bool);
115 
116  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MemoryMappedFile)
117 };
118 
119 } // namespace juce
120 
121 /** @}*/
juce::MemoryMappedFile::getRange
Range< int64 > getRange() const noexcept
Returns the section of the file at which the mapped memory represents.
Definition: juce_MemoryMappedFile.h:101
JUCE_API
#define JUCE_API
This macro is added to all JUCE public class declarations.
Definition: juce_StandardHeader.h:143
juce::MemoryMappedFile
Maps a file into virtual memory for easy reading and/or writing.
Definition: juce_MemoryMappedFile.h:36
juce::File
Represents a local file or directory.
Definition: juce_File.h:44
juce::MemoryMappedFile::readOnly
@ readOnly
Indicates that the memory can only be read.
Definition: juce_MemoryMappedFile.h:42
juce::MemoryMappedFile::getData
void * getData() const noexcept
Returns the address at which this file has been mapped, or a null pointer if the file couldn't be suc...
Definition: juce_MemoryMappedFile.h:93
juce::Range< int64 >
juce::MemoryMappedFile::AccessMode
AccessMode
The read/write flags used when opening a memory mapped file.
Definition: juce_MemoryMappedFile.h:40
juce::MemoryMappedFile::getSize
size_t getSize() const noexcept
Returns the number of bytes of data that are available for reading or writing.
Definition: juce_MemoryMappedFile.h:98