[llvm-commits] [llvm] r120201 - /llvm/trunk/include/llvm/ADT/InMemoryStruct.h

Daniel Dunbar daniel at zuster.org
Sun Nov 28 13:41:00 PST 2010


On Sun, Nov 28, 2010 at 11:38 AM, Chris Lattner <clattner at apple.com> wrote:
>
> On Nov 27, 2010, at 12:11 AM, Daniel Dunbar wrote:
>
>> Author: ddunbar
>> Date: Sat Nov 27 02:11:02 2010
>> New Revision: 120201
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=120201&view=rev
>> Log:
>> ADT/InMemoryStruct: Add an experimental helper class intended for use in
>> situations where on the common path an API can return a pointer to some direct
>> memory, but which on an exceptional path may need to return a pointer to a
>> temporary struct.
>
> I see that you're using this for cases when you need to do bswap in the macho reader, apparently trying to avoid a copy of the struct when no bswap is needed.
>
> However, I don't see how this is really a win.  You are avoiding a memcpy of a few smallish (~20-40 byte) structs, but this add a lot of complexity, and you have the have the same amount of stack space reserved.
>
> It seems more straight-forward and cleaner to just use structs directly.  Is the complexity this introduces solving a measured performance issue?

Nope, it is experimental.

I generally agree with your points, except that I don't see it adding
complexity on the client side.

 - Daniel

>
> -Chris
>
>>
>> Added:
>>    llvm/trunk/include/llvm/ADT/InMemoryStruct.h
>>
>> Added: llvm/trunk/include/llvm/ADT/InMemoryStruct.h
>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/InMemoryStruct.h?rev=120201&view=auto
>> ==============================================================================
>> --- llvm/trunk/include/llvm/ADT/InMemoryStruct.h (added)
>> +++ llvm/trunk/include/llvm/ADT/InMemoryStruct.h Sat Nov 27 02:11:02 2010
>> @@ -0,0 +1,77 @@
>> +//===- InMemoryStruct.h - Indirect Struct Access Smart Pointer --*- C++ -*-===//
>> +//
>> +//                     The LLVM Compiler Infrastructure
>> +//
>> +// This file is distributed under the University of Illinois Open Source
>> +// License. See LICENSE.TXT for details.
>> +//
>> +//===----------------------------------------------------------------------===//
>> +
>> +#ifndef LLVM_ADT_INMEMORYSTRUCT_H
>> +#define LLVM_ADT_INMEMORYSTRUCT_H
>> +
>> +#include <cassert>
>> +
>> +namespace llvm {
>> +
>> +/// \brief Helper object for abstracting access to an in-memory structure which
>> +/// may require some kind of temporary storage.
>> +///
>> +/// This class is designed to be used for accessing file data structures which
>> +/// in the common case can be accessed from a direct pointer to a memory mapped
>> +/// object, but which in some cases may require indirect access to a temporary
>> +/// structure (which, for example, may have undergone endianness translation).
>> +template<typename T>
>> +class InMemoryStruct {
>> +  typedef T value_type;
>> +  typedef value_type &reference;
>> +  typedef value_type *pointer;
>> +  typedef const value_type &const_reference;
>> +  typedef const value_type *const_pointer;
>> +
>> +  /// \brief The smart pointer target.
>> +  value_type *Target;
>> +
>> +  /// \brief A temporary object which can be used as a target of the smart
>> +  /// pointer.
>> +  value_type Contents;
>> +
>> +private:
>> +
>> +public:
>> +  InMemoryStruct() : Target(0) {}
>> +  InMemoryStruct(reference Value) : Target(&Contents), Contents(Value) {}
>> +  InMemoryStruct(pointer Value) : Target(Value) {}
>> +  InMemoryStruct(const InMemoryStruct<T> &Value) { *this = Value; }
>> +
>> +  void operator=(const InMemoryStruct<T> &Value) {
>> +    if (Value.Target != &Value.Contents) {
>> +      Target = Value.Target;
>> +    } else {
>> +      Target = &Contents;
>> +      Contents = Value.Contents;
>> +    }
>> +  }
>> +
>> +  const_reference operator*() const {
>> +    assert(Target && "Cannot dereference null pointer");
>> +    return *Target;
>> +  }
>> +  reference operator*() {
>> +    assert(Target && "Cannot dereference null pointer");
>> +    return *Target;
>> +  }
>> +
>> +  const_pointer operator->() const {
>> +    return Target;
>> +  }
>> +  pointer operator->() {
>> +    return Target;
>> +  }
>> +
>> +  operator bool() const { return Target != 0; }
>> +};
>> +
>> +}
>> +
>> +#endif
>>
>>
>> _______________________________________________
>> llvm-commits mailing list
>> llvm-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>
>




More information about the llvm-commits mailing list