Next: , Previous: chpsim-function example, Up: Extending simulation


5.6 Global Initialization

External functions often access global variables outside the scope of the simulator. Global variables and objects in modules can be initialized in several ways. One way is to initialize object through their class constructors. Global objects are constructed in source order in their respective translation units, which is also how functions are registered. Plain-old-data, however, are often initialized through a function call. Another way is to do initialization work (directly, or through function call) in a static object constructor.

class module_init {
public:
    module_init() {
        // initializations and resource acquisitions...
    }
    ~module_init() {
	// deallocations and resource releasing...
    }
};

static const module_init __init__;

Above, the __init__ object's constructor will perform the initializations and resource allocations for this module. Since modules are not explicitly unloaded (by ‘dlclose’), module destruction will not occur until chpsim exits. [Ask me if you need destruction earlier.]

C++ Reminder: global object destruction occurs in the reverse order of construction. C Reminder: the order of initialization between different translation units (even in the same module) is undefined, and should not be relied upon.