Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 1 | //===- Buffer.h -------------------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_TOOLS_OBJCOPY_BUFFER_H |
| 10 | #define LLVM_TOOLS_OBJCOPY_BUFFER_H |
| 11 | |
| 12 | #include "llvm/ADT/StringRef.h" |
| 13 | #include "llvm/Support/FileOutputBuffer.h" |
| 14 | #include "llvm/Support/MemoryBuffer.h" |
| 15 | #include <memory> |
| 16 | |
| 17 | namespace llvm { |
| 18 | namespace objcopy { |
| 19 | |
| 20 | // The class Buffer abstracts out the common interface of FileOutputBuffer and |
| 21 | // WritableMemoryBuffer so that the hierarchy of Writers depends on this |
| 22 | // abstract interface and doesn't depend on a particular implementation. |
| 23 | // TODO: refactor the buffer classes in LLVM to enable us to use them here |
| 24 | // directly. |
| 25 | class Buffer { |
| 26 | StringRef Name; |
| 27 | |
| 28 | public: |
| 29 | virtual ~Buffer(); |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 30 | virtual Error allocate(size_t Size) = 0; |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 31 | virtual uint8_t *getBufferStart() = 0; |
| 32 | virtual Error commit() = 0; |
| 33 | |
| 34 | explicit Buffer(StringRef Name) : Name(Name) {} |
| 35 | StringRef getName() const { return Name; } |
| 36 | }; |
| 37 | |
| 38 | class FileBuffer : public Buffer { |
| 39 | std::unique_ptr<FileOutputBuffer> Buf; |
Jordan Rupprecht | b2702d6 | 2019-01-28 15:02:40 +0000 | [diff] [blame] | 40 | // Indicates that allocate(0) was called, and commit() should create or |
| 41 | // truncate a file instead of using a FileOutputBuffer. |
| 42 | bool EmptyFile = false; |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 43 | |
| 44 | public: |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 45 | Error allocate(size_t Size) override; |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 46 | uint8_t *getBufferStart() override; |
| 47 | Error commit() override; |
| 48 | |
| 49 | explicit FileBuffer(StringRef FileName) : Buffer(FileName) {} |
| 50 | }; |
| 51 | |
| 52 | class MemBuffer : public Buffer { |
| 53 | std::unique_ptr<WritableMemoryBuffer> Buf; |
| 54 | |
| 55 | public: |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 56 | Error allocate(size_t Size) override; |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 57 | uint8_t *getBufferStart() override; |
| 58 | Error commit() override; |
| 59 | |
| 60 | explicit MemBuffer(StringRef Name) : Buffer(Name) {} |
| 61 | |
| 62 | std::unique_ptr<WritableMemoryBuffer> releaseMemoryBuffer(); |
| 63 | }; |
| 64 | |
| 65 | } // end namespace objcopy |
| 66 | } // end namespace llvm |
| 67 | |
| 68 | #endif // LLVM_TOOLS_OBJCOPY_BUFFER_H |