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_mem_qrealloc( FT_Memory memory, FT_Long item_size, FT_Long cur_count, FT_Long new_count, void* block, FT_Error *p_error ) { FT_Error error = FT_Err_Ok; /* Note that we now accept `item_size == 0' as a valid parameter, in * order to cover very weird cases where an ALLOC_MULT macro would be * called. */ if ( cur_count < 0 || new_count < 0 || item_size < 0 ) { /* may help catch/prevent nasty security issues */ error = FT_Err_Invalid_Argument; } else if ( new_count == 0 || item_size == 0 ) { ft_mem_free( memory, block ); block = NULL; } else if ( new_count > FT_INT_MAX/item_size ) { error = FT_Err_Array_Too_Large; } else if ( cur_count == 0 ) { FT_ASSERT( block == NULL ); block = ft_mem_alloc( memory, new_count*item_size, &error ); } else { FT_Pointer block2; FT_Long cur_size = cur_count*item_size; FT_Long new_size = new_count*item_size; block2 = memory->realloc( memory, cur_size, new_size, block ); if ( block2 == NULL ) error = FT_Err_Out_Of_Memory; else block = block2; } *p_error = error; return block; }
ft_mem_qrealloc( FT_Memory memory, FT_Long item_size, FT_Long cur_count, FT_Long new_count, void* block, FT_Error *p_error ) { FT_Error error = FT_Err_Ok; if ( cur_count < 0 || new_count < 0 || item_size <= 0 ) { /* may help catch/prevent nasty security issues */ error = FT_Err_Invalid_Argument; } else if ( new_count == 0 ) { ft_mem_free( memory, block ); block = NULL; } else if ( new_count > FT_INT_MAX/item_size ) { error = FT_Err_Array_Too_Large; } else if ( cur_count == 0 ) { FT_ASSERT( block == NULL ); block = ft_mem_alloc( memory, new_count*item_size, &error ); } else { FT_Pointer block2; FT_Long cur_size = cur_count*item_size; FT_Long new_size = new_count*item_size; block2 = memory->realloc( memory, cur_size, new_size, block ); if ( block2 == NULL ) error = FT_Err_Out_Of_Memory; else block = block2; } *p_error = error; return block; }
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 ); }