]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/llvm-objcopy/Buffer.h
Vendor import of llvm trunk r351319 (just before the release_80 branch
[FreeBSD/FreeBSD.git] / tools / llvm-objcopy / Buffer.h
1 //===- Buffer.h -------------------------------------------------*- C++ -*-===//
2 //
3 //                      The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_TOOLS_OBJCOPY_BUFFER_H
11 #define LLVM_TOOLS_OBJCOPY_BUFFER_H
12
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/FileOutputBuffer.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include <memory>
17
18 namespace llvm {
19 namespace objcopy {
20
21 // The class Buffer abstracts out the common interface of FileOutputBuffer and
22 // WritableMemoryBuffer so that the hierarchy of Writers depends on this
23 // abstract interface and doesn't depend on a particular implementation.
24 // TODO: refactor the buffer classes in LLVM to enable us to use them here
25 // directly.
26 class Buffer {
27   StringRef Name;
28
29 public:
30   virtual ~Buffer();
31   virtual void allocate(size_t Size) = 0;
32   virtual uint8_t *getBufferStart() = 0;
33   virtual Error commit() = 0;
34
35   explicit Buffer(StringRef Name) : Name(Name) {}
36   StringRef getName() const { return Name; }
37 };
38
39 class FileBuffer : public Buffer {
40   std::unique_ptr<FileOutputBuffer> Buf;
41
42 public:
43   void allocate(size_t Size) override;
44   uint8_t *getBufferStart() override;
45   Error commit() override;
46
47   explicit FileBuffer(StringRef FileName) : Buffer(FileName) {}
48 };
49
50 class MemBuffer : public Buffer {
51   std::unique_ptr<WritableMemoryBuffer> Buf;
52
53 public:
54   void allocate(size_t Size) override;
55   uint8_t *getBufferStart() override;
56   Error commit() override;
57
58   explicit MemBuffer(StringRef Name) : Buffer(Name) {}
59
60   std::unique_ptr<WritableMemoryBuffer> releaseMemoryBuffer();
61 };
62
63 } // end namespace objcopy
64 } // end namespace llvm
65
66 #endif // LLVM_TOOLS_OBJCOPY_BUFFER_H