Skip to content

Commit c7e719c

Browse files
authored
Merge pull request #731 from devreal/parsec-object-release
Store release callback in parsec_object_t
2 parents 758dc3c + 24867d0 commit c7e719c

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

parsec/class/parsec_object.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,21 @@ void parsec_class_finalize(void)
188188
}
189189
}
190190

191+
void parsec_obj_release(parsec_object_t *object)
192+
{
193+
PARSEC_OBJ_SET_MAGIC_ID((object), 0);
194+
object->obj_release(object);
195+
}
196+
197+
void parsec_obj_destruct(parsec_object_t * object) {
198+
PARSEC_OBJ_SET_MAGIC_ID((object), 0);
199+
parsec_obj_run_destructors(object);
200+
}
201+
202+
void parsec_obj_destruct_and_free(parsec_object_t * object) {
203+
parsec_obj_destruct(object);
204+
free(object);
205+
}
191206

192207
static void save_class(parsec_class_t *cls)
193208
{

parsec/class/parsec_object.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ typedef struct parsec_object_t parsec_object_t;
137137
typedef struct parsec_class_t parsec_class_t;
138138
typedef void (*parsec_construct_t) (parsec_object_t *);
139139
typedef void (*parsec_destruct_t) (parsec_object_t *);
140+
typedef void (*parsec_release_t) (parsec_object_t *);
140141

141142

142143
/* types **************************************************************/
@@ -185,6 +186,7 @@ struct parsec_object_t {
185186
#endif /* defined(PARSEC_DEBUG_PARANOID) */
186187
parsec_class_t *obj_class; /**< class descriptor */
187188
volatile int32_t obj_reference_count; /**< reference count */
189+
parsec_release_t obj_release; /**< destruct and release */
188190
#if defined(PARSEC_DEBUG_PARANOID)
189191
const char* cls_init_file_name; /**< In debug mode store the file where the object get contructed */
190192
int cls_init_lineno; /**< In debug mode store the line number where the object get contructed */
@@ -295,6 +297,9 @@ static inline parsec_object_t *parsec_obj_new_debug(parsec_class_t* type, const
295297
#define PARSEC_OBJ_SET_MAGIC_ID( OBJECT, VALUE )
296298
#endif /* defined(PARSEC_DEBUG_PARANOID) */
297299

300+
void parsec_obj_destruct(parsec_object_t * object);
301+
void parsec_obj_destruct_and_free(parsec_object_t * object);
302+
298303
/**
299304
* Release an object (by decrementing its reference count). If the
300305
* reference count reaches zero, destruct (finalize) the object and
@@ -311,25 +316,21 @@ static inline parsec_object_t *parsec_obj_new_debug(parsec_class_t* type, const
311316
assert(NULL != ((parsec_object_t *) (object))->obj_class); \
312317
assert(PARSEC_OBJ_MAGIC_ID == ((parsec_object_t *) (object))->obj_magic_id); \
313318
if (0 == parsec_obj_update((parsec_object_t *) (object), -1)) { \
314-
parsec_obj_run_destructors((parsec_object_t *) (object)); \
315-
PARSEC_OBJ_SET_MAGIC_ID((object), 0); \
316319
PARSEC_OBJ_REMEMBER_FILE_AND_LINENO( object, __FILE__, __LINE__ ); \
317-
free(object); \
320+
((parsec_object_t*)object)->obj_release((parsec_object_t*)object); \
318321
object = NULL; \
319322
} \
320323
} while (0)
321324
#else
322325
#define PARSEC_OBJ_RELEASE(object) \
323326
do { \
324327
if (0 == parsec_obj_update((parsec_object_t *) (object), -1)) { \
325-
parsec_obj_run_destructors((parsec_object_t *) (object)); \
326-
free(object); \
328+
((parsec_object_t*)object)->obj_release((parsec_object_t*)object); \
327329
object = NULL; \
328330
} \
329331
} while (0)
330332
#endif
331333

332-
333334
/**
334335
* Construct (initialize) objects that are not dynamically allocated.
335336
*
@@ -339,17 +340,26 @@ static inline parsec_object_t *parsec_obj_new_debug(parsec_class_t* type, const
339340

340341
#define PARSEC_OBJ_CONSTRUCT(object, type) \
341342
do { \
342-
PARSEC_OBJ_CONSTRUCT_INTERNAL((object), PARSEC_OBJ_CLASS(type)); \
343+
PARSEC_OBJ_CONSTRUCT_WRELEASE_INTERNAL((object), PARSEC_OBJ_CLASS(type), &parsec_obj_destruct); \
344+
} while (0)
345+
346+
#define PARSEC_OBJ_CONSTRUCT_WRELEASE(object, type, release) \
347+
do { \
348+
PARSEC_OBJ_CONSTRUCT_WRELEASE_INTERNAL((object), PARSEC_OBJ_CLASS(type), release); \
343349
} while (0)
344350

345-
#define PARSEC_OBJ_CONSTRUCT_INTERNAL(object, type) \
351+
/* backwards compatible macro for mempool */
352+
#define PARSEC_OBJ_CONSTRUCT_INTERNAL(object, type) PARSEC_OBJ_CONSTRUCT_WRELEASE_INTERNAL(object, type, &parsec_obj_destruct)
353+
354+
#define PARSEC_OBJ_CONSTRUCT_WRELEASE_INTERNAL(object, type, release) \
346355
do { \
347356
PARSEC_OBJ_SET_MAGIC_ID((object), PARSEC_OBJ_MAGIC_ID); \
348357
if (0 == (type)->cls_initialized) { \
349358
parsec_class_initialize((type)); \
350359
} \
351360
((parsec_object_t *) (object))->obj_class = (type); \
352361
((parsec_object_t *) (object))->obj_reference_count = 1; \
362+
((parsec_object_t *) (object))->obj_release = (parsec_release_t)release; \
353363
parsec_obj_run_constructors((parsec_object_t *) (object)); \
354364
PARSEC_OBJ_REMEMBER_FILE_AND_LINENO( object, __FILE__, __LINE__ ); \
355365
} while (0)
@@ -470,6 +480,7 @@ static inline parsec_object_t *parsec_obj_new(parsec_class_t * cls)
470480
if (NULL != object) {
471481
object->obj_class = cls;
472482
object->obj_reference_count = 1;
483+
object->obj_release = &parsec_obj_destruct_and_free;
473484
parsec_obj_run_constructors(object);
474485
}
475486
return object;

0 commit comments

Comments
 (0)