[PATCH] [RFC] Introduce MemoryBuffer::getSlice.

Alp Toker alp at nuanti.com
Fri Jul 4 16:06:59 PDT 2014


On 05/07/2014 01:53, Peter Collingbourne wrote:
> Hi rafael,
>
> http://reviews.llvm.org/D4394
>
> Files:
>    include/llvm/Support/MemoryBuffer.h
>    lib/Support/MemoryBuffer.cpp
>
> Index: include/llvm/Support/MemoryBuffer.h
> ===================================================================
> --- include/llvm/Support/MemoryBuffer.h
> +++ include/llvm/Support/MemoryBuffer.h
> @@ -134,6 +134,13 @@
>                                           std::unique_ptr<MemoryBuffer> &Result,
>                                           int64_t FileSize = -1);
>   
> +  /// \brief Returns a slice of the given MemoryBuffer, to which ownership is
> +  /// transferred. Note that the slice must be null terminated if
> +  /// RequiresNullTerminator is true.
> +  static MemoryBuffer *getSlice(std::unique_ptr<MemoryBuffer> Underlying,
> +                                uint64_t Offset, uint64_t Len,
> +                                bool RequiresNullTerminator);
> +
>     //===--------------------------------------------------------------------===//
>     // Provided for performance analysis.
>     //===--------------------------------------------------------------------===//
> Index: lib/Support/MemoryBuffer.cpp
> ===================================================================
> --- lib/Support/MemoryBuffer.cpp
> +++ lib/Support/MemoryBuffer.cpp
> @@ -168,6 +168,45 @@
>   
>   
>   //===----------------------------------------------------------------------===//
> +// MemoryBufferSlice implementation.
> +//===----------------------------------------------------------------------===//
> +
> +namespace {
> +/// MemoryBufferSlice - slice of an owned underlying MemoryBuffer.
> +class MemoryBufferSlice : public MemoryBuffer {
> +  std::unique_ptr<MemoryBuffer> Underlying;

Why should a slice of an existing memory buffer take over unique 
ownership of the buffer it references?

This seems to defeat the purpose of building a non-intrusive slice of 
some existing buffer because it would require the slice to take over 
ownership from the primary consumer.

I posted a similar feature a couple of minutes ago that preserves 
original ownership and instead just maintains a reference. That's likely 
closer to what's needed, see thread: Re: [PATCH] LTO: introduce object 
file-based on-disk module format.

Alp.


> +
> +public:
> +  MemoryBufferSlice(std::unique_ptr<MemoryBuffer> U, uint64_t Offset,
> +                    uint64_t Len, bool RequiresNullTerminator)
> +      : Underlying(std::move(U)) {
> +    init(Underlying->getBufferStart() + Offset,
> +         Underlying->getBufferStart() + Offset + Len, RequiresNullTerminator);
> +  }
> +
> +  const char *getBufferIdentifier() const override {
> +    return Underlying->getBufferIdentifier();
> +  }
> +
> +  BufferKind getBufferKind() const override {
> +    return Underlying->getBufferKind();
> +  }
> +};
> +}
> +
> +/// getSlice - returns a slice of the given MemoryBuffer, to which ownership is
> +/// transferred. Note that the slice must be null terminated if
> +/// RequiresNullTerminator is true!
> +MemoryBuffer *MemoryBuffer::getSlice(std::unique_ptr<MemoryBuffer> Underlying,
> +                                     uint64_t Offset, uint64_t Len,
> +                                     bool RequiresNullTerminator) {
> +  assert(Offset + Len < Underlying->getBufferSize());
> +  return new MemoryBufferSlice(std::move(Underlying), Offset, Len,
> +                               RequiresNullTerminator);
> +}
> +
> +
> +//===----------------------------------------------------------------------===//
>   // MemoryBuffer::getFile implementation.
>   //===----------------------------------------------------------------------===//
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits

-- 
http://www.nuanti.com
the browser experts




More information about the llvm-commits mailing list