blob: 2da03dee1affa9b7f24efccc4e9883db45bec80d [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
20void 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
29Error FileBuffer::commit() { return Buf->commit(); }
30
31uint8_t *FileBuffer::getBufferStart() {
32 return reinterpret_cast<uint8_t *>(Buf->getBufferStart());
33}
34
35void MemBuffer::allocate(size_t Size) {
36 Buf = WritableMemoryBuffer::getNewMemBuffer(Size, getName());
37}
38
39Error MemBuffer::commit() { return Error::success(); }
40
41uint8_t *MemBuffer::getBufferStart() {
42 return reinterpret_cast<uint8_t *>(Buf->getBufferStart());
43}
44
45std::unique_ptr<WritableMemoryBuffer> MemBuffer::releaseMemoryBuffer() {
46 return std::move(Buf);
47}
48
49} // end namespace objcopy
50} // end namespace llvm