telecom • networking • design

an EventHelix.com blog


Leave a comment

C++ Internals: Mapping Inheritance and Virtual Functions to C

C++ to C conversionWe have already looked at mapping from C++ classes and methods to C structures and functions in a previous blog post. Here we will map inheritance and virtual functions to C code.

This blog post is based on the article mapping from C++ inheritance and virtual functions to C code. The contents of the article are summarized below.

Mapping a base class with virtual functions to C

A C++ base class with virtual functions requires extra logic to support the machinery for handling virtual functions. The following C++ base class….

class Shape
{
protected:
  int m_x;
  int m_y; 

public:
  virtual void Draw() = 0; 
  virtual void MoveTo(int newX, int newY);
  void Erase();
  Shape(int x, int y);
  virtual ~Shape();
};

…maps to the following C code.

typedef void (*VirtualFunctionPointer)(...);

struct VTable
{
   int d;  /* d and i are used in multiple inheritance */
   int i;
   VirtualFunctionPointer pFunc;
};

VTable VTableArrayForShape[] =
{
    { 0, 0, (VirtualFunctionPointer) pure_virtual_called_error_handler },
    { 0, 0, (VirtualFunctionPointer) Shape_MoveTo },
    { 0, 0, (VirtualFunctionPointer) Shape_Destructor }
};
struct Shape
{
  int m_x;
  int m_y;
  VTable *pVTable;
};

Points to note here are:

  • VTable structure used by the compiler to keep track of the virtual functions associated with a class.There is one instance of a VTable for every class containing virtual functions. All instances of a given class point to the same VTable.
  • The C++ compiler inserts an extra pointer to a vtable which will keep a function pointer to the virtual function that should be called.
  • The VTable pointer in the base class points to an array of VTables. The number of entries in the array of VTables depends upon the number of virtual functions in the class.
  • Note that a pure virtual function maps to function that will result in an exception.

For details refer to the article: Mapping from C++ inheritance and virtual functions to C code

Mapping an inherited class with virtual functions to C

The inheriting Circle class….

class Circle : public Shape
{
private:
   int m_radius;    // Radius of the circle

public:
   // Override to draw a circle
   virtual void Draw();   

   // Constructor for Circle
   Circle(int x, int y, int radius);

   // Destructor for Circle
   virtual ~Circle();
};

…maps to a new VTable array and a new struct:

VTable VTableArrayForCircle[] =
{
    { 0, 0, (VirtualFunctionPointer) Circle_Draw },
    { 0, 0, (VirtualFunctionPointer) Shape_MoveTo },
    { 0, 0, (VirtualFunctionPointer) Circle_Destructor }
};

struct Circle
{
   /* Fields inherited from Shape */
   int m_x;
   int m_y;
   VTable *pVTable;

   /* Fields added by Circle */
   int m_radius;
};
  • The inheriting class preserves structure offsets for inherited base class fields and the VTable array pointers.
  • A new VTable array is defined for the inherited class.
    • The pure virtual function entry has now been replaced with a call to Circle’s Draw function.
    • The entry for a function that is not being inherited is preserved (Shape’s MoveTo)
    • Shape class has a virtual destructor. The destructor vtable entry now points to Circle’s destructor.

For details refer to the article: Mapping from C++ inheritance and virtual functions to C code

Mapping a virtual function call to C code

A virtual function call like…

 pShape->Draw();

…is replaced by a call through a virtual table function pointer.

  (pShape->pVTable[0].pFunc)(pShape);

Explore More

We have covered basics of inheritance handling here. For details about constructor and destructor definitions and vtable array pointer initialization refer to the article:  Mapping from C++ inheritance and virtual functions to C code.

Advertisements


2 Comments

C++ Internals: Mapping from C++ classes and methods to C structs and functions

Mapping from C++ to C code

C++ internals can be better understood by mapping them to corresponding C code.

Refer to the article mapping from C++ classes and methods to C structs and functions.

The contents of the article are summarized below.

This pointer maps to an additional parameter

A C++ method maps to a C method with an additional this pointer. For example, the UpdateX method C++ method…

void A::UpdateX(int newX)
{
  if (g_y != 0 && m_x < newX)
  {
    m_x = newX;
  }
}

…translates to the following C code:

void A_UpdateX(A *this_ptr, int newX)
{
  if (g_y != 0 && this_ptr->m_x < newX)
  {
     this_ptr->m_x = newX;
  }
}

Complete article: Mapping from C++ classes and methods to C structs and functions.

A class maps to a struct with associated methods

A C++ class is equivalent to a C structure with a class methods. The following class…

class A
{
private:
  int m_x;
  static int g_y;
  int m_z;

  // Should be invoked when the object ends
  void InformEnd();

public:
  A(int x);
  ~A();
  void UpdateX(int newX);
  static void UpdateY(int newY);
};

…is represented as the C structure with functions.

  • Methods have a corresponding this pointer.
  • Static methods are functions that do not get an additional this pointer.
  • Static variables map to global variables.
struct A
{
  int m_x;
  int m_z;
};
int g_y = 0; /* Represents static variable */
A *A_Constructor(A *this_ptr, int x);
void A_Destructor(A *this_ptr, BOOLEAN dynamic);
void A_UpdateX(A *this_ptr, int newX);
void A_UpdateY(int newY); /* Represents a static method. */

Complete article: Mapping from C++ classes and methods to C structs and functions.

Constructor representation in C

In C++, objects may be allocated on the heap or the stack. This has implications in the mapping from a C++ constructor to an initialization function in C.

