SmallVectorMemoryBuffer.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- SmallVectorMemoryBuffer.h --------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file declares a wrapper class to hold the memory into which an
  15. // object will be generated.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_SUPPORT_SMALLVECTORMEMORYBUFFER_H
  19. #define LLVM_SUPPORT_SMALLVECTORMEMORYBUFFER_H
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/Support/MemoryBuffer.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. namespace llvm {
  24. /// SmallVector-backed MemoryBuffer instance.
  25. ///
  26. /// This class enables efficient construction of MemoryBuffers from SmallVector
  27. /// instances. This is useful for MCJIT and Orc, where object files are streamed
  28. /// into SmallVectors, then inspected using ObjectFile (which takes a
  29. /// MemoryBuffer).
  30. class SmallVectorMemoryBuffer : public MemoryBuffer {
  31. public:
  32. /// Construct a SmallVectorMemoryBuffer from the given SmallVector r-value.
  33. SmallVectorMemoryBuffer(SmallVectorImpl<char> &&SV,
  34. bool RequiresNullTerminator = true)
  35. : SmallVectorMemoryBuffer(std::move(SV), "<in-memory object>",
  36. RequiresNullTerminator) {}
  37. /// Construct a named SmallVectorMemoryBuffer from the given SmallVector
  38. /// r-value and StringRef.
  39. SmallVectorMemoryBuffer(SmallVectorImpl<char> &&SV, StringRef Name,
  40. bool RequiresNullTerminator = true)
  41. : SV(std::move(SV)), BufferName(std::string(Name)) {
  42. if (RequiresNullTerminator) {
  43. this->SV.push_back('\0');
  44. this->SV.pop_back();
  45. }
  46. init(this->SV.begin(), this->SV.end(), false);
  47. }
  48. // Key function.
  49. ~SmallVectorMemoryBuffer() override;
  50. StringRef getBufferIdentifier() const override { return BufferName; }
  51. BufferKind getBufferKind() const override { return MemoryBuffer_Malloc; }
  52. private:
  53. SmallVector<char, 0> SV;
  54. std::string BufferName;
  55. };
  56. } // namespace llvm
  57. #endif
  58. #ifdef __GNUC__
  59. #pragma GCC diagnostic pop
  60. #endif