Previous - Up - Next

6.3   Objects

C/C++

In C/C++ one must manually maintain a corresponding object structure that is used to hold data about individual objects for each Simics class. Simics's class system supports a very limited form of inheritance, where all classes must inherit the class conf_object_t, the basic configuration object class, either directly or indirectly. When declaring an object structure the first element must be the object structure of the class it inherits. An object structure for a class that directly inherits conf_object_t is declared as:

typedef struct my_object {
        conf_object_t obj;
        // Other variables...
} my_object_t;

A Simics object contains (in the conf_object_t field) all the information related to its class and its attributes. When an object is created, the new_instance() function declared in the class definition is called. The new_instance() function is responsible for allocating an object structure for the new object and initializing the all fields except the conf_object_t part. A typical function would look like this:

static conf_object_t *
my_new_instance(parse_object_t *parse_obj)
{
        my_object_t *mo = MM_ZALLOC(1, my_object_t);
        // Initializations...
        return &mo->obj;
}

When inheriting some other class the object structure should instead include the object structure of that class as its first field. The new_instance() function should also call some function that initializes the object structure of the inherited class. If the object inherits directly from conf_object_t, the conf_object_t part of the structure can be initialized by calling the SIM_object_constructor() function.

A very common class to inherit is log_object_t, which automatically provides log facilities to the inheriting classes. The log_object_t part of the object structure can be initialized by calling the SIM_log_constructor() function. The object structure and new_instance() function would then look like this:

typedef struct my_object {
        log_object_t log;
        // Other variables...
} my_object_t;

static conf_object_t *
my_new_instance(parse_object_t *parse_obj)
{
        my_object_t *mo = MM_ZALLOC(1, my_object_t);
        SIM_log_constructor(&mo->log, parse_obj);
        // Initializations...
        return &mo->log.obj;
}

Python

In Python, an object is an instance of a Python class, created by the new_instance() function referred to when registering the class. The python class is usually written as:

class sample_python_class:
    def __init__(self, obj):
        obj.object_data = self
        self.obj = obj

        # other inits

    # misc methods

A typical new_instance() function looks like:

def new_instance(parse_obj):
    obj = VT_alloc_log_object(parse_obj)
    sample_python_class(obj)
    return obj

All objects must hold a log object, created with a call to VT_alloc_log_object(), and the log object must refer to the instance of the python class.

Previous - Up - Next