fa_buffer_t fa_buffer_create(size_t size) { fa_ptr_t buffer_impl(fa_id_t interface); fa_buffer_t buffer = fa_new(buffer); buffer->impl = &buffer_impl; buffer->size = size; buffer->data = fa_malloc(size); buffer->ref_count = fa_atomic(); buffer->marked_for_destruction = fa_atomic(); //fa_slog_info("fa_buffer_create ", fa_i32(size)); buffer->destroy_function = default_destroy; buffer->destroy_data = NULL; buffer->meta = fa_map_empty(); fa_map_set_value_destructor(buffer->meta, fa_destroy); memset(buffer->data, 0, buffer->size); if (!buffer->data) { if (errno == ENOMEM) { buffer_fatal("Out of memory", errno); } else { buffer_fatal("Unknown", errno); } } buffer_warn(fa_string("Buffer created")); return buffer; }
fa_buffer_t fa_buffer_create(size_t size) { fa_ptr_t buffer_impl(fa_id_t interface); buffer_t buffer = fa_new(buffer); buffer->impl = &buffer_impl; buffer->size = size; buffer->data = fa_malloc(size); buffer->destroy_function = default_destroy; buffer->destroy_data = NULL; memset(buffer->data, 0, buffer->size); if (!buffer->data) { if (errno == ENOMEM) { buffer_fatal("Out of memory", errno); } else { buffer_fatal("Unknown", errno); } } return buffer; }
fa_buffer_t fa_buffer_wrap(fa_ptr_t pointer, size_t size, fa_unary_t destroy_function, fa_ptr_t destroy_data) { fa_ptr_t buffer_impl(fa_id_t interface); buffer_t b = fa_new(buffer); b->impl = &buffer_impl; b->size = size; b->data = pointer; b->destroy_function = destroy_function; b->destroy_data = destroy_data; return b; }
fa_buffer_t fa_buffer_wrap(fa_ptr_t pointer, size_t size, fa_unary_t destroy_function, fa_ptr_t destroy_data) { fa_ptr_t buffer_impl(fa_id_t interface); fa_buffer_t b = fa_new(buffer); b->impl = &buffer_impl; b->size = size; b->data = pointer; b->ref_count = fa_atomic(); b->marked_for_destruction = fa_atomic(); b->destroy_function = destroy_function; b->destroy_data = destroy_data; b->meta = fa_map_empty(); buffer_warn(fa_string("Buffer wrapped")); return b; }
fa_buffer_t fa_buffer_resize(size_t size, fa_buffer_t buffer) { fa_ptr_t buffer_impl(fa_id_t interface); buffer_t copy = fa_new(buffer); copy->impl = &buffer_impl; copy->size = size; copy->data = fa_malloc(size); copy->destroy_function = buffer->destroy_function; copy->destroy_data = buffer->destroy_data; if (!copy->data) { if (errno == ENOMEM) { buffer_fatal("Out of memory", errno); } else { buffer_fatal("Unknown", errno); } } copy->data = memcpy(copy->data, buffer->data, size); return copy; }