ObjectMemoryBuffer.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //===- ObjectMemoryBuffer.h - SmallVector-backed MemoryBuffrer -*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file declares a wrapper class to hold the memory into which an
  11. // object will be generated.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_EXECUTIONENGINE_OBJECTMEMORYBUFFER_H
  15. #define LLVM_EXECUTIONENGINE_OBJECTMEMORYBUFFER_H
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/Support/MemoryBuffer.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. namespace llvm {
  20. /// \brief SmallVector-backed MemoryBuffer instance.
  21. ///
  22. /// This class enables efficient construction of MemoryBuffers from SmallVector
  23. /// instances. This is useful for MCJIT and Orc, where object files are streamed
  24. /// into SmallVectors, then inspected using ObjectFile (which takes a
  25. /// MemoryBuffer).
  26. class ObjectMemoryBuffer : public MemoryBuffer {
  27. public:
  28. /// \brief Construct an ObjectMemoryBuffer from the given SmallVector r-value.
  29. ///
  30. /// FIXME: It'd be nice for this to be a non-templated constructor taking a
  31. /// SmallVectorImpl here instead of a templated one taking a SmallVector<N>,
  32. /// but SmallVector's move-construction/assignment currently only take
  33. /// SmallVectors. If/when that is fixed we can simplify this constructor and
  34. /// the following one.
  35. ObjectMemoryBuffer(SmallVectorImpl<char> &&SV)
  36. : SV(std::move(SV)), BufferName("<in-memory object>") {
  37. init(this->SV.begin(), this->SV.end(), false);
  38. }
  39. /// \brief Construct a named ObjectMemoryBuffer from the given SmallVector
  40. /// r-value and StringRef.
  41. ObjectMemoryBuffer(SmallVectorImpl<char> &&SV, StringRef Name)
  42. : SV(std::move(SV)), BufferName(Name) {
  43. init(this->SV.begin(), this->SV.end(), false);
  44. }
  45. const char* getBufferIdentifier() const override { return BufferName.c_str(); }
  46. BufferKind getBufferKind() const override { return MemoryBuffer_Malloc; }
  47. private:
  48. SmallVector<char, 0> SV;
  49. std::string BufferName;
  50. };
  51. } // namespace llvm
  52. #endif