Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is marshalling? What is happening when something is "marshalled?"

I know this question has been asked, at least here.

But there wasn't a satisfactory answer, at least not to me. There is a lot of talk about marshalling as regards interoperating with unmanaged code, but what about marshalling from one thread to another, as we have to do in .NET sometimes.

This makes me ask, what is marshalling, really? When you give a definition of marshalling, how would you define it so that it is explaining the case of interoperability, as well as the cases where you are "marshalling" between threads?

like image 640
richard Avatar asked Apr 08 '11 21:04

richard


People also ask

What do you mean by marshalling?

1 : to place in proper rank or position marshaling the troops. 2 : to bring together and order in an appropriate or effective way marshal arguments marshaled her thoughts before answering the question. 3 : to lead ceremoniously or solicitously : usher marshaling her little group of children down the street.

What is marshalling and why do we need it?

Marshalling is a "medium" for want of a better word or a gateway, to communicate with the unmanaged world's data types and vice versa, by using the pinvoke, and ensures the data is returned back in a safe manner.

Why is it called marshalling?

Usually the word "marshalling" is used when you're crossing some sort of boundary. Three obvious use cases: Remoting: the RPC data is marshalled to a separate machine (usually) AppDomains: an object crossing an AppDomain boundary needs to be marshalled (or it may be marshalled by reference)

Is marshalling the same as serialization?

Marshaling and serialization are loosely synonymous in the context of remote procedure call, but semantically different as a matter of intent. In particular, marshaling is about getting parameters from here to there, while serialization is about copying structured data to or from a primitive form such as a byte stream.


2 Answers

Computations often need to move data from one site to another, and don't have any shared memory. So one computation sends a message containing the data to the other.

How should that data, if it is arbitrarily complicated, be sent in a message?

Marshalling is the process of converting a data field, or an entire set of related structures, into a serialized string that can be sent in a message. To marshall a binary number, one might convert it to hexadecimal digit string, if the message format must be text. If the message will carry binary data, the binary number might be converted into 4 little-endian normalized binary bytes and sent that way. Pointers are harder; one often has to convert them into an abstract reference (e.g., a "node number") that is independent of the actual memory locations.

Of course, if you "marshall" data, you must eventually "unmarshall", which is the process of reading the serial stream and reconstructing the transmitted data (structure).

Often there are (un)marshalling routines in a library that are used to accomplish this purpose, and sometimes there are even tools that will manufacture all the calls needed on the (un)marshalling routines to send/recieve the data.

like image 72
Ira Baxter Avatar answered Oct 06 '22 01:10

Ira Baxter


Marshalling is taking data, of some form, and translating it into a separate form. It's a very generic term, and used in many places with subtle differences in meaning.

For example, in .NET, the interop layer when you're working with native types "marshals" your data from the .NET type into the appropriate form to call the native method, then "marshals" the results back.

As for "marshalling" between threads - Often, you'll need to have code to run on a different thread than the current one. For example, if you're using Windows Forms, you can't change a UI element on a threadpool thread, so you'll need to "marshal" the call back to the UI thread. This is done by creating a delegate, and passing the delegate back to the user interface thread via Control.Invoke (which uses a rather complex system to post this back to the proper synchronization context), which in turn runs the delegate on the user interface thread for you.

like image 23
Reed Copsey Avatar answered Oct 06 '22 02:10

Reed Copsey