#include "window.h" #include static int slot_index(unsigned slot) { return slot < CW_SLOTS; } static int span_valid(unsigned first, unsigned count) { if (first > CW_CAPACITY) { return 0; } if (count > CW_CAPACITY - first) { return 0; } return 1; } void store_reset(CodeWorkbench *work) { memset(work, 0, sizeof(*work)); } int store_write(CodeWorkbench *work, unsigned slot, unsigned first, const uint16_t *words, unsigned count) { if (!slot_index(slot)) { return CW_ADAPTER; } if (words == NULL || count == 0 || !span_valid(first, count)) { return CW_ADAPTER; } CodeSlot *target = &work->slots[slot]; for (unsigned index = 0; index < count; ++index) { target->data[first + index] = words[index]; } work->counts.writes += count; return CW_OK; } int store_sync(CodeWorkbench *work, unsigned slot, unsigned first, unsigned end) { if (!slot_index(slot)) { return CW_ADAPTER; } if (end <= first || end > CW_CAPACITY) { return CW_ADAPTER; } CodeSlot *target = &work->slots[slot]; for (unsigned index = first; index < end; ++index) { target->execution[index] = target->data[index]; } work->counts.sync_calls += 1; work->counts.sync_words += end - first; return CW_OK; } static int publication_valid(Publication kind, unsigned length) { if (kind == CW_DROP) { return length == 0; } if (kind != CW_INSTALL && kind != CW_PATCH) { return 0; } return length > 0 && length <= CW_CAPACITY; } int store_publish(CodeWorkbench *work, unsigned slot, unsigned length, Publication kind) { if (!slot_index(slot) || !publication_valid(kind, length)) { return CW_ADAPTER; } CodeSlot *target = &work->slots[slot]; target->active = kind != CW_DROP; target->length = length; target->generation += 1; work->counts.serial += 1; switch (kind) { case CW_INSTALL: work->counts.installs += 1; break; case CW_PATCH: work->counts.patches += 1; break; case CW_DROP: work->counts.drops += 1; break; default: return CW_ADAPTER; } return CW_OK; }