Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.7 KiB

  1. //===---- ObjectBuffer.h - Utility class to wrap object image memory -----===//
  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_OBJECTBUFFER_H
  15. #define LLVM_EXECUTIONENGINE_OBJECTBUFFER_H
  16. #include "llvm/ADT/OwningPtr.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/Support/MemoryBuffer.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. namespace llvm {
  21. /// ObjectBuffer - This class acts as a container for the memory buffer used during
  22. /// generation and loading of executable objects using MCJIT and RuntimeDyld. The
  23. /// underlying memory for the object will be owned by the ObjectBuffer instance
  24. /// throughout its lifetime. The getMemBuffer() method provides a way to create a
  25. /// MemoryBuffer wrapper object instance to be owned by other classes (such as
  26. /// ObjectFile) as needed, but the MemoryBuffer instance returned does not own the
  27. /// actual memory it points to.
  28. class ObjectBuffer {
  29. public:
  30. ObjectBuffer() {}
  31. ObjectBuffer(MemoryBuffer* Buf) : Buffer(Buf) {}
  32. virtual ~ObjectBuffer() {}
  33. /// getMemBuffer - Like MemoryBuffer::getMemBuffer() this function
  34. /// returns a pointer to an object that is owned by the caller. However,
  35. /// the caller does not take ownership of the underlying memory.
  36. MemoryBuffer *getMemBuffer() const {
  37. return MemoryBuffer::getMemBuffer(Buffer->getBuffer(), "", false);
  38. }
  39. const char *getBufferStart() const { return Buffer->getBufferStart(); }
  40. size_t getBufferSize() const { return Buffer->getBufferSize(); }
  41. protected:
  42. // The memory contained in an ObjectBuffer
  43. OwningPtr<MemoryBuffer> Buffer;
  44. };
  45. /// ObjectBufferStream - This class encapsulates the SmallVector and
  46. /// raw_svector_ostream needed to generate an object using MC code emission
  47. /// while providing a common ObjectBuffer interface for access to the
  48. /// memory once the object has been generated.
  49. class ObjectBufferStream : public ObjectBuffer {
  50. public:
  51. ObjectBufferStream() : OS(SV) {}
  52. virtual ~ObjectBufferStream() {}
  53. raw_ostream &getOStream() { return OS; }
  54. void flush()
  55. {
  56. OS.flush();
  57. // Make the data accessible via the ObjectBuffer::Buffer
  58. Buffer.reset(MemoryBuffer::getMemBuffer(StringRef(SV.data(), SV.size()),
  59. "",
  60. false));
  61. }
  62. protected:
  63. SmallVector<char, 4096> SV; // Working buffer into which we JIT.
  64. raw_svector_ostream OS; // streaming wrapper
  65. };
  66. } // namespace llvm
  67. #endif