The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_MEMORYMAPPEDFILE_JUCEHEADER__
  19. #define __JUCE_MEMORYMAPPEDFILE_JUCEHEADER__
  20. #include "juce_File.h"
  21. //==============================================================================
  22. /**
  23. Maps a file into virtual memory for easy reading and/or writing.
  24. */
  25. class JUCE_API MemoryMappedFile
  26. {
  27. public:
  28. /** The read/write flags used when opening a memory mapped file. */
  29. enum AccessMode
  30. {
  31. readOnly, /**< Indicates that the memory can only be read. */
  32. readWrite /**< Indicates that the memory can be read and written to - changes that are
  33. made will be flushed back to disk at the whim of the OS. */
  34. };
  35. /** Opens a file and maps it to an area of virtual memory.
  36. The file should already exist, and should already be the size that you want to work with
  37. when you call this. If the file is resized after being opened, the behaviour is undefined.
  38. If the file exists and the operation succeeds, the getData() and getSize() methods will
  39. return the location and size of the data that can be read or written. Note that the entire
  40. file is not read into memory immediately - the OS simply creates a virtual mapping, which
  41. will lazily pull the data into memory when blocks are accessed.
  42. If the file can't be opened for some reason, the getData() method will return a null pointer.
  43. */
  44. MemoryMappedFile (const File& file, AccessMode mode);
  45. /** Destructor. */
  46. ~MemoryMappedFile();
  47. /** Returns the address at which this file has been mapped, or a null pointer if
  48. the file couldn't be successfully mapped.
  49. */
  50. void* getData() const noexcept { return address; }
  51. /** Returns the number of bytes of data that are available for reading or writing.
  52. This will normally be the size of the file.
  53. */
  54. size_t getSize() const noexcept { return length; }
  55. private:
  56. //==============================================================================
  57. void* address;
  58. size_t length;
  59. #if JUCE_WINDOWS
  60. void* fileHandle;
  61. #else
  62. int fileHandle;
  63. #endif
  64. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MemoryMappedFile);
  65. };
  66. #endif // __JUCE_MEMORYMAPPEDFILE_JUCEHEADER__