diff --git a/src/lam/lfc/object.h b/src/lam/lfc/object.h index e53ae9ed47..43965af6ff 100644 --- a/src/lam/lfc/object.h +++ b/src/lam/lfc/object.h @@ -8,6 +8,7 @@ #include <stdlib.h> #include "lam/types.h" #include "lam/atomic.h" +#include "lam/mem/malloc.h" /* * @@ -28,16 +29,6 @@ typedef struct lam_class_info class_destroy_t cls_destroy; } lam_class_info_t; -#define OBJECT(obj) ((lam_object_t *)(obj)) -#define CREATE_OBJECT(obj, lam_obj_type, cls_ptr) \ - do { \ - obj = (lam_obj_type *)malloc(sizeof(lam_obj_type)); \ - if ( obj ) \ - { \ - OBJECT(obj)->obj_class = cls_ptr; \ - OBJECT(obj)->obj_class->cls_init(OBJECT(obj)); \ - } \ - } while (0) /* * @@ -71,6 +62,7 @@ extern lam_class_info_t lam_object_cls; } while (0) #define SUPER_DESTROY(obj, super_cls) (super_cls)->cls_destroy(OBJECT(obj)) +#define OBJECT(obj) ((lam_object_t *)(obj)) #define OBJ_RETAIN(obj) if ( obj ) lam_obj_retain(OBJECT(obj)) #define OBJ_RELEASE(obj) if ( obj ) lam_obj_release(OBJECT(obj)) @@ -83,6 +75,22 @@ typedef struct lam_object void lam_obj_init(lam_object_t *obj); void lam_obj_destroy(lam_object_t *obj); + +static inline lam_object_t* lam_create_object(size_t size, lam_class_info_t* class_info) +{ + lam_object_t *obj = (lam_object_t*)LAM_MALLOC(size); + if ( NULL != obj ) { + obj->obj_class = class_info; + obj->obj_class->cls_init(obj); + } + return obj; +} + +#define NEW(obj_type, class_info) \ + ((obj_type*)lam_create_object(sizeof(obj_type), class_info)) +#define CREATE_OBJECT(obj, obj_type, class_info) \ + (obj = (obj_type*)lam_create_object(sizeof(obj_type, class_info))) + /* * 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 diff --git a/src/lam/mem/free_lists.c b/src/lam/mem/free_lists.c index 2ad18be397..6c0acff3ab 100644 --- a/src/lam/mem/free_lists.c +++ b/src/lam/mem/free_lists.c @@ -325,7 +325,7 @@ static int lam_free_lists_mem_pool_init(lam_free_lists_t *flist, STATIC_INIT(flist->fl_pool, &shmem_pool_cls); } else { /* process private memory allocation */ - CREATE_OBJECT(flist->fl_pool, lam_mem_pool_t, &mem_pool_cls); + flist->fl_pool = NEW(lam_mem_pool_t, &mem_pool_cls); } err = lam_mp_init_with( diff --git a/src/lam/mem/mem_pool.c b/src/lam/mem/mem_pool.c index 73e85ee6d1..1f7e925663 100644 --- a/src/lam/mem/mem_pool.c +++ b/src/lam/mem/mem_pool.c @@ -23,7 +23,7 @@ void lam_mp_init(lam_mem_pool_t *pool) { SUPER_INIT(pool, mem_pool_cls.cls_parent); - CREATE_OBJECT(pool->mp_private_alloc, lam_allocator_t, &allocator_cls); + pool->mp_private_alloc = NEW(lam_allocator_t, &allocator_cls); lam_mutex_init(&(pool->mp_lock)); pool->mp_dev_alloc = NULL; } @@ -32,7 +32,7 @@ void lam_mp_shared_init(lam_mem_pool_t *pool) { SUPER_INIT(pool, shmem_pool_cls.cls_parent); - CREATE_OBJECT(pool->mp_private_alloc, lam_allocator_t, &allocator_cls); + pool->mp_private_alloc = NEW(lam_allocator_t, &allocator_cls); lam_mutex_init(&(pool->mp_lock)); lam_alc_set_is_shared(pool->mp_private_alloc, 1); lam_alc_set_mem_prot(pool->mp_private_alloc, MMAP_SHARED_PROT); @@ -282,7 +282,7 @@ void lam_fmp_init(lam_fixed_mpool_t *pool) { SUPER_INIT(pool, &lam_object_cls); - CREATE_OBJECT(pool->fmp_private_alloc, lam_allocator_t, &allocator_cls); + pool->fmp_private_alloc = NEW(lam_allocator_t, &allocator_cls); lam_alc_set_is_shared(pool->fmp_private_alloc, 1); lam_alc_set_mem_prot(pool->fmp_private_alloc, MMAP_SHARED_PROT);