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 | |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame^] | 20 | Error FileBuffer::allocate(size_t Size) { |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 21 | Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| 22 | FileOutputBuffer::create(getName(), Size, FileOutputBuffer::F_executable); |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame^] | 23 | // FileOutputBuffer::create() returns an Error that is just a wrapper around |
| 24 | // std::error_code. Wrap it in FileError to include the actual filename. |
| 25 | if (!BufferOrErr) |
| 26 | return createFileError(getName(), BufferOrErr.takeError()); |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 27 | Buf = std::move(*BufferOrErr); |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame^] | 28 | return Error::success(); |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame^] | 31 | Error FileBuffer::commit() { |
| 32 | Error Err = Buf->commit(); |
| 33 | // FileOutputBuffer::commit() returns an Error that is just a wrapper around |
| 34 | // std::error_code. Wrap it in FileError to include the actual filename. |
| 35 | return Err ? createFileError(getName(), std::move(Err)) : std::move(Err); |
| 36 | } |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 37 | |
| 38 | uint8_t *FileBuffer::getBufferStart() { |
| 39 | return reinterpret_cast<uint8_t *>(Buf->getBufferStart()); |
| 40 | } |
| 41 | |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame^] | 42 | Error MemBuffer::allocate(size_t Size) { |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 43 | Buf = WritableMemoryBuffer::getNewMemBuffer(Size, getName()); |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame^] | 44 | return Error::success(); |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | Error MemBuffer::commit() { return Error::success(); } |
| 48 | |
| 49 | uint8_t *MemBuffer::getBufferStart() { |
| 50 | return reinterpret_cast<uint8_t *>(Buf->getBufferStart()); |
| 51 | } |
| 52 | |
| 53 | std::unique_ptr<WritableMemoryBuffer> MemBuffer::releaseMemoryBuffer() { |
| 54 | return std::move(Buf); |
| 55 | } |
| 56 | |
| 57 | } // end namespace objcopy |
| 58 | } // end namespace llvm |