Skip to content

[Linux-Port] Design and Implementation of adapter classes for COM #1341

Closed

Description

@ehsannas

Purpose

The purpose of this issue is to summarize one possible implementation for adapter classes/mechanisms that can enable COM-related code in DXC to compile and run successfully on Linux.

Background

In case the reader is not very familiar with COM, I highly suggest reading the Microsoft documentation and all of its related articles, specifically the COM Technical Overview.

Needless to say, the current usages of COM in DXC adhere to the technical requirements described in the above documentation. In order to enable a working code on Linux, however, we need to fill the void of some Windows-specific definitions/types/classes/mechanisms.

Below I point out a few important points from the documentation that inspire our implementation.

Object Life Cycle

One of the important aspects of enabling COM, is object life-cycle management. According to the reference:

All COM interfaces inherit from the IUnknown interface. The IUnknown interface contains the fundamental COM operations for polymorphism and instance lifetime management.
...
The lifetime of a COM object instance is controlled by its reference count.

Therefore we need to implement the IUnknown on Linux, and control the object life cycle through its methods.

Unique IIDs (Object runtime type information)

A COM object exposes its features through an interface, which is a collection of member functions.

In order to use interfaces, we need to uniquely identify them, and also be able to invoke methods of each interface.

Every interface has its own unique interface identifier, named an IID, which eliminates collisions that could occur with human-readable names.

A COM class is identified by using a unique 128-bit Class ID (CLSID) that associates a class with a particular deployment in the file system

Interfaces defined in the code base specify GUIDs such as:
https://github.com/Microsoft/DirectXShaderCompiler/blob/f8fa9c92a073aadec2720e17154f58d39ac22cf9/include/dxc/dxcapi.h#L146-L147

We need to find an alternative solution for these IDs.

QueryInterface

The IIDs described above are used (and passed around) throughout the code base. One of their most important usages is to enable the QueryInterface mechanism.

The QueryInterface member function provides polymorphism for COM. Call QueryInterface to determine at run time whether a COM object supports a particular interface. The COM object returns an interface pointer in the ppvObjectout parameter if it implements the requested interface, otherwise it returns NULL. The QueryInterface member function enables navigation among all of the interfaces that a COM object supports.

In order for QueryInterface to work, it needs to be able to compare the IID of different interfaces. It does so using the __uuidof(NAME) operator. This operator is also Microsoft-specific, therefore our solution should be able to replace this mechanism with an equivalent that can work on Linux.

IMalloc

IMalloc is used by COM libraries and object handlers to manage memory. Since IMalloc is not implemented on Linux, we will need to provide an adapter class for it.

IStream

The IStream interface lets you read and write data to stream objects.

Since IStream is not implemented on Linux, we will need to provide an adapter class for it.

CComPtr

CComPtr is a smart pointer class for managing COM interface pointers. We need to implement an equivalent in Linux.

Implementation

This is just a high-level overview of the implementation, and code snippets provided are just for guidance. For full working code please consider looking at changelists/pull requests. Also note that interfaces defined below satisfy the IID requirements explained above using DECLARE_CROSS_PLATFORM_UUIDOF. Since that is a long discussion on its own, please refer to issue #1342 for explanation of how we implement that aspect.

IUnknown

Our implementation is inspired by this article.

  • In order to provide thread-safe reference counting, we use std::atomic for the counter.
  • virtual HRESULT QueryInterface(REFIID riid, void **ppvObject) = 0; makes IUnknown an abstract class as expected.
struct IUnknown {
  virtual HRESULT QueryInterface(REFIID riid, void **ppvObject) = 0;
  virtual ULONG AddRef() {
    ++m_count;
    return m_count;
  }
  virtual ULONG Release() {
    --m_count;
    if (m_count == 0) {
      delete this;
    }
    return m_count;
  }
  virtual ~IUnknown() {}
  template <class Q> HRESULT QueryInterface(Q **pp) {
    return QueryInterface(__uuidof(Q), (void **)pp);
  }
  private:
  std::atomic<unsigned long> m_count;

  DECLARE_CROSS_PLATFORM_UUIDOF(IUnknown)
};

This implementation takes care of reference counting and object life management. Refer to issue #... for how we satisfy the IID requirements.

IMalloc

Similar to any other COM interface, IMalloc inherits from IUnknown. We implement Alloc using malloc, Realloc using realloc, and Free using free.

struct IMalloc : public IUnknown {
  virtual void *Alloc(size_t size) { return malloc(size); }
  virtual void *Realloc(void *ptr, size_t size) { return realloc(ptr, size); }
  virtual void Free(void *ptr) { free(ptr); }
  ...
};

IStream

Again inheriting from IUnkown. We currently do not provide any implementation for any of the methods for IStream, however, we do provide the interface declaration so that the compiler can compile its heavy usage throughout the code base. Implementation can be provided later if need be.

struct ISequentialStream : public IUnknown {
  virtual HRESULT Read(void *pv, ULONG cb, ULONG *pcbRead) = 0;
  virtual HRESULT Write(const void *pv, ULONG cb, ULONG *pcbWritten) = 0;
  ...
  DECLARE_CROSS_PLATFORM_UUIDOF(ISequentialStream)
};

struct IStream : public ISequentialStream {
  virtual HRESULT Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin,
                       ULARGE_INTEGER *plibNewPosition) = 0;
  ...
  DECLARE_CROSS_PLATFORM_UUIDOF(IStream)
};

CComPtr

We implement a smart pointer class based on standard C++ practices. We implement all necessary methods such as Attach, Detach, Swap, etc in CComPtrBase, and other methods such as IsEqualObject and ComPtrAssign in CComPtr.

template <class T> class CComPtrBase {
  ...
  void Swap(CComPtrBase &other) { ... }
  ...
  virtual ~CComPtrBase() throw() { ... }
  ...
  // Define all kinds of operators
  ...
  void Release() throw() { ... }
  void Attach(T *p2) throw() { ... }
  T *Detach() throw() { ... }
  HRESULT CopyTo(T **ppT) throw() { ... }
  template <class Q> HRESULT QueryInterface(Q **pp) const throw() { ... }
  ...
};

template <class T> class CComPtr : public CComPtrBase<T> {
  ...
  bool IsEqualObject(IUnknown *pOther) throw() { ... }
  ...
  void ComPtrAssign(IUnknown **pp, IUnknown *lp, REFIID riid) { ... }
  ...
};

Activity

  1. added
    linux
    on Jun 4, 2018
  2. marcelolr commented on Jun 5, 2018

    @marcelolr
    Contributor

    Nice! Are the inlined implementations for expository purposes? Keeping the interface separate from the declarations is a good trait to keep - plus it helps when we end up inheriting from IUnknown multiple times to implement various interfaces.

  3. ehsannas commented on Jun 5, 2018

    @ehsannas
    ContributorAuthor

    Noted. We will do so in our pull requests.

  4. ehsannas commented on Jun 5, 2018

    @ehsannas
    ContributorAuthor

    For the templated classes we'll need to keep their function definitions in the header file, but we will separate out the implementation for the rest of the classes/structs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

linux

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions