Q:

How to ensure that a specific code is not run while a condition is not met

I work with C/CPP on Embedded systems, and in my code I have some sections that is run from some memory that is "invalid" and MUST NOT be called until some condition is met.  
for simplicity:
Lets say foo() is such function and is invalid while g_isMemoryValid == false
except for foo() there are couple of houndred other functions in the invalid memory section with several calls each from all the modules.
my problem is to find all instances where i enter the invalid section and verify i didnt miss any call!
How can i ensure that all branches where foo() is run are meet g_isMemoryValid == true

Solution Should output:
The line which jumps to the code(or instruction address),
but even if it will emit the module name only,
Or Hell... [OK, NOT_OK] will do :)
Note: Even solutions that will solve only subset of the scenarios will do!

Notice that there many scenarios in addition to simple if(g_isMemoryValid) wrapper like call chaining the calling function in itself not called while condition is not met or some different syntax of the branch
if (g_isMemoryValid) 
    foo(); // should pass

if (!g_isMemoryValid)
    return; 
foo(); // should pass too

A:

One solution that, in my opinion at least, is more elegant than a macro would be to use a function pointer:
#include <stdio.h>

// The real foo()
void foo()
{
    puts("OK");
}

// Handle 'invalid' memory (error message?)
void mem_invalid()
{
    puts("Not OK");
}

typedef void (*foo_t)();

foo_t foo_ptr = &mem_invalid;

void main(void)
{
    // Memory is 'invalid', calls error routine
    foo_ptr();
    ...

    // Memory became good somehow
    foo_ptr = &foo;
    ...

    // Call now succeeds
    foo_ptr();
}

This uses the function pointer in lieu of the flag.
Depending on your embedded system there may be some overhead associated with the indirect function call but unless it is in a critical area like an inner loop it shouldn't be an issue.  The error handler doesn't even need to do anything - or it can be as complicated as you like.

