2003-12-22 19:29:21 +03:00
|
|
|
/*
|
2004-01-07 21:39:35 +03:00
|
|
|
* $HEADER$
|
2003-12-22 19:29:21 +03:00
|
|
|
*/
|
2004-01-07 10:57:00 +03:00
|
|
|
|
2004-02-10 17:04:27 +03:00
|
|
|
/**
|
2004-02-11 01:15:55 +03:00
|
|
|
* @file:
|
2004-02-10 17:04:27 +03:00
|
|
|
*
|
2004-02-13 02:51:07 +03:00
|
|
|
* A simple C-language object-oriented system with single inheritance
|
2004-02-10 17:04:27 +03:00
|
|
|
* and ownership-based memory management using a retain/release model.
|
|
|
|
*
|
2004-02-13 00:30:06 +03:00
|
|
|
* A class consists of a struct and singly-instantiated class
|
2004-02-10 17:04:27 +03:00
|
|
|
* descriptor. The first element of the struct must be the parent
|
2004-02-10 17:34:18 +03:00
|
|
|
* class's struct. The class descriptor must be given a well-known
|
2004-02-13 02:51:07 +03:00
|
|
|
* name based upon the class struct name (if the struct is sally_t,
|
|
|
|
* the class descriptor should be sally_t_class) and must be
|
|
|
|
* statically initialized as discussed below.
|
2004-02-10 17:04:27 +03:00
|
|
|
*
|
|
|
|
* (a) To define a class
|
|
|
|
*
|
|
|
|
* In a interface (.h) file, define the class. The first element
|
2004-02-13 02:51:07 +03:00
|
|
|
* should always be the parent class,
|
2004-02-10 17:04:27 +03:00
|
|
|
* for example
|
2004-02-13 00:30:06 +03:00
|
|
|
* @code
|
2004-02-10 17:04:27 +03:00
|
|
|
* typedef struct sally_t sally_t;
|
|
|
|
* struct sally_t
|
|
|
|
* {
|
2004-02-13 02:51:07 +03:00
|
|
|
* parent_t parent;
|
2004-02-10 17:04:27 +03:00
|
|
|
* void *first_member;
|
|
|
|
* ...
|
|
|
|
* };
|
|
|
|
*
|
2004-02-13 01:42:39 +03:00
|
|
|
* extern lam_class_t sally_t_class;
|
2004-02-13 00:30:06 +03:00
|
|
|
* @endcode
|
2004-02-13 02:51:07 +03:00
|
|
|
* All classes must have a parent which is also class.
|
2004-02-10 17:04:27 +03:00
|
|
|
*
|
2004-02-13 00:30:06 +03:00
|
|
|
* In an implementation (.c) file, instantiate a class descriptor for
|
2004-02-13 02:51:07 +03:00
|
|
|
* the class, and should be the name of the struct with "_class"
|
2004-02-13 00:30:06 +03:00
|
|
|
* appended:
|
|
|
|
* @code
|
2004-02-13 01:42:39 +03:00
|
|
|
* lam_class_t sally_t_class = {
|
2004-02-10 17:04:27 +03:00
|
|
|
* "sally_t",
|
2004-02-13 01:42:39 +03:00
|
|
|
* OBJ_CLASS(parent_t), // pointer to parent_t_class
|
2004-02-10 17:04:27 +03:00
|
|
|
* sally_construct,
|
2004-02-13 02:51:07 +03:00
|
|
|
* sally_destruct,
|
|
|
|
* 0, 0, NULL, NULL
|
2004-02-10 17:04:27 +03:00
|
|
|
* };
|
2004-02-13 00:30:06 +03:00
|
|
|
* @endcode
|
2004-02-10 17:04:27 +03:00
|
|
|
* This variable should be publically advertised using the "extern"
|
|
|
|
* statement in the interface file as shown above.
|
|
|
|
*
|
|
|
|
* sally_construct, and sally_destruct are function pointers to the
|
|
|
|
* constructor and destructor for the class and are best defined as
|
|
|
|
* static functions in the implementation file.
|
|
|
|
*
|
|
|
|
* Other class methods may be added to the struct.
|
|
|
|
*
|
|
|
|
* (b) Class instantiation: dynamic
|
|
|
|
*
|
|
|
|
* To create a instance of a class (an object) use OBJ_NEW:
|
2004-02-13 00:30:06 +03:00
|
|
|
* @code
|
2004-02-10 17:04:27 +03:00
|
|
|
* sally_t *sally = OBJ_NEW(sally_t);
|
2004-02-13 00:30:06 +03:00
|
|
|
* @endcode
|
2004-02-10 17:04:27 +03:00
|
|
|
* which allocates memory of sizeof(sally_t) and runs the class's
|
2004-02-13 02:51:07 +03:00
|
|
|
* constructors.
|
2004-02-10 17:04:27 +03:00
|
|
|
*
|
|
|
|
* Use OBJ_RETAIN, OBJ_RELEASE to do reference-count-based
|
|
|
|
* memory management:
|
2004-02-13 00:30:06 +03:00
|
|
|
* @code
|
2004-02-10 17:04:27 +03:00
|
|
|
* OBJ_RETAIN(sally);
|
|
|
|
* OBJ_RELEASE(sally);
|
|
|
|
* OBJ_RELEASE(sally);
|
2004-02-13 00:30:06 +03:00
|
|
|
* @endcode
|
2004-02-10 17:04:27 +03:00
|
|
|
* When the reference count reaches zero, the class's "fini" method
|
|
|
|
* is run and the memory is freed.
|
|
|
|
*
|
|
|
|
* N.B. There is no explicit free/delete method for dynamic objects in
|
|
|
|
* this model.
|
|
|
|
*
|
|
|
|
* (c) Class instantiation: static
|
|
|
|
*
|
|
|
|
* For an object with static (or stack) allocation, it is only
|
|
|
|
* necessary to initialize the memory, which is done using
|
|
|
|
* OBJ_CONSTRUCT:
|
2004-02-13 00:30:06 +03:00
|
|
|
* @code
|
2004-02-10 17:04:27 +03:00
|
|
|
* sally_t sally;
|
|
|
|
*
|
|
|
|
* OBJ_CONSTRUCT(&sally, sally_t);
|
2004-02-13 00:30:06 +03:00
|
|
|
* @endcode
|
2004-02-10 17:04:27 +03:00
|
|
|
* The retain/release model is not necessary here, but before the
|
|
|
|
* object goes out of scope, OBJ_DESTRUCT should be run to release
|
|
|
|
* initialized resources:
|
2004-02-13 00:30:06 +03:00
|
|
|
* @code
|
2004-02-10 17:04:27 +03:00
|
|
|
* OBJ_DESTRUCT(&sally);
|
2004-02-13 00:30:06 +03:00
|
|
|
* @endcode
|
2004-02-10 17:04:27 +03:00
|
|
|
*/
|
|
|
|
|
2004-01-07 10:57:00 +03:00
|
|
|
#ifndef LAM_OBJECT_H
|
|
|
|
#define LAM_OBJECT_H
|
2003-12-22 19:29:21 +03:00
|
|
|
|
2004-02-13 00:30:06 +03:00
|
|
|
#include <assert.h>
|
2003-12-22 19:29:21 +03:00
|
|
|
#include <stdlib.h>
|
2004-02-10 17:04:27 +03:00
|
|
|
|
2004-02-13 00:30:06 +03:00
|
|
|
#include "lam_config.h"
|
2004-01-10 01:09:51 +03:00
|
|
|
#include "lam/types.h"
|
|
|
|
#include "lam/atomic.h"
|
2004-01-16 03:32:41 +03:00
|
|
|
#include "lam/mem/malloc.h"
|
2003-12-22 19:29:21 +03:00
|
|
|
|
|
|
|
/*
|
2004-02-13 00:30:06 +03:00
|
|
|
* BEGIN_C_DECLS should be used at the beginning of your declarations,
|
|
|
|
* so that C++ compilers don't mangle their names. Use END_C_DECLS at
|
|
|
|
* the end of C declarations.
|
2003-12-22 19:29:21 +03:00
|
|
|
*/
|
2004-02-13 00:30:06 +03:00
|
|
|
#undef BEGIN_C_DECLS
|
|
|
|
#undef END_C_DECLS
|
|
|
|
#ifdef __cplusplus
|
|
|
|
# define BEGIN_C_DECLS extern "C" {
|
|
|
|
# define END_C_DECLS }
|
|
|
|
#else
|
|
|
|
# define BEGIN_C_DECLS /* empty */
|
|
|
|
# define END_C_DECLS /* empty */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* typedefs ***********************************************************/
|
2003-12-22 19:29:21 +03:00
|
|
|
|
2004-02-10 17:04:27 +03:00
|
|
|
typedef struct lam_object_t lam_object_t;
|
2004-02-13 01:42:39 +03:00
|
|
|
typedef struct lam_class_t lam_class_t;
|
2004-02-10 17:04:27 +03:00
|
|
|
typedef void (*lam_construct_t) (lam_object_t *);
|
|
|
|
typedef void (*lam_destruct_t) (lam_object_t *);
|
2003-12-22 19:29:21 +03:00
|
|
|
|
2004-02-13 00:30:06 +03:00
|
|
|
|
|
|
|
/* types **************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class descriptor.
|
|
|
|
*
|
|
|
|
* There should be a single instance of this descriptor for each class
|
|
|
|
* definition.
|
|
|
|
*/
|
2004-02-13 01:42:39 +03:00
|
|
|
struct lam_class_t {
|
2004-02-13 00:30:06 +03:00
|
|
|
const char *cls_name; /**< symbolic name for class */
|
2004-02-13 01:42:39 +03:00
|
|
|
lam_class_t *cls_parent; /**< parent class descriptor */
|
2004-02-13 00:30:06 +03:00
|
|
|
lam_construct_t cls_construct; /**< class constructor */
|
|
|
|
lam_destruct_t cls_destruct; /**< class destructor */
|
|
|
|
int cls_initialized; /**< is class initialized */
|
|
|
|
int cls_depth; /**< depth of class hierarchy tree */
|
|
|
|
lam_construct_t *cls_construct_array;
|
2004-02-13 02:51:07 +03:00
|
|
|
/**< array of parent class constructors */
|
2004-02-13 00:30:06 +03:00
|
|
|
lam_destruct_t *cls_destruct_array;
|
2004-02-13 02:51:07 +03:00
|
|
|
/**< array of parent class destructors */
|
2004-02-10 17:04:27 +03:00
|
|
|
};
|
2003-12-22 19:29:21 +03:00
|
|
|
|
|
|
|
|
2004-02-10 17:04:27 +03:00
|
|
|
/**
|
2004-02-13 00:30:06 +03:00
|
|
|
* Base object.
|
|
|
|
*
|
|
|
|
* This is special and does not follow the pattern for other classes.
|
2003-12-22 19:29:21 +03:00
|
|
|
*/
|
2004-02-10 17:04:27 +03:00
|
|
|
struct lam_object_t {
|
2004-02-13 01:42:39 +03:00
|
|
|
lam_class_t *obj_class; /**< class descriptor */
|
|
|
|
int obj_reference_count; /**< reference count */
|
2004-02-10 17:04:27 +03:00
|
|
|
};
|
2003-12-22 19:29:21 +03:00
|
|
|
|
2004-01-16 21:28:23 +03:00
|
|
|
|
2004-02-13 00:30:06 +03:00
|
|
|
/* macros ************************************************************/
|
2003-12-22 19:29:21 +03:00
|
|
|
|
2004-02-10 17:04:27 +03:00
|
|
|
/**
|
2004-02-13 00:30:06 +03:00
|
|
|
* Return a pointer to the class descriptor associated with a
|
|
|
|
* class type.
|
2004-02-10 17:04:27 +03:00
|
|
|
*
|
2004-02-13 00:30:06 +03:00
|
|
|
* @param type Name of class
|
|
|
|
* @return Pointer to class descriptor
|
|
|
|
*/
|
2004-02-13 01:42:39 +03:00
|
|
|
#define OBJ_CLASS(type) (&(type ## _class))
|
2004-02-13 00:30:06 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static initializer for a class descriptor
|
2004-02-10 17:04:27 +03:00
|
|
|
*
|
2004-02-13 20:10:16 +03:00
|
|
|
* @param NAME Name of class
|
|
|
|
* @param PARENT Name of parent class
|
2004-02-13 00:30:06 +03:00
|
|
|
* @param CONSTRUCTOR Pointer to constructor
|
|
|
|
* @param DESTRUCTOR Pointer to destructor
|
2004-02-10 17:04:27 +03:00
|
|
|
*/
|
2004-02-13 20:10:16 +03:00
|
|
|
#define OBJ_CLASS_INSTANCE(NAME, PARENT, CONSTRUCTOR, DESTRUCTOR) \
|
|
|
|
lam_class_t NAME ## _class = { \
|
|
|
|
# NAME, \
|
|
|
|
OBJ_CLASS(PARENT), \
|
|
|
|
(lam_construct_t)CONSTRUCTOR, \
|
|
|
|
(lam_destruct_t)DESTRUCTOR, \
|
|
|
|
0, 0, NULL, NULL \
|
|
|
|
}
|
2004-01-16 03:32:41 +03:00
|
|
|
|
2004-02-13 00:30:06 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an object: dynamically allocate storage and run the class
|
|
|
|
* constructor.
|
|
|
|
*
|
|
|
|
* @param type Type (class) of the object
|
|
|
|
* @return Pointer to the object
|
2004-01-10 01:09:51 +03:00
|
|
|
*/
|
2004-02-13 00:30:06 +03:00
|
|
|
#define OBJ_NEW(type) \
|
2004-02-13 01:42:39 +03:00
|
|
|
((type *) lam_obj_new(sizeof(type), OBJ_CLASS(type)))
|
2004-01-10 01:09:51 +03:00
|
|
|
|
2003-12-22 19:29:21 +03:00
|
|
|
|
2004-02-10 17:04:27 +03:00
|
|
|
/**
|
|
|
|
* Retain an object (by incrementing its reference count)
|
|
|
|
*
|
2004-02-13 00:30:06 +03:00
|
|
|
* @param object Pointer to the object
|
2004-02-10 17:04:27 +03:00
|
|
|
*/
|
2004-02-13 00:30:06 +03:00
|
|
|
#define OBJ_RETAIN(object) \
|
|
|
|
do { \
|
|
|
|
if (object) { \
|
|
|
|
lam_obj_retain((lam_object_t *) object); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2004-02-10 17:04:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Release an object (by decrementing its reference count). If the
|
|
|
|
* reference count reaches zero, destruct (finalize) the object and
|
|
|
|
* free its storage.
|
|
|
|
*
|
2004-02-13 00:30:06 +03:00
|
|
|
* @param object Pointer to the object
|
2004-02-10 17:04:27 +03:00
|
|
|
*/
|
2004-02-13 00:30:06 +03:00
|
|
|
#define OBJ_RELEASE(object) \
|
|
|
|
do { \
|
|
|
|
if (object) { \
|
|
|
|
lam_obj_release((lam_object_t *) object); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2003-12-22 19:29:21 +03:00
|
|
|
|
2004-02-10 17:04:27 +03:00
|
|
|
|
|
|
|
/**
|
2004-02-13 00:30:06 +03:00
|
|
|
* Construct (initialize) objects that are not dynamically allocated.
|
2004-02-10 17:04:27 +03:00
|
|
|
*
|
2004-02-13 00:30:06 +03:00
|
|
|
* @param object Pointer to the object
|
|
|
|
* @param type The object type
|
2004-02-10 17:04:27 +03:00
|
|
|
*/
|
2004-02-13 00:30:06 +03:00
|
|
|
#define OBJ_CONSTRUCT(object, type) \
|
|
|
|
do { \
|
2004-02-13 01:42:39 +03:00
|
|
|
if (0 == OBJ_CLASS(type)->cls_initialized) { \
|
|
|
|
lam_class_initialize(OBJ_CLASS(type)); \
|
2004-02-13 00:30:06 +03:00
|
|
|
} \
|
|
|
|
if (object) { \
|
2004-02-13 01:42:39 +03:00
|
|
|
((lam_object_t *) object)->obj_class = OBJ_CLASS(type); \
|
2004-02-13 00:30:06 +03:00
|
|
|
((lam_object_t *) object)->obj_reference_count = 1; \
|
|
|
|
lam_obj_run_constructors((lam_object_t *) object); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2004-02-10 17:04:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2004-02-13 00:30:06 +03:00
|
|
|
* Destruct (finalize) an object that is not dynamically allocated.
|
2004-02-10 17:04:27 +03:00
|
|
|
*
|
2004-02-13 00:30:06 +03:00
|
|
|
* @param object Pointer to the object
|
2004-02-10 17:04:27 +03:00
|
|
|
*/
|
2004-02-13 00:30:06 +03:00
|
|
|
#define OBJ_DESTRUCT(object) \
|
|
|
|
do { \
|
|
|
|
if (object) { \
|
|
|
|
lam_obj_run_destructors((lam_object_t *) object); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
|
|
/* declarations *******************************************************/
|
|
|
|
|
|
|
|
BEGIN_C_DECLS
|
2004-02-10 17:04:27 +03:00
|
|
|
|
2004-02-13 01:42:39 +03:00
|
|
|
extern lam_class_t lam_object_t_class;
|
2004-02-10 17:04:27 +03:00
|
|
|
|
|
|
|
/**
|
2004-02-13 00:30:06 +03:00
|
|
|
* Lazy initialization of class descriptor.
|
|
|
|
*
|
|
|
|
* Specifically cache arrays of function pointers for the constructor
|
|
|
|
* and destructor hierarchies for this class.
|
2004-02-10 17:04:27 +03:00
|
|
|
*
|
2004-02-13 00:30:06 +03:00
|
|
|
* @param class Pointer to class descriptor
|
2004-02-10 17:04:27 +03:00
|
|
|
*/
|
2004-02-13 01:42:39 +03:00
|
|
|
void lam_class_initialize(lam_class_t *);
|
2004-02-10 17:04:27 +03:00
|
|
|
|
2004-02-13 00:30:06 +03:00
|
|
|
END_C_DECLS
|
2004-02-10 17:04:27 +03:00
|
|
|
|
|
|
|
/**
|
2004-02-13 00:30:06 +03:00
|
|
|
* Run the hierarchy of class constructors for this object, in a
|
|
|
|
* parent-first order.
|
|
|
|
*
|
|
|
|
* Do not use this function directly: use OBJ_CONSTRUCT() instead.
|
|
|
|
*
|
|
|
|
* WARNING: This implementation relies on a hardwired maximum depth of
|
|
|
|
* the inheritance tree!!!
|
2004-02-10 17:04:27 +03:00
|
|
|
*
|
2004-02-13 00:30:06 +03:00
|
|
|
* Hardwired for fairly shallow inheritance trees
|
|
|
|
* @param size Pointer to the object.
|
2004-02-10 17:04:27 +03:00
|
|
|
*/
|
2004-02-13 00:30:06 +03:00
|
|
|
static inline void lam_obj_run_constructors(lam_object_t *object)
|
|
|
|
{
|
2004-02-13 01:42:39 +03:00
|
|
|
lam_class_t *cls;
|
2004-02-13 00:30:06 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
assert(NULL != object);
|
2004-02-13 01:42:39 +03:00
|
|
|
assert(NULL != object->obj_class);
|
2004-02-13 00:30:06 +03:00
|
|
|
|
2004-02-13 01:42:39 +03:00
|
|
|
cls = object->obj_class;
|
2004-02-13 00:30:06 +03:00
|
|
|
for (i = cls->cls_depth - 1; i >= 0; i--) {
|
|
|
|
if (cls->cls_construct_array[i]) {
|
|
|
|
(cls->cls_construct_array[i])(object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-02-10 17:04:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2004-02-13 00:30:06 +03:00
|
|
|
* Run the hierarchy of class destructors for this object, in a
|
|
|
|
* parent-last order.
|
2004-02-10 17:04:27 +03:00
|
|
|
*
|
2004-02-13 00:30:06 +03:00
|
|
|
* Do not use this function directly: use OBJ_DESTRUCT() instead.
|
|
|
|
*
|
|
|
|
* @param size Pointer to the object.
|
2004-02-10 17:04:27 +03:00
|
|
|
*/
|
2004-02-13 00:30:06 +03:00
|
|
|
static inline void lam_obj_run_destructors(lam_object_t *object)
|
|
|
|
{
|
2004-02-13 01:42:39 +03:00
|
|
|
lam_class_t *cls;
|
2004-02-13 00:30:06 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
assert(NULL != object);
|
2004-02-13 01:42:39 +03:00
|
|
|
assert(NULL != object->obj_class);
|
2004-02-13 00:30:06 +03:00
|
|
|
|
2004-02-13 01:42:39 +03:00
|
|
|
cls = object->obj_class;
|
2004-02-13 00:30:06 +03:00
|
|
|
for (i = 0; i < cls->cls_depth; i++) {
|
|
|
|
if (cls->cls_destruct_array[i]) {
|
|
|
|
(cls->cls_destruct_array[i])(object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-02-10 17:04:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2004-02-13 00:30:06 +03:00
|
|
|
* Create new object: dynamically allocate storage and run the class
|
|
|
|
* constructor.
|
|
|
|
*
|
|
|
|
* Do not use this function directly: use OBJ_NEW() instead.
|
2004-02-10 17:04:27 +03:00
|
|
|
*
|
2004-02-13 00:30:06 +03:00
|
|
|
* @param size Size of the object
|
|
|
|
* @param cls Pointer to the class descriptor of this object
|
|
|
|
* @return Pointer to the object
|
2004-02-10 17:04:27 +03:00
|
|
|
*/
|
2004-02-13 00:30:06 +03:00
|
|
|
static inline lam_object_t *lam_obj_new(size_t size,
|
2004-02-13 01:42:39 +03:00
|
|
|
lam_class_t *cls)
|
2004-02-13 00:30:06 +03:00
|
|
|
{
|
|
|
|
lam_object_t *object;
|
|
|
|
|
|
|
|
assert(size >= sizeof(lam_object_t));
|
|
|
|
|
|
|
|
object = (lam_object_t *) malloc(size);
|
2004-02-13 16:56:55 +03:00
|
|
|
if (0 == cls->cls_initialized) {
|
|
|
|
lam_class_initialize(cls);
|
|
|
|
}
|
2004-02-13 00:30:06 +03:00
|
|
|
if (NULL != object) {
|
2004-02-13 01:42:39 +03:00
|
|
|
object->obj_class = cls;
|
2004-02-13 00:30:06 +03:00
|
|
|
object->obj_reference_count = 1;
|
|
|
|
lam_obj_run_constructors(object);
|
|
|
|
}
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is used by inline functions later in this file, and
|
|
|
|
* it must be defined by other header files later (eg., one of the
|
|
|
|
* atomic.h's).
|
|
|
|
*/
|
|
|
|
static inline int fetchNadd(volatile int *addr, int inc);
|
2004-02-10 17:04:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2004-02-13 00:30:06 +03:00
|
|
|
* Retain an object (by incrementing its reference count)
|
2004-02-10 17:04:27 +03:00
|
|
|
*
|
2004-02-13 00:30:06 +03:00
|
|
|
* Do not use this function directly: use OBJ_RETAIN instead.
|
|
|
|
*
|
|
|
|
* @param object Pointer to the object
|
2004-02-10 17:04:27 +03:00
|
|
|
*/
|
2004-02-13 00:30:06 +03:00
|
|
|
static inline void lam_obj_retain(lam_object_t *object)
|
|
|
|
{
|
|
|
|
assert(NULL != object);
|
2004-02-13 01:42:39 +03:00
|
|
|
assert(NULL != object->obj_class);
|
2004-02-13 00:30:06 +03:00
|
|
|
|
|
|
|
fetchNadd(&(object->obj_reference_count), 1);
|
|
|
|
|
|
|
|
assert(object->obj_reference_count >= 0);
|
|
|
|
}
|
2004-02-10 17:04:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2004-02-13 00:30:06 +03:00
|
|
|
* Release an object (by decrementing its reference count). If the
|
|
|
|
* reference count reaches zero, destruct (finalize) the object and
|
|
|
|
* free its storage.
|
|
|
|
*
|
|
|
|
* Do not use this function directly: use OBJ_RELEASE instead.
|
2004-02-10 17:04:27 +03:00
|
|
|
*
|
2004-02-13 00:30:06 +03:00
|
|
|
* @param object Pointer to the object
|
2004-02-10 17:04:27 +03:00
|
|
|
*/
|
2004-02-13 00:30:06 +03:00
|
|
|
static inline void lam_obj_release(lam_object_t *object)
|
|
|
|
{
|
|
|
|
assert(NULL != object);
|
2004-02-13 01:42:39 +03:00
|
|
|
assert(NULL != object->obj_class);
|
2004-02-13 00:30:06 +03:00
|
|
|
|
|
|
|
if (fetchNadd(&object->obj_reference_count, -1) == 1) {
|
2004-02-13 01:42:39 +03:00
|
|
|
object->obj_class->cls_destruct(object);
|
2004-02-13 00:30:06 +03:00
|
|
|
free(object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************/
|
2004-02-10 17:04:27 +03:00
|
|
|
|
|
|
|
#endif /* LAM_OBJECT_H */
|