Buffer.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //===- Buffer.h -------------------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLVM_TOOLS_OBJCOPY_BUFFER_H
  9. #define LLVM_TOOLS_OBJCOPY_BUFFER_H
  10. #include "llvm/ADT/StringRef.h"
  11. #include "llvm/Support/FileOutputBuffer.h"
  12. #include "llvm/Support/MemoryBuffer.h"
  13. #include <memory>
  14. namespace llvm {
  15. namespace objcopy {
  16. // The class Buffer abstracts out the common interface of FileOutputBuffer and
  17. // WritableMemoryBuffer so that the hierarchy of Writers depends on this
  18. // abstract interface and doesn't depend on a particular implementation.
  19. // TODO: refactor the buffer classes in LLVM to enable us to use them here
  20. // directly.
  21. class Buffer {
  22. StringRef Name;
  23. public:
  24. virtual ~Buffer();
  25. virtual Error allocate(size_t Size) = 0;
  26. virtual uint8_t *getBufferStart() = 0;
  27. virtual Error commit() = 0;
  28. explicit Buffer(StringRef Name) : Name(Name) {}
  29. StringRef getName() const { return Name; }
  30. };
  31. class FileBuffer : public Buffer {
  32. std::unique_ptr<FileOutputBuffer> Buf;
  33. // Indicates that allocate(0) was called, and commit() should create or
  34. // truncate a file instead of using a FileOutputBuffer.
  35. bool EmptyFile = false;
  36. public:
  37. Error allocate(size_t Size) override;
  38. uint8_t *getBufferStart() override;
  39. Error commit() override;
  40. explicit FileBuffer(StringRef FileName) : Buffer(FileName) {}
  41. };
  42. class MemBuffer : public Buffer {
  43. std::unique_ptr<WritableMemoryBuffer> Buf;
  44. public:
  45. Error allocate(size_t Size) override;
  46. uint8_t *getBufferStart() override;
  47. Error commit() override;
  48. explicit MemBuffer(StringRef Name) : Buffer(Name) {}
  49. std::unique_ptr<WritableMemoryBuffer> releaseMemoryBuffer();
  50. };
  51. } // end namespace objcopy
  52. } // end namespace llvm
  53. #endif // LLVM_TOOLS_OBJCOPY_BUFFER_H