From ba689eeb38e962419746ef863eef56761a6f5a0e Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Tue, 14 Oct 2014 00:30:59 +0000 Subject: [PATCH] Introduce LLVMWriteBitcodeToMemoryBuffer C API function. llvm-svn: 219643 --- llvm/include/llvm-c/BitWriter.h | 3 +++ llvm/lib/Bitcode/Writer/BitWriter.cpp | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/llvm/include/llvm-c/BitWriter.h b/llvm/include/llvm-c/BitWriter.h index 0ec8e752dcc4..f25ad3a445f5 100644 --- a/llvm/include/llvm-c/BitWriter.h +++ b/llvm/include/llvm-c/BitWriter.h @@ -45,6 +45,9 @@ int LLVMWriteBitcodeToFD(LLVMModuleRef M, int FD, int ShouldClose, descriptor. Returns 0 on success. Closes the Handle. */ int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int Handle); +/** Writes a module to a new memory buffer and returns it. */ +LLVMMemoryBufferRef LLVMWriteBitcodeToMemoryBuffer(LLVMModuleRef M); + /** * @} */ diff --git a/llvm/lib/Bitcode/Writer/BitWriter.cpp b/llvm/lib/Bitcode/Writer/BitWriter.cpp index 6c9f7b3276fd..7218ea05bb5b 100644 --- a/llvm/lib/Bitcode/Writer/BitWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitWriter.cpp @@ -39,3 +39,11 @@ int LLVMWriteBitcodeToFD(LLVMModuleRef M, int FD, int ShouldClose, int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) { return LLVMWriteBitcodeToFD(M, FileHandle, true, false); } + +LLVMMemoryBufferRef LLVMWriteBitcodeToMemoryBuffer(LLVMModuleRef M) { + std::string Data; + raw_string_ostream OS(Data); + + WriteBitcodeToFile(unwrap(M), OS); + return wrap(MemoryBuffer::getMemBufferCopy(OS.str()).release()); +}