mapped_file.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2015 The Bazel Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef INCLUDED_THIRD_PARTY_IJAR_MAPPED_FILE_H
  15. #define INCLUDED_THIRD_PARTY_IJAR_MAPPED_FILE_H
  16. #include "third_party/ijar/common.h"
  17. namespace devtools_ijar {
  18. struct MappedInputFileImpl;
  19. struct MappedOutputFileImpl;
  20. // A memory mapped input file.
  21. class MappedInputFile {
  22. private:
  23. MappedInputFileImpl *impl_;
  24. protected:
  25. const char* errmsg_;
  26. bool opened_;
  27. u1* buffer_;
  28. size_t length_;
  29. public:
  30. MappedInputFile(const char* name);
  31. virtual ~MappedInputFile();
  32. // If opening the file succeeded or not.
  33. bool Opened() const { return opened_; }
  34. // Description of the last error that happened.
  35. const char* Error() const { return errmsg_; }
  36. // The mapped contents of the file.
  37. u1* Buffer() const { return buffer_ ; }
  38. // The length of the file.
  39. size_t Length() const { return length_; }
  40. // Unmap a given number of bytes from the beginning of the file.
  41. void Discard(size_t bytes);
  42. int Close();
  43. };
  44. class MappedOutputFile {
  45. private:
  46. MappedOutputFileImpl *impl_;
  47. protected:
  48. const char* errmsg_;
  49. bool opened_;
  50. u1* buffer_;
  51. size_t estimated_size_;
  52. public:
  53. MappedOutputFile(const char* name, size_t estimated_size);
  54. virtual ~MappedOutputFile();
  55. // If opening the file succeeded or not.
  56. bool Opened() const { return opened_; }
  57. // Description of the last error that happened.
  58. const char* Error() const { return errmsg_; }
  59. // The mapped contents of the file.
  60. u1* Buffer() const { return buffer_; }
  61. int Close(size_t size);
  62. };
  63. } // namespace devtools_ijar
  64. #endif