A::A(int x)
{
  m_x = x;
  m_z = 0;
}

The mapped constructor takes an additional this pointer. A NULL is passed as the this pointer when the object has to be allocated on the heap. A valid stack address is passed when an object is on the stack.

If a NULL pointer is passed to the construction function, it allocates memory before performing the initialization.

A *A_Constructor(A *this_ptr, int x)
{
  /*Check if memory has been allocated for struct A. */
  if (this_ptr == NULL)
  {
    /*Allocate memory of size A. */
    this_ptr = (A *) malloc(sizeof(A));
  }

  /* Once the memory has been allocated for A, initialize members of A. */
  if (this_ptr)
  {
    this_ptr->m_x = x;
    this_ptr->m_z = 0;
  }
  return this_ptr;
}

Complete article: Mapping from C++ classes and methods to C structs and functions.

Destructor representation in C

Destructors also deal with deletion of stack and heap objects.

A::~A()
{
  InformEnd();
}

If the object was allocated on the heap, a boolean is passed to signal to the construction function that the memory was allocated dynamically and needs to be freed.

void A_Destructor(A *this_ptr, BOOLEAN dynamic)
{
  InformEnd(this_ptr);

  /* If the memory was dynamically allocated for A, explicitly free it. */
  if (dynamic)
  {
    free(this_ptr);
  }
}

Complete article: Mapping from C++ classes and methods to C structs and functions.


1 Comment

SCTP connection setup, reconfiguration and release

Stream Control Transmission Protocol (SCTP) connection setup, reconfiguration and release

Stream Control Transmission Protocol (SCTP) connection setup, reconfiguration and release

Stream Control Transmission Protocol (SCTP) is a relatively new transport layer in the IP Protocol Stack. In an LTE network, SCTP is used to provide guaranteed message delivery between the MME and eNodeB.

SCTP belongs to the SIGTRAN protocol family and has been used as the transport layer for carrying telecom signaling over IP. SCTP provides a reliable transport service that operates at message level (unlike TCP that provides a byte stream
interface with no message boundaries).

SCTP allows multiple message streams to be exchanged on a single SCTP connection. Data from multiple streams can be sent in a single SCTP message as chunks. Selective acknowledgements are supported at individual chunk level.

SCTP protocol allows dynamic configuration of the IP addresses. Similar to SS7, SCTP can be switched over from one link to another. SCTP supports a make-before-break changeover, i.e. the packet stream is moved before removing the link that needs to be taken out of service.

In this sequence diagram we will be examining some of the features of SCTP.

  1. SCTP Connection establishment.
  2. SCTP data exchange and selective acknowledgement.
  3. Addition of a new IP address to an SCTP connection.
  4. Switching over to the new IP address.
  5. Removing the old IP address.
  6. SCTP connection release

SCTP Connection Setup, Reconfiguration and Release


Leave a comment

LTE Attach and Default Bearer Setup Sequence Diagram

LTE Attach and Default Bearer Setup

Click to open the PDF sequence diagram describing LTE attach and default bearer setup

This call flow describes the signaling involved in LTE attach and default bearer setup. The sequence diagram covers the following phases:

  1. Random Access Procedure: The UE uses a slotted aloha procedure to access the eNodeB. The UE sends a preamble on a random access channel, the network replies with corrected timing and resource assignments.
  2. RRC Connection Establishment: The UE and the eNodeB then interact to establish a Signaling Radio Bearer (SRB). The SRB will be used for the signaling between the eNodeB and the UE.
  3. Attach and Authentication: The UE now registers with the Core Network. Session contexts are setup at the MME and the Serving Gateway. This step also results in the authentication of the UE and the Network.
  4. Default Radio Bearer Setup: Finally, the default bearer for data transfer is established. Default bearer session is established at the UE, eNodeB, MME, Serving GW and PDN Gateway. User data sessions is exchanged once the default bearer is setup.

The sequence diagram also contains links to access further details about the messages. Click on the messages with blue title for details about the message.

RRC Connection, LTE Attach and Default Radio Bearer Setup

 


1 Comment

LTE Visualization Tool

The LTE Visualization Tool shows the organization of LTE channels in a subcarrier-symbol grid.

You can display:

Signals

LTE Signals

REF, S-SYNC and P-SYNC signals are shown on a subcarrier-symbol grid.

Channels

LTE Channels

PHICH, PDCCH, PBCH and PHICH channels are shown on the subcarrier symbol grid.

Throughput

LTE Throughput and Resource Element Usage

Total count of PDSCH, PBCH, PCFICH, PDCCH channels is shown. The throughout with QPSK, 16QAM and 64QAM is also calculated.

You can configure the:

  • Channel bandwidth
  • Number of antennas
  • Cell identity
  • PHICH duration
  • PHICH group scaling
  • PDCCH Quadruplets

The LTE Visualization Tool may be downloaded for free from:

LTE Visualization Tool


1 Comment

LTE RRC Connection Setup

lte-rrc-connection

Establishing an RRC connection in LTE typically involves the following steps:

  1. UE initiates a session to the eNodeB using a random RA-RNTI.
  2. eNodeB assigns a C-RNTI and sends timing adjustment to the UE.
  3. UE adjusts the the timing and requests an RRC Connection Setup via the UL-SCH channel.
  4. The eNodeB sends the RRC Connection Setup to the UE on the DL-SCH channel. This message sets up the SRB. DRB may also be established at this point.
  5. Finally, the UE acknowledges the message with RRC Connection Complete message.

The messages in the RRC Connection Setup are described in the following presentation.

LTE RRC Connection Setup Messaging