VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / modules / juce_core / files / juce_MemoryMappedFile.h
blob97a39de8dcaa6d194587f126be01aa54807935b2
1 /*
2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
20 ==============================================================================
23 namespace juce
26 //==============================================================================
27 /**
28 Maps a file into virtual memory for easy reading and/or writing.
30 @tags{Core}
32 class JUCE_API MemoryMappedFile
34 public:
35 /** The read/write flags used when opening a memory mapped file. */
36 enum AccessMode
38 readOnly, /**< Indicates that the memory can only be read. */
39 readWrite /**< Indicates that the memory can be read and written to - changes that are
40 made will be flushed back to disk at the whim of the OS. */
43 /** Opens a file and maps it to an area of virtual memory.
45 The file should already exist, and should already be the size that you want to work with
46 when you call this. If the file is resized after being opened, the behaviour is undefined.
48 If the file exists and the operation succeeds, the getData() and getSize() methods will
49 return the location and size of the data that can be read or written. Note that the entire
50 file is not read into memory immediately - the OS simply creates a virtual mapping, which
51 will lazily pull the data into memory when blocks are accessed.
53 If the file can't be opened for some reason, the getData() method will return a null pointer.
55 If exclusive is false then other apps can also open the same memory mapped file and use this
56 mapping as an effective way of communicating. If exclusive is true then the mapped file will
57 be opened exclusively - preventing other apps to access the file which may improve the
58 performance of accessing the file.
60 MemoryMappedFile (const File& file, AccessMode mode, bool exclusive = false);
62 /** Opens a section of a file and maps it to an area of virtual memory.
64 The file should already exist, and should already be the size that you want to work with
65 when you call this. If the file is resized after being opened, the behaviour is undefined.
67 If the file exists and the operation succeeds, the getData() and getSize() methods will
68 return the location and size of the data that can be read or written. Note that the entire
69 file is not read into memory immediately - the OS simply creates a virtual mapping, which
70 will lazily pull the data into memory when blocks are accessed.
72 If the file can't be opened for some reason, the getData() method will return a null pointer.
74 NOTE: The start of the actual range used may be rounded-down to a multiple of the OS's page-size,
75 so do not assume that the mapped memory will begin at exactly the position you requested - always
76 use getRange() to check the actual range that is being used.
78 MemoryMappedFile (const File& file,
79 const Range<int64>& fileRange,
80 AccessMode mode,
81 bool exclusive = false);
83 /** Destructor. */
84 ~MemoryMappedFile();
86 /** Returns the address at which this file has been mapped, or a null pointer if
87 the file couldn't be successfully mapped.
89 void* getData() const noexcept { return address; }
91 /** Returns the number of bytes of data that are available for reading or writing.
92 This will normally be the size of the file.
94 size_t getSize() const noexcept { return (size_t) range.getLength(); }
96 /** Returns the section of the file at which the mapped memory represents. */
97 Range<int64> getRange() const noexcept { return range; }
99 private:
100 //==============================================================================
101 void* address = nullptr;
102 Range<int64> range;
104 #if JUCE_WINDOWS
105 void* fileHandle = nullptr;
106 #else
107 int fileHandle = 0;
108 #endif
110 void openInternal (const File&, AccessMode, bool);
112 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MemoryMappedFile)
115 } // namespace juce