[PATCH] unique_ptrify llvm::getLazyBitcodeModule's MemoryBuffer parameter

Duncan P. N. Exon Smith dexonsmith at apple.com
Tue Aug 12 16:07:49 PDT 2014


> On 2014-Aug-12, at 15:50, David Blaikie <dblaikie at gmail.com> wrote:
> 
> Maybe that's what it'll come to - I really dislike optional ownership,
> but if used judiciously it might be a tool we need in our toolkit.
> This certainly isn't the only place where this API conundrum has come
> up.
> 
> - David

I guess another option is to use `shared_ptr<MemoryBuffer>`.  Then the
caller decides whether to keep ownership.

You can use a null deleter to handle unowned MemoryBuffers if necessary.

    template <class T> struct null_deleter {
      void operator()(T *) const {}
    };
    template <class T> std::shared_ptr<T> make_unowned_shared_ptr(T *P) {
      return std::shared_ptr<T>(P, null_deleter<T>());
    };
    ErrorOr<Module *> foo(MemoryBuffer &Buffer) {
      return getLazyBitcodeModule(make_unowned_shared_ptr(&Buffer));
    };

Of course, this is just another way to "lie" about ownership.



More information about the llvm-commits mailing list