116 #ifndef OPAL_OBJECT_H
117 #define OPAL_OBJECT_H
119 #include "opal_config.h"
125 #if OPAL_ENABLE_MULTI_THREADS
131 #if OPAL_ENABLE_DEBUG
133 #define OPAL_OBJ_MAGIC_ID ((0xdeafbeedULL << 32) + 0xdeafbeedULL)
171 #if OPAL_ENABLE_DEBUG
172 #define OPAL_OBJ_STATIC_INIT(BASE_CLASS) { OPAL_OBJ_MAGIC_ID, OBJ_CLASS(BASE_CLASS), 1, __FILE__, __LINE__ }
174 #define OPAL_OBJ_STATIC_INIT(BASE_CLASS) { OBJ_CLASS(BASE_CLASS), 1 }
183 #if OPAL_ENABLE_DEBUG
186 uint64_t obj_magic_id;
190 #if OPAL_ENABLE_DEBUG
191 const char* cls_init_file_name;
205 #define OBJ_CLASS(NAME) (&(NAME ## _class))
218 #define OBJ_CLASS_INSTANCE(NAME, PARENT, CONSTRUCTOR, DESTRUCTOR) \
219 opal_class_t NAME ## _class = { \
222 (opal_construct_t) CONSTRUCTOR, \
223 (opal_destruct_t) DESTRUCTOR, \
236 #define OBJ_CLASS_DECLARATION(NAME) \
237 extern opal_class_t NAME ## _class
248 #if OPAL_ENABLE_DEBUG
252 object->obj_magic_id = OPAL_OBJ_MAGIC_ID;
253 object->cls_init_file_name = file;
254 object->cls_init_lineno = line;
257 #define OBJ_NEW(type) \
258 ((type *)opal_obj_new_debug(OBJ_CLASS(type), __FILE__, __LINE__))
260 #define OBJ_NEW(type) \
261 ((type *) opal_obj_new(OBJ_CLASS(type)))
269 #if OPAL_ENABLE_DEBUG
270 #define OBJ_RETAIN(object) \
272 assert(NULL != ((opal_object_t *) (object))->obj_class); \
273 assert(OPAL_OBJ_MAGIC_ID == ((opal_object_t *) (object))->obj_magic_id); \
274 opal_obj_update((opal_object_t *) (object), 1); \
275 assert(((opal_object_t *) (object))->obj_reference_count >= 0); \
278 #define OBJ_RETAIN(object) opal_obj_update((opal_object_t *) (object), 1);
285 #if OPAL_ENABLE_DEBUG
286 #define OBJ_REMEMBER_FILE_AND_LINENO( OBJECT, FILE, LINENO ) \
288 ((opal_object_t*)(OBJECT))->cls_init_file_name = FILE; \
289 ((opal_object_t*)(OBJECT))->cls_init_lineno = LINENO; \
291 #define OBJ_SET_MAGIC_ID( OBJECT, VALUE ) \
293 ((opal_object_t*)(OBJECT))->obj_magic_id = (VALUE); \
296 #define OBJ_REMEMBER_FILE_AND_LINENO( OBJECT, FILE, LINENO )
297 #define OBJ_SET_MAGIC_ID( OBJECT, VALUE )
310 #if OPAL_ENABLE_DEBUG
311 #define OBJ_RELEASE(object) \
313 assert(NULL != ((opal_object_t *) (object))->obj_class); \
314 assert(OPAL_OBJ_MAGIC_ID == ((opal_object_t *) (object))->obj_magic_id); \
315 if (0 == opal_obj_update((opal_object_t *) (object), -1)) { \
316 OBJ_SET_MAGIC_ID((object), 0); \
317 opal_obj_run_destructors((opal_object_t *) (object)); \
318 OBJ_REMEMBER_FILE_AND_LINENO( object, __FILE__, __LINE__ ); \
324 #define OBJ_RELEASE(object) \
326 if (0 == opal_obj_update((opal_object_t *) (object), -1)) { \
327 opal_obj_run_destructors((opal_object_t *) (object)); \
342 #define OBJ_CONSTRUCT(object, type) \
344 OBJ_CONSTRUCT_INTERNAL((object), OBJ_CLASS(type)); \
347 #define OBJ_CONSTRUCT_INTERNAL(object, type) \
349 OBJ_SET_MAGIC_ID((object), OPAL_OBJ_MAGIC_ID); \
350 if (0 == (type)->cls_initialized) { \
351 opal_class_initialize((type)); \
353 ((opal_object_t *) (object))->obj_class = (type); \
354 ((opal_object_t *) (object))->obj_reference_count = 1; \
355 opal_obj_run_constructors((opal_object_t *) (object)); \
356 OBJ_REMEMBER_FILE_AND_LINENO( object, __FILE__, __LINE__ ); \
365 #if OPAL_ENABLE_DEBUG
366 #define OBJ_DESTRUCT(object) \
368 assert(OPAL_OBJ_MAGIC_ID == ((opal_object_t *) (object))->obj_magic_id); \
369 OBJ_SET_MAGIC_ID((object), 0); \
370 opal_obj_run_destructors((opal_object_t *) (object)); \
371 OBJ_REMEMBER_FILE_AND_LINENO( object, __FILE__, __LINE__ ); \
374 #define OBJ_DESTRUCT(object) \
376 opal_obj_run_destructors((opal_object_t *) (object)); \
377 OBJ_REMEMBER_FILE_AND_LINENO( object, __FILE__, __LINE__ ); \
420 opal_construct_t* cls_construct;
424 cls_construct =
object->obj_class->cls_construct_array;
425 while( NULL != *cls_construct ) {
426 (*cls_construct)(object);
442 opal_destruct_t* cls_destruct;
446 cls_destruct =
object->obj_class->cls_destruct_array;
447 while( NULL != *cls_destruct ) {
448 (*cls_destruct)(object);
473 if (NULL !=
object) {
474 object->obj_class = cls;
475 object->obj_reference_count = 1;
495 #if OPAL_ENABLE_MULTI_THREADS
498 object->obj_reference_count += inc;
499 return object->obj_reference_count;
size_t cls_sizeof
size of an object instance
Definition: opal_object.h:163
opal_construct_t * cls_construct_array
array of parent class constructors
Definition: opal_object.h:159
OPAL_DECLSPEC void opal_class_initialize(opal_class_t *)
Lazy initialization of class descriptor.
Definition: opal_object.c:70
opal_destruct_t cls_destruct
class destructor
Definition: opal_object.h:156
static void opal_obj_run_destructors(opal_object_t *object)
Run the hierarchy of class destructors for this object, in a parent-last order.
Definition: opal_object.h:440
volatile int32_t obj_reference_count
reference count
Definition: opal_object.h:189
const char * cls_name
symbolic name for class
Definition: opal_object.h:153
int cls_depth
depth of class hierarchy tree
Definition: opal_object.h:158
Class descriptor.
Definition: opal_object.h:152
static opal_object_t * opal_obj_new(opal_class_t *cls)
Create an object: dynamically allocate storage and run the class constructor.
Definition: opal_object.h:464
static int opal_obj_update(opal_object_t *object, int inc) __opal_attribute_always_inline__
Atomically update the object's reference count by some increment.
Definition: opal_object.h:493
int cls_initialized
is class initialized
Definition: opal_object.h:157
opal_class_t * cls_parent
parent class descriptor
Definition: opal_object.h:154
Base object.
Definition: opal_object.h:182
OPAL_DECLSPEC int opal_class_finalize(void)
Shut down the class system and release all memory.
Definition: opal_object.c:166
opal_destruct_t * cls_destruct_array
array of parent class destructors
Definition: opal_object.h:161
static void opal_obj_run_constructors(opal_object_t *object)
Run the hierarchy of class constructors for this object, in a parent-first order. ...
Definition: opal_object.h:418
opal_construct_t cls_construct
class constructor
Definition: opal_object.h:155
#define OBJ_CLASS_DECLARATION(NAME)
Declaration for class descriptor.
Definition: opal_object.h:236
opal_class_t * obj_class
class descriptor
Definition: opal_object.h:188