Pointer Swizzling Technique

When we send structures containing pointers, data copy is involved to flatten the structure into contiguous buffer at the sender side and another copy is involved at the receiver side.

We can avoid one data copy at the receiver side using pointer swizzling

pointer swizzling

The contigous buffer should include space for the pointer member also during the marshall process as shown above. For the request structure, contigous buffer size would be

Sizeof(int) + sizeof(void*) + strlen(“Sample String”);

As the pointer value sent over the socket would not make any sense at the reciver end, it is sent with the buffer. At the receiver end, after receiving the  buffer, it would not copy the data to structure by creating a new object. Instead it would recalculate the pointer member and typecast the buffer to (struct Request*) and use it directly. It is illustrated in the below code snippet.

Sender Side
The below code snippet marshals the Request structure as per the pointer swizzling requirement. It copies the actual structure value at the beginning of the buffer and the actual values pointed by pointer after that.

Marshalling code:

   char buf[512];

   char *ptr = buf;

   memcpy(ptr, &request, sizeof(request)); //copy the struct at the beginning of the buffer

   ptr = ptr + sizeof(request);//copy the string value after the structure value

   strcpy(ptr, request.sVal);

   ret = write(sock, &buf, sizeof(buf));

   if(ret == -1){

      printf(“unable to send the request\n”);

      return 1;

   }

Receiver Side
The below code snippet unmarshals the Request structure. It resets the pointer value and sets it correctly to the actual value.

Unmarshalling Code:

      char buf[512];

      ret = read(new_sock, &buf, sizeof(buf));

      if(ret == -1){

          printf(“unable to receive the request\n”);

          return 1;

      }

      char *ptr = buf+ sizeof(int);

      //swizzle the pointer to the actual value(after the structure value in buffer)

      *(void**)ptr = buf + sizeof(struct Request);

      struct Request *request = (struct Request*) buf;

      printf(“Request got: %d %s\n”, request->iVal, request->sVal);

Advertisements

One thought on “Pointer Swizzling Technique

  1. Hi,

    Thanks for the blog post.
    I know that, this has been written long back. But, I came across your post today, when I searched for “Swizzling” and want to understand your code.

    So, I want to know the structure of your Request.
    So, can you please present a complete program, that I can take and compile on my system and test?

    Regards,
    Narendra

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s