blob: 1789097f276a8d29a3bbcac58df26d91910c7678 [file] [log] [blame]
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +00001//===- Buffer.cpp ---------------------------------------------------------===//
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#include "Buffer.h"
10#include "llvm-objcopy.h"
11#include "llvm/Support/FileOutputBuffer.h"
12#include "llvm/Support/MemoryBuffer.h"
13#include <memory>
14
15namespace llvm {
16namespace objcopy {
17
18Buffer::~Buffer() {}
19
Jordan Rupprecht881cae72019-01-22 23:49:16 +000020Error FileBuffer::allocate(size_t Size) {
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000021 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
22 FileOutputBuffer::create(getName(), Size, FileOutputBuffer::F_executable);
Jordan Rupprecht881cae72019-01-22 23:49:16 +000023 // 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 Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000027 Buf = std::move(*BufferOrErr);
Jordan Rupprecht881cae72019-01-22 23:49:16 +000028 return Error::success();
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000029}
30
Jordan Rupprecht881cae72019-01-22 23:49:16 +000031Error 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 Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000037
38uint8_t *FileBuffer::getBufferStart() {
39 return reinterpret_cast<uint8_t *>(Buf->getBufferStart());
40}
41
Jordan Rupprecht881cae72019-01-22 23:49:16 +000042Error MemBuffer::allocate(size_t Size) {
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000043 Buf = WritableMemoryBuffer::getNewMemBuffer(Size, getName());
Jordan Rupprecht881cae72019-01-22 23:49:16 +000044 return Error::success();
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000045}
46
47Error MemBuffer::commit() { return Error::success(); }
48
49uint8_t *MemBuffer::getBufferStart() {
50 return reinterpret_cast<uint8_t *>(Buf->getBufferStart());
51}
52
53std::unique_ptr<WritableMemoryBuffer> MemBuffer::releaseMemoryBuffer() {
54 return std::move(Buf);
55}
56
57} // end namespace objcopy
58} // end namespace llvm