#include "stack_domain.h" #include #define ATTR_READY UINT64_C(0x534441545452) static int attr_live(const struct sd_attr *attr) { return attr != NULL && attr->ready == ATTR_READY; } int sd_attr_init(struct sd_attr *out, const struct sd_port *port) { struct sd_attr next; uint64_t bytes; int rc; if (out == NULL) { return SD_EINVAL; } rc = sd_port_default(port, &bytes); if (rc != SD_OK) { return rc; } memset(&next, 0, sizeof(next)); next.port = *port; next.size = bytes; next.revision = 1; next.explicit_size = 0; next.ready = ATTR_READY; *out = next; return SD_OK; } int sd_attr_set(struct sd_attr *attr, uint64_t bytes) { uint64_t rounded; int rc; if (!attr_live(attr)) { return SD_EINVAL; } rc = sd_port_round(&attr->port, bytes, &rounded); if (rc != SD_OK) { return rc; } if (attr->revision == UINT64_MAX) { return SD_EINVAL; } attr->size = rounded; attr->revision += 1; attr->explicit_size = 1; return SD_OK; } int sd_attr_get(const struct sd_attr *attr, uint64_t *bytes, uint64_t *revision, int *explicit_size) { if (!attr_live(attr) || bytes == NULL || revision == NULL || explicit_size == NULL) { return SD_EINVAL; } *bytes = attr->size; *revision = attr->revision; *explicit_size = attr->explicit_size; return SD_OK; } int sd_attr_copy(struct sd_attr *out, const struct sd_attr *source) { if (out == NULL || !attr_live(source)) { return SD_EINVAL; } if (out != source) { *out = *source; } return SD_OK; } int sd_attr_reset(struct sd_attr *attr) { uint64_t bytes; int rc; if (!attr_live(attr)) { return SD_EINVAL; } rc = sd_port_default(&attr->port, &bytes); if (rc != SD_OK || attr->revision == UINT64_MAX) { return SD_EINVAL; } attr->size = bytes; attr->revision += 1; attr->explicit_size = 0; return SD_OK; } int sd_attr_destroy(struct sd_attr *attr) { if (!attr_live(attr)) { return SD_EINVAL; } memset(attr, 0, sizeof(*attr)); return SD_OK; }