ft_object_create( FT_Object *pobject, FT_Class clazz, FT_Pointer init_data ) { FT_Memory memory; FT_Error error; FT_Object obj; FT_ASSERT_IS_CLASS(clazz); memory = FT_CLASS__MEMORY(clazz); if ( !FT_ALLOC( obj, clazz->obj_size ) ) { obj->clazz = clazz; obj->ref_count = 1; if ( clazz->obj_init ) { error = clazz->obj_init( obj, init_data ); if ( error ) { /* IMPORTANT: call the destructor when an error */ /* was detected in the constructor !! */ if ( clazz->obj_done ) clazz->obj_done( obj ); FT_FREE( obj ); } } } *pobject = obj; return error; }
/* the cleanup routine for all objects */ static void ft_object_cleanup( FT_Object object ) { FT_Memory memory = FT_OBJECT__MEMORY(object); FT_Class clazz = FT_OBJECT__CLASS(object); if ( clazz->obj_done ) clazz->obj_done( object ); FT_FREE( object ); }
/* destroy a given class */ static void ft_class_hnode_destroy( FT_ClassHNode node ) { FT_Class clazz = node->clazz; FT_Memory memory = clazz->memory; if ( clazz->class_done ) clazz->class_done( (FT_Object) clazz ); FT_FREE( clazz ); node->clazz = NULL; node->type = NULL; FT_FREE( node ); }
ft_object_new( FT_Class clazz, FT_Pointer init_data ) { FT_Memory memory; FT_Object obj; FT_ASSERT_IS_CLASS(clazz); memory = FT_CLASS__MEMORY(clazz); obj = ft_mem_alloc( clazz->obj_size, memory ); obj->clazz = clazz; obj->ref_count = 1; if ( clazz->obj_init ) { FT_CleanupStack stack = FT_MEMORY__CLEANUP(memory); ft_cleanup_push( stack, obj, (FT_CleanupFunc) ft_object_cleanup, NULL ); clazz->obj_init( obj, init_data ); ft_cleanup_pop( stack, obj, 0 ); } return obj; }
ft_object_create( FT_Object *pobject, FT_Class clazz, FT_Pointer init_data ) { FT_Memory memory; FT_Object obj; FT_ASSERT_IS_CLASS(clazz); memory = FT_CLASS__MEMORY(memory); obj = ft_mem_alloc( clazz->obj_size, memory ); obj->clazz = clazz; obj->ref_count = 1; *pobject = obj; if ( clazz->obj_init ) clazz->obj_init( obj, init_data ); }