diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-02-13 10:54:46 -0500 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-02-13 10:54:46 -0500 |
| commit | 02f70cda8a9fb038705e03b6e65625119bdef4e7 (patch) | |
| tree | f604b9c3d64b807298eaa36b42d38cc12194c076 /src/zig_llvm.cpp | |
| parent | 2dcff95bd2fb8f377491ac48b0ecf961183abcd7 (diff) | |
| download | zig-02f70cda8a9fb038705e03b6e65625119bdef4e7.tar.gz zig-02f70cda8a9fb038705e03b6e65625119bdef4e7.zip | |
zig_llvm.cpp uses new(std::nothrow)
This fixes a mismatched malloc/delete because
we were allocating with malloc and then llvm was
freeing with delete.
Diffstat (limited to 'src/zig_llvm.cpp')
| -rw-r--r-- | src/zig_llvm.cpp | 35 |
1 files changed, 8 insertions, 27 deletions
diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index 3e92752d9f..97c07ab820 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -43,31 +43,8 @@ #include <stdlib.h> -#if defined(_MSC_VER) -#define ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict) -#else -#define ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__)) -#endif - using namespace llvm; -template<typename T, typename... Args> -ATTRIBUTE_RETURNS_NOALIAS static inline T * create(Args... args) { - T * ptr = reinterpret_cast<T*>(malloc(sizeof(T))); - if (ptr == nullptr) - return nullptr; - new (ptr) T(args...); - return ptr; -} - -template<typename T> -static inline void destroy(T * ptr) { - if (ptr != nullptr) { - ptr[0].~T(); - } - free(ptr); -} - void ZigLLVMInitializeLoopStrengthReducePass(LLVMPassRegistryRef R) { initializeLoopStrengthReducePass(*unwrap(R)); } @@ -116,7 +93,11 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM Module* module = unwrap(module_ref); - PassManagerBuilder *PMBuilder = create<PassManagerBuilder>(); + PassManagerBuilder *PMBuilder = new(std::nothrow) PassManagerBuilder(); + if (PMBuilder == nullptr) { + *error_message = strdup("memory allocation failure"); + return true; + } PMBuilder->OptLevel = target_machine->getOptLevel(); PMBuilder->SizeLevel = 0; @@ -150,7 +131,8 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM // Set up the per-function pass manager. legacy::FunctionPassManager FPM = legacy::FunctionPassManager(module); - FPM.add(create<TargetLibraryInfoWrapperPass>(tlii)); + auto tliwp = new(std::nothrow) TargetLibraryInfoWrapperPass(tlii); + FPM.add(tliwp); FPM.add(createTargetTransformInfoWrapperPass(target_machine->getTargetIRAnalysis())); if (assertions_on) { FPM.add(createVerifierPass()); @@ -446,10 +428,9 @@ unsigned ZigLLVMTag_DW_union_type(void) { } ZigLLVMDIBuilder *ZigLLVMCreateDIBuilder(LLVMModuleRef module, bool allow_unresolved) { - DIBuilder *di_builder = reinterpret_cast<DIBuilder*>(malloc(sizeof(DIBuilder))); + DIBuilder *di_builder = new(std::nothrow) DIBuilder(*unwrap(module), allow_unresolved); if (di_builder == nullptr) return nullptr; - new (di_builder) DIBuilder(*unwrap(module), allow_unresolved); return reinterpret_cast<ZigLLVMDIBuilder *>(di_builder); } |
