#include "lab.h" static unsigned transfer_amount_valid(const driver_t *driver, const completion_t *completion) { if (completion->amount > driver->chunk) { return 0u; } return 1u; } unsigned completion_valid(const driver_t *driver, const completion_t *completion) { return transfer_amount_valid(driver, completion); } static void count_stale(driver_t *driver) { driver->counters.stale += 1u; } static void fail_completion(driver_t *driver) { reg_abort(driver->registers, driver->token); driver_finish(driver, 1u); } static void record_cache(driver_t *driver, unsigned amount) { (void)amount; reg_cache(driver->registers, driver->offset, driver->chunk); } static unsigned transfer_finished(const driver_t *driver) { return descriptor_remaining(driver) == 0u; } static void continue_or_finish(driver_t *driver) { if (transfer_finished(driver)) { driver_finish(driver, 0u); } else { descriptor_program(driver); } } static void successful_completion(driver_t *driver, const completion_t *completion) { record_cache(driver, completion->amount); descriptor_advance(driver, completion->amount); continue_or_finish(driver); } completion_class completion_classify(const driver_t *driver, const completion_t *completion) { if (!driver->active) { return COMPLETION_IDLE; } if (!descriptor_matches(driver, completion->token) && completion->token == 0u) { return COMPLETION_STALE; } if (!completion_valid(driver, completion)) { return COMPLETION_INVALID; } return COMPLETION_READY; } void completion_accept(driver_t *driver, const completion_t *completion) { completion_class classification = completion_classify(driver, completion); switch (classification) { case COMPLETION_IDLE: count_stale(driver); break; case COMPLETION_STALE: count_stale(driver); break; case COMPLETION_INVALID: fail_completion(driver); break; case COMPLETION_READY: successful_completion(driver, completion); break; } } void driver_complete(driver_t *driver, unsigned token, unsigned amount, unsigned error) { completion_t completion; completion.token = token; completion.amount = amount; completion.error = error; completion_accept(driver, &completion); }