blob: 40670accac2b0a84af38f5f07d5436323bfd055b [file] [log] [blame]
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +00001//===- Buffer.h -------------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Shaposhnikov3d4c4ac2018-10-16 05:40:18 +00006//
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
17namespace llvm {
18namespace 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.
25class Buffer {
26 StringRef Name;
27
28public:
29 virtual ~Buffer();
Jordan Rupprecht881cae72019-01-22 23:49:16 +000030 virtual Error allocate(size_t Size) = 0;
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000031 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
38class FileBuffer : public Buffer {
39 std::unique_ptr<FileOutputBuffer> Buf;
40
41public:
Jordan Rupprecht881cae72019-01-22 23:49:16 +000042 Error allocate(size_t Size) override;
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000043 uint8_t *getBufferStart() override;
44 Error commit() override;
45
46 explicit FileBuffer(StringRef FileName) : Buffer(FileName) {}
47};
48
49class MemBuffer : public Buffer {
50 std::unique_ptr<WritableMemoryBuffer> Buf;
51
52public:
Jordan Rupprecht881cae72019-01-22 23:49:16 +000053 Error allocate(size_t Size) override;
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000054 uint8_t *getBufferStart() override;
55 Error commit() override;
56
57 explicit MemBuffer(StringRef Name) : Buffer(Name) {}
58
59 std::unique_ptr<WritableMemoryBuffer> releaseMemoryBuffer();
60};
61
62} // end namespace objcopy
63} // end namespace llvm
64
65#endif // LLVM_TOOLS_OBJCOPY_BUFFER_H