#include "descriptor.h" #include void batch_initialize(struct batch *batch) { if (batch != NULL) { memset(batch, 0, sizeof(*batch)); } } uint64_t batch_cookie(const struct batch *batch) { if (batch == NULL) { return 0; } return ((uint64_t)batch->epoch << 32) | batch->count; } int batch_begin(struct batch *batch, uint64_t capacity, uint64_t alignment) { struct descriptor_table next; int result; if (batch == NULL || capacity == 0 || capacity > MAX_ENTRIES) { return RESULT_INPUT; } if (alignment != 32 && alignment != 64 && alignment != 128) { return RESULT_INPUT; } if (batch->phase == PHASE_BUILDING || batch->phase == PHASE_SUBMITTED) { return RESULT_STATE; } if (batch->epoch == UINT32_MAX) { return RESULT_STATE; } memset(&next, 0, sizeof(next)); result = table_create(&next, (unsigned)capacity, (unsigned)alignment); if (result != RESULT_OK) { return result; } table_destroy(&batch->table); batch->table = next; batch->count = 0; batch->epoch += 1; batch->phase = PHASE_BUILDING; return RESULT_OK; } int batch_append(struct batch *batch, uint64_t address, uint64_t length, uint64_t control) { struct transfer transfer; int result; if (batch == NULL) { return RESULT_INPUT; } if (batch->phase != PHASE_BUILDING) { return RESULT_STATE; } if (length == 0 || length > MAX_TRANSFER || control > 3 || address > UINT64_MAX - length) { return RESULT_INPUT; } if (batch->count == batch->table.capacity) { return RESULT_FULL; } transfer.address = address; transfer.length = (uint32_t)length; transfer.control = (uint32_t)control; result = table_store(&batch->table, batch->count, transfer, batch_cookie(batch)); if (result != RESULT_OK) { return result; } batch->count += 1; return RESULT_OK; } int batch_submit(struct batch *batch) { int result; if (batch == NULL) { return RESULT_INPUT; } if (batch->phase != PHASE_BUILDING || batch->count == 0) { return RESULT_STATE; } result = table_publish(&batch->table, batch->count, batch_cookie(batch)); if (result != RESULT_OK) { return result; } batch->phase = PHASE_SUBMITTED; return RESULT_OK; } int batch_complete(struct batch *batch, uint64_t cookie) { if (batch == NULL) { return RESULT_INPUT; } if (batch->phase != PHASE_SUBMITTED) { return RESULT_STATE; } if (cookie != batch_cookie(batch)) { return RESULT_STALE; } batch->phase = PHASE_COMPLETE; return RESULT_OK; } int batch_cancel(struct batch *batch) { if (batch == NULL) { return RESULT_INPUT; } table_destroy(&batch->table); batch->count = 0; batch->phase = PHASE_IDLE; return RESULT_OK; } void batch_destroy(struct batch *batch) { if (batch != NULL) { table_destroy(&batch->table); batch_initialize(batch); } }