amos 8b7616bfd8 Batch 22 (20261027-C-hwe-hard-22): add 63 calibrated HWE hard-design tasks
From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27,
11 canonical families), 38 skipped for scope or duplication. Each task
has at least one independent contract-edge defect, boundary types
rotate, and public docs state trap clauses as plain contract prose.
Claude reviewed 63/63 USABLE against current digests. Difficulty is
unmeasured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 16:14:57 +08:00

84 lines
2.3 KiB
C

#include "dma.h"
static int backend_valid(const struct dma_backend *backend)
{
if (backend == NULL) {
return 0;
}
if (backend->streaming == NULL || backend->coherent == NULL) {
return 0;
}
if (backend->restore == NULL) {
return 0;
}
return 1;
}
static int device_valid(const struct dma_device *device)
{
if (device == NULL || device->bound != 1) {
return 0;
}
if (!backend_valid(&device->backend)) {
return 0;
}
if (device->streaming_bits > 64 || device->coherent_bits > 64) {
return 0;
}
return 1;
}
int dma_device_bind(struct dma_device *device, const struct dma_backend *backend,
unsigned streaming_bits, unsigned coherent_bits)
{
struct dma_device fresh;
if (device == NULL || !backend_valid(backend)) {
return DMA_EINVAL;
}
if (streaming_bits > 64 || coherent_bits > 64) {
return DMA_EINVAL;
}
fresh.backend = *backend;
fresh.streaming_bits = streaming_bits;
fresh.coherent_bits = coherent_bits;
fresh.bound = 1;
*device = fresh;
return DMA_OK;
}
int dma_device_state(const struct dma_device *device, unsigned *streaming_bits,
unsigned *coherent_bits)
{
if (!device_valid(device) || streaming_bits == NULL || coherent_bits == NULL) {
return DMA_EINVAL;
}
*streaming_bits = device->streaming_bits;
*coherent_bits = device->coherent_bits;
return DMA_OK;
}
int dma_device_pair(struct dma_device *device, unsigned bits)
{
unsigned old_streaming;
unsigned old_coherent;
int error;
if (!device_valid(device) || bits < 1 || bits > 64) {
return DMA_EINVAL;
}
old_streaming = device->streaming_bits;
old_coherent = device->coherent_bits;
error = device->backend.streaming(device->backend.context, bits);
if (error != DMA_OK) {
device->backend.restore(device->backend.context, old_streaming, old_coherent);
return error;
}
error = device->backend.coherent(device->backend.context, bits);
if (error != DMA_OK) {
device->backend.restore(device->backend.context, old_streaming, old_coherent);
return error;
}
device->streaming_bits = bits;
device->coherent_bits = bits;
return DMA_OK;
}