Thursday, October 13, 2005
Thunks in C++
Thunks in C++
Advanced C++ programmers might know about "thunks". Thunks are compiler generated very small functions that typically do some pointer or address adjutment. Why is it needed? Consider
delete [] arr;
where arr is points to an array of 10 elements from dynamically allocated memory. When you do delete, how does the size of array obtained and correct number of destructors are called? This can be implented by a compiler by providing thunks: a small function that will retrieve the number of elements in a dynamically allocated array from memory and give it to the delete so that it will call the destructors that many number of times. Usually, thunks are used in vtables where a this pointer manipulation for doing a virtual function call for complex cases in multiple virtual inheritance.
This is all fine, but I came across an intersting fact that "thunks" were originally introduced in compiler implementations during Algol days. Algol has two argument passing mechanisms: pass by value and pass by name. Pass by name is problematic, and works just like macros in C. With pass by value, the name is bound at runtime, an a replacement is done. That sounds like magic, how can it be done?
P. Z. Ingerman is believed to have invented thunks in 1961 as a mechanism of implementing pass by name to bind actual parameters to formal definitions. Pass by name didn't live the test of time and later programming languages didn't use it, but the term "thunk" stuck. Interesting indeed!
Advanced C++ programmers might know about "thunks". Thunks are compiler generated very small functions that typically do some pointer or address adjutment. Why is it needed? Consider
delete [] arr;
where arr is points to an array of 10 elements from dynamically allocated memory. When you do delete, how does the size of array obtained and correct number of destructors are called? This can be implented by a compiler by providing thunks: a small function that will retrieve the number of elements in a dynamically allocated array from memory and give it to the delete so that it will call the destructors that many number of times. Usually, thunks are used in vtables where a this pointer manipulation for doing a virtual function call for complex cases in multiple virtual inheritance.
This is all fine, but I came across an intersting fact that "thunks" were originally introduced in compiler implementations during Algol days. Algol has two argument passing mechanisms: pass by value and pass by name. Pass by name is problematic, and works just like macros in C. With pass by value, the name is bound at runtime, an a replacement is done. That sounds like magic, how can it be done?
P. Z. Ingerman is believed to have invented thunks in 1961 as a mechanism of implementing pass by name to bind actual parameters to formal definitions. Pass by name didn't live the test of time and later programming languages didn't use it, but the term "thunk" stuck. Interesting indeed!