Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect correct function call pairs

Tags:

c++

c

I am looking a tool able to detect ordered function call pairs in a nested fashion as shown below:

f()   // depth 0
   f()  //depth 1
   g()
g()

At each depth of call f() there must be a call of g() forming function call pair. This is particularly important in critical section entry and exit.

like image 587
berk Avatar asked Aug 13 '10 20:08

berk


1 Answers

In C++, one option is to wrap the calls to f() and g() in the constructor and destructor of a class and only call those functions by instantiating an instance of that class. For example,

struct FAndGCaller
{
    FAndGCaller() { f(); }
    ~FAndGCaller() { g(); }
};

This can then be used in any scope block like so:

{
    FAndGCaller call_f_then_later_g; // calls f()

} // calls g()

Obviously in real code you'd want to name things more appropriately, and often you'll simply want to have the contents of f() and g() in the constructor and destructor bodies, rather than in separate functions.

This idiom of Scope Bound Resource Management (SBRM, or more commonly referred to as Resource Acquisition is Initialization, RAII) is quite common.

like image 153
James McNellis Avatar answered Oct 22 '22 09:10

James McNellis