Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 1 | //===- Buffer.cpp ---------------------------------------------------------===// |
| 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 | #include "Buffer.h" |
| 10 | #include "llvm-objcopy.h" |
| 11 | #include "llvm/Support/FileOutputBuffer.h" |
| 12 | #include "llvm/Support/MemoryBuffer.h" |
| 13 | #include <memory> |
| 14 | |
| 15 | namespace llvm { |
| 16 | namespace objcopy { |
| 17 | |
| 18 | Buffer::~Buffer() {} |
| 19 | |
| 20 | void FileBuffer::allocate(size_t Size) { |
| 21 | Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| 22 | FileOutputBuffer::create(getName(), Size, FileOutputBuffer::F_executable); |
| 23 | handleAllErrors(BufferOrErr.takeError(), [this](const ErrorInfoBase &E) { |
| 24 | error("failed to open " + getName() + ": " + E.message()); |
| 25 | }); |
| 26 | Buf = std::move(*BufferOrErr); |
| 27 | } |
| 28 | |
| 29 | Error FileBuffer::commit() { return Buf->commit(); } |
| 30 | |
| 31 | uint8_t *FileBuffer::getBufferStart() { |
| 32 | return reinterpret_cast<uint8_t *>(Buf->getBufferStart()); |
| 33 | } |
| 34 | |
| 35 | void MemBuffer::allocate(size_t Size) { |
| 36 | Buf = WritableMemoryBuffer::getNewMemBuffer(Size, getName()); |
| 37 | } |
| 38 | |
| 39 | Error MemBuffer::commit() { return Error::success(); } |
| 40 | |
| 41 | uint8_t *MemBuffer::getBufferStart() { |
| 42 | return reinterpret_cast<uint8_t *>(Buf->getBufferStart()); |
| 43 | } |
| 44 | |
| 45 | std::unique_ptr<WritableMemoryBuffer> MemBuffer::releaseMemoryBuffer() { |
| 46 | return std::move(Buf); |
| 47 | } |
| 48 | |
| 49 | } // end namespace objcopy |
| 50 | } // end namespace llvm |